UNPKG

13 kBJavaScriptView Raw
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.
14import { ServiceObject, } from './nodejs-common/index.js';
15import { IdempotencyStrategy } from './storage.js';
16import { promisifyAll } from '@google-cloud/promisify';
17/**
18 * The API-formatted resource description of the HMAC key.
19 *
20 * Note: This is not guaranteed to be up-to-date when accessed. To get the
21 * latest record, call the `getMetadata()` method.
22 *
23 * @name HmacKey#metadata
24 * @type {object}
25 */
26/**
27 * An HmacKey object contains metadata of an HMAC key created from a
28 * service account through the {@link Storage} client using
29 * {@link Storage#createHmacKey}.
30 *
31 * See {@link https://cloud.google.com/storage/docs/authentication/hmackeys| HMAC keys documentation}
32 *
33 * @class
34 */
35export class HmacKey extends ServiceObject {
36 /**
37 * @typedef {object} HmacKeyOptions
38 * @property {string} [projectId] The project ID of the project that owns
39 * the service account of the requested HMAC key. If not provided,
40 * the project ID used to instantiate the Storage client will be used.
41 */
42 /**
43 * Constructs an HmacKey object.
44 *
45 * Note: this only create a local reference to an HMAC key, to create
46 * an HMAC key, use {@link Storage#createHmacKey}.
47 *
48 * @param {Storage} storage The Storage instance this HMAC key is
49 * attached to.
50 * @param {string} accessId The unique accessId for this HMAC key.
51 * @param {HmacKeyOptions} options Constructor configurations.
52 * @example
53 * ```
54 * const {Storage} = require('@google-cloud/storage');
55 * const storage = new Storage();
56 * const hmacKey = storage.hmacKey('access-id');
57 * ```
58 */
59 constructor(storage, accessId, options) {
60 const methods = {
61 /**
62 * @typedef {object} DeleteHmacKeyOptions
63 * @property {string} [userProject] This parameter is currently ignored.
64 */
65 /**
66 * @typedef {array} DeleteHmacKeyResponse
67 * @property {object} 0 The full API response.
68 */
69 /**
70 * @callback DeleteHmacKeyCallback
71 * @param {?Error} err Request error, if any.
72 * @param {object} apiResponse The full API response.
73 */
74 /**
75 * Deletes an HMAC key.
76 * Key state must be set to `INACTIVE` prior to deletion.
77 * Caution: HMAC keys cannot be recovered once you delete them.
78 *
79 * The authenticated user must have `storage.hmacKeys.delete` permission for the project in which the key exists.
80 *
81 * @method HmacKey#delete
82 * @param {DeleteHmacKeyOptions} [options] Configuration options.
83 * @param {DeleteHmacKeyCallback} [callback] Callback function.
84 * @returns {Promise<DeleteHmacKeyResponse>}
85 *
86 * @example
87 * ```
88 * const {Storage} = require('@google-cloud/storage');
89 * const storage = new Storage();
90 *
91 * //-
92 * // Delete HMAC key after making the key inactive.
93 * //-
94 * const hmacKey = storage.hmacKey('ACCESS_ID');
95 * hmacKey.setMetadata({state: 'INACTIVE'}, (err, hmacKeyMetadata) => {
96 * if (err) {
97 * // The request was an error.
98 * console.error(err);
99 * return;
100 * }
101 * hmacKey.delete((err) => {
102 * if (err) {
103 * console.error(err);
104 * return;
105 * }
106 * // The HMAC key is deleted.
107 * });
108 * });
109 *
110 * //-
111 * // If the callback is omitted, a promise is returned.
112 * //-
113 * const hmacKey = storage.hmacKey('ACCESS_ID');
114 * hmacKey
115 * .setMetadata({state: 'INACTIVE'})
116 * .then(() => {
117 * return hmacKey.delete();
118 * });
119 * ```
120 */
121 delete: true,
122 /**
123 * @callback GetHmacKeyCallback
124 * @param {?Error} err Request error, if any.
125 * @param {HmacKey} hmacKey this {@link HmacKey} instance.
126 * @param {object} apiResponse The full API response.
127 */
128 /**
129 * @typedef {array} GetHmacKeyResponse
130 * @property {HmacKey} 0 This {@link HmacKey} instance.
131 * @property {object} 1 The full API response.
132 */
133 /**
134 * @typedef {object} GetHmacKeyOptions
135 * @property {string} [userProject] This parameter is currently ignored.
136 */
137 /**
138 * Retrieves and populate an HMAC key's metadata, and return
139 * this {@link HmacKey} instance.
140 *
141 * HmacKey.get() does not give the HMAC key secret, as
142 * it is only returned on creation.
143 *
144 * The authenticated user must have `storage.hmacKeys.get` permission
145 * for the project in which the key exists.
146 *
147 * @method HmacKey#get
148 * @param {GetHmacKeyOptions} [options] Configuration options.
149 * @param {GetHmacKeyCallback} [callback] Callback function.
150 * @returns {Promise<GetHmacKeyResponse>}
151 *
152 * @example
153 * ```
154 * const {Storage} = require('@google-cloud/storage');
155 * const storage = new Storage();
156 *
157 * //-
158 * // Get the HmacKey's Metadata.
159 * //-
160 * storage.hmacKey('ACCESS_ID')
161 * .get((err, hmacKey) => {
162 * if (err) {
163 * // The request was an error.
164 * console.error(err);
165 * return;
166 * }
167 * // do something with the returned HmacKey object.
168 * });
169 *
170 * //-
171 * // If the callback is omitted, a promise is returned.
172 * //-
173 * storage.hmacKey('ACCESS_ID')
174 * .get()
175 * .then((data) => {
176 * const hmacKey = data[0];
177 * });
178 * ```
179 */
180 get: true,
181 /**
182 * @typedef {object} GetHmacKeyMetadataOptions
183 * @property {string} [userProject] This parameter is currently ignored.
184 */
185 /**
186 * Retrieves and populate an HMAC key's metadata, and return
187 * the HMAC key's metadata as an object.
188 *
189 * HmacKey.getMetadata() does not give the HMAC key secret, as
190 * it is only returned on creation.
191 *
192 * The authenticated user must have `storage.hmacKeys.get` permission
193 * for the project in which the key exists.
194 *
195 * @method HmacKey#getMetadata
196 * @param {GetHmacKeyMetadataOptions} [options] Configuration options.
197 * @param {HmacKeyMetadataCallback} [callback] Callback function.
198 * @returns {Promise<HmacKeyMetadataResponse>}
199 *
200 * @example
201 * ```
202 * const {Storage} = require('@google-cloud/storage');
203 * const storage = new Storage();
204 *
205 * //-
206 * // Get the HmacKey's metadata and populate to the metadata property.
207 * //-
208 * storage.hmacKey('ACCESS_ID')
209 * .getMetadata((err, hmacKeyMetadata) => {
210 * if (err) {
211 * // The request was an error.
212 * console.error(err);
213 * return;
214 * }
215 * console.log(hmacKeyMetadata);
216 * });
217 *
218 * //-
219 * // If the callback is omitted, a promise is returned.
220 * //-
221 * storage.hmacKey('ACCESS_ID')
222 * .getMetadata()
223 * .then((data) => {
224 * const hmacKeyMetadata = data[0];
225 * console.log(hmacKeyMetadata);
226 * });
227 * ```
228 */
229 getMetadata: true,
230 /**
231 * @typedef {object} SetHmacKeyMetadata Subset of {@link HmacKeyMetadata} to update.
232 * @property {string} state New state of the HmacKey. Either 'ACTIVE' or 'INACTIVE'.
233 * @property {string} [etag] Include an etag from a previous get HMAC key request
234 * to perform safe read-modify-write.
235 */
236 /**
237 * @typedef {object} SetHmacKeyMetadataOptions
238 * @property {string} [userProject] This parameter is currently ignored.
239 */
240 /**
241 * @callback HmacKeyMetadataCallback
242 * @param {?Error} err Request error, if any.
243 * @param {HmacKeyMetadata} metadata The updated {@link HmacKeyMetadata} object.
244 * @param {object} apiResponse The full API response.
245 */
246 /**
247 * @typedef {array} HmacKeyMetadataResponse
248 * @property {HmacKeyMetadata} 0 The updated {@link HmacKeyMetadata} object.
249 * @property {object} 1 The full API response.
250 */
251 /**
252 * Updates the state of an HMAC key. See {@link SetHmacKeyMetadata} for
253 * valid states.
254 *
255 * @method HmacKey#setMetadata
256 * @param {SetHmacKeyMetadata} metadata The new metadata.
257 * @param {SetHmacKeyMetadataOptions} [options] Configuration options.
258 * @param {HmacKeyMetadataCallback} [callback] Callback function.
259 * @returns {Promise<HmacKeyMetadataResponse>}
260 *
261 * @example
262 * ```
263 * const {Storage} = require('@google-cloud/storage');
264 * const storage = new Storage();
265 *
266 * const metadata = {
267 * state: 'INACTIVE',
268 * };
269 *
270 * storage.hmacKey('ACCESS_ID')
271 * .setMetadata(metadata, (err, hmacKeyMetadata) => {
272 * if (err) {
273 * // The request was an error.
274 * console.error(err);
275 * return;
276 * }
277 * console.log(hmacKeyMetadata);
278 * });
279 *
280 * //-
281 * // If the callback is omitted, a promise is returned.
282 * //-
283 * storage.hmacKey('ACCESS_ID')
284 * .setMetadata(metadata)
285 * .then((data) => {
286 * const hmacKeyMetadata = data[0];
287 * console.log(hmacKeyMetadata);
288 * });
289 * ```
290 */
291 setMetadata: {
292 reqOpts: {
293 method: 'PUT',
294 },
295 },
296 };
297 const projectId = (options && options.projectId) || storage.projectId;
298 super({
299 parent: storage,
300 id: accessId,
301 baseUrl: `/projects/${projectId}/hmacKeys`,
302 methods,
303 });
304 this.storage = storage;
305 this.instanceRetryValue = storage.retryOptions.autoRetry;
306 }
307 setMetadata(metadata, optionsOrCallback, cb) {
308 // ETag preconditions are not currently supported. Retries should be disabled if the idempotency strategy is not set to RetryAlways
309 if (this.storage.retryOptions.idempotencyStrategy !==
310 IdempotencyStrategy.RetryAlways) {
311 this.storage.retryOptions.autoRetry = false;
312 }
313 const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
314 cb =
315 typeof optionsOrCallback === 'function'
316 ? optionsOrCallback
317 : cb;
318 super
319 .setMetadata(metadata, options)
320 .then(resp => cb(null, ...resp))
321 .catch(cb)
322 .finally(() => {
323 this.storage.retryOptions.autoRetry = this.instanceRetryValue;
324 });
325 }
326}
327/*! Developer Documentation
328 *
329 * All async methods (except for streams) will return a Promise in the event
330 * that a callback is omitted.
331 */
332promisifyAll(HmacKey);