sarutak commented on code in PR #57262: URL: https://github.com/apache/spark/pull/57262#discussion_r3592799575
########## core/src/main/java/org/apache/spark/security/FileTokenIngestor.java: ########## @@ -0,0 +1,155 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.security; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.Instant; +import java.util.Base64; +import java.util.Optional; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.apache.spark.annotation.Private; +import org.apache.spark.internal.LogKeys; +import org.apache.spark.internal.MDC; +import org.apache.spark.internal.SparkLogger; +import org.apache.spark.internal.SparkLoggerFactory; + +/** + * A {@link TokenIngestor} that reads an OIDC identity token from a file. + * <p> + * The file path is typically a Kubernetes projected service account token + * (e.g., {@code /var/run/secrets/tokens/spark-identity}) or a path configured via + * {@code spark.security.credentials.identityToken.file}. + * <p> + * This implementation: + * <ul> + * <li>Detects file rotation via mtime change (only re-parses when the file changes)</li> + * <li>Parses JWT claims by Base64-decoding the payload segment directly, without + * signature verification, since the token is trusted from the local filesystem + * and works with both signed (RS256, ES256) and unsigned tokens</li> + * <li>Handles errors gracefully: malformed JWT, missing file, or empty content + * returns empty rather than throwing</li> + * </ul> + * + * @since 4.3.0 + */ +@Private +public class FileTokenIngestor implements TokenIngestor { + + private static final SparkLogger LOG = SparkLoggerFactory.getLogger(FileTokenIngestor.class); + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private final Path tokenPath; + + // Cached state for rotation detection. + // Write order matters for thread-safety: cachedContext must be visible before + // lastMtime, so a concurrent reader never sees a new mtime with a stale context. + private volatile UserContext cachedContext = null; + private volatile long lastMtime = -1L; + + /** + * Construct a new FileTokenIngestor. + * + * @param tokenPath path to the identity token file (must not be null) + */ + public FileTokenIngestor(Path tokenPath) { + this.tokenPath = tokenPath; + } + + @Override + public Optional<UserContext> load() { + try { + if (!Files.exists(tokenPath)) { + LOG.debug("Token file does not exist: {}", tokenPath); + return Optional.empty(); + } + + // File did not change since last successful parse + long currentMtime = Files.getLastModifiedTime(tokenPath).toMillis(); + if (currentMtime == lastMtime && cachedContext != null) { + return Optional.of(cachedContext); + } + + String content = new String(Files.readAllBytes(tokenPath), StandardCharsets.UTF_8).trim(); + if (content.isEmpty()) { + LOG.warn("{}", MDC.of(LogKeys.PATH, tokenPath + " token file is empty")); Review Comment: The MDC usage doesn't match Spark's convention. Currently: ```java LOG.warn("{}", MDC.of(LogKeys.PATH, tokenPath + " token file is empty")); LOG.warn("{}", e, MDC.of(LogKeys.PATH, tokenPath + " failed to load token: " + e.getClass().getName())); ``` The MDC value should contain only the structured data (the path), not the message text concatenated into it. The message goes in the format string. See existing patterns in `BytesToBytesMap.java`: ```java logger.error("Was unable to delete spill file {}", MDC.of(LogKeys.PATH, file.getAbsolutePath())); ``` Suggested: ```java LOG.warn("Token file is empty: {}", MDC.of(LogKeys.PATH, tokenPath)); LOG.warn("Failed to load token from {}: {}", MDC.of(LogKeys.PATH, tokenPath), MDC.of(LogKeys.CLASS_NAME, e.getClass().getName())); ``` Similarly for the `parseJwt` catch block. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
