UNPKG

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