Repository navigation
Require Metadata-Flavor header on sidecar context endpoints - #16340
shruthi713 wants to merge 1 commit into
Conversation
The sidecar metadata server checked for the Metadata-Flavor: Google header on the status and token endpoints but not on /set-context and /clear-context. Require it there too, and send it from GcpMetadataTaskContextUtil, the only caller. Authorization for credential provisioning is still enforced by App Fabric. AI_ORCHESTRATOR=all-of-creation
There was a problem hiding this comment.
Code Review
This pull request introduces a security enhancement by requiring the 'Metadata-Flavor: Google' header for the '/set-context' and '/clear-context' endpoints in GcpMetadataHttpHandlerInternal. Requests lacking this header or containing an invalid value will now be rejected with a ForbiddenException. The GcpMetadataTaskContextUtil utility has been updated to automatically attach this header, and comprehensive unit tests have been added to validate the new validation logic. There are no review comments, and the changes look solid.
|
| private static void requireMetadataFlavorHeader(HttpRequest request) | ||
| throws ForbiddenException { | ||
| if (request.headers().contains(METADATA_FLAVOR_HEADER_KEY, | ||
| METADATA_FLAVOR_HEADER_VALUE, true)) { | ||
| return; | ||
| } | ||
| throw new ForbiddenException( | ||
| String.format("Request is missing required %s header. To access the metadata server, " | ||
| + "you must add the %s: %s header to your request.", METADATA_FLAVOR_HEADER_KEY, | ||
| METADATA_FLAVOR_HEADER_KEY, METADATA_FLAVOR_HEADER_VALUE)); | ||
| } | ||
|
|
There was a problem hiding this comment.
Early throw would simplify the code:
private static void requireMetadataFlavorHeader(HttpRequest request)
throws ForbiddenException {
if (!request.headers().contains(METADATA_FLAVOR_HEADER_KEY,
METADATA_FLAVOR_HEADER_VALUE, true)) {
throw new ForbiddenException(
String.format("Request is missing required %s header. To access the metadata server, "
+ "you must add the %s: %s header to your request.", METADATA_FLAVOR_HEADER_KEY,
METADATA_FLAVOR_HEADER_KEY, METADATA_FLAVOR_HEADER_VALUE));
}
}


Summary
Fixes b/531578226.
The sidecar metadata server (
GcpMetadataHttpHandlerInternal) checks for theMetadata-Flavor: Googleheader on the status and token endpoints, but/set-contextand/clear-contexthad no such check. This PR requires the header on those two endpoints as well, and makesGcpMetadataTaskContextUtil, their only caller, send it.Details
GcpMetadataHttpHandlerInternal:setContextandclearContextreject a request without the header (HTTP 403) before reading or changing any state.status()andtoken()are unchanged.GcpMetadataTaskContextUtil: adds the header to thePUT /set-contextandDELETE /clear-contextrequests. The util is a no-op unless the Namespaced Service Accounts feature is enabled, so deployments without it are unaffected.PROVISION_CREDENTIALon the namespace).Why there is no lock on
/set-contextA state lock that rejects
PUT /set-contextwhile a context is already active (untilDELETE /clear-contextis called) was considered to stop user plugin code from overwriting the active namespace mid-task, and left out:localhostinside the pod and has no way to distinguish a request from the CDAP task-worker harness vs. user plugin code running inside the same container. IfPUT /set-contextrejected overwrites while a context was set, plugin code could simply callDELETE /clear-contextfirst (unlocking it) and then callPUT /set-contextwith a different namespace. Nor can/clear-contextbe gated on a harness-only secret, becauseRunnableTaskLauncherruns plugin code in the same JVM as the harness.PUT /set-context. With a lock, any run that failed to call/clear-contextwould leave the sidecar permanently stuck, rejecting every subsequent task or preview on that pod until restart. Two ways that can happen today:finallyblock inTaskWorkerHttpHandlerInternalcalls/clear-context;DefaultPreviewRunnersets the context and then throws inprepareStartorrun, which sit outside thetryblock that clears context on deploy failures.Testing
GcpMetadataHttpHandlerInternalTest(11/11): missing header and wrong header value are rejected on both endpoints and leave the context unchanged; a lowercase header value is accepted, as on the token endpoint; a rejected/set-contextkeeps an existing context;GcpMetadataTaskContextUtilsets and clears the context against the real handler. The tests use a small fake of the App Fabric provision endpoint so they can assert which namespace context is active.develop(expected 403, got 200). Removing either header check, or the header from either util call, fails a specific test.TaskWorkerServiceTest,RemoteConfiguratorTest,RemoteTaskExecutorTest,ArtifactLocalizerServiceTest. Verified locally on JDK 8 with Checkstyle.feature.namespaced.service.accounts.enabled=true) with a Namespace Service Account attached via Workload Identity:task-workerpod (localhost:11021):PUT /set-contextandDELETE /clear-contextwithoutMetadata-Flavor: Google(or withMetadata-Flavor: AWS) returned HTTP 403; withMetadata-Flavor: Google(and lowercasemetadata-flavor: google) returned HTTP 200.DataFusionQuickstart) and pipeline preview (GCSFile->BigQueryTable) both succeeded (HTTP 200andCOMPLETED), withGcpMetadataTaskContextUtillogging HTTP 200 on both/set-contextand/clear-contextintask-workerandpreview-runnerpods (0 non-200 responses).