Search in sources :

Example 96 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-secretmanager by googleapis.

the class ITSystemTest method setUp.

@BeforeClass
public static void setUp() throws IOException {
    /* create secret */
    client = SecretManagerServiceClient.create();
    Replication createReplication = Replication.newBuilder().setUserManaged(Replication.UserManaged.newBuilder().addReplicas(Replication.UserManaged.Replica.newBuilder().setLocation(LOCATION).build())).build();
    Secret createSecret = Secret.newBuilder().setReplication(createReplication).build();
    CreateSecretRequest secretRequest = CreateSecretRequest.newBuilder().setParent(PROJECT_NAME).setSecretId(SECRET_ID).setSecret(createSecret).build();
    secret = client.createSecret(secretRequest);
    secretId = getName(secret.getName());
    formattedSecretName = SecretName.of(PROJECT_ID, secretId).toString();
    /* create secret version */
    AddSecretVersionRequest versionRequest = AddSecretVersionRequest.newBuilder().setParent(formattedSecretName).setPayload(PAYLOAD).build();
    secretVersion = client.addSecretVersion(versionRequest);
    secretVersionId = getName(secretVersion.getName());
    formattedSecretVersion = SecretVersionName.of(PROJECT_ID, secretId, secretVersionId).toString();
}
Also used : Secret(com.google.cloud.secretmanager.v1beta1.Secret) AddSecretVersionRequest(com.google.cloud.secretmanager.v1beta1.AddSecretVersionRequest) CreateSecretRequest(com.google.cloud.secretmanager.v1beta1.CreateSecretRequest) Replication(com.google.cloud.secretmanager.v1beta1.Replication) BeforeClass(org.junit.BeforeClass)

Example 97 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-secretmanager by googleapis.

the class SecretManagerServiceClientTest method listSecretVersionsTest.

@Test
public void listSecretVersionsTest() throws Exception {
    SecretVersion responsesElement = SecretVersion.newBuilder().build();
    ListSecretVersionsResponse expectedResponse = ListSecretVersionsResponse.newBuilder().setNextPageToken("").addAllVersions(Arrays.asList(responsesElement)).build();
    mockSecretManagerService.addResponse(expectedResponse);
    SecretName parent = SecretName.of("[PROJECT]", "[SECRET]");
    ListSecretVersionsPagedResponse pagedListResponse = client.listSecretVersions(parent);
    List<SecretVersion> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getVersionsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockSecretManagerService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListSecretVersionsRequest actualRequest = ((ListSecretVersionsRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : ListSecretVersionsPagedResponse(com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretVersionsPagedResponse) AbstractMessage(com.google.protobuf.AbstractMessage) Test(org.junit.Test)

Example 98 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-secretmanager by googleapis.

the class DisableSecretVersion method disableSecretVersion.

// Disable an existing secret version.
public void disableSecretVersion(String projectId, String secretId, String versionId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        // Build the name from the version.
        SecretVersionName secretVersionName = SecretVersionName.of(projectId, secretId, versionId);
        // Create the secret.
        SecretVersion version = client.disableSecretVersion(secretVersionName);
        System.out.printf("Disabled secret version %s\n", version.getName());
    }
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Example 99 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-secretmanager by googleapis.

the class GetSecret method getSecret.

// Get an existing secret.
public void getSecret(String projectId, String secretId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        // Build the name.
        SecretName secretName = SecretName.of(projectId, secretId);
        // Create the secret.
        Secret secret = client.getSecret(secretName);
        // Get the replication policy.
        String replication = "";
        if (secret.getReplication().getAutomatic() != null) {
            replication = "AUTOMATIC";
        } else if (secret.getReplication().getUserManaged() != null) {
            replication = "MANAGED";
        } else {
            throw new IllegalStateException("Unknown replication type");
        }
        System.out.printf("Secret %s, replication %s\n", secret.getName(), replication);
    }
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) Secret(com.google.cloud.secretmanager.v1.Secret) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Example 100 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-secretmanager by googleapis.

the class AccessSecretVersion method accessSecretVersion.

// Access the payload for the given secret version if one exists. The version
// can be a version number as a string (e.g. "5") or an alias (e.g. "latest").
public void accessSecretVersion(String projectId, String secretId, String versionId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        SecretVersionName secretVersionName = SecretVersionName.of(projectId, secretId, versionId);
        // Access the secret version.
        AccessSecretVersionResponse response = client.accessSecretVersion(secretVersionName);
        // Print the secret payload.
        // 
        // WARNING: Do not print the secret in a production environment - this
        // snippet is showing how to access the secret material.
        String payload = response.getPayload().getData().toStringUtf8();
        System.out.printf("Plaintext: %s\n", payload);
    }
}
Also used : SecretVersionName(com.google.cloud.secretmanager.v1.SecretVersionName) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) AccessSecretVersionResponse(com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)

Aggregations

SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)49 SecretName (com.google.cloud.secretmanager.v1.SecretName)43 Test (org.junit.Test)42 SecretVersionName (com.google.cloud.secretmanager.v1.SecretVersionName)37 SecretVersion (com.google.cloud.secretmanager.v1.SecretVersion)20 Secret (com.google.cloud.secretmanager.v1.Secret)19 ProjectName (com.google.cloud.secretmanager.v1.ProjectName)14 ByteString (com.google.protobuf.ByteString)10 AccessSecretVersionResponse (com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)7 CreateSecretRequest (com.google.cloud.secretmanager.v1.CreateSecretRequest)6 ListSecretsPagedResponse (com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretsPagedResponse)6 SecretPayload (com.google.cloud.secretmanager.v1.SecretPayload)5 Test (org.junit.jupiter.api.Test)5 AddSecretVersionRequest (com.google.cloud.secretmanager.v1.AddSecretVersionRequest)4 DeleteSecretRequest (com.google.cloud.secretmanager.v1.DeleteSecretRequest)4 ListSecretVersionsPagedResponse (com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretVersionsPagedResponse)4 Binding (com.google.iam.v1.Binding)4 Policy (com.google.iam.v1.Policy)4 ListSecretsRequest (com.google.cloud.secretmanager.v1.ListSecretsRequest)3 Secret (com.google.cloud.secretmanager.v1beta1.Secret)3