Java Code Examples for com.google.protobuf.util.FieldMaskUtil#fromString()

The following examples show how to use com.google.protobuf.util.FieldMaskUtil#fromString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UpdateKeyRemoveRotation.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
public void updateKeyRemoveRotation(
    String projectId, String locationId, String keyRingId, String keyId) 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 (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
    // Build the name from the project, location, key ring, and keyId.
    CryptoKeyName cryptoKeyName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);

    // Build an empty key with no labels.
    CryptoKey key =
        CryptoKey.newBuilder()
            .setName(cryptoKeyName.toString())
            .clearRotationPeriod()
            .clearNextRotationTime()
            .build();

    // Construct the field mask.
    FieldMask fieldMask = FieldMaskUtil.fromString("rotation_period,next_rotation_time");

    // Create the key.
    CryptoKey createdKey = client.updateCryptoKey(key, fieldMask);
    System.out.printf("Updated key %s%n", createdKey.getName());
  }
}
 
Example 2
Source File: UpdateKeyRemoveLabels.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
public void updateKeyRemoveLabels(
    String projectId, String locationId, String keyRingId, String keyId) 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 (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
    // Build the name from the project, location, key ring, and keyId.
    CryptoKeyName cryptoKeyName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);

    // Build an empty key with no labels.
    CryptoKey key = CryptoKey.newBuilder().setName(cryptoKeyName.toString()).build();

    // Construct the field mask.
    FieldMask fieldMask = FieldMaskUtil.fromString("labels");

    // Create the key.
    CryptoKey createdKey = client.updateCryptoKey(key, fieldMask);
    System.out.printf("Updated key %s%n", createdKey.getName());
  }
}
 
Example 3
Source File: SnippetsIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@AfterClass
public static void afterAll() throws IOException {
  Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID));

  // Iterate over each key ring's key's crypto key versions and destroy.
  try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
    for (CryptoKey key : client.listCryptoKeys(getKeyRingName()).iterateAll()) {
      if (key.hasRotationPeriod() || key.hasNextRotationTime()) {
        CryptoKey keyWithoutRotation = CryptoKey.newBuilder().setName(key.getName()).build();
        FieldMask fieldMask = FieldMaskUtil.fromString("rotation_period,next_rotation_time");
        client.updateCryptoKey(keyWithoutRotation, fieldMask);
      }

      ListCryptoKeyVersionsRequest listVersionsRequest =
          ListCryptoKeyVersionsRequest.newBuilder()
              .setParent(key.getName())
              .setFilter("state != DESTROYED AND state != DESTROY_SCHEDULED")
              .build();
      for (CryptoKeyVersion version :
          client.listCryptoKeyVersions(listVersionsRequest).iterateAll()) {
        client.destroyCryptoKeyVersion(version.getName());
      }
    }
  }
}
 
Example 4
Source File: UpdateSecret.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
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());
  }
}
 
Example 5
Source File: DisableKeyVersion.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public void disableKeyVersion(
    String projectId, String locationId, String keyRingId, String keyId, String keyVersionId)
    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 (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
    // Build the key version name from the project, location, key ring, key,
    // and key version.
    CryptoKeyVersionName keyVersionName =
        CryptoKeyVersionName.of(projectId, locationId, keyRingId, keyId, keyVersionId);

    // Build the updated key version, setting it to disbaled.
    CryptoKeyVersion keyVersion =
        CryptoKeyVersion.newBuilder()
            .setName(keyVersionName.toString())
            .setState(CryptoKeyVersionState.DISABLED)
            .build();

    // Create a field mask of updated values.
    FieldMask fieldMask = FieldMaskUtil.fromString("state");

    // Destroy the key version.
    CryptoKeyVersion response = client.updateCryptoKeyVersion(keyVersion, fieldMask);
    System.out.printf("Disabled key version: %s%n", response.getName());
  }
}
 
Example 6
Source File: UpdateKeyUpdateLabels.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public void updateKeyUpdateLabels(
    String projectId, String locationId, String keyRingId, String keyId) 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 (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
    // Build the parent name from the project, location, and key ring.
    CryptoKeyName cryptoKeyName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);

    //
    // Step 1 - get the current set of labels on the key
    //

    // Get the current key.
    CryptoKey key = client.getCryptoKey(cryptoKeyName);

    //
    // Step 2 - add a label to the list of labels
    //

    // Add a new label.
    key = key.toBuilder().putLabels("new_label", "new_value").build();

    // Construct the field mask.
    FieldMask fieldMask = FieldMaskUtil.fromString("labels");

    // Update the key.
    CryptoKey updatedKey = client.updateCryptoKey(key, fieldMask);
    System.out.printf("Updated key %s%n", updatedKey.getName());
  }
}
 
Example 7
Source File: EnableKeyVersion.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public void enableKeyVersion(
    String projectId, String locationId, String keyRingId, String keyId, String keyVersionId)
    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 (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
    // Build the key version name from the project, location, key ring, key,
    // and key version.
    CryptoKeyVersionName keyVersionName =
        CryptoKeyVersionName.of(projectId, locationId, keyRingId, keyId, keyVersionId);

    // Build the updated key version, setting it to enabled.
    CryptoKeyVersion keyVersion =
        CryptoKeyVersion.newBuilder()
            .setName(keyVersionName.toString())
            .setState(CryptoKeyVersionState.ENABLED)
            .build();

    // Create a field mask of updated values.
    FieldMask fieldMask = FieldMaskUtil.fromString("state");

    // Destroy the key version.
    CryptoKeyVersion response = client.updateCryptoKeyVersion(keyVersion, fieldMask);
    System.out.printf("Enabled key version: %s%n", response.getName());
  }
}
 
Example 8
Source File: UpdateKeyAddRotation.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public void updateKeyAddRotation(
    String projectId, String locationId, String keyRingId, String keyId) 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 (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
    // Build the name from the project, location, and key ring.
    CryptoKeyName cryptoKeyName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);

    // Calculate the date 24 hours from now (this is used below).
    long tomorrow = java.time.Instant.now().plus(24, ChronoUnit.HOURS).getEpochSecond();

    // Build the key to update with a rotation schedule.
    CryptoKey key =
        CryptoKey.newBuilder()
            .setName(cryptoKeyName.toString())
            .setPurpose(CryptoKeyPurpose.ENCRYPT_DECRYPT)
            .setVersionTemplate(
                CryptoKeyVersionTemplate.newBuilder()
                    .setAlgorithm(CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION))

            // Rotate every 30 days.
            .setRotationPeriod(
                Duration.newBuilder().setSeconds(java.time.Duration.ofDays(30).getSeconds()))

            // Start the first rotation in 24 hours.
            .setNextRotationTime(Timestamp.newBuilder().setSeconds(tomorrow))
            .build();

    // Construct the field mask.
    FieldMask fieldMask = FieldMaskUtil.fromString("rotation_period,next_rotation_time");

    // Update the key.
    CryptoKey updatedKey = client.updateCryptoKey(key, fieldMask);
    System.out.printf("Updated key %s%n", updatedKey.getName());
  }
}