Skip to content

Commit d100ff7

Browse files
committed
refactor encryption apis to ts
1 parent e1d730c commit d100ff7

File tree

14 files changed

+163
-263
lines changed

14 files changed

+163
-263
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ The complete API Reference is available here:
212212
- [set-bucket-replication.mjs](https://github.com/minio/minio-js/blob/master/examples/set-bucket-replication.mjs)
213213
- [get-bucket-replication.mjs](https://github.com/minio/minio-js/blob/master/examples/get-bucket-replication.mjs)
214214
- [remove-bucket-replication.mjs](https://github.com/minio/minio-js/blob/master/examples/remove-bucket-replication.mjs)
215+
- [set-bucket-encryption.mjs](https://github.com/minio/minio-js/blob/master/examples/set-bucket-encryption.mjs)
216+
- [get-bucket-encryption.mjs](https://github.com/minio/minio-js/blob/master/examples/get-bucket-encryption.mjs)
217+
- [remove-bucket-encryption.mjs](https://github.com/minio/minio-js/blob/master/examples/remove-bucket-encryption.mjs)
215218

216219
#### File Object Operations
217220

docs/API.md

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -767,94 +767,69 @@ await minioClient.getObjectLockConfig('my-bucketname')
767767

768768
<a name="setBucketEncryption"></a>
769769

770-
### setBucketEncryption(bucketName [,encryptionConfig, callback])
770+
### setBucketEncryption(bucketName [,encryptionConfig])
771771

772772
Set encryption configuration on a Bucket
773773

774774
**Parameters**
775775

776-
| Param | Type | Description |
777-
| ------------------ | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
778-
| `bucketName` | _string_ | Name of the bucket. |
779-
| `encryptionConfig` | _object_ | Encryption Configuration can be either omitted or `{}` or a valid and supported encryption config. by default: `{Rule:[{ApplyServerSideEncryptionByDefault:{SSEAlgorithm:"AES256"}}]}` is applied. |
780-
| `callback(err)` | _function_ | Callback is called with `err` in case of error. |
776+
| Param | Type | Description |
777+
| ------------------ | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
778+
| `bucketName` | _string_ | Name of the bucket. |
779+
| `encryptionConfig` | _object_ | Encryption Configuration can be either omitted or `{}` or a valid and supported encryption config. by default: `{Rule:[{ApplyServerSideEncryptionByDefault:{SSEAlgorithm:"AES256"}}]}` is applied. |
781780

782781
**Example **
783782
Set Encryption configuration on a Bucket
784783

785784
```js
786-
s3Client.setBucketEncryption('my-bucketname', function (err, lockConfig) {
787-
if (err) {
788-
return console.log(err)
789-
}
790-
console.log(lockConfig)
791-
})
785+
await s3Client.setBucketEncryption('my-bucketname')
792786
```
793787

794788
**Example 1**
795789
Set Encryption configuration on a Bucket with an Algorithm
796790

797791
```js
798-
s3Client.setBucketEncryption(
799-
'my-bucketname',
800-
{ Rule: [{ ApplyServerSideEncryptionByDefault: { SSEAlgorithm: 'AES256' } }] },
801-
function (err, lockConfig) {
802-
if (err) {
803-
return console.log(err)
804-
}
805-
console.log('Success')
806-
},
807-
)
792+
await s3Client.setBucketEncryption('my-bucketname', {
793+
Rule: [{ ApplyServerSideEncryptionByDefault: { SSEAlgorithm: 'AES256' } }],
794+
})
808795
```
809796

810797
<a name="getBucketEncryption"></a>
811798

812-
### getBucketEncryption(bucketName [, callback])
799+
### getBucketEncryption(bucketName)
813800

814801
Get encryption configuration of a Bucket
815802

816803
**Parameters**
817804

818-
| Param | Type | Description |
819-
| -------------------------- | ---------- | ----------------------------------------------------------------------------------------- |
820-
| `bucketName` | _string_ | Name of the bucket. |
821-
| `callback(err, encConfig)` | _function_ | Callback is called with `err` in case of error. else it is called with lock configuration |
805+
| Param | Type | Description |
806+
| ------------ | -------- | ------------------- |
807+
| `bucketName` | _string_ | Name of the bucket. |
822808

823809
**Example **
824810
Get Encryption configuration of a Bucket
825811

826812
```js
827-
s3Client.getBucketEncryption('my-bucketname', function (err, encConfig) {
828-
if (err) {
829-
return console.log(err)
830-
}
831-
console.log(encConfig)
832-
})
813+
await s3Client.getBucketEncryption('my-bucketname')
833814
```
834815

835816
<a name="removeBucketEncryption"></a>
836817

837-
### removeBucketEncryption(bucketName [, callback])
818+
### removeBucketEncryption(bucketName)
838819

839820
Remove encryption configuration of a Bucket
840821

841822
**Parameters**
842823

843-
| Param | Type | Description |
844-
| --------------- | ---------- | ----------------------------------------------- |
845-
| `bucketName` | _string_ | Name of the bucket. |
846-
| `callback(err)` | _function_ | Callback is called with `err` in case of error. |
824+
| Param | Type | Description |
825+
| ------------ | -------- | ------------------- |
826+
| `bucketName` | _string_ | Name of the bucket. |
847827

848828
**Example **
849829
Remove Encryption configuration of a Bucket
850830

851831
```js
852-
s3Client.removeBucketEncryption('my-bucketname', function (err) {
853-
if (err) {
854-
return console.log(err)
855-
}
856-
console.log('Success')
857-
})
832+
await s3Client.removeBucketEncryption('my-bucketname')
858833
```
859834

860835
## 3. Object operations
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,5 @@ const s3Client = new Minio.Client({
2525
secretKey: 'YOUR-SECRETACCESSKEY',
2626
})
2727

28-
s3Client.removeBucketEncryption('my-bucket', function (error) {
29-
if (error) {
30-
return console.log(error)
31-
}
32-
console.log('Success')
33-
})
28+
const encConfig = await s3Client.getBucketEncryption('test-bucket')
29+
console.log(encConfig)
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,5 @@ const s3Client = new Minio.Client({
2525
secretKey: 'YOUR-SECRETACCESSKEY',
2626
})
2727

28-
s3Client.getBucketEncryption('my-bucket', function (error, enConfig) {
29-
if (error) {
30-
return console.log(error)
31-
}
32-
console.log(enConfig)
33-
})
28+
await s3Client.removeBucketEncryption('test-bucket')
29+
console.log('Successfully removed bucket encryption')
Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ const s3Client = new Minio.Client({
2626
})
2727

2828
//Apply default encryption.
29-
s3Client.setBucketEncryption('my-bucket', function (error) {
30-
if (error) {
31-
return console.log(error)
32-
}
33-
console.log('Success')
34-
})
29+
try {
30+
await s3Client.setBucketEncryption('test-bucket')
31+
console.log('Successfully set bucket default encryption with AES256 Algorithm')
32+
} catch (err) {
33+
console.error(err)
34+
}
3535

3636
//Set Encryption Rule. Only one rule is allowed.
3737

@@ -50,12 +50,8 @@ const encryptionConfig = {
5050
],
5151
}
5252

53-
s3Client.setBucketEncryption('my-bucket', encryptionConfig, function (error) {
54-
if (error) {
55-
return console.log(error)
56-
}
57-
console.log('Success')
58-
})
53+
await s3Client.setBucketEncryption('test-bucket', encryptionConfig)
54+
console.log('Successfully set bucket encryption AES256 Algorithm')
5955

6056
/**
6157
* KMS ID based SSE Encryption
@@ -96,9 +92,5 @@ const kmsIdEncryptionConfig = {
9692
],
9793
}
9894

99-
s3Client.setBucketEncryption('my-bucket', kmsIdEncryptionConfig, function (error) {
100-
if (error) {
101-
return console.log(error)
102-
}
103-
console.log('Success')
104-
})
95+
await s3Client.setBucketEncryption('test-bucket', kmsIdEncryptionConfig)
96+
console.log('Successfully set bucket encryption with kms key')

package-lock.json

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/internal/client.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import type {
5959
BucketItemStat,
6060
BucketStream,
6161
BucketVersioningConfiguration,
62+
EncryptionConfig,
6263
GetObjectLegalHoldOptions,
6364
IncompleteUploadedBucketItem,
6465
IRequest,
@@ -2290,4 +2291,62 @@ export class TypedClient {
22902291
const body = await readAsBuffer(res)
22912292
return parseSelectObjectContentResponse(body)
22922293
}
2294+
async setBucketEncryption(bucketName: string, encryptionConfig?: EncryptionConfig): Promise<void> {
2295+
if (!isValidBucketName(bucketName)) {
2296+
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
2297+
}
2298+
if (!_.isEmpty(encryptionConfig) && encryptionConfig.Rule.length > 1) {
2299+
throw new errors.InvalidArgumentError('Invalid Rule length. Only one rule is allowed.: ' + encryptionConfig.Rule)
2300+
}
2301+
2302+
let encryptionObj = encryptionConfig
2303+
if (_.isEmpty(encryptionConfig)) {
2304+
encryptionObj = {
2305+
// Default MinIO Server Supported Rule
2306+
Rule: [
2307+
{
2308+
ApplyServerSideEncryptionByDefault: {
2309+
SSEAlgorithm: 'AES256',
2310+
},
2311+
},
2312+
],
2313+
}
2314+
}
2315+
2316+
const method = 'PUT'
2317+
const query = 'encryption'
2318+
const builder = new xml2js.Builder({
2319+
rootName: 'ServerSideEncryptionConfiguration',
2320+
renderOpts: { pretty: false },
2321+
headless: true,
2322+
})
2323+
const payload = builder.buildObject(encryptionObj)
2324+
2325+
const headers: RequestHeaders = {}
2326+
headers['Content-MD5'] = toMd5(payload)
2327+
2328+
await this.makeRequestAsyncOmit({ method, bucketName, query, headers }, payload)
2329+
}
2330+
2331+
async getBucketEncryption(bucketName: string) {
2332+
if (!isValidBucketName(bucketName)) {
2333+
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
2334+
}
2335+
const method = 'GET'
2336+
const query = 'encryption'
2337+
2338+
const res = await this.makeRequestAsync({ method, bucketName, query })
2339+
const body = await readAsString(res)
2340+
return xmlParsers.parseBucketEncryptionConfig(body)
2341+
}
2342+
2343+
async removeBucketEncryption(bucketName: string) {
2344+
if (!isValidBucketName(bucketName)) {
2345+
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
2346+
}
2347+
const method = 'DELETE'
2348+
const query = 'encryption'
2349+
2350+
await this.makeRequestAsyncOmit({ method, bucketName, query }, '', [204])
2351+
}
22932352
}

src/internal/type.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,15 @@ export type SelectOptions = {
316316
requestProgress?: SelectProgress
317317
scanRange?: ScanRange
318318
}
319+
export type ApplySSEByDefault = {
320+
KmsMasterKeyID?: string
321+
SSEAlgorithm: string
322+
}
323+
324+
export type EncryptionRule = {
325+
ApplyServerSideEncryptionByDefault?: ApplySSEByDefault
326+
}
327+
328+
export type EncryptionConfig = {
329+
Rule: EncryptionRule[]
330+
}

src/internal/xml-parser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,7 @@ export function parseSelectObjectContentResponse(res: Buffer) {
539539
}
540540
}
541541
}
542+
543+
export function parseBucketEncryptionConfig(xml: string) {
544+
return parseXml(xml)
545+
}

src/minio.d.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -111,37 +111,19 @@ export type LockUnit = RETENTION_VALIDITY_UNITS
111111

112112
export type VersioningConfig = Record<string | number | symbol, unknown>
113113
export type TagList = Record<string, string>
114-
export type Lifecycle = LifecycleConfig | null | ''
115-
export type Encryption = EncryptionConfig | EmptyObject
116114
export interface PostPolicyResult {
117115
postURL: string
118116
formData: {
119117
[key: string]: any
120118
}
121119
}
122120

123-
export interface LifecycleConfig {
124-
Rule: LifecycleRule[]
125-
}
126-
127-
export interface LifecycleRule {
128-
[key: string]: any
129-
}
130-
131121
export interface LockConfig {
132122
mode: RETENTION_MODES
133123
unit: RETENTION_VALIDITY_UNITS
134124
validity: number
135125
}
136126

137-
export interface EncryptionConfig {
138-
Rule: EncryptionRule[]
139-
}
140-
141-
export interface EncryptionRule {
142-
[key: string]: any
143-
}
144-
145127
export interface LegalHoldOptions {
146128
versionId: string
147129
status: LEGAL_HOLD_STATUS
@@ -161,27 +143,6 @@ export class Client extends TypedClient {
161143

162144
listObjectsV2(bucketName: string, prefix?: string, recursive?: boolean, startAfter?: string): BucketStream<BucketItem>
163145

164-
setBucketVersioning(bucketName: string, versioningConfig: any, callback: NoResultCallback): void
165-
setBucketVersioning(bucketName: string, versioningConfig: any): Promise<void>
166-
167-
setBucketLifecycle(bucketName: string, lifecycleConfig: Lifecycle, callback: NoResultCallback): void
168-
setBucketLifecycle(bucketName: string, lifecycleConfig: Lifecycle): Promise<void>
169-
170-
getBucketLifecycle(bucketName: string, callback: ResultCallback<Lifecycle>): void
171-
getBucketLifecycle(bucketName: string): Promise<Lifecycle>
172-
173-
removeBucketLifecycle(bucketName: string, callback: NoResultCallback): void
174-
removeBucketLifecycle(bucketName: string): Promise<void>
175-
176-
getBucketEncryption(bucketName: string, callback: ResultCallback<Encryption>): void
177-
getBucketEncryption(bucketName: string): Promise<Encryption>
178-
179-
setBucketEncryption(bucketName: string, encryptionConfig: Encryption, callback: NoResultCallback): void
180-
setBucketEncryption(bucketName: string, encryptionConfig: Encryption): Promise<void>
181-
182-
removeBucketEncryption(bucketName: string, callback: NoResultCallback): void
183-
removeBucketEncryption(bucketName: string): Promise<void>
184-
185146
copyObject(
186147
bucketName: string,
187148
objectName: string,

0 commit comments

Comments
 (0)