Skip to content

Commit 08a9ed3

Browse files
authored
feat(secret): add endpoint to generate password (#669)
1 parent f035cd6 commit 08a9ed3

File tree

4 files changed

+85
-35
lines changed

4 files changed

+85
-35
lines changed

packages/clients/src/api/secret/v1alpha1/api.gen.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
marshalAddSecretOwnerRequest,
1212
marshalCreateSecretRequest,
1313
marshalCreateSecretVersionRequest,
14+
marshalGeneratePasswordRequest,
1415
marshalUpdateSecretRequest,
1516
marshalUpdateSecretVersionRequest,
1617
unmarshalAccessSecretVersionResponse,
@@ -30,6 +31,7 @@ import type {
3031
DestroySecretVersionRequest,
3132
DisableSecretVersionRequest,
3233
EnableSecretVersionRequest,
34+
GeneratePasswordRequest,
3335
GetSecretByNameRequest,
3436
GetSecretRequest,
3537
GetSecretVersionByNameRequest,
@@ -238,6 +240,33 @@ export class API extends ParentAPI {
238240
unmarshalSecretVersion,
239241
)
240242

243+
/**
244+
* Generate a password in a new version. Generate a password for the given
245+
* secret specified by the `region` and `secret_id` parameters. This will also
246+
* create a new version of the secret that will store the password.
247+
*
248+
* @param request - The request {@link GeneratePasswordRequest}
249+
* @returns A Promise of SecretVersion
250+
*/
251+
generatePassword = (request: Readonly<GeneratePasswordRequest>) =>
252+
this.client.fetch<SecretVersion>(
253+
{
254+
body: JSON.stringify(
255+
marshalGeneratePasswordRequest(request, this.client.settings),
256+
),
257+
headers: jsonContentHeaders,
258+
method: 'POST',
259+
path: `/secret-manager/v1alpha1/regions/${validatePathParam(
260+
'region',
261+
request.region ?? this.client.settings.defaultRegion,
262+
)}/secrets/${validatePathParam(
263+
'secretId',
264+
request.secretId,
265+
)}/generate-password`,
266+
},
267+
unmarshalSecretVersion,
268+
)
269+
241270
/**
242271
* Get metadata of a secret's version using the secret's ID. Retrieve the
243272
* metadata of a secret's given version specified by the `region`, `secret_id`

packages/clients/src/api/secret/v1alpha1/index.gen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export type {
1212
DestroySecretVersionRequest,
1313
DisableSecretVersionRequest,
1414
EnableSecretVersionRequest,
15+
GeneratePasswordRequest,
1516
GetSecretByNameRequest,
1617
GetSecretRequest,
1718
GetSecretVersionByNameRequest,

packages/clients/src/api/secret/v1alpha1/marshalling.gen.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// If you have any remark or suggestion do not hesitate to open an issue.
33
import {
44
isJSONObject,
5-
resolveOneOf,
65
unmarshalArrayOfObject,
76
unmarshalDate,
87
} from '../../../bridge'
@@ -12,9 +11,9 @@ import type {
1211
AddSecretOwnerRequest,
1312
CreateSecretRequest,
1413
CreateSecretVersionRequest,
14+
GeneratePasswordRequest,
1515
ListSecretVersionsResponse,
1616
ListSecretsResponse,
17-
PasswordGenerationParams,
1817
Secret,
1918
SecretVersion,
2019
UpdateSecretRequest,
@@ -102,17 +101,6 @@ export const unmarshalListSecretsResponse = (data: unknown) => {
102101
} as ListSecretsResponse
103102
}
104103

105-
const marshalPasswordGenerationParams = (
106-
request: PasswordGenerationParams,
107-
defaults: DefaultValues,
108-
): Record<string, unknown> => ({
109-
additional_chars: request.additionalChars,
110-
length: request.length,
111-
no_digits: request.noDigits,
112-
no_lowercase_letters: request.noLowercaseLetters,
113-
no_uppercase_letters: request.noUppercaseLetters,
114-
})
115-
116104
export const marshalAddSecretOwnerRequest = (
117105
request: AddSecretOwnerRequest,
118106
defaults: DefaultValues,
@@ -138,14 +126,19 @@ export const marshalCreateSecretVersionRequest = (
138126
data_crc32: request.dataCrc32,
139127
description: request.description,
140128
disable_previous: request.disablePrevious,
141-
...resolveOneOf([
142-
{
143-
param: 'password_generation',
144-
value: request.passwordGeneration
145-
? marshalPasswordGenerationParams(request.passwordGeneration, defaults)
146-
: undefined,
147-
},
148-
]),
129+
})
130+
131+
export const marshalGeneratePasswordRequest = (
132+
request: GeneratePasswordRequest,
133+
defaults: DefaultValues,
134+
): Record<string, unknown> => ({
135+
additional_chars: request.additionalChars,
136+
description: request.description,
137+
disable_previous: request.disablePrevious,
138+
length: request.length,
139+
no_digits: request.noDigits,
140+
no_lowercase_letters: request.noLowercaseLetters,
141+
no_uppercase_letters: request.noUppercaseLetters,
149142
})
150143

151144
export const marshalUpdateSecretRequest = (

packages/clients/src/api/secret/v1alpha1/types.gen.ts

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export interface SecretVersion {
117117
updatedAt?: Date
118118
/** Description of the version. */
119119
description?: string
120-
/** True if the version is the latest one. */
120+
/** Returns `true` if the version is the latest. */
121121
isLatest: boolean
122122
}
123123

@@ -234,23 +234,50 @@ export type CreateSecretVersionRequest = {
234234
*/
235235
disablePrevious?: boolean
236236
/**
237-
* Options to generate a password. Optional. If specified, a random password
238-
* will be generated. The `data` and `data_crc32` fields must be empty. By
239-
* default, the generator will use upper and lower case letters, and digits.
240-
* This behavior can be tuned using the generation parameters.
241-
*
242-
* One-of ('PasswordGeneration'): at most one of 'passwordGeneration' could be
237+
* (Optional.) The CRC32 checksum of the data as a base-10 integer. If
238+
* specified, Secret Manager will verify the integrity of the data received
239+
* against the given CRC32 checksum. An error is returned if the CRC32 does
240+
* not match. If, however, the CRC32 matches, it will be stored and returned
241+
* along with the SecretVersion on future access requests.
242+
*/
243+
dataCrc32?: number
244+
}
245+
246+
export type GeneratePasswordRequest = {
247+
/**
248+
* Region to target. If none is passed will use default region from the
249+
* config.
250+
*/
251+
region?: Region
252+
/** ID of the secret. */
253+
secretId: string
254+
/** Description of the version. */
255+
description?: string
256+
/**
257+
* (Optional.) Disable the previous secret version. This has no effect if
258+
* there is no previous version or if the previous version was already
259+
* disabled.
260+
*/
261+
disablePrevious?: boolean
262+
/** Length of the password to generate (between 1 and 1024 characters). */
263+
length: number
264+
/**
265+
* (Optional.) Exclude lower case letters by default in the password character
243266
* set.
244267
*/
245-
passwordGeneration?: PasswordGenerationParams
268+
noLowercaseLetters?: boolean
246269
/**
247-
* The CRC32 checksum of the data as a base-10 integer. Optional. If
248-
* specified, Secret Manager will verify the integrity of the data received
249-
* against the given CRC32. An error is returned if the CRC32 does not match.
250-
* Otherwise, the CRC32 will be stored and returned along with the
251-
* SecretVersion on futur accesses.
270+
* (Optional.) Exclude upper case letters by default in the password character
271+
* set.
252272
*/
253-
dataCrc32?: number
273+
noUppercaseLetters?: boolean
274+
/** (Optional.) Exclude digits by default in the password character set. */
275+
noDigits?: boolean
276+
/**
277+
* (Optional.) Additional ASCII characters to be included in the password
278+
* character set.
279+
*/
280+
additionalChars?: string
254281
}
255282

256283
export type GetSecretVersionRequest = {

0 commit comments

Comments
 (0)