use of com.google.cloud.secretmanager.v1beta1.Secret in project spring-cloud-gcp by GoogleCloudPlatform.
the class SecretManagerTemplateTests method verifyCreateSecretRequest.
private void verifyCreateSecretRequest(String secretId, String projectId) {
Secret secretToAdd = Secret.newBuilder().setReplication(Replication.newBuilder().setAutomatic(Replication.Automatic.newBuilder()).build()).build();
CreateSecretRequest createSecretRequest = CreateSecretRequest.newBuilder().setParent("projects/" + projectId).setSecretId(secretId).setSecret(secretToAdd).build();
verify(this.client).createSecret(createSecretRequest);
}
use of com.google.cloud.secretmanager.v1beta1.Secret in project spring-cloud-gcp by GoogleCloudPlatform.
the class SecretManagerTemplate method createSecretInternal.
/**
* Creates a new secret for the GCP Project.
*
* <p>Note that the {@link Secret} object does not contain the secret payload. You must create
* versions of the secret which stores the payload of the secret.
*/
private void createSecretInternal(String secretId, String projectId) {
ProjectName projectName = ProjectName.of(projectId);
Secret secret = Secret.newBuilder().setReplication(Replication.newBuilder().setAutomatic(Replication.Automatic.getDefaultInstance())).build();
CreateSecretRequest request = CreateSecretRequest.newBuilder().setParent(projectName.toString()).setSecretId(secretId).setSecret(secret).build();
this.secretManagerServiceClient.createSecret(request);
}
use of com.google.cloud.secretmanager.v1beta1.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();
}
use of com.google.cloud.secretmanager.v1beta1.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);
}
}
use of com.google.cloud.secretmanager.v1beta1.Secret in project yakc by manusa.
the class AuthIT method retrieveSecretForServiceAccount.
private Secret retrieveSecretForServiceAccount() throws IOException {
final ServiceAccount sa = KC.create(CoreV1Api.class).listNamespacedServiceAccount(NAMESPACE).stream().findFirst().orElseThrow(() -> new AssertionError("No Service Account found"));
final String secretName = sa.getSecrets() == null ? null : sa.getSecrets().stream().findFirst().map(ObjectReference::getName).orElse(null);
if (secretName != null) {
return KC.create(CoreV1Api.class).listNamespacedSecret(NAMESPACE).stream().filter(s -> s.getType().equals("kubernetes.io/service-account-token")).filter(s -> s.getMetadata().getName().equals(secretName)).findAny().orElseThrow(() -> new AssertionError(String.format("Secret %s doesn't exist", secretName)));
} else {
// https://kubernetes.io/docs/concepts/configuration/secret/#service-account-token-secrets
final Secret serviceAccountTokenSecret = Secret.builder().metadata(ObjectMeta.builder().name(sa.getMetadata().getName() + "-token").putInAnnotations("kubernetes.io/service-account.name", sa.getMetadata().getName()).build()).type("kubernetes.io/service-account-token").putInStringData("token", "my-secret-token").build();
return KC.create(CoreV1Api.class).createNamespacedSecret(NAMESPACE, serviceAccountTokenSecret).get();
}
}
Aggregations