HomeSecurity & IdentitySecret ManagerAPI/CLI
Generating passwords with Secret Manager using the CLI and GO
Jump toUpdate content

Generating passwords with Secret Manager using the CLI and GO

Reviewed on 24 May 2023Published on 24 May 2023

Once you have created a secret with Scaleway Secret Manager, you can send a request to generate a password for it. This will create a new version for your secret, in which your password will be generated and securely stored.

Generate a password with the Scaleway CLI

Security & Identity (IAM):

You may need certain IAM permissions to carry out some actions described on this page. This means:

  • you are the Owner of the Scaleway Organization in which the actions will be carried out, or
  • you are an IAM user of the Organization, with a policy granting you the necessary permission sets
Requirements:
  1. Run the following command to create a secret. Make sure you replace <project-ID> with the ID of the Project in which you want to create the secret.

    scw secret secret create project-id=<project-ID> name=test-password

    An output similar to the following displays:

    ID 8aa046bd-5a52-477e-9d23-99df84735a82
    ProjectID 2e5ee9ae-6798-419c-a6ce-0e2bf979b78c
    Name test-password
    Status ready
    CreatedAt now
    UpdatedAt now
    Region fr-par
    VersionCount 0
    Description -

    Copy the ID as you will need it for the next step.

  2. Run the following command to generate the password for your newly-created secret. Make sure you replace <secret-ID> with the ID of your secret.

    scw secret version generate-password secret-id=<secret-ID> length=12

    This generates a password of 12 characters. Your password is then stored in Secret Manager as a new version of your secret.

  3. Run the following command to retrieve your newly-generated password:

    scw secret version access secret-id=<secret-ID> revision=latest

    Your base64-encoded-password displays in the output.

Generate a password with the Scaleway GO SDK

Security & Identity (IAM):

You may need certain IAM permissions to carry out some actions described on this page. This means:

  • you are the Owner of the Scaleway Organization in which the actions will be carried out, or
  • you are an IAM user of the Organization, with a policy granting you the necessary permission sets

The following code allows you to create a Scaleway account, a secret and to generate a password as a new version of that secret.

package main
import (
"fmt"
"github.com/scaleway/scaleway-sdk-go/scw"
secret_manager "github.com/scaleway/scaleway-sdk-go/api/secret/v1alpha1"
)
func main() {
// Create a Scaleway client
client, err := scw.NewClient(
// Get your organization ID at https://console.scaleway.com/organization/settings
scw.WithDefaultOrganizationID("SCW_DEFAULT_ORGANIZATION_ID"),
// Get your credentials at https://console.scaleway.com/iam/api-keys
scw.WithAuth("SCW_ACCESS_KEY", "SCW_SECRET_KEY"),
// Get more about our availability zones at https://www.scaleway.com/en/docs/console/my-account/reference-content/products-availability/
scw.WithDefaultRegion("SCW_REGION"),
)
if err != nil {
panic(err)
}
api := secret_manager.NewAPI(client)
// Create the secret
secret, err := api.CreateSecret(&secret_manager.CreateSecretRequest{
ProjectID: "your project ID",
Name: "test-password"
})
if err != nil {
panic(err)
}
// Generate a password of length 12 and store it as a new version
_, err := api.GeneratePassword(&secret_manager.GeneratePasswordRequest{
SecretID: secret.ID,
Length: 12,
})
if err != nil {
panic(err)
}
// Get the generated password
password, err := api.AccessSecretVersion(&secret_manager.AccessSecretVersionRequest{
SecretID: secret.ID,
Revision: "latest",
})
if err != nil {
panic(err)
}
fmt.Println(password)
}