I have a task to go through all the s3 buckets in my account and find the ones that are not encrypted by the default kms encryption. I have drafted the following two commands with a loop to go through each bucket name and check its encryption level
output="$(aws s3api list-buckets --query 'Buckets[*].Name')"
for i in $output; do aws s3api get-bucket-encryption --bucket $i; done
The script gives me the following error:
Invalid bucket name ""cdktoolkit-stagingbucket-30v8nlr122c0",": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$" or be an ARN matching the regex "^arn:(aws).*:s3:[a-z\-0-9]+:[0-9]{12}:accesspoint[/:][a-zA-Z0-9\-]{1,63}$"
it works for the first part now, the output variable doesn't contain the question mark anymore, thanks for the help.
But as i go further, when i ran the second cli command "for i in $output; do aws s3api get-bucket-encryption --bucket $i; done", it returns another JSON format output, how could I grep the bucket name without AES256 encryption enabled.
Jasons-Air:~ jason$ for i in $output; do aws s3api get-bucket-encryption --bucket $i; done
{
"ServerSideEncryptionConfiguration": {
"Rules": [
{
"ApplyServerSideEncryptionByDefault": {
"SSEAlgorithm": "aws:kms"
}
}
]
}
}
An error occurred (ServerSideEncryptionConfigurationNotFoundError) when calling the GetBucketEncryption operation: The server side encryption configuration was not found
An error occurred (ServerSideEncryptionConfigurationNotFoundError) when calling the GetBucketEncryption operation: The server side encryption configuration was not found
An error occurred (ServerSideEncryptionConfigurationNotFoundError) when calling the GetBucketEncryption operation: The server side encryption configuration was not found
{
"ServerSideEncryptionConfiguration": {
"Rules": [
{
"ApplyServerSideEncryptionByDefault": {
"SSEAlgorithm": "AES256"
}
}
]
}
}
I tried to append "--query 'ServerSideEncryptionConfiguration[].Rules[].ApplyServerSideEncryptionByDefault[*].SSEAlgorithm'" to my command, but it shows result as "null" instead of "AES256".
Jasons-Air:~ jason$ for i in $output; do aws s3api get-bucket-encryption --bucket $i --query 'ServerSideEncryptionConfiguration[*].Rules[*].ApplyServerSideEncryptionByDefault[*].SSEAlgorithm'; done
null
An error occurred (ServerSideEncryptionConfigurationNotFoundError) when calling the GetBucketEncryption operation: The server side encryption configuration was not found
An error occurred (ServerSideEncryptionConfigurationNotFoundError) when calling the GetBucketEncryption operation: The server side encryption configuration was not found
An error occurred (ServerSideEncryptionConfigurationNotFoundError) when calling the GetBucketEncryption operation: The server side encryption configuration was not found
null
null
null
aws s3api list-buckets --query 'Buckets[*].Name'
return? (Please add to your question, and if the information is sensitive replace it with made-up data that exactly matches the style of the real output.) – Chris Davies Jun 15 '20 at 17:43[ "cdktoolkit-stagingbucket-30v8nlr122c0", "elasticbeanstalk-us-east-1-194639014949", "elasticbeanstalk-us-west-1-194639014949", "elasticbeanstalk-us-west-2-194639014949", "intangible-dev-terraform-state", "intangible-ssp-global-tf-states", "testbucketwithencryptionenabled", "thedevopguy" ]
– condescendent Jun 15 '20 at 17:47aws
command troughjq -r '.[]'
. – Kusalananda Jun 15 '20 at 17:55aws s3api list-buckets --query 'Buckets[*].Name'
return? – Chris Davies Jun 15 '20 at 21:27