Skip to content
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repositories {

defaultTasks 'clean', 'check', 'installDist'

version = '0.2.4'
version = '0.2.5'
jar.archiveName = "${jar.baseName}.${jar.extension}"
distZip.archiveName = "${jar.baseName}.zip"

Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/amazonaws/gurureviewercli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ public static void main(String[] argv) {
if (scanMetaData != null) {
// try to clean up objects from S3.
main.tryDeleteS3Object(config.getS3Client(),
config.getAccountId(),
scanMetaData.getBucketName(),
scanMetaData.getSourceKey());
main.tryDeleteS3Object(config.getS3Client(),
config.getAccountId(),
scanMetaData.getBucketName(),
scanMetaData.getBuildKey());
}
Expand Down Expand Up @@ -250,10 +252,15 @@ private void validateInitialConfig(final Configuration config) throws IOExceptio
config.setKeyId(this.kmsKeyId);
}

private void tryDeleteS3Object(final S3Client s3Client, final String s3Bucket, final String s3Key) {
private void tryDeleteS3Object(final S3Client s3Client, final String accountId,
final String s3Bucket, final String s3Key) {
try {
if (s3Key != null) {
s3Client.deleteObject(DeleteObjectRequest.builder().bucket(s3Bucket).key(s3Key).build());
s3Client.deleteObject(DeleteObjectRequest.builder()
.bucket(s3Bucket)
.key(s3Key)
.expectedBucketOwner(accountId)
.build());
}
} catch (Exception e) {
Log.warn("Failed to delete %s from %s. Please delete the object by hand.", s3Key, s3Bucket);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
import software.amazon.awssdk.services.s3.model.HeadBucketRequest;
import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
import software.amazon.awssdk.services.s3.model.S3Exception;

import com.amazonaws.gurureviewercli.exceptions.GuruCliException;
import com.amazonaws.gurureviewercli.model.Configuration;
Expand All @@ -29,6 +30,7 @@
public final class AssociationAdapter {

private static final String BUCKET_NAME_PATTERN = "codeguru-reviewer-cli-%s-%s";
private static final int HTTP_FORBIDDEN = 403;
private static final long WAIT_TIME_IN_SECONDS = 1L;

/**
Expand Down Expand Up @@ -92,7 +94,12 @@ private static RepositoryAssociation createBucketAndAssociation(final Configurat
bucketName = String.format(BUCKET_NAME_PATTERN, config.getAccountId(), config.getRegion());
}
try {
config.getS3Client().headBucket(HeadBucketRequest.builder().bucket(bucketName).build());
// Pin the expected owner so a bucket with this (predictable) name that was pre-created in another
// account is never adopted. S3 answers 403 when the owner does not match.
config.getS3Client().headBucket(HeadBucketRequest.builder()
.bucket(bucketName)
.expectedBucketOwner(config.getAccountId())
.build());
} catch (NoSuchBucketException e) {
Log.info("CodeGuru Reviewer requires an S3 bucket to upload the analysis artifacts to.");
val createBucket =
Expand All @@ -109,6 +116,14 @@ private static RepositoryAssociation createBucketAndAssociation(final Configurat
throw new GuruCliException(ErrorCodes.USER_ABORT, "CodeGuru needs an S3 bucket to continue.");
}

} catch (S3Exception e) {
if (e.statusCode() == HTTP_FORBIDDEN) {
val msg = String.format("Your %s account does not have permissions to the provided bucket %s. "
+ "Pass --bucket-name with a bucket owned by your account or a bucket "
+ "for which you have permissions.", config.getAccountId(), bucketName);
throw new GuruCliException(ErrorCodes.BUCKET_ACCESS_DENIED, msg);
}
throw e;
}

val repository = Repository.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public enum ErrorCodes {
GIT_EMPTY_DIFF("Git Diff is empty"),
AWS_INIT_ERROR("Failed to initialize AWS API"),
BAD_BUCKET_NAME("CodeGuru Reviewer expects bucket names to start with codeguru-reviewer-"),
BUCKET_ACCESS_DENIED("No permissions to the provided S3 bucket"),
USER_ABORT("Abort");

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.HeadBucketRequest;
import software.amazon.awssdk.services.s3.model.HeadBucketResponse;
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
import software.amazon.awssdk.services.s3.model.S3Exception;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.amazonaws.gurureviewercli.exceptions.GuruCliException;
Expand Down Expand Up @@ -185,4 +190,55 @@ public void test_getAssociatedGuruRepo_createNewWithCreateBucketInteractiveAbort
AssociationAdapter.getAssociatedGuruRepo(config));
Assertions.assertEquals(ErrorCodes.USER_ABORT, ret.getErrorCode());
}
}

@Test
public void test_getAssociatedGuruRepo_headBucketPinsExpectedOwner() {
val accountId = "111122223333";
val expected = RepositoryAssociation.builder()
.associationArn("123")
.state(RepositoryAssociationState.ASSOCIATED)
.build();
when(guruFrontendService.listRepositoryAssociations(any(ListRepositoryAssociationsRequest.class)))
.thenReturn(ListRepositoryAssociationsResponse.builder()
.repositoryAssociationSummaries(Collections.emptyList())
.build());
when(s3client.headBucket(any(HeadBucketRequest.class))).thenReturn(HeadBucketResponse.builder().build());
when(guruFrontendService.associateRepository(any(AssociateRepositoryRequest.class)))
.thenReturn(AssociateRepositoryResponse.builder().repositoryAssociation(expected).build());
when(guruFrontendService.describeRepositoryAssociation(any(DescribeRepositoryAssociationRequest.class)))
.thenReturn(DescribeRepositoryAssociationResponse.builder().repositoryAssociation(expected).build());
val config = Configuration.builder()
.guruFrontendService(guruFrontendService)
.interactiveMode(false)
.s3Client(s3client)
.accountId(accountId)
.region("us-east-1")
.repoName("some-repo-name")
.build();
AssociationAdapter.getAssociatedGuruRepo(config);
verify(s3client).headBucket(argThat((HeadBucketRequest r) -> accountId.equals(r.expectedBucketOwner())));
}

@Test
public void test_getAssociatedGuruRepo_bucketOwnedByOtherAccountFailsClosed() {
when(guruFrontendService.listRepositoryAssociations(any(ListRepositoryAssociationsRequest.class)))
.thenReturn(ListRepositoryAssociationsResponse.builder()
.repositoryAssociationSummaries(Collections.emptyList())
.build());
when(s3client.headBucket(any(HeadBucketRequest.class)))
.thenThrow(S3Exception.builder().statusCode(403).message("Forbidden").build());
val config = Configuration.builder()
.guruFrontendService(guruFrontendService)
.interactiveMode(false)
.s3Client(s3client)
.accountId("111122223333")
.region("us-east-1")
.repoName("some-repo-name")
.build();
GuruCliException ret = Assertions.assertThrows(GuruCliException.class, () ->
AssociationAdapter.getAssociatedGuruRepo(config));
Assertions.assertEquals(ErrorCodes.BUCKET_ACCESS_DENIED, ret.getErrorCode());
verify(s3client, never()).createBucket(any(CreateBucketRequest.class));
verify(guruFrontendService, never()).associateRepository(any(AssociateRepositoryRequest.class));
}
}
Loading