Skip to content

Commit 68be73e

Browse files
Pedestrian93PeterRao
authored andcommitted
feat: bucket severside encryption (#707)
* feat: bucket severside encryption * chore: ReadMe * fix: remove test sleep
1 parent 0ed0d57 commit 68be73e

File tree

6 files changed

+156
-1
lines changed

6 files changed

+156
-1
lines changed

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ All operation use es7 async/await to implement. All api is async function.
9191
- RequestPayment
9292
- [.getBucketRequestPayment(bucketName[, options])](#getbucketrequestpaymentbucketname-options)
9393
- [.putBucketRequestPayment(bucketName, payer[, options])](#putBucketRequestpaymentbucketname-payer-options)
94+
- BucketEncryption
95+
- [.putBucketEncryption(name[, options])](#putbucketencryptionbucketname-options)
96+
- [.getBucketEncryption(name)](#getbucketencryptionbucketname-options)
97+
- [.deleteBucketEncryption(name)](#deletebucketencryptionbucketname-options)
9498
- [Object Operations](#object-operations)
9599
- [.list(query[, options])](#listquery-options)
96100
- [.put(name, file[, options])](#putname-file-options)
@@ -954,6 +958,57 @@ Success will return:
954958

955959
---
956960

961+
### .putBucketEncryption(name, rules)
962+
963+
put BucketEncryption value of the bucket object.
964+
965+
parameters:
966+
967+
- name {String} bucket name
968+
- [rules] {Object} parameters
969+
- SSEAlgorithm {String} encryption type, expect AES256 or KMS
970+
- {KMSMasterKeyID} {String} needed when encryption type is KMS
971+
972+
Success will return:
973+
974+
- status {Number} response status
975+
- res {Object} response info
976+
977+
---
978+
979+
### .getBucketEncryption(name)
980+
981+
get BucketEncryption rule value of the bucket object.
982+
983+
parameters:
984+
985+
- name {String} bucket name
986+
987+
Success will return:
988+
989+
- status {Number} response status
990+
- res {Object} response info
991+
- encryption {Object} rules
992+
- SSEAlgorithm {String} encryption type, AES256 or KMS
993+
- {KMSMasterKeyID} {String} will be return when encryption type is KMS
994+
995+
---
996+
997+
### .deleteBucketEncryption(name)
998+
999+
delete BucketEncryption rule value of the bucket object.
1000+
1001+
parameters:
1002+
1003+
- name {String} bucket name
1004+
1005+
Success will return:
1006+
1007+
- status {Number} response status
1008+
- res {Object} response info
1009+
1010+
---
1011+
9571012
## Object Operations
9581013

9591014
All operations function return Promise, except `signatureUrl`.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const proto = exports;
2+
// const jstoxml = require('jstoxml');
3+
/**
4+
* deleteBucketEncryption
5+
* @param {String} bucketName - bucket name
6+
*/
7+
8+
proto.deleteBucketEncryption = async function deleteBucketEncryption(bucketName) {
9+
this._checkBucketName(bucketName);
10+
const params = this._bucketRequestParams('DELETE', bucketName, 'encryption');
11+
params.successStatuses = [204];
12+
params.xmlResponse = true;
13+
const result = await this.request(params);
14+
return {
15+
status: result.status,
16+
res: result.res
17+
};
18+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const proto = exports;
2+
/**
3+
* getBucketEncryption
4+
* @param {String} bucketName - bucket name
5+
*/
6+
7+
proto.getBucketEncryption = async function getBucketEncryption(bucketName) {
8+
this._checkBucketName(bucketName);
9+
const params = this._bucketRequestParams('GET', bucketName, 'encryption');
10+
params.successStatuses = [200];
11+
params.xmlResponse = true;
12+
const result = await this.request(params);
13+
const encryption = result.data.ApplyServerSideEncryptionByDefault;
14+
return {
15+
encryption,
16+
status: result.status,
17+
res: result.res
18+
};
19+
};

lib/common/bucket/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ const proto = exports;
44

55
merge(proto, require('./getBucketRequestPayment.js'));
66
merge(proto, require('./putBucketRequestPayment.js'));
7-
7+
merge(proto, require('./putBucketEncryption.js'));
8+
merge(proto, require('./getBucketEncryption.js'));
9+
merge(proto, require('./deleteBucketEncryption.js'));
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const proto = exports;
2+
// const jstoxml = require('jstoxml');
3+
const obj2xml = require('../utils/obj2xml');
4+
/**
5+
* putBucketEncryption
6+
* @param {String} bucketName - bucket name
7+
* @param {Object} options
8+
*/
9+
10+
proto.putBucketEncryption = async function putBucketEncryption(bucketName, options) {
11+
options = options || {};
12+
this._checkBucketName(bucketName);
13+
const params = this._bucketRequestParams('PUT', bucketName, 'encryption', options);
14+
params.successStatuses = [200];
15+
const paramXMLObj = {
16+
ServerSideEncryptionRule: {
17+
ApplyServerSideEncryptionByDefault: {
18+
SSEAlgorithm: options.SSEAlgorithm
19+
}
20+
}
21+
};
22+
if (options.KMSMasterKeyID !== undefined) {
23+
paramXMLObj.ServerSideEncryptionRule.ApplyServerSideEncryptionByDefault.KMSMasterKeyID = options.KMSMasterKeyID;
24+
}
25+
const paramXML = obj2xml(paramXMLObj, {
26+
headers: true
27+
});
28+
params.mime = 'xml';
29+
params.content = paramXML;
30+
const result = await this.request(params);
31+
return {
32+
status: result.status,
33+
res: result.res
34+
};
35+
};

test/node/bucket.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,4 +475,30 @@ describe('test/bucket.test.js', () => {
475475
}
476476
});
477477
});
478+
describe('putBucketEncryption(), getBucketEncryption(), deleteBucketEncryption()', () => {
479+
it('should create, get and delete the bucket encryption', async () => {
480+
// put with AES256
481+
const putresult1 = await store.putBucketEncryption(bucket, {
482+
SSEAlgorithm: 'AES256'
483+
});
484+
assert.equal(putresult1.res.status, 200);
485+
// put again with KMS will be fine
486+
// const putresult2 = await store.putBucketEncryption(bucket, {
487+
// SSEAlgorithm: 'KMS',
488+
// KMSMasterKeyID: '1b2c3132-b2ce-4ba3-a4dd-9885904099ad'
489+
// });
490+
// assert.equal(putresult2.res.status, 200);
491+
// await utils.sleep(ms(metaSyncTime));
492+
// get
493+
const getBucketEncryption = await store.getBucketEncryption(bucket);
494+
assert.equal(getBucketEncryption.res.status, 200);
495+
assert.deepEqual(getBucketEncryption.encryptions, {
496+
SSEAlgorithm: 'AES256'
497+
// KMSMasterKeyID: '1b2c3132-b2ce-4ba3-a4dd-9885904099ad'
498+
});
499+
// delete
500+
const deleteResult = await store.deleteBucketEncryption(bucket);
501+
assert.equal(deleteResult.res.status, 204);
502+
});
503+
});
478504
});

0 commit comments

Comments
 (0)