Class SecretManagerTemplate

java.lang.Object
com.google.cloud.spring.secretmanager.SecretManagerTemplate
All Implemented Interfaces:
SecretManagerOperations

public class SecretManagerTemplate extends Object implements SecretManagerOperations
Offers convenience methods for performing common operations on Secret Manager including creating and reading secrets.
Since:
1.2.2
  • Field Details

    • LATEST_VERSION

      public static final String LATEST_VERSION
      Default value for the latest version of the secret.
      See Also:
  • Constructor Details

    • SecretManagerTemplate

      public SecretManagerTemplate(com.google.cloud.secretmanager.v1.SecretManagerServiceClient secretManagerServiceClient, GcpProjectIdProvider projectIdProvider)
  • Method Details

    • setAllowDefaultSecretValue

      public SecretManagerTemplate setAllowDefaultSecretValue(boolean allowDefaultSecretValue)
    • getProjectId

      public String getProjectId()
    • createSecret

      public void createSecret(String secretId, String payload)
      Description copied from interface: SecretManagerOperations
      Creates a new secret or a new version of existing secret with the provided payload.

      If there is already a secret saved in SecretManager with the specified secretId, then it simply creates a new version under the secret with the secret payload.

      Specified by:
      createSecret in interface SecretManagerOperations
      Parameters:
      secretId - the secret ID of the secret to create.
      payload - the secret payload string.
    • createSecret

      public void createSecret(String secretId, byte[] payload)
      Description copied from interface: SecretManagerOperations
      Creates a new secret or a new version of existing secret with the provided payload.

      If there is already a secret saved in SecretManager with the specified secretId, then it simply creates a new version under the secret with the secret payload.

      Specified by:
      createSecret in interface SecretManagerOperations
      Parameters:
      secretId - the secret ID of the secret to create.
      payload - the secret payload as a byte array.
    • createSecret

      public void createSecret(String secretId, byte[] payload, String projectId)
      Description copied from interface: SecretManagerOperations
      Creates a new secret or a new version of existing secret with the provided payload for a specific projectId.

      If there is already a secret saved in SecretManager with the specified secretId, then it simply creates a new version under the secret with the secret payload.

      Specified by:
      createSecret in interface SecretManagerOperations
      Parameters:
      secretId - the secret ID of the secret to create.
      payload - the secret payload as a byte array.
      projectId - unique identifier of your project.
    • getSecretString

      @Nullable public String getSecretString(String secretIdentifier)
      Description copied from interface: SecretManagerOperations
      Gets the secret payload of the specified secretIdentifier secret.

      The secretIdentifier must either be a secret ID or a fully qualified `sm://` protocol string which specifies the secret (see javadocs of SecretManagerOperations for the protocol format).

      If the secret ID string is passed in, then this will return the payload of the secret for the default project at the latest version.

      Specified by:
      getSecretString in interface SecretManagerOperations
      Parameters:
      secretIdentifier - the GCP secret ID of the secret or a sm:// formatted string specifying the secret.
      Returns:
      The secret payload as String
    • getSecretBytes

      @Nullable public byte[] getSecretBytes(String secretIdentifier)
      Description copied from interface: SecretManagerOperations
      Gets the secret payload of the specified secretIdentifier secret.

      The secretIdentifier must either be a secret ID or a fully qualified `sm://` protocol string which specifies the secret (see javadocs of SecretManagerOperations for the protocol format).

      If the secret ID string is passed in, then this will return the payload of the secret for the default project at the latest version.

      Specified by:
      getSecretBytes in interface SecretManagerOperations
      Parameters:
      secretIdentifier - the GCP secret ID of the secret or a sm:// formatted string specifying the secret.
      Returns:
      The secret payload as byte array
    • secretExists

      public boolean secretExists(String secretId)
      Description copied from interface: SecretManagerOperations
      Returns true if there already exists a secret under the GCP project with the secretId.
      Specified by:
      secretExists in interface SecretManagerOperations
      Parameters:
      secretId - unique identifier of your secret in Secret Manager.
      Returns:
      true if the secret exists in Secret Manager; false otherwise
    • secretExists

      public boolean secretExists(String secretId, String projectId)
      Description copied from interface: SecretManagerOperations
      Returns true if there already exists a secret under the GCP projectId with the secretId.
      Specified by:
      secretExists in interface SecretManagerOperations
      Parameters:
      secretId - unique identifier of your secret in Secret Manager.
      projectId - unique identifier of your project.
      Returns:
      true if the secret exists in Secret Manager; false otherwise
    • disableSecretVersion

      public void disableSecretVersion(String secretId, String version)
      Description copied from interface: SecretManagerOperations
      Disables the specified secret version under the default-configured project.
      Specified by:
      disableSecretVersion in interface SecretManagerOperations
      Parameters:
      secretId - the secret ID of the secret to disable.
      version - the version to disable
    • disableSecretVersion

      public void disableSecretVersion(String secretId, String version, String projectId)
      Description copied from interface: SecretManagerOperations
      Disables the secret version under the specified project.
      Specified by:
      disableSecretVersion in interface SecretManagerOperations
      Parameters:
      secretId - the secret ID of the secret to disable.
      version - the version to disable
      projectId - unique identifier of your project.
    • enableSecretVersion

      public void enableSecretVersion(String secretId, String version)
      Description copied from interface: SecretManagerOperations
      Enables the specified secret version under the default-configured project.
      Specified by:
      enableSecretVersion in interface SecretManagerOperations
      Parameters:
      secretId - the secret ID of the secret to enable.
      version - the version to enable
    • enableSecretVersion

      public void enableSecretVersion(String secretId, String version, String projectId)
      Description copied from interface: SecretManagerOperations
      Enables the secret version under the specified project.
      Specified by:
      enableSecretVersion in interface SecretManagerOperations
      Parameters:
      secretId - the secret ID of the secret to enable.
      version - the version to enable
      projectId - unique identifier of your project.
    • deleteSecret

      public void deleteSecret(String secretId)
      Description copied from interface: SecretManagerOperations
      Deletes the specified secret under the default-configured project.
      Specified by:
      deleteSecret in interface SecretManagerOperations
      Parameters:
      secretId - the secret ID of the secret to delete.
    • deleteSecret

      public void deleteSecret(String secretId, String projectId)
      Description copied from interface: SecretManagerOperations
      Deletes the specified secret.
      Specified by:
      deleteSecret in interface SecretManagerOperations
      Parameters:
      secretId - the secret ID of the secret to delete.
      projectId - the GCP project containing the secret to delete.
    • deleteSecretVersion

      public void deleteSecretVersion(String secretId, String version, String projectId)
      Description copied from interface: SecretManagerOperations
      Deletes the specified secret version.
      Specified by:
      deleteSecretVersion in interface SecretManagerOperations
      Parameters:
      secretId - the secret ID of the secret to delete.
      version - the version to delete
      projectId - the GCP project containing the secret to delete.