Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove s3:listAllMyBuckets permission #2131

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion athena-aws-cmdb/athena-aws-cmdb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ Resources:
- rds:Describe*
- rds:ListTagsForResource
- athena:GetQueryExecution
- s3:ListAllMyBuckets
- s3:ListBucket
- athena:GetQueryExecution
Effect: Allow
Expand Down
1 change: 0 additions & 1 deletion athena-clickhouse/athena-clickhouse.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ Resources:
Resource: !Sub 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${LambdaFunctionName}:*'
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
- Action:
Expand Down
1 change: 0 additions & 1 deletion athena-cloudera-hive/athena-cloudera-hive.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ Resources:
- Statement:
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
Version: '2012-10-17'
Expand Down
1 change: 0 additions & 1 deletion athena-cloudera-impala/athena-cloudera-impala.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ Resources:
- Statement:
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
Version: '2012-10-17'
Expand Down
1 change: 0 additions & 1 deletion athena-cloudwatch-metrics/athena-cloudwatch-metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ Resources:
- cloudwatch:Get*
- cloudwatch:List*
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
Version: '2012-10-17'
Expand Down
1 change: 0 additions & 1 deletion athena-cloudwatch/athena-cloudwatch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ Resources:
- logs:TestMetricFilter
- logs:FilterLogEvents
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
- Action:
Expand Down
1 change: 0 additions & 1 deletion athena-datalakegen2/athena-datalakegen2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ Resources:
- Statement:
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
Version: '2012-10-17'
Expand Down
1 change: 0 additions & 1 deletion athena-db2-as400/athena-db2-as400.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ Resources:
- Statement:
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
Version: '2012-10-17'
Expand Down
1 change: 0 additions & 1 deletion athena-db2/athena-db2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ Resources:
- Statement:
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
Version: '2012-10-17'
Expand Down
1 change: 0 additions & 1 deletion athena-docdb/athena-docdb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ Resources:
- glue:GetPartition
- glue:GetDatabase
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
Version: '2012-10-17'
Expand Down
1 change: 0 additions & 1 deletion athena-dynamodb/athena-dynamodb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ Resources:
- glue:GetPartition
- glue:GetDatabase
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
- Action:
Expand Down
1 change: 0 additions & 1 deletion athena-elasticsearch/athena-elasticsearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ Resources:
- glue:GetPartition
- glue:GetDatabase
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
Version: '2012-10-17'
Expand Down
1 change: 0 additions & 1 deletion athena-example/athena-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ Resources:
- Statement:
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
Version: '2012-10-17'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ private PolicyDocument getGlueAthenaS3AccessPolicy()
statementActionsPolicy.add("glue:GetPartition");
statementActionsPolicy.add("glue:GetDatabase");
statementActionsPolicy.add("athena:GetQueryExecution");
statementActionsPolicy.add("s3:ListAllMyBuckets");

return PolicyDocument.Builder.create()
.statements(ImmutableList.of(PolicyStatement.Builder.create()
Expand Down
6 changes: 0 additions & 6 deletions athena-federation-sdk/athena-federation-sdk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ Resources:
Timeout: !Ref LambdaTimeout
MemorySize: !Ref LambdaMemory
Policies:
- Statement:
- Action:
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
Version: '2012-10-17'
#S3CrudPolicy allows our connector to spill large responses to S3. You can optionally replace this pre-made policy
#with one that is more restrictive and can only 'put' but not read,delete, or overwrite files.
- S3CrudPolicy:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package com.amazonaws.athena.connector.lambda.domain.spill;

/*-
* #%L
* Amazon Athena Query Federation SDK
Expand All @@ -20,15 +18,15 @@
* #L%
*/

package com.amazonaws.athena.connector.lambda.domain.spill;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.HeadBucketRequest;
import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Set;
import java.util.stream.Collectors;

/**
* This class is used to track the bucket and its state, and check its validity
*/
Expand Down Expand Up @@ -85,19 +83,21 @@ public void checkBucketAuthZ(String spillBucket)
void updateBucketState()
{
try {
Set<String> buckets = amazons3.listBuckets().stream().map(b -> b.getName()).collect(Collectors.toSet());

if (!buckets.contains(bucket)) {
amazons3.headBucket(new HeadBucketRequest(bucket));
state = BucketState.VALID;
}
catch (AmazonServiceException ex) {
int statusCode = ex.getStatusCode();
// returns 404 if bucket was not found, 403 if bucket access is forbidden
if (statusCode == 404 || statusCode == 403) {
state = BucketState.INVALID;
}
else {
state = BucketState.VALID;
throw new RuntimeException("Error while checking bucket ownership for " + bucket, ex);
}

logger.info("The state of bucket {} has been updated to {} from {}", bucket, state, BucketState.UNCHECKED);
}
catch (AmazonS3Exception ex) {
throw new RuntimeException("Error while checking bucket ownership for " + bucket, ex);
finally {
logger.info("The state of bucket {} has been updated to {} from {}", bucket, state, BucketState.UNCHECKED);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.amazonaws.athena.connector.lambda.domain.spill;

import com.amazonaws.AmazonServiceException;

/*-
* #%L
* Amazon Athena Query Federation SDK
Expand All @@ -22,10 +24,15 @@

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.HeadBucketRequest;
import com.amazonaws.services.s3.model.HeadBucketResult;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Spy;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -35,7 +42,10 @@
import java.util.Random;

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
Expand All @@ -56,8 +66,7 @@ public void setup()
logger.info("setUpBefore - enter");

bucketNames = Arrays.asList("bucket1", "bucket2", "bucket3");
List<Bucket> buckets = createBuckets(bucketNames);
AmazonS3 mockS3 = createMockS3(buckets);
AmazonS3 mockS3 = createMockS3(bucketNames);
spyVerifier = spy(new SpillLocationVerifier(mockS3));

logger.info("setUpBefore - exit");
Expand Down Expand Up @@ -112,7 +121,7 @@ public void checkBucketAuthZFail()
{
logger.info("checkBucketAuthZFail - enter");

String bucketNotOwn = "spill-bucket";
String bucketNotOwn = "forbidden";

try {
spyVerifier.checkBucketAuthZ(bucketNotOwn);
Expand All @@ -137,22 +146,26 @@ public void checkBucketAuthZFail()
logger.info("checkBucketAuthZFail - exit");
}

private AmazonS3 createMockS3(List<Bucket> buckets)
private AmazonS3 createMockS3(List<String> buckets)
{
AmazonS3 s3mock = mock(AmazonS3.class);
when(s3mock.listBuckets()).thenReturn(buckets);
when(s3mock.headBucket(any(HeadBucketRequest.class)))
.thenAnswer((Answer<HeadBucketResult>) invocationOnMock -> {
String bucketName = ((HeadBucketRequest) invocationOnMock.getArguments()[0]).getBucketName();
if (buckets.contains(bucketName)) {
return null;
}
AmazonServiceException exception;
if (bucketName.equals("forbidden")) {
exception = new AmazonServiceException("Forbidden");
exception.setStatusCode(403);
}
else {
exception = new AmazonServiceException("Not Found");
exception.setStatusCode(404);
}
throw exception;
});
return s3mock;
}

private List<Bucket> createBuckets(List<String> names)
{
List<Bucket> buckets = new ArrayList();
for (String name : names) {
Bucket bucket = mock(Bucket.class);
when(bucket.getName()).thenReturn(name);
buckets.add(bucket);
}

return buckets;
}
}
1 change: 0 additions & 1 deletion athena-gcs/athena-gcs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ Resources:
- Statement:
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
- glue:GetTableVersions
- glue:GetPartitions
- glue:GetTables
Expand Down
1 change: 0 additions & 1 deletion athena-google-bigquery/athena-google-bigquery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ Resources:
- Statement:
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
Version: '2012-10-17'
Expand Down
1 change: 0 additions & 1 deletion athena-hbase/athena-hbase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ Resources:
- glue:GetPartition
- glue:GetDatabase
- athena:GetQueryExecution
- s3:ListAllMyBuckets
- s3:ListBucket
- s3:GetObject
- s3:GetBucketLocation
Expand Down
1 change: 0 additions & 1 deletion athena-hortonworks-hive/athena-hortonworks-hive.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ Resources:
- Statement:
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
Version: '2012-10-17'
Expand Down
1 change: 0 additions & 1 deletion athena-kafka/athena-kafka.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ Resources:
Resource: !Sub 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${LambdaFunctionName}:*'
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
- s3:ListBucket
- s3:GetObject
- s3:GetBucketLocation
Expand Down
1 change: 0 additions & 1 deletion athena-msk/athena-msk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ Resources:
Resource: !Sub 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${LambdaFunctionName}:*'
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
- s3:ListBucket
- s3:GetObject
- s3:GetBucketLocation
Expand Down
1 change: 0 additions & 1 deletion athena-mysql/athena-mysql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ Resources:
Resource: !Sub 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${LambdaFunctionName}:*'
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
- Action:
Expand Down
1 change: 0 additions & 1 deletion athena-neptune/athena-neptune.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ Resources:
- glue:GetPartition
- glue:GetDatabase
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
- Action:
Expand Down
1 change: 0 additions & 1 deletion athena-oracle/athena-oracle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ Resources:
Resource: !Sub 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${LambdaFunctionName}:*'
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
- Action:
Expand Down
1 change: 0 additions & 1 deletion athena-postgresql/athena-postgresql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ Resources:
Resource: !Sub 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${LambdaFunctionName}:*'
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
- Action:
Expand Down
1 change: 0 additions & 1 deletion athena-redis/athena-redis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ Resources:
- glue:GetPartition
- glue:GetDatabase
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
Version: '2012-10-17'
Expand Down
1 change: 0 additions & 1 deletion athena-redshift/athena-redshift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ Resources:
- Effect: Allow
Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Resource: '*'
- Effect: Allow
Action:
Expand Down
1 change: 0 additions & 1 deletion athena-saphana/athena-saphana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ Resources:
- Statement:
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
Version: '2012-10-17'
Expand Down
1 change: 0 additions & 1 deletion athena-snowflake/athena-snowflake.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ Resources:
- Statement:
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
Version: '2012-10-17'
Expand Down
1 change: 0 additions & 1 deletion athena-sqlserver/athena-sqlserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ Resources:
Resource: !Sub 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${LambdaFunctionName}:*'
- Action:
- athena:GetQueryExecution
- s3:ListAllMyBuckets
Effect: Allow
Resource: '*'
- Action:
Expand Down
Loading