Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hmac service account #751

Merged
merged 61 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
1c6bc94
feat: HmacKey CRUD methods
jkwlui Jun 19, 2019
1342a17
export types
jkwlui Jun 19, 2019
211eefc
fix docs
jkwlui Jun 19, 2019
89283e0
move tests
jkwlui Jun 19, 2019
e329709
fix type
jkwlui Jun 19, 2019
41fb442
json instead of body
jkwlui Jun 20, 2019
cbdd865
npm run fix
jkwlui Jun 20, 2019
f6416d4
Merge branch 'master' into hmac-sa-admin
jkwlui Jun 20, 2019
e677191
add request error test for createHmacKey
jkwlui Jun 21, 2019
1c7819d
Merge branch 'hmac-sa-admin' of github.com:googleapis/nodejs-storage …
jkwlui Jun 21, 2019
0c734f1
fix-lint
jkwlui Jun 21, 2019
645a231
do not stub metadata
jkwlui Jul 2, 2019
d70e045
expose inherited getMetadata instead of overriden get
jkwlui Jul 3, 2019
7311019
Add back get method inherited from ServiceObject
jkwlui Jul 3, 2019
762c8c1
Merge branch 'master' into hmac-sa-admin
jkwlui Jul 3, 2019
9266001
fix docs
jkwlui Jul 3, 2019
a4628b8
createHmacKey callback (err, hmacKey, secret, apiResponse)
jkwlui Jul 8, 2019
a9aaac1
npm run fix
jkwlui Jul 8, 2019
d090e38
fix docs
jkwlui Jul 9, 2019
cc59c28
fix dead link
jkwlui Jul 9, 2019
9c02842
add link
jkwlui Jul 9, 2019
d6888b2
override setMetadata to send PUT request
jkwlui Jul 12, 2019
2fef2de
npm run fix
jkwlui Jul 12, 2019
77630a3
remove unnecessary tests
jkwlui Jul 12, 2019
09a61a6
require @google-cloud/2.0.4
jkwlui Jul 12, 2019
c2a8d18
Merge branch 'master' into hmac-sa-admin
jkwlui Jul 16, 2019
29a351f
Merge branch 'master' into hmac-sa-admin
jkwlui Jul 18, 2019
7c6d7ab
move accessId argument check to Storage#hmacKey()
jkwlui Jul 18, 2019
8933248
remove assertion that test the HmacKey class
jkwlui Jul 18, 2019
536c8aa
change Storage#createHmacKey option test to be arbitrary object and t…
jkwlui Jul 18, 2019
5e44f08
combine return HmacKey object
jkwlui Jul 18, 2019
355b731
Storage#createHmacKey assert apiResponse is sent on Error
jkwlui Jul 18, 2019
8d74689
style
jkwlui Jul 18, 2019
c5a4752
do not set accessId property of HmacKey instance
jkwlui Jul 18, 2019
6d16dcf
getHmacKeys test user query is preserved in nextQuery
jkwlui Jul 18, 2019
70a03f0
style fixes
jkwlui Jul 18, 2019
cd42d00
nit: missing ;
jkwlui Jul 18, 2019
0b8cbb1
Merge branch 'master' into hmac-sa-admin
jkwlui Jul 29, 2019
ecd1e34
Merge branch 'master' into hmac-sa-admin
jkwlui Aug 1, 2019
fe38200
inherited methods are already promisified
jkwlui Aug 1, 2019
d0cc1d1
Merge branch 'hmac-sa-admin' of github.com:googleapis/nodejs-storage …
jkwlui Aug 1, 2019
ab11feb
remove test already done in parent
jkwlui Aug 1, 2019
dec9f1e
fix tests
jkwlui Aug 1, 2019
f12c4d8
docs(samples): HMAC SA Admin Samples (#793)
frankyn Aug 5, 2019
89fd9e6
Fix region tag.
frankyn Aug 6, 2019
941cb36
feat(hmacKey): support projectId option (#801)
jkwlui Aug 8, 2019
50bd079
Merge branch 'master' into hmac-sa-admin
frankyn Aug 8, 2019
3f00d92
Merge branch 'master' into hmac-sa-admin
stephenplusplus Aug 9, 2019
ea82660
fix(ts): HmacKey Methods
jkwlui Aug 9, 2019
b270054
Merge branch 'master' into hmac-sa-admin
jkwlui Aug 9, 2019
a4c4a66
feat: HMAC System tests (#750)
jkwlui Aug 13, 2019
28f5f33
feat(test): hmac key - service account pool and fix tests (#810)
jkwlui Aug 14, 2019
b1a2c61
Merge branch 'master' into hmac-sa-admin
jkwlui Aug 14, 2019
25ac621
Merge branch 'master' into hmac-sa-admin
jkwlui Aug 14, 2019
05e37be
npm run fix
jkwlui Aug 14, 2019
c816660
Merge branch 'master' into hmac-sa-admin
jkwlui Aug 15, 2019
19be674
fix copyright header
jkwlui Aug 19, 2019
bd26755
Merge branch 'hmac-sa-admin' of github.com:googleapis/nodejs-storage …
jkwlui Aug 19, 2019
48b7c43
Merge branch 'master' into hmac-sa-admin
jkwlui Aug 19, 2019
9588702
Merge branch 'master' into hmac-sa-admin
jkwlui Aug 21, 2019
1b80e24
fix header
jkwlui Aug 21, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
378 changes: 378 additions & 0 deletions src/hmacKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,378 @@
/**
* Copyright 2019 Google LLC. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {Metadata, ServiceObject} from '@google-cloud/common';
import {promisifyAll} from '@google-cloud/promisify';

import {Storage} from './storage';
import {normalize} from './util';

export interface HmacKeyMetadata {
accessId: string;
etag?: string;
id?: string;
projectId?: string;
serviceAccountEmail?: string;
state?: string;
timeCreated?: string;
updated?: string;
}

export interface UpdateHmacKeyOptions {
/**
* This parameter is currently ignored.
*/
userProject?: string;
}

export interface UpdateHmacKeyMetadata {
state?: 'ACTIVE' | 'INACTIVE';
etag?: string;
}

export interface ToggleHmacKeyOptions {
etag?: string;
}

export interface HmacKeyMetadataCallback {
(err: Error | null, metadata?: HmacKeyMetadata, apiResponse?: Metadata): void;
}

export type HmacKeyMetadataResponse = [HmacKeyMetadata, Metadata];

/**
* An HmacKey object contains metadata of an HMAC key created from a
* service account through the {@link Storage} client using
* {@link Storage#createHmacKey}.
*
* @see [HMAC keys documentation]{@link https://cloud.google.com/storage/docs/authentication/hmackeys}
*
* @class
*/
export class HmacKey extends ServiceObject<HmacKeyMetadata | undefined> {
accessId: string;
metadata: HmacKeyMetadata | undefined;

/**
* Constructs an HmacKey object.
*
* Note: this only create a local reference to an HMAC key, to create
* an HMAC key, use {@link Storage#createHmacKey}.
*
* @param {Storage} storage The Storage instance this HMAC key is
* attached to.
* @param {string} accessId The unique accessId for this HMAC key.
* @example
* const {Storage} = require('@google-cloud/storage');
* const storage = new Storage();
* const hmacKey = storage.hmacKey('access-id');
*/
constructor(storage: Storage, accessId: string) {
if (!accessId) {
jkwlui marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('An access ID is needed to create an HmacKey object.');
}

const methods = {
/**
* @typedef {object} DeleteHmacKeyOptions
* @property {string} [userProject] This parameter is currently ignored.
*/
/**
* @typedef {array} DeleteHmacKeyResponse
* @property {object} 0 The full API response.
*/
/**
* @callback DeleteHmacKeyCallback
* @param {?Error} err Request error, if any.
* @param {object} apiResponse The full API response.
*/
/**
* Deletes an HMAC key.
* Key state must be set to `INACTIVE` prior to deletion.
* Caution: HMAC keys cannot be recovered once you delete them.
*
* The authenticated user must have `storage.hmacKeys.delete` permission for the project in which the key exists.
*
* @method HmacKey#delete
* @param {DeleteHmacKeyOptions} [options] Configuration options.
* @param {DeleteHmacKeyCallback} [callback] Callback function.
* @returns {Promise<DeleteHmacKeyResponse>}
*
* @example
* const {Storage} = require('@google-cloud/storage');
* const storage = new Storage();
*
* //-
* // Delete HMAC key after making the key inactive.
jkwlui marked this conversation as resolved.
Show resolved Hide resolved
* //-
* const hmacKey = storage.hmacKey('ACCESS_ID');
* hmacKey.update({state: 'INACTIVE'}, (err, hmacKeyMetadata) => {
* if (err) {
* // The request was an error.
* console.error(err);
* return;
* }
* hmacKey.delete((err) => {
* if (err) {
* console.error(err);
* return;
* }
* // The HMAC key is deleted.
* })
jkwlui marked this conversation as resolved.
Show resolved Hide resolved
* });
*
* //-
* // If the callback is omitted, a promise is returned.
* //-
* const hmacKey = storage.hmacKey('ACCESS_ID');
* hmacKey
* .update({state: 'INACTIVE'})
* .then(() => {
* return hmacKey.delete();
* });
*/
delete: true,
/**
* @typedef {object} GetHmacKeyMetadataOptions
* @property {string} [userProject] This parameter is currently ignored.
*/
/**
* Retrieves and populate an HMAC key's metadata, and return
* the HMAC key's metadata as an object.
*
* HmacKey.getMetadata() does not give the HMAC key secret, as
* it is only returned on creation.
*
* The authenticated user must have `storage.hmacKeys.get` permission
* for the project in which the key exists.
*
* @method HmacKey#getMetadata
* @param {GetHmacKeyMetadataOptions} [options] Configuration options.
* @param {HmacKeyMetadataCallback} [callback] Callback function.
* @returns {Promise<HmacKeyMetadataResponse>}
*
* @example
* const {Storage} = require('@google-cloud/storage');
* const storage = new Storage();
*
* //-
* // Get the HmacKey's metadata and populate to the metadata property.
* //-
* storage.hmacKey('ACCESS_ID')
* .getMetadata((err, hmacKeyMetadata) => {
* if (err) {
* // The request was an error.
* console.error(err);
* return;
* }
* console.log(hmacKeyMetadata);
* });
*
* //-
* // If the callback is omitted, a promise is returned.
* //-
* storage.hmacKey('ACCESS_ID')
* .getMetadata()
* .then((data) => {
* const hmacKeyMetadata = data[0];
* console.log(hmacKeyMetadata);
* });
*/
getMetadata: true,
/**
* @callback GetHmacKeyCallback
* @param {?Error} err Request error, if any.
* @param {HmacKey} hmacKey this {@link HmacKey} instance.
* @param {object} apiResponse The full API response.
*/
/**
* @typedef {array} GetHmacKeyResponse
* @property {HmacKey} 0 This {@link HmacKey} instance.
* @property {object} 1 The full API response.
*/
/**
* @typedef {object} GetHmacKeyOptions
* @property {string} [userProject] This parameter is currently ignored.
*/
/**
* Retrieves and populate an HMAC key's metadata, and return
* this {@link HmacKey} instance.
*
* HmacKey.get() does not give the HMAC key secret, as
* it is only returned on creation.
*
* The authenticated user must have `storage.hmacKeys.get` permission
* for the project in which the key exists.
*
* @method HmacKey#get
* @param {GetHmacKeyOptions} [options] Configuration options.
* @param {GetHmacKeyCallback} [callback] Callback function.
* @returns {Promise<GetHmacKeyResponse>}
*
* @example
* const {Storage} = require('@google-cloud/storage');
* const storage = new Storage();
*
* //-
* // Get the HmacKey's Metadata.
* //-
* storage.hmacKey('ACCESS_ID')
* .get((err, hmacKey) => {
* if (err) {
* // The request was an error.
* console.error(err);
* return;
* }
* // do something with the returned HmacKey object.
* });
*
* //-
* // If the callback is omitted, a promise is returned.
* //-
* storage.hmacKey('ACCESS_ID')
* .get()
* .then((data) => {
* const hmacKey = data[0];
* });
*/
get: true,
};

super({
parent: storage,
baseUrl: `/projects/${storage.projectId}/hmacKeys`,
id: accessId,
methods,
});

this.accessId = accessId;
jkwlui marked this conversation as resolved.
Show resolved Hide resolved
}

update(
metadata: UpdateHmacKeyMetadata,
options?: UpdateHmacKeyOptions
): Promise<HmacKeyMetadataResponse>;
update(
metadata: UpdateHmacKeyMetadata,
callback: HmacKeyMetadataCallback
): void;
update(
metadata: UpdateHmacKeyMetadata,
options: UpdateHmacKeyOptions,
callback: HmacKeyMetadataCallback
): void;
/**
* @typedef {object} UpdateHmacKeyMetadata Subset of {@link HmacKeyMetadata} to update.
* @property {string} state New state of the HmacKey. Either 'ACTIVE' or 'INACTIVE'.
* @property {string} [etag] Include an etag from a previous get HMAC key request
* to perform safe read-modify-write.
*/
/**
* @typedef {object} UpdateHmacKeyOptions
* @property {string} [userProject] This parameter is currently ignored.
*/
/**
* @callback HmacKeyMetadataCallback
* @param {?Error} err Request error, if any.
* @param {HmacKeyMetadata} metadata The updated {@link HmacKeyMetadata} object.
* @param {object} apiResponse The full API response.
*/
/**
* @typedef {array} HmacKeyMetadataResponse
* @property {HmacKeyMetadata} 0 The updated {@link HmacKeyMetadata} object.
* @property {object} 1 The full API response.
*/
/**
* Updates the state of an HMAC key. See {@link UpdateHmacKeyMetadata} for
* valid states.
*
* @param {UpdateHmacKeyMetadata} metadata The new metadata.
* @param {UpdateHmacKeyOptions} [options] Configuration options.
* @param {HmacKeyMetadataCallback} [callback] Callback function.
* @returns {Promise<HmacKeyMetadataResponse>}
*
* @example
* const {Storage} = require('@google-cloud/storage');
* const storage = new Storage();
*
* const metadata = {
* state: 'INACTIVE',
* };
*
* storage.hmacKey('ACCESS_ID')
* .update(metadata, (err, hmacKeyMetadata) => {
* if (err) {
* // The request was an error.
* console.error(err);
* return;
* }
* console.log(hmacKeyMetadata);
* });
*
* //-
* // If the callback is omitted, a promise is returned.
* //-
* storage.hmacKey('ACCESS_ID')
* .update(metadata)
* .then((data) => {
* const hmacKeyMetadata = data[0];
* console.log(hmacKeyMetadata);
* });
*/
update(
metadata: UpdateHmacKeyMetadata,
optionsOrCb?: UpdateHmacKeyOptions | HmacKeyMetadataCallback,
cb?: HmacKeyMetadataCallback
): Promise<HmacKeyMetadataResponse> | void {
if (
typeof metadata !== 'object' ||
Object.getOwnPropertyNames(metadata).length === 0
) {
throw new Error(
'Cannot update HmacKey with an undefined/empty options object.'
);
}

const {options, callback} = normalize<
UpdateHmacKeyOptions,
HmacKeyMetadataCallback
>(optionsOrCb, cb);

this.request(
jkwlui marked this conversation as resolved.
Show resolved Hide resolved
{
uri: '/',
method: 'put',
qs: options,
json: metadata,
},
(err: Error | null, metadata?: HmacKeyMetadata, res?: Metadata) => {
if (err) {
callback!(err);
return;
}
this.metadata = metadata!;
callback!(err, metadata, res);
}
);
}
}

/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
promisifyAll(HmacKey);
jkwlui marked this conversation as resolved.
Show resolved Hide resolved
Loading