|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +const {Storage} = require('@google-cloud/storage'); |
| 18 | +const {assert} = require('chai'); |
| 19 | +const {before, after, afterEach, it} = require('mocha'); |
| 20 | +const cp = require('child_process'); |
| 21 | +const uuid = require('uuid'); |
| 22 | + |
| 23 | +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); |
| 24 | + |
| 25 | +const storage = new Storage(); |
| 26 | +const samplesTestBucketPrefix = `nodejs-storage-samples-${uuid.v4()}`; |
| 27 | +const bucketName = `${samplesTestBucketPrefix}-a`; |
| 28 | +const defaultKmsKeyName = process.env.GOOGLE_CLOUD_KMS_KEY_ASIA; |
| 29 | +const bucket = storage.bucket(bucketName); |
| 30 | + |
| 31 | +before(async () => { |
| 32 | + await storage.createBucket(bucketName); |
| 33 | +}); |
| 34 | + |
| 35 | +async function deleteAllBucketsAsync() { |
| 36 | + const [buckets] = await storage.getBuckets({prefix: samplesTestBucketPrefix}); |
| 37 | + |
| 38 | + for (const bucket of buckets) { |
| 39 | + await bucket.deleteFiles({force: true}); |
| 40 | + await bucket.delete({ignoreNotFound: true}); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +after(deleteAllBucketsAsync); |
| 45 | +afterEach(async () => { |
| 46 | + await new Promise(res => setTimeout(res, 1000)); |
| 47 | +}); |
| 48 | + |
| 49 | +it('should set bucket encryption enforcement configuration', async function () { |
| 50 | + if (!defaultKmsKeyName) { |
| 51 | + this.skip(); |
| 52 | + } |
| 53 | + const output = execSync( |
| 54 | + `node setBucketEncryptionEnforcementConfig.js ${bucketName} ${defaultKmsKeyName}` |
| 55 | + ); |
| 56 | + |
| 57 | + assert.include( |
| 58 | + output, |
| 59 | + `Encryption enforcement configuration updated for bucket ${bucketName}.` |
| 60 | + ); |
| 61 | + |
| 62 | + assert.include(output, `Default KMS Key: ${defaultKmsKeyName}`); |
| 63 | + |
| 64 | + assert.include(output, 'Google Managed (GMEK) Enforcement:'); |
| 65 | + assert.include(output, 'Mode: FullyRestricted'); |
| 66 | + |
| 67 | + assert.include(output, 'Customer Managed (CMEK) Enforcement:'); |
| 68 | + assert.include(output, 'Mode: NotRestricted'); |
| 69 | + |
| 70 | + assert.include(output, 'Customer Supplied (CSEK) Enforcement:'); |
| 71 | + assert.include(output, 'Mode: FullyRestricted'); |
| 72 | + |
| 73 | + assert.match(output, new RegExp('Effective:')); |
| 74 | + |
| 75 | + const [metadata] = await bucket.getMetadata(); |
| 76 | + const encryption = metadata.encryption || {}; |
| 77 | + assert.strictEqual( |
| 78 | + encryption.googleManagedEncryptionEnforcementConfig?.restrictionMode, |
| 79 | + 'FullyRestricted' |
| 80 | + ); |
| 81 | + assert.strictEqual( |
| 82 | + encryption.customerManagedEncryptionEnforcementConfig?.restrictionMode, |
| 83 | + 'NotRestricted' |
| 84 | + ); |
| 85 | + assert.strictEqual( |
| 86 | + encryption.customerSuppliedEncryptionEnforcementConfig?.restrictionMode, |
| 87 | + 'FullyRestricted' |
| 88 | + ); |
| 89 | +}); |
| 90 | + |
| 91 | +it('should get bucket encryption enforcement configuration', async function () { |
| 92 | + if (!defaultKmsKeyName) { |
| 93 | + this.skip(); |
| 94 | + } |
| 95 | + const output = execSync( |
| 96 | + `node getBucketEncryptionEnforcementConfig.js ${bucketName}` |
| 97 | + ); |
| 98 | + |
| 99 | + assert.include( |
| 100 | + output, |
| 101 | + `Encryption enforcement configuration for bucket ${bucketName}.` |
| 102 | + ); |
| 103 | + assert.include(output, `Default KMS Key: ${defaultKmsKeyName}`); |
| 104 | + |
| 105 | + assert.include(output, 'Google Managed (GMEK) Enforcement:'); |
| 106 | + assert.include(output, 'Mode: FullyRestricted'); |
| 107 | + assert.match(output, /Effective:/); |
| 108 | + |
| 109 | + const [metadata] = await bucket.getMetadata(); |
| 110 | + const encryption = metadata.encryption || {}; |
| 111 | + |
| 112 | + assert.strictEqual(encryption.defaultKmsKeyName, defaultKmsKeyName); |
| 113 | + assert.strictEqual( |
| 114 | + encryption.googleManagedEncryptionEnforcementConfig?.restrictionMode, |
| 115 | + 'FullyRestricted' |
| 116 | + ); |
| 117 | + assert.strictEqual( |
| 118 | + encryption.customerManagedEncryptionEnforcementConfig?.restrictionMode, |
| 119 | + 'NotRestricted' |
| 120 | + ); |
| 121 | + assert.strictEqual( |
| 122 | + encryption.customerSuppliedEncryptionEnforcementConfig?.restrictionMode, |
| 123 | + 'FullyRestricted' |
| 124 | + ); |
| 125 | +}); |
| 126 | + |
| 127 | +it('should update and then remove bucket encryption enforcement configuration', async () => { |
| 128 | + const output = execSync( |
| 129 | + `node updateBucketEncryptionEnforcementConfig.js ${bucketName}` |
| 130 | + ); |
| 131 | + |
| 132 | + assert.include( |
| 133 | + output, |
| 134 | + `Google-managed encryption enforcement set to FullyRestricted for ${bucketName}.` |
| 135 | + ); |
| 136 | + assert.include( |
| 137 | + output, |
| 138 | + `All encryption enforcement configurations removed from bucket ${bucketName}.` |
| 139 | + ); |
| 140 | + |
| 141 | + const [metadata] = await bucket.getMetadata(); |
| 142 | + assert.ok(!metadata.encryption); |
| 143 | +}); |
0 commit comments