in app/collectors/bucket.scala [85:143]
private def arn(bucketName: String) = s"arn:aws:s3:::$bucketName"
def fromApiData(
bucket: AWSBucket,
client: S3Client,
origin: AmazonOrigin,
bucketWithCorrectCreatedTime: AWSBucket
): Bucket = {
val bucketName = bucket.name
val bucketRegion =
try {
Option(
client
.getBucketLocation(
GetBucketLocationRequest.builder.bucket(bucketName).build
)
.locationConstraintAsString
)
.filterNot(region => "" == region)
.orElse(Some(Region.US_EAST_1.id))
} catch {
case e: S3Exception if e.awsErrorDetails.errorCode == "NoSuchBucket" =>
log.info(
s"NoSuchBucket for $bucketName in account ${origin.account}",
e
)
None
case e: S3Exception
if e.awsErrorDetails.errorCode == "AuthorizationHeaderMalformed" =>
log.info(
s"AuthorizationHeaderMalformed for $bucketName in account ${origin.account}",
e
)
None
/*
Reaching this case means that the bucket exists, but the user does not have access to it.
For example, the bucket's policy might be set to only allow s3:* access from a specific IP address.
*/
case e: S3Exception
if e.awsErrorDetails().errorCode == "AccessDenied" =>
log.warn(
s"AccessDenied for $bucketName in account ${origin.account}",
e
)
None
case NonFatal(t) =>
throw new IllegalStateException(
s"Failed when building info for bucket $bucketName",
t
)
}
Bucket(
arn = arn(bucketName),
name = bucketName,
region = bucketRegion,
createdTime = bucketWithCorrectCreatedTime.creationDate
)
}