【问题标题】:AWS CLI to list encryption status of all S3 bucketsAWS CLI 列出所有 S3 存储桶的加密状态
【发布时间】:2022-05-13 05:25:43
【问题描述】:

我的帐户有几百个存储桶,我需要能够显示所有这些存储桶的加密状态。我希望能够通过 CLI 执行此操作,我看到有一个命令“get-bucket-encryption”操作,但我不知道如何针对所有存储桶而不是特定存储桶运行它。

【问题讨论】:

  • 进展如何?仍然不清楚如何列出状态?

标签: amazon-web-services amazon-s3 aws-cli


【解决方案1】:

您可以在list-buckets 的结果上循环运行它。

例如:

for bucket_name in $(aws s3api list-buckets --query "Buckets[].Name" --output text); do

    echo ${bucket_name}
    
    encryption_info=$(aws s3api get-bucket-encryption \
        --bucket ${bucket_name} 2>/dev/null)
        
    if [[ $? != 0 ]]; then
        echo " - no-encryption"
    else
        echo " - ${encryption_info}"
    fi
done

如果bucket没有加密get-bucket-encryption返回错误,所以我上面假设任何错误都意味着没有加密。

【讨论】:

    【解决方案2】:

    我知道这个问题是针对 CLI 的,但这是 Nodejs 中的答案

    假设您已经设置了所有凭据并安装了aws-sdk,这就是您应该运行的内容

    const AWS = require('aws-sdk');
    const s3 = new AWS.S3();
    
    s3.listBuckets(function(err, data) {
        if (err) console.log(err, err.stack);
        let bucketData = data.Buckets;
        let bucketLength = data.Buckets.length;
    
        for (let i = 0; i < bucketLength; i++) {
            var params = {
                Bucket: `${bucketData[i].Name}` /* required */
            };
            s3.getBucketEncryption(params, function(err, data) {
                try {       // first it will print all non encypted buckets and then vice versa
                    if(err){
                        console.log(bucketData[i].Name)   // Non Encrypted Bucket List
                    }
                    if (data) {
                        console.log(bucketData[i].Name);  // Encrpted Bucket List 
                    }
                } catch (err) {}
            });
        }
    });
    

    【讨论】:

      【解决方案3】:

      只需在 python3 答案中添加这个稍旧的问题

      就像我上面的 Nodejs 一样,它还假设您具有正确的设置凭据以及已安装的 boto3 sdk。

      import boto3, botocore.exceptions
      
      def main():
          client = boto3.client('s3')
          bucket_list = client.list_buckets()
          encrypted_buckets = []
          unencrypted_buckets = []
      
          for item in bucket_list['Buckets']:
              try:
                  encryption_info = client.get_bucket_encryption(
                      Bucket=item['Name']
                  )
                  encrypted_buckets.append([item['Name'],(encryption_info['ServerSideEncryptionConfiguration'])])
              except botocore.exceptions.ClientError as error:
                  if error.response['Error']['Code'] == 'ServerSideEncryptionConfigurationNotFoundError':
                      unencrypted_buckets.append(item['Name'])
          
          print("Encrypted Buckets - Encryption Type")
          for item in encrypted_buckets:
              print(item)
          print("\nUnencrypted Buckets")  
          for item in unencrypted_buckets:
              print(item)
              
      if __name__ == "__main__":
          main()
      

      这将输出一个加密桶列表,然后将未加密桶输出到命令行

      【讨论】:

        【解决方案4】:

        单线选项。这将打印服务器端加密算法(即 AES256)或在未启用 SSE 时打印错误消息。

        aws s3api list-buckets --query "Buckets[].Name" | jq -r ".[]" | xargs -I {} bash -c "echo {}; aws s3api get-bucket-encryption --bucket {} | jq -r '.ServerSideEncryptionConfiguration.Rules[0].ApplyServerSideEncryptionByDefault.SSEAlgorithm'"
        

        【讨论】:

          猜你喜欢
          • 2017-06-26
          • 2015-10-23
          • 2023-03-17
          • 1970-01-01
          • 2023-01-18
          • 2019-11-10
          • 1970-01-01
          • 2022-01-24
          • 2020-05-22
          相关资源
          最近更新 更多