sarutak commented on code in PR #57655:
URL: https://github.com/apache/spark/pull/57655#discussion_r3742234539


##########
connector/credential-aws/src/main/java/org/apache/spark/security/aws/AwsStsCredentialProvider.java:
##########
@@ -18,29 +18,277 @@
 package org.apache.spark.security.aws;
 
 import java.net.URI;
+import java.time.Duration;
+import java.time.Instant;
 import java.util.Map;
 import java.util.Set;
+import java.util.regex.Pattern;
+import java.util.stream.Stream;
+
+import com.google.common.annotations.VisibleForTesting;
+import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider;
+import software.amazon.awssdk.core.exception.SdkException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.sts.StsClient;
+import software.amazon.awssdk.services.sts.StsClientBuilder;
+import 
software.amazon.awssdk.services.sts.model.AssumeRoleWithWebIdentityRequest;
+import 
software.amazon.awssdk.services.sts.model.AssumeRoleWithWebIdentityResponse;
+import software.amazon.awssdk.services.sts.model.Credentials;
 
-import org.apache.spark.annotation.DeveloperApi;
 import org.apache.spark.security.CredentialProvider;
 import org.apache.spark.security.CredentialResolutionException;
 import org.apache.spark.security.ServiceCredential;
 import org.apache.spark.security.UserContext;
 
 /**
- * :: DeveloperApi ::
- * AWS STS credential provider that exchanges an OIDC identity token for 
temporary
- * AWS credentials via {@code AssumeRoleWithWebIdentity}.
+ * A {@link CredentialProvider} that exchanges an OIDC identity token for 
temporary
+ * AWS credentials via the STS {@code AssumeRoleWithWebIdentity} API.
+ * <p>
+ * The returned {@link ServiceCredential} contains S3A-compatible Hadoop 
configuration
+ * properties ({@code fs.s3a.access.key}, {@code fs.s3a.secret.key},
+ * {@code fs.s3a.session.token}) that can be propagated to executors for 
accessing
+ * S3-compatible storage.
+ * <p>
+ * <b>Configuration keys</b> (passed via {@code spark.security.oidc.*}):
+ * <ul>
+ *   <li>{@code spark.security.oidc.aws.roleArn} (required) -- the ARN of the 
IAM role
+ *       to assume</li>
+ *   <li>{@code spark.security.oidc.aws.sessionName} (optional) -- the role 
session name;
+ *       defaults to a value derived from the user's principal or 
"spark-oidc"</li>
+ *   <li>{@code spark.security.oidc.aws.durationSeconds} (optional) -- 
credential duration
+ *       in seconds (900-43200); if unset, STS uses the role's default 
maximum</li>
+ *   <li>{@code spark.security.oidc.aws.region} (optional) -- the AWS region 
for the STS
+ *       endpoint; defaults to us-east-1 when only {@code stsEndpoint} is set. 
When neither
+ *       {@code region} nor {@code stsEndpoint} is configured, the STS client 
falls back to
+ *       the AWS SDK default region resolution (AWS_REGION / 
AWS_DEFAULT_REGION environment
+ *       variables, then the ~/.aws/config profile).</li>
+ *   <li>{@code spark.security.oidc.aws.stsEndpoint} (optional) -- a custom 
STS endpoint URL
+ *       for non-AWS environments (MinIO, Ceph, LocalStack, etc.)</li>
+ * </ul>
+ * <p>
+ * <b>Security note:</b> The OIDC raw token is never included in log messages 
or
+ * exception messages. It is passed directly to the STS API call and discarded.
  *
  * @since 4.3.0
  */
-@DeveloperApi
 public class AwsStsCredentialProvider implements CredentialProvider {
 
+  // Configuration key constants
+  static final String CONF_ROLE_ARN = "spark.security.oidc.aws.roleArn";
+  static final String CONF_SESSION_NAME = 
"spark.security.oidc.aws.sessionName";
+  static final String CONF_DURATION_SECONDS = 
"spark.security.oidc.aws.durationSeconds";
+  static final String CONF_REGION = "spark.security.oidc.aws.region";
+  static final String CONF_STS_ENDPOINT = 
"spark.security.oidc.aws.stsEndpoint";
+
+  /** Minimum duration allowed by STS AssumeRoleWithWebIdentity (15 minutes). 
*/
+  static final int MIN_DURATION_SECONDS = 900;
+  /** Maximum duration allowed by STS AssumeRoleWithWebIdentity (12 hours). */
+  static final int MAX_DURATION_SECONDS = 43200;
+
+  private static final String DEFAULT_REGION = "us-east-1";
+  private static final String DEFAULT_SESSION_NAME = "spark-oidc";
+
+  /**
+   * Precompiled pattern matching characters that are NOT valid in STS session 
names.
+   * Valid characters are: alphanumeric, underscore, plus, equals, comma, 
period, at, hyphen.
+   */
+  private static final Pattern SESSION_NAME_INVALID_CHARS =
+      Pattern.compile("[^a-zA-Z0-9_+=,.@\\-]");
+
+  /**
+   * Precompiled pattern for validating a configured STS session name.
+   * Must match {@code [\w+=,.@-]{2,64}} per AWS STS documentation.
+   */
+  private static final Pattern SESSION_NAME_VALID_PATTERN =
+      Pattern.compile("[\\w+=,.@\\-]{2,64}");

Review Comment:
   `SESSION_NAME_VALID_PATTERN` uses `[\\w+=,.@\\-]{2,64}`, but Java's `\w` 
matches Unicode word characters (e.g., accented letters, CJK characters), not 
just ASCII. STS only accepts ASCII `[a-zA-Z0-9_+=,.@-]`.
   
   This is inconsistent with `SESSION_NAME_INVALID_CHARS` which correctly uses 
the explicit ASCII character class `[^a-zA-Z0-9_+=,.@\\-]`.
   
   A user setting `spark.security.oidc.aws.sessionName=café` would pass the 
validation in `init()` but be rejected by STS at resolve time -- defeating the 
fail-fast intent.
   
   Could you change the validation pattern to match the sanitization pattern?
   
   ```java
   private static final Pattern SESSION_NAME_VALID_PATTERN =
       Pattern.compile("[a-zA-Z0-9_+=,.@\\-]{2,64}");
   ```
   
   Also please update the related references:
   - The error message at L182: `"must match [\\w+=,.@-]{2,64}"` -> `"must 
match [a-zA-Z0-9_+=,.@-]{2,64}"`
   - The Javadoc for `SESSION_NAME_VALID_PATTERN`
   - The Javadoc for `sanitizeSessionName`, which ironically says "We use an 
explicit character class rather than `\w`" while the validation pattern does 
the opposite
   - The PR description mentions `[\w+=,.@-]{2,64}`. Please update that as well



-- 
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]

Reply via email to