简体   繁体   中英

How to get the default encryption settings of amazon S3 buckets via Javascript SDK

I'd like to list the default encryption settings(none, AES-256, AWS-KMS) of all my s3 buckets by using the AWS SDK for javascript.(2.305)

First I'm fetching all buckets with listBuckets and then iterate over all buckets with the getBucketEncryption function.

My problem is that getBucketEncryption is async and therefore I use promises to wait for the processing of all requests. So I add all the promisses to a list and wait for them with Promise.all() . Finally when I get all the results, I don't know to which bucket the results belong because getBucketEncryption does not return the bucket name with the data and the promisses could resolve in any order.

Somehow I need to wrap a promise around the s3.getBucketEncryption call where I can pass the bucketname so that I have it later on for evaluation... but how?

Here comes the code:

console.log('Loading function');

const aws = require('aws-sdk');

const s3 = new aws.S3({ apiVersion: '2006-03-01', region: 'eu-west-1' });

function reflect(promise){
    return promise.then(
        function(v){ return {cryptSetting:v, status: "COMPLIANT" }},
        function(e){ return {error:e, status: "NON_COMPLIANT"}});
}

s3.listBuckets({},function(err,data){
    if (err){
        console.log(err, err.stack); // an error occurred
    } 
    else{

        var bucketList = JSON.parse(JSON.stringify(data.Buckets));

        var list = new Array();

        for(let i in bucketList){
            list.push(s3.getBucketEncryption({Bucket: bucketList[i].Name})
            .promise());      
        }   

        Promise.all(list.map(reflect)).then(function(values) {

            for(let i in values){
                // at this point I do not have the bucket name any more
                // because it's not included in the values array
                console.log("Bucketname missing here " + values[i].cryptSetting
                + ' ' + values[i].status);
            }
          });

     }  
});

The output looks like this:

Loading function

Bucketname missing here [object Object] COMPLIANT

Bucketname missing here [object Object] COMPLIANT

Bucketname missing here [object Object] COMPLIANT

Bucketname missing here [object Object] COMPLIANT

Bucketname missing here [object Object] COMPLIANT

Bucketname missing here [object Object] COMPLIANT

Bucketname missing here [object Object] COMPLIANT

Bucketname missing here undefined NON_COMPLIANT

Bucketname missing here undefined NON_COMPLIANT

Bucketname missing here [object Object] COMPLIANT

Bucketname missing here undefined NON_COMPLIANT

Good news ..... it's really simple.

The values array is guaranteed to be congruous with the bucketList array, regardless of the order in which the promises settled.

So bucketList[i] will correspond with values[i] .

s3.listBuckets({}, function(err, data) {
    if (err) {
        console.log(err, err.stack); // an error occurred
    } else {
        var bucketList = JSON.parse(JSON.stringify(data.Buckets));
        var promises = bucketList.map(function(b) {
            return s3.getBucketEncryption({ 'Bucket':b.Name }).promise();
        });
        Promise.all(promises.map(reflect)).then(function(values) {
            for(let i in values) {
                // `values` is guaranteed to be congruous with `bucketList`.
                // ie. `bucketList[i]` corresponds with `values[i]`.
                console.log([bucketList[i].name, values[i].cryptSetting, values[i].status].join(' '));
            }
        });
    }  
});

Another way would be to :

  • move reflect up into the bucketList.map() stage.
  • decorate (in the bucketList.map(...) closure) each of the values delivered by reflect() with a .bucket property.
s3.listBuckets({}, function(err, data) {
    if (err) {
        console.log(err, err.stack); // an error occurred
    } else {
        var bucketList = JSON.parse(JSON.stringify(data.Buckets));
        var promises = bucketList.map(function(b) {
            var promise = s3.getBucketEncryption({ 'Bucket':b.Name }).promise();
            return reflect(promise).then(function(value) {
                return Object.assign(value, { 'bucket':b });
            });
        });
        Promise.all(promises).then(function(values) {
            for(let i in values) {
                // each value now has a '.bucket' property.
                console.log([values[i].bucket.name, values[i].cryptSetting, values[i].status].join(' '));
            }
        });
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM