Skip to content

Commit 71da28f

Browse files
authored
feat(secret): add endpoint to generate password (#185)
1 parent 1b0252f commit 71da28f

File tree

6 files changed

+268
-96
lines changed

6 files changed

+268
-96
lines changed

scaleway-async/scaleway_async/secret/v1alpha1/api.py

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@
1717
AccessSecretVersionResponse,
1818
ListSecretVersionsResponse,
1919
ListSecretsResponse,
20-
PasswordGenerationParams,
2120
Secret,
2221
SecretVersion,
2322
CreateSecretRequest,
2423
UpdateSecretRequest,
2524
AddSecretOwnerRequest,
2625
CreateSecretVersionRequest,
26+
GeneratePasswordRequest,
2727
UpdateSecretVersionRequest,
2828
)
2929
from .marshalling import (
3030
marshal_AddSecretOwnerRequest,
3131
marshal_CreateSecretRequest,
3232
marshal_CreateSecretVersionRequest,
33+
marshal_GeneratePasswordRequest,
3334
marshal_UpdateSecretRequest,
3435
marshal_UpdateSecretVersionRequest,
3536
unmarshal_Secret,
@@ -395,7 +396,6 @@ async def create_secret_version(
395396
region: Optional[Region] = None,
396397
description: Optional[str] = None,
397398
disable_previous: Optional[bool] = None,
398-
password_generation: Optional[PasswordGenerationParams] = None,
399399
data_crc32: Optional[int] = None,
400400
) -> SecretVersion:
401401
"""
@@ -407,12 +407,8 @@ async def create_secret_version(
407407
:param description: Description of the version.
408408
:param disable_previous: Disable the previous secret version.
409409
Optional. If there is no previous version or if the previous version was already disabled, does nothing.
410-
:param password_generation: Options to generate a password.
411-
Optional. If specified, a random password will be generated. The `data` and `data_crc32` fields must be empty. By default, the generator will use upper and lower case letters, and digits. This behavior can be tuned using the generation parameters.
412-
413-
One-of ('_password_generation'): at most one of 'password_generation' could be set.
414-
:param data_crc32: The CRC32 checksum of the data as a base-10 integer.
415-
Optional. If specified, Secret Manager will verify the integrity of the data received against the given CRC32. An error is returned if the CRC32 does not match. Otherwise, the CRC32 will be stored and returned along with the SecretVersion on futur accesses.
410+
:param data_crc32: (Optional.) The CRC32 checksum of the data as a base-10 integer.
411+
If specified, Secret Manager will verify the integrity of the data received against the given CRC32 checksum. An error is returned if the CRC32 does not match. If, however, the CRC32 matches, it will be stored and returned along with the SecretVersion on future access requests.
416412
:return: :class:`SecretVersion <SecretVersion>`
417413
418414
Usage:
@@ -439,7 +435,6 @@ async def create_secret_version(
439435
region=region,
440436
description=description,
441437
disable_previous=disable_previous,
442-
password_generation=password_generation,
443438
data_crc32=data_crc32,
444439
),
445440
self.client,
@@ -449,6 +444,70 @@ async def create_secret_version(
449444
self._throw_on_error(res)
450445
return unmarshal_SecretVersion(res.json())
451446

447+
async def generate_password(
448+
self,
449+
*,
450+
secret_id: str,
451+
length: int,
452+
region: Optional[Region] = None,
453+
description: Optional[str] = None,
454+
disable_previous: Optional[bool] = None,
455+
no_lowercase_letters: Optional[bool] = None,
456+
no_uppercase_letters: Optional[bool] = None,
457+
no_digits: Optional[bool] = None,
458+
additional_chars: Optional[str] = None,
459+
) -> SecretVersion:
460+
"""
461+
Generate a password in a new version.
462+
Generate a password for the given secret specified by the `region` and `secret_id` parameters. This will also create a new version of the secret that will store the password.
463+
:param region: Region to target. If none is passed will use default region from the config.
464+
:param secret_id: ID of the secret.
465+
:param description: Description of the version.
466+
:param disable_previous: (Optional.) Disable the previous secret version.
467+
This has no effect if there is no previous version or if the previous version was already disabled.
468+
:param length: Length of the password to generate (between 1 and 1024 characters).
469+
:param no_lowercase_letters: (Optional.) Exclude lower case letters by default in the password character set.
470+
:param no_uppercase_letters: (Optional.) Exclude upper case letters by default in the password character set.
471+
:param no_digits: (Optional.) Exclude digits by default in the password character set.
472+
:param additional_chars: (Optional.) Additional ASCII characters to be included in the password character set.
473+
:return: :class:`SecretVersion <SecretVersion>`
474+
475+
Usage:
476+
::
477+
478+
result = await api.generate_password(
479+
secret_id="example",
480+
length=1,
481+
)
482+
"""
483+
484+
param_region = validate_path_param(
485+
"region", region or self.client.default_region
486+
)
487+
param_secret_id = validate_path_param("secret_id", secret_id)
488+
489+
res = self._request(
490+
"POST",
491+
f"/secret-manager/v1alpha1/regions/{param_region}/secrets/{param_secret_id}/generate-password",
492+
body=marshal_GeneratePasswordRequest(
493+
GeneratePasswordRequest(
494+
secret_id=secret_id,
495+
length=length,
496+
region=region,
497+
description=description,
498+
disable_previous=disable_previous,
499+
no_lowercase_letters=no_lowercase_letters,
500+
no_uppercase_letters=no_uppercase_letters,
501+
no_digits=no_digits,
502+
additional_chars=additional_chars,
503+
),
504+
self.client,
505+
),
506+
)
507+
508+
self._throw_on_error(res)
509+
return unmarshal_SecretVersion(res.json())
510+
452511
async def get_secret_version(
453512
self,
454513
*,

scaleway-async/scaleway_async/secret/v1alpha1/marshalling.py

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,18 @@
44
from typing import Any, Dict
55

66
from scaleway_core.profile import ProfileDefaults
7-
from scaleway_core.utils import (
8-
OneOfPossibility,
9-
resolve_one_of,
10-
)
117
from dateutil import parser
128
from .types import (
139
AccessSecretVersionResponse,
1410
ListSecretVersionsResponse,
1511
ListSecretsResponse,
16-
PasswordGenerationParams,
1712
Secret,
1813
SecretVersion,
1914
CreateSecretRequest,
2015
UpdateSecretRequest,
2116
AddSecretOwnerRequest,
2217
CreateSecretVersionRequest,
18+
GeneratePasswordRequest,
2319
UpdateSecretVersionRequest,
2420
)
2521

@@ -157,19 +153,6 @@ def unmarshal_ListSecretsResponse(data: Any) -> ListSecretsResponse:
157153
return ListSecretsResponse(**args)
158154

159155

160-
def marshal_PasswordGenerationParams(
161-
request: PasswordGenerationParams,
162-
defaults: ProfileDefaults,
163-
) -> Dict[str, Any]:
164-
return {
165-
"additional_chars": request.additional_chars,
166-
"length": request.length,
167-
"no_digits": request.no_digits,
168-
"no_lowercase_letters": request.no_lowercase_letters,
169-
"no_uppercase_letters": request.no_uppercase_letters,
170-
}
171-
172-
173156
def marshal_AddSecretOwnerRequest(
174157
request: AddSecretOwnerRequest,
175158
defaults: ProfileDefaults,
@@ -196,25 +179,28 @@ def marshal_CreateSecretVersionRequest(
196179
defaults: ProfileDefaults,
197180
) -> Dict[str, Any]:
198181
return {
199-
**resolve_one_of(
200-
[
201-
OneOfPossibility(
202-
"password_generation",
203-
marshal_PasswordGenerationParams(
204-
request.password_generation, defaults
205-
)
206-
if request.password_generation is not None
207-
else None,
208-
),
209-
]
210-
),
211182
"data": request.data,
212183
"data_crc32": request.data_crc32,
213184
"description": request.description,
214185
"disable_previous": request.disable_previous,
215186
}
216187

217188

189+
def marshal_GeneratePasswordRequest(
190+
request: GeneratePasswordRequest,
191+
defaults: ProfileDefaults,
192+
) -> Dict[str, Any]:
193+
return {
194+
"additional_chars": request.additional_chars,
195+
"description": request.description,
196+
"disable_previous": request.disable_previous,
197+
"length": request.length,
198+
"no_digits": request.no_digits,
199+
"no_lowercase_letters": request.no_lowercase_letters,
200+
"no_uppercase_letters": request.no_uppercase_letters,
201+
}
202+
203+
218204
def marshal_UpdateSecretRequest(
219205
request: UpdateSecretRequest,
220206
defaults: ProfileDefaults,

scaleway-async/scaleway_async/secret/v1alpha1/types.py

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class SecretVersion:
244244

245245
is_latest: bool
246246
"""
247-
True if the version is the latest one.
247+
Returns `true` if the version is the latest.
248248
"""
249249

250250

@@ -428,18 +428,59 @@ class CreateSecretVersionRequest:
428428
Optional. If there is no previous version or if the previous version was already disabled, does nothing.
429429
"""
430430

431-
password_generation: Optional[PasswordGenerationParams]
431+
data_crc32: Optional[int]
432432
"""
433-
Options to generate a password.
434-
Optional. If specified, a random password will be generated. The `data` and `data_crc32` fields must be empty. By default, the generator will use upper and lower case letters, and digits. This behavior can be tuned using the generation parameters.
435-
436-
One-of ('_password_generation'): at most one of 'password_generation' could be set.
433+
(Optional.) The CRC32 checksum of the data as a base-10 integer.
434+
If specified, Secret Manager will verify the integrity of the data received against the given CRC32 checksum. An error is returned if the CRC32 does not match. If, however, the CRC32 matches, it will be stored and returned along with the SecretVersion on future access requests.
437435
"""
438436

439-
data_crc32: Optional[int]
437+
438+
@dataclass
439+
class GeneratePasswordRequest:
440+
region: Optional[Region]
440441
"""
441-
The CRC32 checksum of the data as a base-10 integer.
442-
Optional. If specified, Secret Manager will verify the integrity of the data received against the given CRC32. An error is returned if the CRC32 does not match. Otherwise, the CRC32 will be stored and returned along with the SecretVersion on futur accesses.
442+
Region to target. If none is passed will use default region from the config.
443+
"""
444+
445+
secret_id: str
446+
"""
447+
ID of the secret.
448+
"""
449+
450+
description: Optional[str]
451+
"""
452+
Description of the version.
453+
"""
454+
455+
disable_previous: Optional[bool]
456+
"""
457+
(Optional.) Disable the previous secret version.
458+
This has no effect if there is no previous version or if the previous version was already disabled.
459+
"""
460+
461+
length: int
462+
"""
463+
Length of the password to generate (between 1 and 1024 characters).
464+
"""
465+
466+
no_lowercase_letters: Optional[bool]
467+
"""
468+
(Optional.) Exclude lower case letters by default in the password character set.
469+
"""
470+
471+
no_uppercase_letters: Optional[bool]
472+
"""
473+
(Optional.) Exclude upper case letters by default in the password character set.
474+
"""
475+
476+
no_digits: Optional[bool]
477+
"""
478+
(Optional.) Exclude digits by default in the password character set.
479+
"""
480+
481+
additional_chars: Optional[str]
482+
"""
483+
(Optional.) Additional ASCII characters to be included in the password character set.
443484
"""
444485

445486

0 commit comments

Comments
 (0)