Skip to content

Commit 3cb666c

Browse files
committed
feat(storage): introduce HMAC key samples and tests for migration
1 parent 3b66abe commit 3cb666c

14 files changed

Lines changed: 572 additions & 0 deletions

storage/hmacKeyActivate.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
// sample-metadata:
18+
// title: Activate HMAC SA Key.
19+
// description: Activate HMAC SA Key.
20+
// usage: node hmacKeyActivate.js <hmacKeyAccessId> [projectId]
21+
22+
function main(
23+
hmacKeyAccessId = 'GOOG0234230X00',
24+
projectId = 'serviceAccountProjectId'
25+
) {
26+
// [START storage_activate_hmac_key]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The access ID of the HMAC key
31+
// const hmacKeyAccessId = 'GOOG0234230X00';
32+
33+
// The ID of the project to which the service account belongs
34+
// const projectId = 'project-id';
35+
36+
// Imports the Google Cloud client library
37+
const {Storage} = require('@google-cloud/storage');
38+
39+
// Creates a client
40+
const storage = new Storage();
41+
42+
// Activate HMAC SA Key
43+
async function activateHmacKey() {
44+
try {
45+
const hmacKey = storage.hmacKey(hmacKeyAccessId, {projectId});
46+
const [hmacKeyMetadata] = await hmacKey.setMetadata({state: 'ACTIVE'});
47+
48+
console.log('The HMAC key is now active.');
49+
console.log('The HMAC key metadata is:');
50+
for (const [key, value] of Object.entries(hmacKeyMetadata)) {
51+
console.log(`${key}: ${value}`);
52+
}
53+
} catch (error) {
54+
console.error(
55+
'Error executing activate hmac key:',
56+
error.message || error
57+
);
58+
}
59+
}
60+
61+
activateHmacKey();
62+
// [END storage_activate_hmac_key]
63+
}
64+
65+
main(...process.argv.slice(2));

storage/hmacKeyCreate.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
// sample-metadata:
18+
// title: Create HMAC SA Key.
19+
// description: Create HMAC SA Key.
20+
// usage: node hmacKeyCreate.js <serviceAccountEmail> [projectId]
21+
22+
function main(
23+
serviceAccountEmail = 'service-account@example.com',
24+
projectId = 'serviceAccountProjectId'
25+
) {
26+
// [START storage_create_hmac_key]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The service account email for which the new HMAC key will be created
31+
// const serviceAccountEmail = 'service-account@iam.gserviceaccount.com';
32+
33+
// The ID of the project to which the service account belongs
34+
// const projectId = 'project-id';
35+
36+
// Imports the Google Cloud client library
37+
const {Storage} = require('@google-cloud/storage');
38+
39+
// Creates a client
40+
const storage = new Storage();
41+
42+
// Create HMAC SA Key
43+
async function createHmacKey() {
44+
try {
45+
const [hmacKey, secret] = await storage.createHmacKey(
46+
serviceAccountEmail,
47+
{
48+
projectId,
49+
}
50+
);
51+
52+
console.log(`The base64 encoded secret is: ${secret}`);
53+
console.log('Do not miss that secret, there is no API to recover it.');
54+
console.log('The HMAC key metadata is:');
55+
for (const [key, value] of Object.entries(hmacKey.metadata)) {
56+
console.log(`${key}: ${value}`);
57+
}
58+
} catch (error) {
59+
console.error('Error executing create hmac key:', error.message || error);
60+
}
61+
}
62+
63+
createHmacKey();
64+
// [END storage_create_hmac_key]
65+
}
66+
67+
main(...process.argv.slice(2));

storage/hmacKeyDeactivate.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
// sample-metadata:
18+
// title: Deactivate HMAC SA Key.
19+
// description: Deactivate HMAC SA Key.
20+
// usage: node hmacKeyDeactivate.js <hmacKeyAccessId> [projectId]
21+
22+
function main(
23+
hmacKeyAccessId = 'GOOG0234230X00',
24+
projectId = 'serviceAccountProjectId'
25+
) {
26+
// [START storage_deactivate_hmac_key]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The access ID of the HMAC key
31+
// const hmacKeyAccessId = 'GOOG0234230X00';
32+
33+
// The ID of the project to which the service account belongs
34+
// const projectId = 'project-id';
35+
36+
// Imports the Google Cloud client library
37+
const {Storage} = require('@google-cloud/storage');
38+
39+
// Creates a client
40+
const storage = new Storage();
41+
42+
// Deactivate HMAC SA Key
43+
async function deactivateHmacKey() {
44+
try {
45+
const hmacKey = storage.hmacKey(hmacKeyAccessId, {projectId});
46+
const [hmacKeyMetadata] = await hmacKey.setMetadata({state: 'INACTIVE'});
47+
48+
console.log('The HMAC key is now inactive.');
49+
console.log('The HMAC key metadata is:');
50+
for (const [key, value] of Object.entries(hmacKeyMetadata)) {
51+
console.log(`${key}: ${value}`);
52+
}
53+
} catch (error) {
54+
console.error(
55+
'Error executing deactivate hmac key:',
56+
error.message || error
57+
);
58+
}
59+
}
60+
61+
deactivateHmacKey();
62+
// [END storage_deactivate_hmac_key]
63+
}
64+
65+
main(...process.argv.slice(2));

storage/hmacKeyDelete.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
// sample-metadata:
18+
// title: Delete HMAC SA Key.
19+
// description: Delete HMAC SA Key.
20+
// usage: node hmacKeyDelete.js <hmacKeyAccessId> [projectId]
21+
22+
function main(
23+
hmacKeyAccessId = 'GOOG0234230X00',
24+
projectId = 'serviceAccountProjectId'
25+
) {
26+
// [START storage_delete_hmac_key]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The access ID of the HMAC key
31+
// const hmacKeyAccessId = 'GOOG0234230X00';
32+
33+
// The ID of the project to which the service account belongs
34+
// const projectId = 'project-id';
35+
36+
// Imports the Google Cloud client library
37+
const {Storage} = require('@google-cloud/storage');
38+
39+
// Creates a client
40+
const storage = new Storage();
41+
42+
// Delete HMAC SA Key
43+
async function deleteHmacKey() {
44+
try {
45+
const hmacKey = storage.hmacKey(hmacKeyAccessId, {projectId});
46+
await hmacKey.delete();
47+
48+
console.log(
49+
'The key is deleted, though it may still appear in getHmacKeys() results.'
50+
);
51+
} catch (error) {
52+
console.error('Error executing delete hmac key:', error.message || error);
53+
}
54+
}
55+
56+
deleteHmacKey();
57+
// [END storage_delete_hmac_key]
58+
}
59+
60+
main(...process.argv.slice(2));

storage/hmacKeyGet.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
// sample-metadata:
18+
// title: Get HMAC SA Key Metadata.
19+
// description: Get HMAC SA Key Metadata.
20+
// usage: node hmacKeyGet.js <hmacKeyAccessId> [projectId]
21+
22+
function main(
23+
hmacKeyAccessId = 'GOOG0234230X00',
24+
projectId = 'serviceAccountProjectId'
25+
) {
26+
// [START storage_get_hmac_key]
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The access ID of the HMAC key
31+
// const hmacKeyAccessId = 'GOOG0234230X00';
32+
33+
// The ID of the project to which the service account belongs
34+
// const projectId = 'project-id';
35+
36+
// Imports the Google Cloud client library
37+
const {Storage} = require('@google-cloud/storage');
38+
39+
// Creates a client
40+
const storage = new Storage();
41+
42+
// Get HMAC SA Key Metadata
43+
async function getHmacKey() {
44+
try {
45+
const hmacKey = storage.hmacKey(hmacKeyAccessId, {projectId});
46+
47+
// Populate the hmacKey object with metadata from server.
48+
await hmacKey.getMetadata();
49+
50+
console.log('The HMAC key metadata is:');
51+
for (const [key, value] of Object.entries(hmacKey.metadata)) {
52+
console.log(`${key}: ${value}`);
53+
}
54+
} catch (error) {
55+
console.error('Error executing get hmac key:', error.message || error);
56+
}
57+
}
58+
59+
getHmacKey();
60+
// [END storage_get_hmac_key]
61+
}
62+
63+
main(...process.argv.slice(2));

storage/hmacKeysList.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
// sample-metadata:
18+
// title: List HMAC SA Keys Metadata.
19+
// description: List HMAC SA Keys Metadata.
20+
// usage: node hmacKeysList.js [projectId]
21+
22+
function main(projectId = 'serviceAccountProjectId') {
23+
// [START storage_list_hmac_keys]
24+
/**
25+
* TODO(developer): Uncomment the following lines before running the sample.
26+
*/
27+
// The ID of the project to which the service account belongs
28+
// const projectId = 'project-id';
29+
30+
// Imports the Google Cloud client library
31+
const {Storage} = require('@google-cloud/storage');
32+
33+
// Creates a client
34+
const storage = new Storage();
35+
36+
// List HMAC SA Keys' Metadata
37+
async function listHmacKeys() {
38+
try {
39+
const [hmacKeys] = await storage.getHmacKeys({projectId});
40+
41+
// hmacKeys is an array of HmacKey objects.
42+
for (const hmacKey of hmacKeys) {
43+
console.log(
44+
`Service Account Email: ${hmacKey.metadata.serviceAccountEmail}`
45+
);
46+
console.log(`Access Id: ${hmacKey.metadata.accessId}`);
47+
}
48+
} catch (error) {
49+
console.error('Error executing list hmac keys:', error.message || error);
50+
}
51+
}
52+
53+
listHmacKeys();
54+
// [END storage_list_hmac_keys]
55+
}
56+
57+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)