xiangfu0 commented on code in PR #20442:
URL: https://github.com/apache/pinot/pull/20442#discussion_r4765006305


##########
pinot-plugins/pinot-file-system/pinot-s3/src/main/java/org/apache/pinot/plugin/filesystem/S3PinotFS.java:
##########
@@ -264,13 +265,18 @@ public AwsCredentials getAwsCredentials() {
 
   /// Executes the given S3 operation, retrying once after refreshing AWS 
credentials if an [S3Exception] occurs.
   ///
+  /// `AccessDenied` is not retried: a refresh cannot grant a missing 
permission, and it closes the S3 client under
+  /// calls that are still using it.
+  ///
   /// @param action the S3 operation to execute
   /// @param <T> the type of the result returned by the operation
   /// @return the result of the S3 operation
-  /// @throws IOException if the operation fails after credential refresh
+  /// @throws IOException if S3 denies access or the retry fails
   private <T> T retryWithS3CredentialRefresh(Supplier<T> action) throws 
IOException {
     try {
       return action.get();
+    } catch (AccessDeniedException e) {

Review Comment:
   Can we keep a one-time provider refresh for Pinot-managed dynamic 
credentials while keeping the S3 client and its open streams alive? For 
example, if the default chain selects the EC2 instance role before an 
authorized AWS profile is mounted, GETs can keep returning `AccessDenied` even 
after the profile appears. The SDK reuses the last successful provider. 
Rebuilding the chain on master lets the next attempt pick up the profile. The 
revoked-session case mentioned in the PR also has a recovery window worth 
calling out: with `sessionDurationSeconds=3600`, the STS provider can keep 
signing requests with the revoked session for most of an hour. [SDK provider 
chain](https://github.com/aws/aws-sdk-java-v2/blob/2.59.10/core/auth/src/main/java/software/amazon/awssdk/auth/credentials/AwsCredentialsProviderChain.java#L99-L115)
 ยท [STS refresh 
timing](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/credential-caching.html)



##########
pinot-plugins/pinot-file-system/pinot-s3/src/test/java/org/apache/pinot/plugin/filesystem/S3PinotFSCredentialRefreshTest.java:
##########
@@ -0,0 +1,131 @@
+/**
+ * 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.pinot.plugin.filesystem;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import javax.annotation.Nullable;
+import org.apache.pinot.spi.env.PinotConfiguration;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
+import software.amazon.awssdk.awscore.exception.AwsErrorDetails;
+import software.amazon.awssdk.core.ResponseInputStream;
+import software.amazon.awssdk.services.s3.S3Client;
+import software.amazon.awssdk.services.s3.S3ServiceClientConfiguration;
+import software.amazon.awssdk.services.s3.model.AccessDeniedException;
+import software.amazon.awssdk.services.s3.model.GetObjectRequest;
+import software.amazon.awssdk.services.s3.model.GetObjectResponse;
+import software.amazon.awssdk.services.s3.model.S3Exception;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertSame;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.expectThrows;
+
+
+/// Tests which S3 errors make [S3PinotFS] refresh its credentials.
+public class S3PinotFSCredentialRefreshTest {
+  private static final URI OBJECT_URI = URI.create("s3://bucket/key");
+  private static final PinotConfiguration CLIENT_CONFIG = new 
PinotConfiguration(Map.of(
+      S3Config.REGION, "us-east-1",
+      S3Config.ACCESS_KEY, "access",
+      S3Config.SECRET_KEY, "secret",
+      S3Config.ENDPOINT, "http://localhost:1";));
+
+  private S3Client _s3Client;
+
+  @BeforeMethod
+  public void setUp() {
+    _s3Client = mock(S3Client.class);
+    
when(_s3Client.serviceClientConfiguration()).thenReturn(S3ServiceClientConfiguration.builder()
+        
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("access",
 "secret")))
+        .build());
+  }
+
+  @Test
+  public void accessDeniedFailsWithoutRefreshingOrClosingTheClient() {
+    AccessDeniedException accessDenied = AccessDeniedException.builder()
+        .statusCode(403)
+        .awsErrorDetails(AwsErrorDetails.builder()
+            .errorCode("AccessDenied")
+            .errorMessage("User: arn:aws:iam::123456789012:user/reader is not 
authorized to perform: s3:GetObject")
+            .build())
+        .build();
+    
when(_s3Client.getObject(any(GetObjectRequest.class))).thenThrow(accessDenied);
+    S3PinotFS s3PinotFS = new S3PinotFS();
+    s3PinotFS.init(_s3Client, null, CLIENT_CONFIG);
+
+    IOException thrown = expectThrows(IOException.class, () -> 
s3PinotFS.open(OBJECT_URI));
+
+    verify(_s3Client, never()).close();
+    verify(_s3Client, times(1)).getObject(any(GetObjectRequest.class));
+    assertSame(thrown.getCause(), accessDenied);
+    assertTrue(thrown.getMessage().contains(accessDenied.getMessage()), 
thrown.getMessage());
+  }
+
+  @DataProvider
+  public Object[][] errorsThatRefresh() {
+    return new Object[][]{
+        {s3Exception(403, "InvalidAccessKeyId")},
+        {s3Exception(403, null)},
+        {(S3Exception) S3Exception.builder().statusCode(403).build()},
+        {s3Exception(401, null)}
+    };
+  }
+
+  @Test(dataProvider = "errorsThatRefresh")
+  public void otherAuthErrorsRefreshOnceAndRetry(S3Exception error)
+      throws IOException {
+    ResponseInputStream<GetObjectResponse> object = mock();
+    AtomicInteger refreshes = new AtomicInteger();
+    
when(_s3Client.getObject(any(GetObjectRequest.class))).thenThrow(error).thenAnswer(invocation
 -> {
+      assertEquals(refreshes.get(), 1, "refreshed before the retry");
+      return object;
+    });
+    S3PinotFS s3PinotFS = new S3PinotFS() {
+      @Override
+      public void initOrRefreshS3Client() {
+        refreshes.incrementAndGet();
+      }
+    };
+    s3PinotFS.init(_s3Client, null, CLIENT_CONFIG);
+
+    assertSame(s3PinotFS.open(OBJECT_URI), object);
+    assertEquals(refreshes.get(), 1);
+    verify(_s3Client, times(2)).getObject(any(GetObjectRequest.class));

Review Comment:
   Could the refresh override install a second mock and have only that client 
return the object? Production replaces the client and closes the original. 
Expecting both GETs on the original mock lets this test pass even if a retry 
accidentally uses the closed client. One GET on each mock would check that the 
retry uses the replacement.



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