diff --git a/java-secretmanager/README.md b/java-secretmanager/README.md index 17127c5a841c..cab2c93dcbd6 100644 --- a/java-secretmanager/README.md +++ b/java-secretmanager/README.md @@ -115,21 +115,6 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-secretmanager | Sample | Source Code | Try it | | --------------------------- | --------------------------------- | ------ | | Native Image Secret Manager Sample | [source code](https://github.com/googleapis/java-secretmanager/blob/main/samples/native-image-sample/src/main/java/secretmanager/NativeImageSecretManagerSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-secretmanager&page=editor&open_in_editor=samples/native-image-sample/src/main/java/secretmanager/NativeImageSecretManagerSample.java) | -| Access Secret Version | [source code](https://github.com/googleapis/java-secretmanager/blob/main/samples/snippets/src/main/java/secretmanager/AccessSecretVersion.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-secretmanager&page=editor&open_in_editor=samples/snippets/src/main/java/secretmanager/AccessSecretVersion.java) | -| Add Secret Version | [source code](https://github.com/googleapis/java-secretmanager/blob/main/samples/snippets/src/main/java/secretmanager/AddSecretVersion.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-secretmanager&page=editor&open_in_editor=samples/snippets/src/main/java/secretmanager/AddSecretVersion.java) | -| Create Secret | [source code](https://github.com/googleapis/java-secretmanager/blob/main/samples/snippets/src/main/java/secretmanager/CreateSecret.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-secretmanager&page=editor&open_in_editor=samples/snippets/src/main/java/secretmanager/CreateSecret.java) | -| Delete Secret | [source code](https://github.com/googleapis/java-secretmanager/blob/main/samples/snippets/src/main/java/secretmanager/DeleteSecret.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-secretmanager&page=editor&open_in_editor=samples/snippets/src/main/java/secretmanager/DeleteSecret.java) | -| Destroy Secret Version | [source code](https://github.com/googleapis/java-secretmanager/blob/main/samples/snippets/src/main/java/secretmanager/DestroySecretVersion.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-secretmanager&page=editor&open_in_editor=samples/snippets/src/main/java/secretmanager/DestroySecretVersion.java) | -| Disable Secret Version | [source code](https://github.com/googleapis/java-secretmanager/blob/main/samples/snippets/src/main/java/secretmanager/DisableSecretVersion.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-secretmanager&page=editor&open_in_editor=samples/snippets/src/main/java/secretmanager/DisableSecretVersion.java) | -| Enable Secret Version | [source code](https://github.com/googleapis/java-secretmanager/blob/main/samples/snippets/src/main/java/secretmanager/EnableSecretVersion.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-secretmanager&page=editor&open_in_editor=samples/snippets/src/main/java/secretmanager/EnableSecretVersion.java) | -| Get Secret | [source code](https://github.com/googleapis/java-secretmanager/blob/main/samples/snippets/src/main/java/secretmanager/GetSecret.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-secretmanager&page=editor&open_in_editor=samples/snippets/src/main/java/secretmanager/GetSecret.java) | -| Get Secret Version | [source code](https://github.com/googleapis/java-secretmanager/blob/main/samples/snippets/src/main/java/secretmanager/GetSecretVersion.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-secretmanager&page=editor&open_in_editor=samples/snippets/src/main/java/secretmanager/GetSecretVersion.java) | -| Iam Grant Access | [source code](https://github.com/googleapis/java-secretmanager/blob/main/samples/snippets/src/main/java/secretmanager/IamGrantAccess.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-secretmanager&page=editor&open_in_editor=samples/snippets/src/main/java/secretmanager/IamGrantAccess.java) | -| Iam Revoke Access | [source code](https://github.com/googleapis/java-secretmanager/blob/main/samples/snippets/src/main/java/secretmanager/IamRevokeAccess.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-secretmanager&page=editor&open_in_editor=samples/snippets/src/main/java/secretmanager/IamRevokeAccess.java) | -| List Secret Versions | [source code](https://github.com/googleapis/java-secretmanager/blob/main/samples/snippets/src/main/java/secretmanager/ListSecretVersions.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-secretmanager&page=editor&open_in_editor=samples/snippets/src/main/java/secretmanager/ListSecretVersions.java) | -| List Secrets | [source code](https://github.com/googleapis/java-secretmanager/blob/main/samples/snippets/src/main/java/secretmanager/ListSecrets.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-secretmanager&page=editor&open_in_editor=samples/snippets/src/main/java/secretmanager/ListSecrets.java) | -| Quickstart | [source code](https://github.com/googleapis/java-secretmanager/blob/main/samples/snippets/src/main/java/secretmanager/Quickstart.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-secretmanager&page=editor&open_in_editor=samples/snippets/src/main/java/secretmanager/Quickstart.java) | -| Update Secret | [source code](https://github.com/googleapis/java-secretmanager/blob/main/samples/snippets/src/main/java/secretmanager/UpdateSecret.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-secretmanager&page=editor&open_in_editor=samples/snippets/src/main/java/secretmanager/UpdateSecret.java) | diff --git a/java-secretmanager/samples/snippets/src/main/java/secretmanager/AccessSecretVersion.java b/java-secretmanager/samples/snippets/src/main/java/secretmanager/AccessSecretVersion.java deleted file mode 100644 index 179d202e6919..000000000000 --- a/java-secretmanager/samples/snippets/src/main/java/secretmanager/AccessSecretVersion.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package secretmanager; - -// [START secretmanager_access_secret_version] -import com.google.cloud.secretmanager.v1.AccessSecretVersionResponse; -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; -import com.google.cloud.secretmanager.v1.SecretVersionName; -import java.io.IOException; - -public class AccessSecretVersion { - - public void accessSecretVersion() throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "your-project-id"; - String secretId = "your-secret-id"; - String versionId = "your-version-id"; - accessSecretVersion(projectId, secretId, versionId); - } - - // 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 { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // 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); - } - } -} -// [END secretmanager_access_secret_version] diff --git a/java-secretmanager/samples/snippets/src/main/java/secretmanager/AddSecretVersion.java b/java-secretmanager/samples/snippets/src/main/java/secretmanager/AddSecretVersion.java deleted file mode 100644 index 0b1ec2f310fa..000000000000 --- a/java-secretmanager/samples/snippets/src/main/java/secretmanager/AddSecretVersion.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package secretmanager; - -// [START secretmanager_add_secret_version] -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; -import com.google.cloud.secretmanager.v1.SecretName; -import com.google.cloud.secretmanager.v1.SecretPayload; -import com.google.cloud.secretmanager.v1.SecretVersion; -import com.google.protobuf.ByteString; -import java.io.IOException; - -public class AddSecretVersion { - - public void addSecretVersion() throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "your-project-id"; - String secretId = "your-secret-id"; - addSecretVersion(projectId, secretId); - } - - // Add a new version to the existing secret. - public void addSecretVersion(String projectId, String secretId) throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // the "close" method on the client to safely clean up any remaining background resources. - try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) { - SecretName secretName = SecretName.of(projectId, secretId); - - // Create the secret payload. - SecretPayload payload = - SecretPayload.newBuilder() - .setData(ByteString.copyFromUtf8("my super secret data")) - .build(); - - // Add the secret version. - SecretVersion version = client.addSecretVersion(secretName, payload); - System.out.printf("Added secret version %s\n", version.getName()); - } - } -} -// [END secretmanager_add_secret_version] diff --git a/java-secretmanager/samples/snippets/src/main/java/secretmanager/CreateSecret.java b/java-secretmanager/samples/snippets/src/main/java/secretmanager/CreateSecret.java deleted file mode 100644 index 494b05b07d81..000000000000 --- a/java-secretmanager/samples/snippets/src/main/java/secretmanager/CreateSecret.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package secretmanager; - -// [START secretmanager_create_secret] -import com.google.cloud.secretmanager.v1.ProjectName; -import com.google.cloud.secretmanager.v1.Replication; -import com.google.cloud.secretmanager.v1.Secret; -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; -import java.io.IOException; - -public class CreateSecret { - - public void createSecret() throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "your-project-id"; - String secretId = "your-secret-id"; - createSecret(projectId, secretId); - } - - // Add a new version to the existing secret. - public void createSecret(String projectId, String secretId) throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // the "close" method on the client to safely clean up any remaining background resources. - try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) { - // Build the parent name from the project. - ProjectName projectName = ProjectName.of(projectId); - - // Build the secret to create. - Secret secret = - Secret.newBuilder() - .setReplication( - Replication.newBuilder() - .setAutomatic(Replication.Automatic.newBuilder().build()) - .build()) - .build(); - - // Create the secret. - Secret createdSecret = client.createSecret(projectName, secretId, secret); - System.out.printf("Created secret %s\n", createdSecret.getName()); - } - } -} -// [END secretmanager_create_secret] diff --git a/java-secretmanager/samples/snippets/src/main/java/secretmanager/DeleteSecret.java b/java-secretmanager/samples/snippets/src/main/java/secretmanager/DeleteSecret.java deleted file mode 100644 index af75ca80dd67..000000000000 --- a/java-secretmanager/samples/snippets/src/main/java/secretmanager/DeleteSecret.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package secretmanager; - -// [START secretmanager_delete_secret] -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; -import com.google.cloud.secretmanager.v1.SecretName; -import java.io.IOException; - -public class DeleteSecret { - - public void deleteSecret() throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "your-project-id"; - String secretId = "your-secret-id"; - deleteSecret(projectId, secretId); - } - - // Delete an existing secret with the given name. - public void deleteSecret(String projectId, String secretId) throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // the "close" method on the client to safely clean up any remaining background resources. - try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) { - // Build the secret name. - SecretName secretName = SecretName.of(projectId, secretId); - - // Create the secret. - client.deleteSecret(secretName); - System.out.printf("Deleted secret %s\n", secretId); - } - } -} -// [END secretmanager_delete_secret] diff --git a/java-secretmanager/samples/snippets/src/main/java/secretmanager/DestroySecretVersion.java b/java-secretmanager/samples/snippets/src/main/java/secretmanager/DestroySecretVersion.java deleted file mode 100644 index 16dce46a57b5..000000000000 --- a/java-secretmanager/samples/snippets/src/main/java/secretmanager/DestroySecretVersion.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package secretmanager; - -// [START secretmanager_destroy_secret_version] -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; -import com.google.cloud.secretmanager.v1.SecretVersion; -import com.google.cloud.secretmanager.v1.SecretVersionName; -import java.io.IOException; - -public class DestroySecretVersion { - - public void destroySecretVersion() throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "your-project-id"; - String secretId = "your-secret-id"; - String versionId = "your-version-id"; - destroySecretVersion(projectId, secretId, versionId); - } - - // Destroy an existing secret version. - public void destroySecretVersion(String projectId, String secretId, String versionId) - throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // 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.destroySecretVersion(secretVersionName); - System.out.printf("Destroyed secret version %s\n", version.getName()); - } - } -} -// [END secretmanager_destroy_secret_version] diff --git a/java-secretmanager/samples/snippets/src/main/java/secretmanager/DisableSecretVersion.java b/java-secretmanager/samples/snippets/src/main/java/secretmanager/DisableSecretVersion.java deleted file mode 100644 index 5f414fec6582..000000000000 --- a/java-secretmanager/samples/snippets/src/main/java/secretmanager/DisableSecretVersion.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package secretmanager; - -// [START secretmanager_disable_secret_version] -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; -import com.google.cloud.secretmanager.v1.SecretVersion; -import com.google.cloud.secretmanager.v1.SecretVersionName; -import java.io.IOException; - -public class DisableSecretVersion { - - public void disableSecretVersion() throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "your-project-id"; - String secretId = "your-secret-id"; - String versionId = "your-version-id"; - disableSecretVersion(projectId, secretId, versionId); - } - - // Disable an existing secret version. - public void disableSecretVersion(String projectId, String secretId, String versionId) - throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // 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()); - } - } -} -// [END secretmanager_disable_secret_version] diff --git a/java-secretmanager/samples/snippets/src/main/java/secretmanager/EnableSecretVersion.java b/java-secretmanager/samples/snippets/src/main/java/secretmanager/EnableSecretVersion.java deleted file mode 100644 index 9369accd1836..000000000000 --- a/java-secretmanager/samples/snippets/src/main/java/secretmanager/EnableSecretVersion.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package secretmanager; - -// [START secretmanager_enable_secret_version] -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; -import com.google.cloud.secretmanager.v1.SecretVersion; -import com.google.cloud.secretmanager.v1.SecretVersionName; -import java.io.IOException; - -public class EnableSecretVersion { - - public void enableSecretVersion() throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "your-project-id"; - String secretId = "your-secret-id"; - String versionId = "your-version-id"; - enableSecretVersion(projectId, secretId, versionId); - } - - // Enable an existing secret version. - public void enableSecretVersion(String projectId, String secretId, String versionId) - throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // 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.enableSecretVersion(secretVersionName); - System.out.printf("Enabled secret version %s\n", version.getName()); - } - } -} -// [END secretmanager_enable_secret_version] diff --git a/java-secretmanager/samples/snippets/src/main/java/secretmanager/GetSecret.java b/java-secretmanager/samples/snippets/src/main/java/secretmanager/GetSecret.java deleted file mode 100644 index 15e859b84e2a..000000000000 --- a/java-secretmanager/samples/snippets/src/main/java/secretmanager/GetSecret.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package secretmanager; - -// [START secretmanager_get_secret] -import com.google.cloud.secretmanager.v1.Secret; -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; -import com.google.cloud.secretmanager.v1.SecretName; -import java.io.IOException; - -public class GetSecret { - - public void getSecret() throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "your-project-id"; - String secretId = "your-secret-id"; - getSecret(projectId, secretId); - } - - // Get an existing secret. - public void getSecret(String projectId, String secretId) throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // 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); - } - } -} -// [END secretmanager_get_secret] diff --git a/java-secretmanager/samples/snippets/src/main/java/secretmanager/GetSecretVersion.java b/java-secretmanager/samples/snippets/src/main/java/secretmanager/GetSecretVersion.java deleted file mode 100644 index 4b4864da3579..000000000000 --- a/java-secretmanager/samples/snippets/src/main/java/secretmanager/GetSecretVersion.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package secretmanager; - -// [START secretmanager_get_secret_version] -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; -import com.google.cloud.secretmanager.v1.SecretVersion; -import com.google.cloud.secretmanager.v1.SecretVersionName; -import java.io.IOException; - -public class GetSecretVersion { - - public void getSecretVersion() throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "your-project-id"; - String secretId = "your-secret-id"; - String versionId = "your-version-id"; - getSecretVersion(projectId, secretId, versionId); - } - - // Get an existing secret version. - public void getSecretVersion(String projectId, String secretId, String versionId) - throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // 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.getSecretVersion(secretVersionName); - System.out.printf("Secret version %s, state %s\n", version.getName(), version.getState()); - } - } -} -// [END secretmanager_get_secret_version] diff --git a/java-secretmanager/samples/snippets/src/main/java/secretmanager/IamGrantAccess.java b/java-secretmanager/samples/snippets/src/main/java/secretmanager/IamGrantAccess.java deleted file mode 100644 index 9afd95e67f8b..000000000000 --- a/java-secretmanager/samples/snippets/src/main/java/secretmanager/IamGrantAccess.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package secretmanager; - -// [START secretmanager_iam_grant_access] -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; -import com.google.cloud.secretmanager.v1.SecretName; -import com.google.iam.v1.Binding; -import com.google.iam.v1.GetIamPolicyRequest; -import com.google.iam.v1.Policy; -import com.google.iam.v1.SetIamPolicyRequest; -import java.io.IOException; - -public class IamGrantAccess { - - public void iamGrantAccess() throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "your-project-id"; - String secretId = "your-secret-id"; - String member = "user:foo@example.com"; - iamGrantAccess(projectId, secretId, member); - } - - // Grant a member access to a particular secret. - public void iamGrantAccess(String projectId, String secretId, String member) throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // 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. - SecretName secretName = SecretName.of(projectId, secretId); - - // Request the current IAM policy. - Policy currentPolicy = - client.getIamPolicy( - GetIamPolicyRequest.newBuilder().setResource(secretName.toString()).build()); - - // Build the new binding. - Binding binding = - Binding.newBuilder() - .setRole("roles/secretmanager.secretAccessor") - .addMembers(member) - .build(); - - // Create a new IAM policy from the current policy, adding the binding. - Policy newPolicy = Policy.newBuilder().mergeFrom(currentPolicy).addBindings(binding).build(); - - // Save the updated IAM policy. - client.setIamPolicy( - SetIamPolicyRequest.newBuilder() - .setResource(secretName.toString()) - .setPolicy(newPolicy) - .build()); - - System.out.printf("Updated IAM policy for %s\n", secretId); - } - } -} -// [END secretmanager_iam_grant_access] diff --git a/java-secretmanager/samples/snippets/src/main/java/secretmanager/IamRevokeAccess.java b/java-secretmanager/samples/snippets/src/main/java/secretmanager/IamRevokeAccess.java deleted file mode 100644 index 59c279872060..000000000000 --- a/java-secretmanager/samples/snippets/src/main/java/secretmanager/IamRevokeAccess.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package secretmanager; - -// [START secretmanager_iam_revoke_access] -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; -import com.google.cloud.secretmanager.v1.SecretName; -import com.google.iam.v1.Binding; -import com.google.iam.v1.GetIamPolicyRequest; -import com.google.iam.v1.Policy; -import com.google.iam.v1.SetIamPolicyRequest; -import java.io.IOException; - -public class IamRevokeAccess { - - public void iamRevokeAccess() throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "your-project-id"; - String secretId = "your-secret-id"; - String member = "user:foo@example.com"; - iamRevokeAccess(projectId, secretId, member); - } - - // Revoke a member access to a particular secret. - public void iamRevokeAccess(String projectId, String secretId, String member) throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // 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. - SecretName secretName = SecretName.of(projectId, secretId); - - // Request the current IAM policy. - Policy policy = - client.getIamPolicy( - GetIamPolicyRequest.newBuilder().setResource(secretName.toString()).build()); - - // Search through bindings and remove matches. - String roleToFind = "roles/secretmanager.secretAccessor"; - for (Binding binding : policy.getBindingsList()) { - if (binding.getRole() == roleToFind && binding.getMembersList().contains(member)) { - binding.getMembersList().remove(member); - } - } - - // Save the updated IAM policy. - client.setIamPolicy( - SetIamPolicyRequest.newBuilder() - .setResource(secretName.toString()) - .setPolicy(policy) - .build()); - - System.out.printf("Updated IAM policy for %s\n", secretId); - } - } -} -// [END secretmanager_iam_revoke_access] diff --git a/java-secretmanager/samples/snippets/src/main/java/secretmanager/ListSecretVersions.java b/java-secretmanager/samples/snippets/src/main/java/secretmanager/ListSecretVersions.java deleted file mode 100644 index eb5cce2c677d..000000000000 --- a/java-secretmanager/samples/snippets/src/main/java/secretmanager/ListSecretVersions.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package secretmanager; - -// [START secretmanager_list_secret_versions] -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretVersionsPagedResponse; -import com.google.cloud.secretmanager.v1.SecretName; -import java.io.IOException; - -public class ListSecretVersions { - - public void listSecretVersions() throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "your-project-id"; - String secretId = "your-secret-id"; - listSecretVersions(projectId, secretId); - } - - // List all secret versions for a secret. - public void listSecretVersions(String projectId, String secretId) throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // the "close" method on the client to safely clean up any remaining background resources. - try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) { - // Build the parent name. - SecretName projectName = SecretName.of(projectId, secretId); - - // Get all versions. - ListSecretVersionsPagedResponse pagedResponse = client.listSecretVersions(projectName); - - // List all versions and their state. - pagedResponse - .iterateAll() - .forEach( - version -> { - System.out.printf("Secret version %s, %s\n", version.getName(), version.getState()); - }); - } - } -} -// [END secretmanager_list_secret_versions] diff --git a/java-secretmanager/samples/snippets/src/main/java/secretmanager/ListSecrets.java b/java-secretmanager/samples/snippets/src/main/java/secretmanager/ListSecrets.java deleted file mode 100644 index ff1aefca366e..000000000000 --- a/java-secretmanager/samples/snippets/src/main/java/secretmanager/ListSecrets.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package secretmanager; - -// [START secretmanager_list_secrets] -import com.google.cloud.secretmanager.v1.ProjectName; -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretsPagedResponse; -import java.io.IOException; - -public class ListSecrets { - - public void listSecrets() throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "your-project-id"; - listSecrets(projectId); - } - - // List all secrets for a project - public void listSecrets(String projectId) throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // the "close" method on the client to safely clean up any remaining background resources. - try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) { - // Build the parent name. - ProjectName projectName = ProjectName.of(projectId); - - // Get all secrets. - ListSecretsPagedResponse pagedResponse = client.listSecrets(projectName); - - // List all secrets. - pagedResponse - .iterateAll() - .forEach( - secret -> { - System.out.printf("Secret %s\n", secret.getName()); - }); - } - } -} -// [END secretmanager_list_secrets] diff --git a/java-secretmanager/samples/snippets/src/main/java/secretmanager/Quickstart.java b/java-secretmanager/samples/snippets/src/main/java/secretmanager/Quickstart.java deleted file mode 100644 index d71e23196fef..000000000000 --- a/java-secretmanager/samples/snippets/src/main/java/secretmanager/Quickstart.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package secretmanager; - -// [START secretmanager_quickstart] -import com.google.cloud.secretmanager.v1.AccessSecretVersionResponse; -import com.google.cloud.secretmanager.v1.ProjectName; -import com.google.cloud.secretmanager.v1.Replication; -import com.google.cloud.secretmanager.v1.Secret; -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; -import com.google.cloud.secretmanager.v1.SecretPayload; -import com.google.cloud.secretmanager.v1.SecretVersion; -import com.google.protobuf.ByteString; - -public class Quickstart { - - public void quickstart() throws Exception { - // TODO(developer): Replace these variables before running the sample. - String projectId = "your-project-id"; - String secretId = "your-secret-id"; - quickstart(projectId, secretId); - } - - public void quickstart(String projectId, String secretId) throws Exception { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // the "close" method on the client to safely clean up any remaining background resources. - try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) { - // Build the parent name from the project. - ProjectName projectName = ProjectName.of(projectId); - - // Create the parent secret. - Secret secret = - Secret.newBuilder() - .setReplication( - Replication.newBuilder() - .setAutomatic(Replication.Automatic.newBuilder().build()) - .build()) - .build(); - - Secret createdSecret = client.createSecret(projectName, secretId, secret); - - // Add a secret version. - SecretPayload payload = - SecretPayload.newBuilder().setData(ByteString.copyFromUtf8("hello world!")).build(); - SecretVersion addedVersion = client.addSecretVersion(createdSecret.getName(), payload); - - // Access the secret version. - AccessSecretVersionResponse response = client.accessSecretVersion(addedVersion.getName()); - - // 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 data = response.getPayload().getData().toStringUtf8(); - System.out.printf("Plaintext: %s\n", data); - } - } -} -// [END secretmanager_quickstart] diff --git a/java-secretmanager/samples/snippets/src/main/java/secretmanager/UpdateSecret.java b/java-secretmanager/samples/snippets/src/main/java/secretmanager/UpdateSecret.java deleted file mode 100644 index f4df8c973c9e..000000000000 --- a/java-secretmanager/samples/snippets/src/main/java/secretmanager/UpdateSecret.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package secretmanager; - -// [START secretmanager_update_secret] -import com.google.cloud.secretmanager.v1.Secret; -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; -import com.google.cloud.secretmanager.v1.SecretName; -import com.google.protobuf.FieldMask; -import com.google.protobuf.util.FieldMaskUtil; -import java.io.IOException; - -public class UpdateSecret { - - public void updateSecret() throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "your-project-id"; - String secretId = "your-secret-id"; - updateSecret(projectId, secretId); - } - - // Update an existing secret. - public void updateSecret(String projectId, String secretId) throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // 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); - - // Build the updated secret. - Secret secret = - Secret.newBuilder() - .setName(secretName.toString()) - .putLabels("secretmanager", "rocks") - .build(); - - // Build the field mask. - FieldMask fieldMask = FieldMaskUtil.fromString("labels"); - - // Create the secret. - Secret updatedSecret = client.updateSecret(secret, fieldMask); - System.out.printf("Updated secret %s\n", updatedSecret.getName()); - } - } -} -// [END secretmanager_update_secret] diff --git a/java-secretmanager/samples/snippets/src/test/java/secretmanager/QuickstartIT.java b/java-secretmanager/samples/snippets/src/test/java/secretmanager/QuickstartIT.java deleted file mode 100644 index 991110084111..000000000000 --- a/java-secretmanager/samples/snippets/src/test/java/secretmanager/QuickstartIT.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package secretmanager; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.cloud.secretmanager.v1.DeleteSecretRequest; -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; -import com.google.cloud.secretmanager.v1.SecretName; -import com.google.common.base.Strings; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.UUID; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Integration (system) tests for {@link Quickstart}. */ -@RunWith(JUnit4.class) -public class QuickstartIT { - - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String SECRET_ID = "java-quickstart-" + UUID.randomUUID().toString(); - - @BeforeClass - public static void beforeAll() throws Exception { - Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); - } - - @AfterClass - public static void afterAll() throws Exception { - Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); - - try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) { - - // Delete the secret created by quickstart - SecretName name = SecretName.of(PROJECT_ID, SECRET_ID); - DeleteSecretRequest deleteRequest = - DeleteSecretRequest.newBuilder().setName(name.toString()).build(); - - client.deleteSecret(deleteRequest); - } - } - - @Test - public void quickstart_test() throws Exception { - PrintStream originalOut = System.out; - ByteArrayOutputStream redirected = new ByteArrayOutputStream(); - - System.setOut(new PrintStream(redirected)); - - try { - new Quickstart().quickstart(PROJECT_ID, SECRET_ID); - assertThat(redirected.toString()).contains("Plaintext: hello world!"); - } finally { - System.setOut(originalOut); - } - } -} diff --git a/java-secretmanager/samples/snippets/src/test/java/secretmanager/SnippetsIT.java b/java-secretmanager/samples/snippets/src/test/java/secretmanager/SnippetsIT.java deleted file mode 100644 index 06a46cdc9f98..000000000000 --- a/java-secretmanager/samples/snippets/src/test/java/secretmanager/SnippetsIT.java +++ /dev/null @@ -1,287 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package secretmanager; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.cloud.secretmanager.v1.AddSecretVersionRequest; -import com.google.cloud.secretmanager.v1.CreateSecretRequest; -import com.google.cloud.secretmanager.v1.DeleteSecretRequest; -import com.google.cloud.secretmanager.v1.DisableSecretVersionRequest; -import com.google.cloud.secretmanager.v1.ProjectName; -import com.google.cloud.secretmanager.v1.Replication; -import com.google.cloud.secretmanager.v1.Secret; -import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; -import com.google.cloud.secretmanager.v1.SecretName; -import com.google.cloud.secretmanager.v1.SecretPayload; -import com.google.cloud.secretmanager.v1.SecretVersion; -import com.google.cloud.secretmanager.v1.SecretVersionName; -import com.google.common.base.Strings; -import com.google.protobuf.ByteString; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintStream; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Integration (system) tests for {@link Snippets}. */ -@RunWith(JUnit4.class) -public class SnippetsIT { - private static final String IAM_USER = - "serviceAccount:iam-samples@java-docs-samples-testing.iam.gserviceaccount.com"; - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - - private static Secret TEST_SECRET; - private static Secret TEST_SECRET_TO_DELETE; - private static Secret TEST_SECRET_WITH_VERSIONS; - private static SecretName TEST_SECRET_TO_CREATE_NAME; - private static SecretVersion TEST_SECRET_VERSION; - private static SecretVersion TEST_SECRET_VERSION_TO_DESTROY; - private static SecretVersion TEST_SECRET_VERSION_TO_DISABLE; - private static SecretVersion TEST_SECRET_VERSION_TO_ENABLE; - - private ByteArrayOutputStream stdOut; - - @BeforeClass - public static void beforeAll() throws IOException { - Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); - - TEST_SECRET = createSecret(); - TEST_SECRET_TO_DELETE = createSecret(); - TEST_SECRET_WITH_VERSIONS = createSecret(); - TEST_SECRET_TO_CREATE_NAME = SecretName.of(PROJECT_ID, randomSecretId()); - - TEST_SECRET_VERSION = addSecretVersion(TEST_SECRET_WITH_VERSIONS); - TEST_SECRET_VERSION_TO_DESTROY = addSecretVersion(TEST_SECRET_WITH_VERSIONS); - TEST_SECRET_VERSION_TO_DISABLE = addSecretVersion(TEST_SECRET_WITH_VERSIONS); - TEST_SECRET_VERSION_TO_ENABLE = addSecretVersion(TEST_SECRET_WITH_VERSIONS); - disableSecretVersion(TEST_SECRET_VERSION_TO_ENABLE); - } - - @Before - public void beforeEach() { - stdOut = new ByteArrayOutputStream(); - System.setOut(new PrintStream(stdOut)); - } - - @After - public void afterEach() { - stdOut = null; - System.setOut(null); - } - - @AfterClass - public static void afterAll() throws IOException { - Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); - - deleteSecret(TEST_SECRET.getName()); - deleteSecret(TEST_SECRET_TO_CREATE_NAME.toString()); - deleteSecret(TEST_SECRET_TO_DELETE.getName()); - deleteSecret(TEST_SECRET_WITH_VERSIONS.getName()); - } - - private static String randomSecretId() { - return "java-" + System.currentTimeMillis(); - } - - private static Secret createSecret() throws IOException { - ProjectName parent = ProjectName.of(PROJECT_ID); - - CreateSecretRequest request = - CreateSecretRequest.newBuilder() - .setParent(parent.toString()) - .setSecretId(randomSecretId()) - .setSecret( - Secret.newBuilder() - .setReplication( - Replication.newBuilder() - .setAutomatic(Replication.Automatic.newBuilder().build()) - .build()) - .build()) - .build(); - - try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) { - return client.createSecret(request); - } - } - - private static SecretVersion addSecretVersion(Secret secret) throws IOException { - SecretName parent = SecretName.parse(secret.getName()); - - AddSecretVersionRequest request = - AddSecretVersionRequest.newBuilder() - .setParent(parent.toString()) - .setPayload( - SecretPayload.newBuilder() - .setData(ByteString.copyFromUtf8("my super secret data")) - .build()) - .build(); - - try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) { - return client.addSecretVersion(request); - } - } - - private static void deleteSecret(String secretId) throws IOException { - DeleteSecretRequest request = DeleteSecretRequest.newBuilder().setName(secretId).build(); - try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) { - try { - client.deleteSecret(request); - } catch (com.google.api.gax.rpc.NotFoundException e) { - // Ignore not found error - secret was already deleted - } catch (io.grpc.StatusRuntimeException e) { - if (e.getStatus().getCode() != io.grpc.Status.Code.NOT_FOUND) { - throw e; - } - } - } - } - - private static SecretVersion disableSecretVersion(SecretVersion version) throws IOException { - DisableSecretVersionRequest request = - DisableSecretVersionRequest.newBuilder().setName(version.getName()).build(); - try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) { - return client.disableSecretVersion(request); - } - } - - @Test - public void testAccessSecretVersion() throws IOException { - SecretVersionName name = SecretVersionName.parse(TEST_SECRET_VERSION.getName()); - new AccessSecretVersion() - .accessSecretVersion(name.getProject(), name.getSecret(), name.getSecretVersion()); - - assertThat(stdOut.toString()).contains("my super secret data"); - } - - @Test - public void testAddSecretVersion() throws IOException { - SecretName name = SecretName.parse(TEST_SECRET_WITH_VERSIONS.getName()); - new AddSecretVersion().addSecretVersion(name.getProject(), name.getSecret()); - - assertThat(stdOut.toString()).contains("Added secret version"); - } - - @Test - public void testCreateSecret() throws IOException { - SecretName name = TEST_SECRET_TO_CREATE_NAME; - new CreateSecret().createSecret(name.getProject(), name.getSecret()); - - assertThat(stdOut.toString()).contains("Created secret"); - } - - @Test - public void testDeleteSecret() throws IOException { - SecretName name = SecretName.parse(TEST_SECRET_TO_DELETE.getName()); - new DeleteSecret().deleteSecret(name.getProject(), name.getSecret()); - - assertThat(stdOut.toString()).contains("Deleted secret"); - } - - @Test - public void testDestroySecretVersion() throws IOException { - SecretVersionName name = SecretVersionName.parse(TEST_SECRET_VERSION_TO_DESTROY.getName()); - new DestroySecretVersion() - .destroySecretVersion(name.getProject(), name.getSecret(), name.getSecretVersion()); - - assertThat(stdOut.toString()).contains("Destroyed secret version"); - } - - @Test - public void testDisableSecretVersion() throws IOException { - SecretVersionName name = SecretVersionName.parse(TEST_SECRET_VERSION_TO_DISABLE.getName()); - new DisableSecretVersion() - .disableSecretVersion(name.getProject(), name.getSecret(), name.getSecretVersion()); - - assertThat(stdOut.toString()).contains("Disabled secret version"); - } - - @Test - public void testEnableSecretVersion() throws IOException { - SecretVersionName name = SecretVersionName.parse(TEST_SECRET_VERSION_TO_ENABLE.getName()); - new EnableSecretVersion() - .enableSecretVersion(name.getProject(), name.getSecret(), name.getSecretVersion()); - - assertThat(stdOut.toString()).contains("Enabled secret version"); - } - - @Test - public void testGetSecretVersion() throws IOException { - SecretVersionName name = SecretVersionName.parse(TEST_SECRET_VERSION.getName()); - new GetSecretVersion() - .getSecretVersion(name.getProject(), name.getSecret(), name.getSecretVersion()); - - assertThat(stdOut.toString()).contains("Secret version"); - assertThat(stdOut.toString()).contains("state ENABLED"); - } - - @Test - public void testGetSecret() throws IOException { - SecretName name = SecretName.parse(TEST_SECRET.getName()); - new GetSecret().getSecret(name.getProject(), name.getSecret()); - - assertThat(stdOut.toString()).contains("Secret"); - assertThat(stdOut.toString()).contains("replication AUTOMATIC"); - } - - @Test - public void testIamGrantAccess() throws IOException { - SecretName name = SecretName.parse(TEST_SECRET.getName()); - new IamGrantAccess().iamGrantAccess(name.getProject(), name.getSecret(), IAM_USER); - - assertThat(stdOut.toString()).contains("Updated IAM policy"); - } - - @Test - public void testIamRevokeAccess() throws IOException { - SecretName name = SecretName.parse(TEST_SECRET.getName()); - new IamRevokeAccess().iamRevokeAccess(name.getProject(), name.getSecret(), IAM_USER); - - assertThat(stdOut.toString()).contains("Updated IAM policy"); - } - - @Test - public void testListSecretVersions() throws IOException { - SecretName name = SecretName.parse(TEST_SECRET_WITH_VERSIONS.getName()); - new ListSecretVersions().listSecretVersions(name.getProject(), name.getSecret()); - - assertThat(stdOut.toString()).contains("Secret version"); - } - - @Test - public void testListSecrets() throws IOException { - SecretName name = SecretName.parse(TEST_SECRET.getName()); - new ListSecrets().listSecrets(name.getProject()); - - assertThat(stdOut.toString()).contains("Secret projects/"); - assertThat(stdOut.toString()).contains(name.getSecret()); - } - - @Test - public void testUpdateSecret() throws IOException { - SecretName name = SecretName.parse(TEST_SECRET.getName()); - new UpdateSecret().updateSecret(name.getProject(), name.getSecret()); - - assertThat(stdOut.toString()).contains("Updated secret"); - } -}