Skip to content

Commit b14c456

Browse files
authored
feat(sm): add endpoint to generate password (#1699)
1 parent 1ece4ca commit b14c456

File tree

1 file changed

+63
-7
lines changed

1 file changed

+63
-7
lines changed

api/secret/v1alpha1/secret_sdk.go

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ type SecretVersion struct {
241241
UpdatedAt *time.Time `json:"updated_at"`
242242
// Description: description of the version.
243243
Description *string `json:"description"`
244-
// IsLatest: true if the version is the latest one.
244+
// IsLatest: returns `true` if the version is the latest.
245245
IsLatest bool `json:"is_latest"`
246246
}
247247

@@ -595,12 +595,8 @@ type CreateSecretVersionRequest struct {
595595
// DisablePrevious: disable the previous secret version.
596596
// Optional. If there is no previous version or if the previous version was already disabled, does nothing.
597597
DisablePrevious *bool `json:"disable_previous"`
598-
// PasswordGeneration: options to generate a password.
599-
// 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.
600-
// Precisely one of PasswordGeneration must be set.
601-
PasswordGeneration *PasswordGenerationParams `json:"password_generation,omitempty"`
602-
// DataCrc32: the CRC32 checksum of the data as a base-10 integer.
603-
// 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.
598+
// DataCrc32: (Optional.) The CRC32 checksum of the data as a base-10 integer.
599+
// 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.
604600
DataCrc32 *uint32 `json:"data_crc32"`
605601
}
606602

@@ -642,6 +638,66 @@ func (s *API) CreateSecretVersion(req *CreateSecretVersionRequest, opts ...scw.R
642638
return &resp, nil
643639
}
644640

641+
type GeneratePasswordRequest struct {
642+
// Region: region to target. If none is passed will use default region from the config.
643+
Region scw.Region `json:"-"`
644+
// SecretID: ID of the secret.
645+
SecretID string `json:"-"`
646+
// Description: description of the version.
647+
Description *string `json:"description"`
648+
// DisablePrevious: (Optional.) Disable the previous secret version.
649+
// This has no effect if there is no previous version or if the previous version was already disabled.
650+
DisablePrevious *bool `json:"disable_previous"`
651+
// Length: length of the password to generate (between 1 and 1024 characters).
652+
Length uint32 `json:"length"`
653+
// NoLowercaseLetters: (Optional.) Exclude lower case letters by default in the password character set.
654+
NoLowercaseLetters *bool `json:"no_lowercase_letters"`
655+
// NoUppercaseLetters: (Optional.) Exclude upper case letters by default in the password character set.
656+
NoUppercaseLetters *bool `json:"no_uppercase_letters"`
657+
// NoDigits: (Optional.) Exclude digits by default in the password character set.
658+
NoDigits *bool `json:"no_digits"`
659+
// AdditionalChars: (Optional.) Additional ASCII characters to be included in the password character set.
660+
AdditionalChars *string `json:"additional_chars"`
661+
}
662+
663+
// GeneratePassword: generate a password in a new version.
664+
// 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.
665+
func (s *API) GeneratePassword(req *GeneratePasswordRequest, opts ...scw.RequestOption) (*SecretVersion, error) {
666+
var err error
667+
668+
if req.Region == "" {
669+
defaultRegion, _ := s.client.GetDefaultRegion()
670+
req.Region = defaultRegion
671+
}
672+
673+
if fmt.Sprint(req.Region) == "" {
674+
return nil, errors.New("field Region cannot be empty in request")
675+
}
676+
677+
if fmt.Sprint(req.SecretID) == "" {
678+
return nil, errors.New("field SecretID cannot be empty in request")
679+
}
680+
681+
scwReq := &scw.ScalewayRequest{
682+
Method: "POST",
683+
Path: "/secret-manager/v1alpha1/regions/" + fmt.Sprint(req.Region) + "/secrets/" + fmt.Sprint(req.SecretID) + "/generate-password",
684+
Headers: http.Header{},
685+
}
686+
687+
err = scwReq.SetBody(req)
688+
if err != nil {
689+
return nil, err
690+
}
691+
692+
var resp SecretVersion
693+
694+
err = s.client.Do(scwReq, &resp, opts...)
695+
if err != nil {
696+
return nil, err
697+
}
698+
return &resp, nil
699+
}
700+
645701
type GetSecretVersionRequest struct {
646702
// Region: region to target. If none is passed will use default region from the config.
647703
Region scw.Region `json:"-"`

0 commit comments

Comments
 (0)