使用boto3检查AWS S3存储桶是否启用了加密。

4
我下面发布的代码可以获取aws上所有s3存储桶列表,我正在尝试编写python代码来检查这些存储桶是否已加密,但是我无法弄清楚如何实现。请问有人能告诉我如何修改我的代码吗?我尝试了在线示例并查看了文档。
我的代码如下:
 from __future__ import print_function
 import boto3
 import os

 os.environ['AWS_DEFAULT_REGION'] = "us-east-1"
 # Create an S3 client
 s3 = boto3.client('s3')
 # Call S3 to list current buckets

 response = s3.list_buckets()

 # Get a list of all bucket names from the response
 buckets = [bucket['Name'] for bucket in response['Buckets']]

 # Print out the bucket list
 print("Bucket List: %s" % buckets)

尝试了以下代码,但它们无法工作:
 s3 = boto3.resource('s3')
 bucket = s3.Bucket('my-bucket-name')
 for obj in bucket.objects.all():
     key = s3.Object(bucket.name, obj.key)
     print key.server_side_encryption

并且。
 #!/usr/bin/env python
 import boto3

 s3_client = boto3.client('s3')
 head = s3_client.head_object(
     Bucket="<S3 bucket name>",
     Key="<S3 object key>"
 )
 if 'ServerSideEncryption' in head:
     print head['ServerSideEncryption']
2个回答

20

首先需要了解一些关于S3和加密的事情。

  1. 当您在S3存储桶上启用默认加密时,实际上是在存储桶上配置了一个服务器端加密配置规则,该规则将导致S3加密在配置规则之后上传到存储桶中的每个对象。
  2. 与第1点无关,您可以将S3存储桶策略应用于存储桶,禁止上传未加密的对象。这将防止您添加未加密的数据,但不会自动加密任何内容。
  3. 您可以按对象为单位进行上传加密;加密不必涵盖整个存储桶。

因此,要找出属于第1类别(将自动加密上传到它们的所有内容)的存储桶之一,可以执行以下操作:

import boto3
from botocore.exceptions import ClientError

s3 = boto3.client('s3')

response = s3.list_buckets()

for bucket in response['Buckets']:
  try:
    enc = s3.get_bucket_encryption(Bucket=bucket['Name'])
    rules = enc['ServerSideEncryptionConfiguration']['Rules']
    print('Bucket: %s, Encryption: %s' % (bucket['Name'], rules))
  except ClientError as e:
    if e.response['Error']['Code'] == 'ServerSideEncryptionConfigurationNotFoundError':
      print('Bucket: %s, no server-side encryption' % (bucket['Name']))
    else:
      print("Bucket: %s, unexpected error: %s" % (bucket['Name'], e))
这将导致类似以下的输出:
Bucket: mycats, no server-side encryption
Bucket: mydogs, no server-side encryption
Bucket: mytaxreturn, Encryption: [{'ApplyServerSideEncryptionByDefault': {'SSEAlgorithm': 'AES256'}}]

0

要使用AWS中的Boto3库检查S3存储桶是否加密,您可以利用get_bucket_encryption()方法-

import boto3

def check_bucket_encryption(bucket_name):
    s3 = boto3.client('s3')
    
    try:
        response = s3.get_bucket_encryption(Bucket=bucket_name)
        # If the bucket is encrypted, the response will contain 'ServerSideEncryptionConfiguration'
        if 'ServerSideEncryptionConfiguration' in response:
            return True
        else:
            return False
    except s3.exceptions.ClientError as e:
        # If there's an error, assume the bucket is not encrypted or access is denied
        error_code = e.response['Error']['Code']
        if error_code == 'ServerSideEncryptionConfigurationNotFoundError':
            return False
        else:
            return False

# Example usage
bucket_name = 'your_bucket_name'
is_encrypted = check_bucket_encryption(bucket_name)
print(f"Is the bucket encrypted? {is_encrypted}")

此外,我发现了一个非常酷的GitHub项目,他们正在创建一个使用Jupyter笔记本编写Runbooks的开源框架。
他们有一个Runbook可以加密任何未加密的S3存储桶,其中他们使用了两个乐高积木:
  1. 查找所有未加密的S3存储桶
  2. 对它们进行加密

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接