[
https://issues.apache.org/jira/browse/FLINK-41435?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
LuChengxiang updated FLINK-41435:
---------------------------------
Description:
h2. Summary
DefaultDelegationTokenManager runs the initial token update directly on the
caller thread, although scheduled refreshes use the configured IO executor.
When the initial call runs on a Pekko ForkJoinWorkerThread,
HadoopFSDelegationTokenProvider performs a blocking HDFS RPC while
freshUGI.doAs(...) is effective. On the Hadoop CompletableFuture IPC path,
ForkJoinPool managed blocking can create a replacement worker during that
window. The Thread constructor copies the current effective
AccessControlContext, so the new pooled worker retains the transient Subject
after doAs returns.
The primary fix is to run the initial update on the same IO executor used by
scheduled refreshes. This keeps provider and receiver work off the callers FJP
thread and preserves the existing synchronous start semantics with FutureTask.
PR: https://github.com/apache/flink/pull/30026
h2. Observed failure
This was observed in a Flink 1.20.5 Kubernetes application-mode job using
ZooKeeper-based high availability and HDFS-backed checkpoint storage. The job
used a Hadoop IPC client with the CompletableFuture-based wait path. The
primary failure was in checkpoint storage:
{noformat}
CheckpointCoordinator.initializeCheckpointLocation
-> FsCheckpointStorageAccess.initializeLocationForCheckpoint
-> HadoopFileSystem.mkdirs
-> DFSClient.mkdirs
{noformat}
After the 24h deadline, the checkpoint-storage client reported InvalidToken for
its startup token even though refresh had obtained a new HDFS token and the
receiver reported success. The same JVM also showed a FileSystemJobResultStore
stack using the expired token during cleanup.
Not every application-mode job is affected. The failure is timing-dependent: a
long-lived Hadoop client must capture one Subject/UGI lifecycle, and a later
token-refresh receiver must run under a different one.
h2. Root cause
The initial update path is inconsistent with the scheduled refresh path:
* {{DefaultDelegationTokenManager.start()}} calls {{startTokensUpdate()}}
directly on the caller thread.
* A scheduled refresh uses {{ioExecutor.execute(this::startTokensUpdate)}}.
* {{ClusterEntrypoint}} creates the cluster IO executor before creating the
token manager, so the executor is already the intended home for blocking token
refresh work.
The problematic startup sequence is:
{noformat}
ResourceManager.onStart()
-> DefaultDelegationTokenManager.start()
-> startTokensUpdate() [Pekko FJP worker]
-> HadoopFSDelegationTokenProvider.obtainDelegationTokens()
-> freshUGI.doAs(...)
-> fs.addDelegationTokens(...)
-> HDFS RPC in CompletableFuture.get() [Hadoop 3.5.0+]
-> ForkJoinPool.managedBlock()
-> tryCompensate()
-> createWorker()
-> new dispatcher worker inherits the fresh Subject
-> later creates a long-lived cluster-io worker
{noformat}
The key is not the thread name itself. The thread constructor captures the
effective AccessControlContext at creation time. A worker created while
freshUGI.doAs(...) is active therefore retains that Subject even after the
provider returns to the login Subject.
The same inheritance mechanism was also observed on a long-lived RpcEndpoint
main-scheduler thread. That thread only forwards scheduled work to the RPC
endpoint main thread and is not a confirmed token-receiver failure path; it is
evidence that the thread-creation hazard is broader than one pool.
h2. Proposed change
Run the initial token update on the configured IO executor, matching the
scheduled refresh path:
* Submit the initial {{startTokensUpdate()}} to {{ioExecutor}}.
* Wait for completion with {{FutureTask.get()}} so {{start()}} remains
synchronous.
* Use {{FutureTask}} rather than {{CompletableFuture.get()}} so the waiting
caller does not itself enter ForkJoinPool managed blocking.
This removes the provider from the Pekko FJP worker on the initial path. The IO
executor creates or reuses its worker before {{startTokensUpdate()}} runs, and
provider/receiver work then executes on the stable IO thread.
h2. Scope and boundaries
h3. Out of scope
* General hardening for all long-lived threads created during a transient
security context. This remains a separate follow-up.
* Changing HadoopDelegationTokenReceiver target selection. The receiver still
depends on its executing thread context; a receiver-side context-first change
is a related latent hardening, not required for the reproduced startup path.
* Rebinding Hadoop clients already created under a different Subject.
* Double security installation and SecurityUtils.install idempotency.
h3. Follow-up hardening
The general invariant remains: long-lived worker threads must not be created
while a transient Subject is effective, or security-sensitive work on
long-lived executors must receive an explicit SecurityContext/UGI rather than
resolving identity from the executing thread. This includes the RpcEndpoint
main-scheduler observation and any other pooled worker created inside
freshUGI.doAs(...).
h2. Deployment and version notes
The trigger is the caller thread and blocking IPC path, not Kubernetes as a
platform name. YARN is not confirmed to reproduce in the tested normal
application-mode path, but is not claimed to be immune.
The CompletableFuture-based IPC wait path is present in Hadoop 3.5.0
(HADOOP-19235). Earlier Hadoop clients that use AsyncGet.Util.wait do not enter
FJP managed blocking from the provider wait itself, although the receiver
target assumption is independent of that version boundary.
h2. Testing
* Unit test verifies that the initial token update runs on the configured IO
executor.
* ./mvnw -pl flink-runtime -Dtest=DefaultDelegationTokenManagerTest
-DfailIfNoTests=false -Denforcer.skip=true test
* Tests run: 17, Failures: 0, Errors: 0, Skipped: 0
* A deployed application-mode job using the CompletableFuture IPC path should
survive the 24h HDFS token deadline and continue checkpointing without
SecretManager$InvalidToken.
* A controlled probe should verify that the initial provider call runs on the
IO executor and does not create a replacement Pekko dispatcher worker inside
freshUGI.doAs(...).
h2. Acceptance criteria
* The initial delegation-token update runs on the configured IO executor.
* The initial provider call does not execute on a Pekko ForkJoinWorkerThread
and does not trigger FJP compensation while freshUGI.doAs(...) is effective.
* A long-running application-mode job using the CompletableFuture IPC path
survives the 24h HDFS token deadline.
* Scheduled refreshes continue to run on the IO executor and preserve the
existing receiver/listener behavior.
was:
h2. Summary
Flink periodically obtains HDFS delegation tokens and distributes them through
HadoopDelegationTokenReceiver. The receiver adds refreshed credentials to
UserGroupInformation.getCurrentUser() of the thread running the refresh. A
long-lived Hadoop client, especially a checkpoint-storage DFSClient, may
instead use the UGI/Subject captured when that client was created. If those
identities differ, the refresh succeeds and the receiver reports success, but
the new credentials are not delivered to the client's UGI. The client keeps
using its startup token and fails at the 24h HDFS token deadline with
SecretManager$InvalidToken.
The defect is receiver target selection, not token acquisition or client
binding. A pooled thread's current UGI can be inherited from a different
Subject lifecycle than the UGI held by HadoopSecurityContext or the login user.
The proposed fix targets the UGI held by HadoopSecurityContext, falling back to
the login UGI, instead of a pooled thread's current UGI.
Here "UGI" means the Hadoop UserGroupInformation wrapper and "Subject" means
the identity it carries; identity comparisons use Subject identity.
h2. Observed failure
This was observed in a Flink 1.20.5 Kubernetes application-mode job using
ZooKeeper-based high availability and HDFS-backed checkpoint storage. The job
used a Hadoop IPC client with the 3.5.0 CompletableFuture-based wait path. The
primary failure was in the checkpoint storage path:
{noformat}
CheckpointCoordinator.initializeCheckpointLocation
-> FsCheckpointStorageAccess.initializeLocationForCheckpoint
-> HadoopFileSystem.mkdirs
-> DFSClient.mkdirs
{noformat}
After the 24h deadline, the checkpoint-storage client reported InvalidToken for
its startup token even though refresh had obtained a new HDFS token and the
receiver reported success.
Not every application-mode job is affected. The failure is timing-dependent: a
long-lived Hadoop client must capture one Subject/UGI lifecycle, and a later
refresh receiver must run under a different one. Whether the provider's
blocking wait enters FJP managed blocking determines whether a replacement
worker can be created during the freshUGI.doAs(...) window.
h2. Root cause
HadoopDelegationTokenReceiver targets UserGroupInformation.getCurrentUser() of
the refresh thread. getCurrentUser() resolves from the thread's effective
AccessControlContext and is not guaranteed to be the UGI held by
HadoopSecurityContext or the UGI captured by a long-lived checkpoint-storage
DFSClient.
In the observed failure, the checkpoint client's captured UGI had the
login/HadoopSecurityContext Subject, while the receiver could run under a
pooled thread's inherited Subject.
h3. How the refresh thread's current UGI can differ
The provider obtains tokens inside freshUGI.doAs(...). With Apache Hadoop 3.5.0
and later, Client.getRpcResponse waits on CompletableFuture.get()
(HADOOP-19235). On a ForkJoinWorkerThread, CompletableFuture.get() enters
ForkJoinPool.managedBlock(); if no idle worker is available and the pool has
capacity, ForkJoinPool.tryCompensate() creates a replacement worker while
freshUGI.doAs(...) is still effective.
The Thread constructor copies the effective AccessControlContext into the new
thread's inheritedAccessControlContext, so a worker created during that window
retains the fresh Subject after doAs returns. A later cluster-io worker created
by one of those workers inherits the same Subject. A receiver running on that
cluster-io worker then resolves getCurrentUser() to the fresh Subject and
writes credentials there, while the login and HadoopSecurityContext UGIs remain
unchanged.
{noformat}
freshUGI.doAs(...)
-> fs.addDelegationTokens(...)
-> HDFS RPC in CompletableFuture.get() [Hadoop 3.5.0+]
-> ForkJoinPool.managedBlock()
-> tryCompensate()
-> createWorker()
-> new dispatcher worker inherits the fresh Subject
-> later creates a cluster-io worker
-> receiver runs with the fresh Subject
{noformat}
The lookup chain is:
{noformat}
Thread.inheritedAccessControlContext
-> AccessController.getContext()
-> Subject.getSubject(acc)
-> UserGroupInformation.getCurrentUser()
{noformat}
getCurrentUser() reads the Subject from the current thread's ACC (the inherited
ACC carries the SubjectDomainCombiner created by doAs), not from a global
current user.
In Apache Hadoop 3.3.x and 3.4.x, the same call waits in AsyncGet.Util.wait
(Object.wait) instead. Object.wait is not an FJP managed block, so the provider
wait itself does not call tryCompensate; the pool may still create workers for
unrelated task submission or other managed blocking. This version boundary
explains why the failure is not reproducible with those clients, but it does
not make the receiver's getCurrentUser() assumption correct.
h2. Proposed change
This issue fixes token delivery for the observed long-lived-client case,
primarily checkpoint storage.
* Add an accessor for the UGI held by HadoopSecurityContext.
* In HadoopDelegationTokenReceiver, select a single target UGI: the UGI held by
HadoopSecurityContext when present; otherwise fall back to
UserGroupInformation.getLoginUser(). Add refreshed credentials only to that
UGI. Do not target UserGroupInformation.getCurrentUser().
* Add unit tests for the receiver's context-first and login-fallback target
selection.
The change makes the delivery target deterministic. It does not rebind a client
already created under a different Subject.
h2. Scope and boundaries
h3. Out of scope
* Preventing HDFS client initialization or blocking RPCs from occurring inside
freshUGI.doAs(...) or the provider's ForkJoinPool thread.
* Closing or rebinding FileSystem instances created under a transient Subject;
this is a separate client-lifecycle cleanup.
* Changing the provider's getCurrentUser() usage. The provider already acquires
tokens inside KerberosLoginProvider.doLoginAndReturnUGI(); its remaining
getCurrentUser() use is a token-required pre-check and renewer-name lookup. A
provider change is a separate follow-up if provider-side behavior is observed.
* Double security installation and SecurityUtils.install idempotency. A
diagnostic build where the second installation was skipped still failed at the
24h deadline.
h3. Follow-up hardening: long-lived threads and inherited temporary Subjects
This issue does not attempt to remove the underlying thread-creation hazard. A
long-lived worker created while a temporary Subject is effective inherits that
Subject through Thread.inheritedAccessControlContext. This is a generic risk,
not receiver-specific. A Flink 1.18.1 / JDK 17 probe using the
CompletableFuture IPC wait path observed ForkJoinPool.createWorker() during the
provider's freshUGI.doAs(...) window; the new dispatcher workers inherited the
fresh Subject. The same probe also observed that Subject on a later RpcEndpoint
main-scheduler thread. That thread only forwards scheduled work to the RPC
endpoint's main thread and is not a confirmed token-receiver path; the
observation is evidence of the general mechanism, not a second reproduced token
failure.
A separate hardening change should establish one of the following invariants:
* Long-lived worker threads must not be created while a transient security
context is effective; or
* Security-sensitive work on long-lived executors must receive an explicit
SecurityContext/UGI rather than resolving the identity from the executing
thread.
That follow-up should also audit other getCurrentUser() uses and
client-creation paths on long-lived threads. It is deliberately not part of
this receiver-target PR.
h3. Version context
Flink: before 1.17, the legacy HadoopModule path added credentials directly to
the login user; the HadoopDelegationTokenReceiver / getCurrentUser() path
starts with 1.17. 1.18, 1.19, and 1.20 share that receiver behavior.
Hadoop: Client.getRpcResponse uses CompletableFuture.get() starting with 3.5.0
(HADOOP-19235). 3.3.x and 3.4.x use AsyncGet.Util.wait. The trigger in this
issue requires the CompletableFuture-based wait path; the receiver defect is
independent of the Hadoop version.
h3. Deployment scope
The failure was observed in a Kubernetes application-mode job using the 3.5.0
IPC behavior. It is not a Kubernetes-specific defect: the trigger is the
client's wait path and the receiver's executing Subject, not the deployment
type. Environments using the pre-3.5.0 AsyncGet.Util.wait path do not create
FJP workers from the provider wait itself.
h2. Testing
* Add receiver unit tests for (a) HadoopSecurityContext UGI precedence, (b)
login fallback when no HadoopSecurityContext is installed, and (c) the
inherited current UGI not being targeted.
* A deployed application-mode job using the Hadoop 3.5.0 IPC behavior should
survive the 24h token deadline and continue checkpointing without
SecretManager$InvalidToken. Verify that the checkpoint-storage DFSClient
receives the new token.
* Run a controlled application-mode probe that verifies the receiver updates
the UGI selected by the new logic rather than a pooled thread's inherited
current UGI; cover the TaskManager receiver path separately.
h2. Acceptance criteria
* A token refresh updates the UGI held by HadoopSecurityContext when present,
otherwise the login UGI, and does not use a pooled thread's inherited current
UGI; a checkpoint-storage client bound to the selected UGI receives the new
token.
* A long-running application-mode job using the Hadoop 3.5.0 IPC behavior
survives the 24h HDFS token deadline.
Summary: Initial delegation token update can leak a transient Subject
into pooled workers (was: Hadoop delegation token refresh can target a
different UGI than Hadoop clients)
> Initial delegation token update can leak a transient Subject into pooled
> workers
> --------------------------------------------------------------------------------
>
> Key: FLINK-41435
> URL: https://issues.apache.org/jira/browse/FLINK-41435
> Project: Flink
> Issue Type: Bug
> Components: Runtime / Configuration
> Affects Versions: 2.0.0, 1.19.0, 1.20.0, 2.1.0, 2.2.0, 2.3.0, 2.4.0
> Environment: Flink 1.19.0 and later (1.19.x, 1.20.x, 2.0 through
> 2.4), plus master (2.5-SNAPSHOT); Kubernetes and standalone application mode
> with Kerberos and HDFS delegation tokens. The mismatch is more likely when
> FileSystemJobResultStore and the token manager share the cluster-io executor
> (FLINK-27204).
> Reporter: LuChengxiang
> Priority: Major
> Labels: pull-request-available
> Fix For: 2.5.0
>
>
> h2. Summary
> DefaultDelegationTokenManager runs the initial token update directly on the
> caller thread, although scheduled refreshes use the configured IO executor.
> When the initial call runs on a Pekko ForkJoinWorkerThread,
> HadoopFSDelegationTokenProvider performs a blocking HDFS RPC while
> freshUGI.doAs(...) is effective. On the Hadoop CompletableFuture IPC path,
> ForkJoinPool managed blocking can create a replacement worker during that
> window. The Thread constructor copies the current effective
> AccessControlContext, so the new pooled worker retains the transient Subject
> after doAs returns.
> The primary fix is to run the initial update on the same IO executor used by
> scheduled refreshes. This keeps provider and receiver work off the callers
> FJP thread and preserves the existing synchronous start semantics with
> FutureTask.
> PR: https://github.com/apache/flink/pull/30026
> h2. Observed failure
> This was observed in a Flink 1.20.5 Kubernetes application-mode job using
> ZooKeeper-based high availability and HDFS-backed checkpoint storage. The job
> used a Hadoop IPC client with the CompletableFuture-based wait path. The
> primary failure was in checkpoint storage:
> {noformat}
> CheckpointCoordinator.initializeCheckpointLocation
> -> FsCheckpointStorageAccess.initializeLocationForCheckpoint
> -> HadoopFileSystem.mkdirs
> -> DFSClient.mkdirs
> {noformat}
> After the 24h deadline, the checkpoint-storage client reported InvalidToken
> for its startup token even though refresh had obtained a new HDFS token and
> the receiver reported success. The same JVM also showed a
> FileSystemJobResultStore stack using the expired token during cleanup.
> Not every application-mode job is affected. The failure is timing-dependent:
> a long-lived Hadoop client must capture one Subject/UGI lifecycle, and a
> later token-refresh receiver must run under a different one.
> h2. Root cause
> The initial update path is inconsistent with the scheduled refresh path:
> * {{DefaultDelegationTokenManager.start()}} calls {{startTokensUpdate()}}
> directly on the caller thread.
> * A scheduled refresh uses {{ioExecutor.execute(this::startTokensUpdate)}}.
> * {{ClusterEntrypoint}} creates the cluster IO executor before creating the
> token manager, so the executor is already the intended home for blocking
> token refresh work.
> The problematic startup sequence is:
> {noformat}
> ResourceManager.onStart()
> -> DefaultDelegationTokenManager.start()
> -> startTokensUpdate() [Pekko FJP worker]
> -> HadoopFSDelegationTokenProvider.obtainDelegationTokens()
> -> freshUGI.doAs(...)
> -> fs.addDelegationTokens(...)
> -> HDFS RPC in CompletableFuture.get() [Hadoop 3.5.0+]
> -> ForkJoinPool.managedBlock()
> -> tryCompensate()
> -> createWorker()
> -> new dispatcher worker inherits the fresh Subject
> -> later creates a long-lived cluster-io worker
> {noformat}
> The key is not the thread name itself. The thread constructor captures the
> effective AccessControlContext at creation time. A worker created while
> freshUGI.doAs(...) is active therefore retains that Subject even after the
> provider returns to the login Subject.
> The same inheritance mechanism was also observed on a long-lived RpcEndpoint
> main-scheduler thread. That thread only forwards scheduled work to the RPC
> endpoint main thread and is not a confirmed token-receiver failure path; it
> is evidence that the thread-creation hazard is broader than one pool.
> h2. Proposed change
> Run the initial token update on the configured IO executor, matching the
> scheduled refresh path:
> * Submit the initial {{startTokensUpdate()}} to {{ioExecutor}}.
> * Wait for completion with {{FutureTask.get()}} so {{start()}} remains
> synchronous.
> * Use {{FutureTask}} rather than {{CompletableFuture.get()}} so the waiting
> caller does not itself enter ForkJoinPool managed blocking.
> This removes the provider from the Pekko FJP worker on the initial path. The
> IO executor creates or reuses its worker before {{startTokensUpdate()}} runs,
> and provider/receiver work then executes on the stable IO thread.
> h2. Scope and boundaries
> h3. Out of scope
> * General hardening for all long-lived threads created during a transient
> security context. This remains a separate follow-up.
> * Changing HadoopDelegationTokenReceiver target selection. The receiver still
> depends on its executing thread context; a receiver-side context-first change
> is a related latent hardening, not required for the reproduced startup path.
> * Rebinding Hadoop clients already created under a different Subject.
> * Double security installation and SecurityUtils.install idempotency.
> h3. Follow-up hardening
> The general invariant remains: long-lived worker threads must not be created
> while a transient Subject is effective, or security-sensitive work on
> long-lived executors must receive an explicit SecurityContext/UGI rather than
> resolving identity from the executing thread. This includes the RpcEndpoint
> main-scheduler observation and any other pooled worker created inside
> freshUGI.doAs(...).
> h2. Deployment and version notes
> The trigger is the caller thread and blocking IPC path, not Kubernetes as a
> platform name. YARN is not confirmed to reproduce in the tested normal
> application-mode path, but is not claimed to be immune.
> The CompletableFuture-based IPC wait path is present in Hadoop 3.5.0
> (HADOOP-19235). Earlier Hadoop clients that use AsyncGet.Util.wait do not
> enter FJP managed blocking from the provider wait itself, although the
> receiver target assumption is independent of that version boundary.
> h2. Testing
> * Unit test verifies that the initial token update runs on the configured IO
> executor.
> * ./mvnw -pl flink-runtime -Dtest=DefaultDelegationTokenManagerTest
> -DfailIfNoTests=false -Denforcer.skip=true test
> * Tests run: 17, Failures: 0, Errors: 0, Skipped: 0
> * A deployed application-mode job using the CompletableFuture IPC path should
> survive the 24h HDFS token deadline and continue checkpointing without
> SecretManager$InvalidToken.
> * A controlled probe should verify that the initial provider call runs on the
> IO executor and does not create a replacement Pekko dispatcher worker inside
> freshUGI.doAs(...).
> h2. Acceptance criteria
> * The initial delegation-token update runs on the configured IO executor.
> * The initial provider call does not execute on a Pekko ForkJoinWorkerThread
> and does not trigger FJP compensation while freshUGI.doAs(...) is effective.
> * A long-running application-mode job using the CompletableFuture IPC path
> survives the 24h HDFS token deadline.
> * Scheduled refreshes continue to run on the IO executor and preserve the
> existing receiver/listener behavior.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)