sarutak commented on code in PR #57140: URL: https://github.com/apache/spark/pull/57140#discussion_r3547893563
########## core/src/main/java/org/apache/spark/security/UserContext.java: ########## @@ -0,0 +1,139 @@ +/* + * 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.time.Instant; +import java.util.Objects; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +import org.apache.spark.annotation.DeveloperApi; + +/** + * :: DeveloperApi :: + * Represents the authenticated user's identity context on the driver side. + * <p> + * This class holds the OIDC token information used to derive short-lived + * {@link ServiceCredential} instances via credential providers. It is intentionally + * <b>not</b> {@link java.io.Serializable} and must never be transmitted to executors. + * The {@code rawToken} field is always redacted in {@link #toString()} and excluded from + * Jackson serialization. + * + * @since 4.3.0 + */ +@DeveloperApi +public final class UserContext { + + private final String principal; + private final String issuer; + @JsonIgnore + private final String rawToken; + private final Instant issuedAt; + private final Instant expiresAt; + + /** + * Constructs a new {@code UserContext}. + * + * @param principal the {@code sub} claim from the JWT (must not be null) + * @param issuer the {@code iss} claim from the JWT (must not be null) + * @param rawToken the raw OIDC JWT string (must not be null) + * @param issuedAt token issue time (may be null) + * @param expiresAt token expiry time (may be null) + */ + public UserContext( + String principal, + String issuer, + String rawToken, + Instant issuedAt, + Instant expiresAt) { + this.principal = Objects.requireNonNull(principal, "principal must not be null"); + this.issuer = Objects.requireNonNull(issuer, "issuer must not be null"); + this.rawToken = Objects.requireNonNull(rawToken, "rawToken must not be null"); + this.issuedAt = issuedAt; + this.expiresAt = expiresAt; + } + + /** Returns the {@code sub} claim (principal identifier). */ + public String getPrincipal() { + return principal; + } + + /** Returns the {@code iss} claim (token issuer). */ + public String getIssuer() { + return issuer; + } + + /** Returns the raw OIDC JWT. This value must never be logged or transmitted to executors. */ + @JsonIgnore Review Comment: `UserContext` is not `Serializable` and not intended to be written as JSON. Is there any reason to annotate as `@JsonIgnore`? ########## core/src/main/java/org/apache/spark/security/UserCredentials.java: ########## @@ -0,0 +1,105 @@ +/* + * 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.io.Serializable; +import java.util.Collections; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +import org.apache.spark.annotation.DeveloperApi; + +/** + * :: DeveloperApi :: + * A bundle of {@link ServiceCredential} instances keyed by scheme (e.g., "s3a", "abfss"). + * <p> + * Scheme keys are normalized to lowercase ({@link Locale#ROOT}) at construction time, and + * lookups via {@link #forScheme(String)} are case-insensitive. If the supplied map contains + * keys that differ only by case, the last entry (in iteration order) wins. + * <p> + * This class is transmitted to executors and does <b>not</b> contain any reference + * to {@link UserContext} or raw identity tokens. It is immutable and {@link Serializable}. + * + * @since 4.3.0 + */ +@DeveloperApi +public final class UserCredentials implements Serializable { + + private static final long serialVersionUID = 1L; + + private final Map<String, ServiceCredential> credentials; + + /** + * Constructs a new {@code UserCredentials} bundle. + * <p> + * Scheme keys are normalized to lowercase using {@link Locale#ROOT}. If multiple keys + * collide after lowercasing, the last entry in iteration order wins. + * + * @param credentials per-scheme map of service credentials (must not be null; defensively copied) + */ + public UserCredentials(Map<String, ServiceCredential> credentials) { + Objects.requireNonNull(credentials, "credentials must not be null"); + Map<String, ServiceCredential> normalized = new HashMap<>(credentials.size()); Review Comment: The comment says "the last entry (in iteration order) wins" for case-colliding keys, but `HashMap`'s iteration order is non-deterministic, making this behavior effectively unpredictable. For a credential system, silent non-deterministic behavior seems risky. Since the SPIP design document only specifies "per-scheme `Map[String, ServiceCredential]`" without requiring last-wins semantics, I'd suggest throwing on collision instead: ```java String normalized = entry.getKey().toLowerCase(Locale.ROOT); if (normalized.put(normalized, entry.getValue()) != null) { throw new IllegalArgumentException( "Duplicate scheme after case normalization: " + entry.getKey()); } ``` And updating the Javadoc accordingly: ``` - If the supplied map contains keys that differ only by case, the last entry (in iteration order) wins. + If the supplied map contains keys that differ only by case, an {@link IllegalArgumentException} is thrown. ``` This is safer and can always be relaxed to last-wins later (with a `LinkedHashMap`) if needed. ########## core/src/main/java/org/apache/spark/security/UserCredentials.java: ########## @@ -0,0 +1,105 @@ +/* + * 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.io.Serializable; +import java.util.Collections; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +import org.apache.spark.annotation.DeveloperApi; + +/** + * :: DeveloperApi :: + * A bundle of {@link ServiceCredential} instances keyed by scheme (e.g., "s3a", "abfss"). + * <p> + * Scheme keys are normalized to lowercase ({@link Locale#ROOT}) at construction time, and + * lookups via {@link #forScheme(String)} are case-insensitive. If the supplied map contains + * keys that differ only by case, the last entry (in iteration order) wins. + * <p> + * This class is transmitted to executors and does <b>not</b> contain any reference + * to {@link UserContext} or raw identity tokens. It is immutable and {@link Serializable}. + * + * @since 4.3.0 + */ +@DeveloperApi +public final class UserCredentials implements Serializable { + + private static final long serialVersionUID = 1L; + + private final Map<String, ServiceCredential> credentials; + + /** + * Constructs a new {@code UserCredentials} bundle. + * <p> + * Scheme keys are normalized to lowercase using {@link Locale#ROOT}. If multiple keys + * collide after lowercasing, the last entry in iteration order wins. + * + * @param credentials per-scheme map of service credentials (must not be null; defensively copied) + */ + public UserCredentials(Map<String, ServiceCredential> credentials) { + Objects.requireNonNull(credentials, "credentials must not be null"); + Map<String, ServiceCredential> normalized = new HashMap<>(credentials.size()); + for (Map.Entry<String, ServiceCredential> entry : credentials.entrySet()) { + normalized.put(entry.getKey().toLowerCase(Locale.ROOT), entry.getValue()); Review Comment: `HashMap` permits null keys, so if a caller passes a map containing a null key, `.toLowerCase()` will throw an NPE without a clear message. A brief null check would make the failure more informative: ```java Objects.requireNonNull(entry.getKey(), "scheme key must not be null"); ``` -- 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]
