Skip to content

Commit

Permalink
chore: upgrade kms samples to new client surface
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Dec 27, 2023
1 parent 1ec5c14 commit dac2f06
Show file tree
Hide file tree
Showing 35 changed files with 310 additions and 99 deletions.
2 changes: 1 addition & 1 deletion kms/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"google/cloud-kms": "^1.12.0"
"google/cloud-kms": "^1.20"
}
}
9 changes: 7 additions & 2 deletions kms/src/create_key_asymmetric_decrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
namespace Google\Cloud\Samples\Kms;

// [START kms_create_key_asymmetric_decrypt]
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\CreateCryptoKeyRequest;
use Google\Cloud\Kms\V1\CryptoKey;
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionAlgorithm;
use Google\Cloud\Kms\V1\CryptoKeyVersionTemplate;
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
use Google\Protobuf\Duration;

function create_key_asymmetric_decrypt(
Expand Down Expand Up @@ -52,7 +53,11 @@ function create_key_asymmetric_decrypt(
);

// Call the API.
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
$createCryptoKeyRequest = (new CreateCryptoKeyRequest())
->setParent($keyRingName)
->setCryptoKeyId($id)
->setCryptoKey($key);
$createdKey = $client->createCryptoKey($createCryptoKeyRequest);
printf('Created asymmetric decryption key: %s' . PHP_EOL, $createdKey->getName());

return $createdKey;
Expand Down
9 changes: 7 additions & 2 deletions kms/src/create_key_asymmetric_sign.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
namespace Google\Cloud\Samples\Kms;

// [START kms_create_key_asymmetric_sign]
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\CreateCryptoKeyRequest;
use Google\Cloud\Kms\V1\CryptoKey;
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionAlgorithm;
use Google\Cloud\Kms\V1\CryptoKeyVersionTemplate;
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
use Google\Protobuf\Duration;

function create_key_asymmetric_sign(
Expand Down Expand Up @@ -52,7 +53,11 @@ function create_key_asymmetric_sign(
);

// Call the API.
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
$createCryptoKeyRequest = (new CreateCryptoKeyRequest())
->setParent($keyRingName)
->setCryptoKeyId($id)
->setCryptoKey($key);
$createdKey = $client->createCryptoKey($createCryptoKeyRequest);
printf('Created asymmetric signing key: %s' . PHP_EOL, $createdKey->getName());

return $createdKey;
Expand Down
9 changes: 7 additions & 2 deletions kms/src/create_key_hsm.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
namespace Google\Cloud\Samples\Kms;

// [START kms_create_key_hsm]
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\CreateCryptoKeyRequest;
use Google\Cloud\Kms\V1\CryptoKey;
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionAlgorithm;
use Google\Cloud\Kms\V1\CryptoKeyVersionTemplate;
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\ProtectionLevel;
use Google\Protobuf\Duration;

Expand Down Expand Up @@ -54,7 +55,11 @@ function create_key_hsm(
);

// Call the API.
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
$createCryptoKeyRequest = (new CreateCryptoKeyRequest())
->setParent($keyRingName)
->setCryptoKeyId($id)
->setCryptoKey($key);
$createdKey = $client->createCryptoKey($createCryptoKeyRequest);
printf('Created hsm key: %s' . PHP_EOL, $createdKey->getName());

return $createdKey;
Expand Down
9 changes: 7 additions & 2 deletions kms/src/create_key_labels.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
namespace Google\Cloud\Samples\Kms;

// [START kms_create_key_labels]
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\CreateCryptoKeyRequest;
use Google\Cloud\Kms\V1\CryptoKey;
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionAlgorithm;
use Google\Cloud\Kms\V1\CryptoKeyVersionTemplate;
use Google\Cloud\Kms\V1\KeyManagementServiceClient;

function create_key_labels(
string $projectId = 'my-project',
Expand All @@ -50,7 +51,11 @@ function create_key_labels(
]);

// Call the API.
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
$createCryptoKeyRequest = (new CreateCryptoKeyRequest())
->setParent($keyRingName)
->setCryptoKeyId($id)
->setCryptoKey($key);
$createdKey = $client->createCryptoKey($createCryptoKeyRequest);
printf('Created labeled key: %s' . PHP_EOL, $createdKey->getName());

return $createdKey;
Expand Down
9 changes: 7 additions & 2 deletions kms/src/create_key_mac.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
namespace Google\Cloud\Samples\Kms;

// [START kms_create_key_mac]
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\CreateCryptoKeyRequest;
use Google\Cloud\Kms\V1\CryptoKey;
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionAlgorithm;
use Google\Cloud\Kms\V1\CryptoKeyVersionTemplate;
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
use Google\Protobuf\Duration;

function create_key_mac(
Expand Down Expand Up @@ -52,7 +53,11 @@ function create_key_mac(
);

// Call the API.
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
$createCryptoKeyRequest = (new CreateCryptoKeyRequest())
->setParent($keyRingName)
->setCryptoKeyId($id)
->setCryptoKey($key);
$createdKey = $client->createCryptoKey($createCryptoKeyRequest);
printf('Created mac key: %s' . PHP_EOL, $createdKey->getName());

return $createdKey;
Expand Down
9 changes: 7 additions & 2 deletions kms/src/create_key_ring.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
namespace Google\Cloud\Samples\Kms;

// [START kms_create_key_ring]
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\CreateKeyRingRequest;
use Google\Cloud\Kms\V1\KeyRing;

function create_key_ring(
Expand All @@ -38,7 +39,11 @@ function create_key_ring(
$keyRing = new KeyRing();

// Call the API.
$createdKeyRing = $client->createKeyRing($locationName, $id, $keyRing);
$createKeyRingRequest = (new CreateKeyRingRequest())
->setParent($locationName)
->setKeyRingId($id)
->setKeyRing($keyRing);
$createdKeyRing = $client->createKeyRing($createKeyRingRequest);
printf('Created key ring: %s' . PHP_EOL, $createdKeyRing->getName());

return $createdKeyRing;
Expand Down
9 changes: 7 additions & 2 deletions kms/src/create_key_rotation_schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
namespace Google\Cloud\Samples\Kms;

// [START kms_create_key_rotation_schedule]
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\CreateCryptoKeyRequest;
use Google\Cloud\Kms\V1\CryptoKey;
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionAlgorithm;
use Google\Cloud\Kms\V1\CryptoKeyVersionTemplate;
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
use Google\Protobuf\Duration;
use Google\Protobuf\Timestamp;

Expand Down Expand Up @@ -57,7 +58,11 @@ function create_key_rotation_schedule(
);

// Call the API.
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
$createCryptoKeyRequest = (new CreateCryptoKeyRequest())
->setParent($keyRingName)
->setCryptoKeyId($id)
->setCryptoKey($key);
$createdKey = $client->createCryptoKey($createCryptoKeyRequest);
printf('Created key with rotation: %s' . PHP_EOL, $createdKey->getName());

return $createdKey;
Expand Down
9 changes: 7 additions & 2 deletions kms/src/create_key_symmetric_encrypt_decrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
namespace Google\Cloud\Samples\Kms;

// [START kms_create_key_symmetric_encrypt_decrypt]
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\CreateCryptoKeyRequest;
use Google\Cloud\Kms\V1\CryptoKey;
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionAlgorithm;
use Google\Cloud\Kms\V1\CryptoKeyVersionTemplate;
use Google\Cloud\Kms\V1\KeyManagementServiceClient;

function create_key_symmetric_encrypt_decrypt(
string $projectId = 'my-project',
Expand All @@ -46,7 +47,11 @@ function create_key_symmetric_encrypt_decrypt(
);

// Call the API.
$createdKey = $client->createCryptoKey($keyRingName, $id, $key);
$createCryptoKeyRequest = (new CreateCryptoKeyRequest())
->setParent($keyRingName)
->setCryptoKeyId($id)
->setCryptoKey($key);
$createdKey = $client->createCryptoKey($createCryptoKeyRequest);
printf('Created symmetric key: %s' . PHP_EOL, $createdKey->getName());

return $createdKey;
Expand Down
8 changes: 6 additions & 2 deletions kms/src/create_key_version.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
namespace Google\Cloud\Samples\Kms;

// [START kms_create_key_version]
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\CreateCryptoKeyVersionRequest;
use Google\Cloud\Kms\V1\CryptoKeyVersion;
use Google\Cloud\Kms\V1\KeyManagementServiceClient;

function create_key_version(
string $projectId = 'my-project',
Expand All @@ -39,7 +40,10 @@ function create_key_version(
$version = new CryptoKeyVersion();

// Call the API.
$createdVersion = $client->createCryptoKeyVersion($keyName, $version);
$createCryptoKeyVersionRequest = (new CreateCryptoKeyVersionRequest())
->setParent($keyName)
->setCryptoKeyVersion($version);
$createdVersion = $client->createCryptoKeyVersion($createCryptoKeyVersionRequest);
printf('Created key version: %s' . PHP_EOL, $createdVersion->getName());

return $createdVersion;
Expand Down
8 changes: 6 additions & 2 deletions kms/src/decrypt_asymmetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
namespace Google\Cloud\Samples\Kms;

// [START kms_decrypt_asymmetric]
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\AsymmetricDecryptRequest;
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;

function decrypt_asymmetric(
string $projectId = 'my-project',
Expand All @@ -37,7 +38,10 @@ function decrypt_asymmetric(
$keyVersionName = $client->cryptoKeyVersionName($projectId, $locationId, $keyRingId, $keyId, $versionId);

// Call the API.
$decryptResponse = $client->asymmetricDecrypt($keyVersionName, $ciphertext);
$asymmetricDecryptRequest = (new AsymmetricDecryptRequest())
->setName($keyVersionName)
->setCiphertext($ciphertext);
$decryptResponse = $client->asymmetricDecrypt($asymmetricDecryptRequest);
printf('Plaintext: %s' . PHP_EOL, $decryptResponse->getPlaintext());

return $decryptResponse;
Expand Down
8 changes: 6 additions & 2 deletions kms/src/decrypt_symmetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
namespace Google\Cloud\Samples\Kms;

// [START kms_decrypt_symmetric]
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\DecryptRequest;

function decrypt_symmetric(
string $projectId = 'my-project',
Expand All @@ -36,7 +37,10 @@ function decrypt_symmetric(
$keyName = $client->cryptoKeyName($projectId, $locationId, $keyRingId, $keyId);

// Call the API.
$decryptResponse = $client->decrypt($keyName, $ciphertext);
$decryptRequest = (new DecryptRequest())
->setName($keyName)
->setCiphertext($ciphertext);
$decryptResponse = $client->decrypt($decryptRequest);
printf('Plaintext: %s' . PHP_EOL, $decryptResponse->getPlaintext());

return $decryptResponse;
Expand Down
7 changes: 5 additions & 2 deletions kms/src/destroy_key_version.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
namespace Google\Cloud\Samples\Kms;

// [START kms_destroy_key_version]
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\DestroyCryptoKeyVersionRequest;

function destroy_key_version(
string $projectId = 'my-project',
Expand All @@ -36,7 +37,9 @@ function destroy_key_version(
$keyVersionName = $client->cryptoKeyVersionName($projectId, $locationId, $keyRingId, $keyId, $versionId);

// Call the API.
$destroyedVersion = $client->destroyCryptoKeyVersion($keyVersionName);
$destroyCryptoKeyVersionRequest = (new DestroyCryptoKeyVersionRequest())
->setName($keyVersionName);
$destroyedVersion = $client->destroyCryptoKeyVersion($destroyCryptoKeyVersionRequest);
printf('Destroyed key version: %s' . PHP_EOL, $destroyedVersion->getName());

return $destroyedVersion;
Expand Down
8 changes: 6 additions & 2 deletions kms/src/disable_key_version.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
namespace Google\Cloud\Samples\Kms;

// [START kms_disable_key_version]
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\CryptoKeyVersion;
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionState;
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\UpdateCryptoKeyVersionRequest;
use Google\Protobuf\FieldMask;

function disable_key_version(
Expand All @@ -48,7 +49,10 @@ function disable_key_version(
->setPaths(['state']);

// Call the API.
$disabledVersion = $client->updateCryptoKeyVersion($keyVersion, $updateMask);
$updateCryptoKeyVersionRequest = (new UpdateCryptoKeyVersionRequest())
->setCryptoKeyVersion($keyVersion)
->setUpdateMask($updateMask);
$disabledVersion = $client->updateCryptoKeyVersion($updateCryptoKeyVersionRequest);
printf('Disabled key version: %s' . PHP_EOL, $disabledVersion->getName());

return $disabledVersion;
Expand Down
8 changes: 6 additions & 2 deletions kms/src/enable_key_version.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
namespace Google\Cloud\Samples\Kms;

// [START kms_enable_key_version]
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\CryptoKeyVersion;
use Google\Cloud\Kms\V1\CryptoKeyVersion\CryptoKeyVersionState;
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\UpdateCryptoKeyVersionRequest;
use Google\Protobuf\FieldMask;

function enable_key_version(
Expand All @@ -48,7 +49,10 @@ function enable_key_version(
->setPaths(['state']);

// Call the API.
$enabledVersion = $client->updateCryptoKeyVersion($keyVersion, $updateMask);
$updateCryptoKeyVersionRequest = (new UpdateCryptoKeyVersionRequest())
->setCryptoKeyVersion($keyVersion)
->setUpdateMask($updateMask);
$enabledVersion = $client->updateCryptoKeyVersion($updateCryptoKeyVersionRequest);
printf('Enabled key version: %s' . PHP_EOL, $enabledVersion->getName());

return $enabledVersion;
Expand Down
8 changes: 6 additions & 2 deletions kms/src/encrypt_symmetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
namespace Google\Cloud\Samples\Kms;

// [START kms_encrypt_symmetric]
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\EncryptRequest;

function encrypt_symmetric(
string $projectId = 'my-project',
Expand All @@ -36,7 +37,10 @@ function encrypt_symmetric(
$keyName = $client->cryptoKeyName($projectId, $locationId, $keyRingId, $keyId);

// Call the API.
$encryptResponse = $client->encrypt($keyName, $plaintext);
$encryptRequest = (new EncryptRequest())
->setName($keyName)
->setPlaintext($plaintext);
$encryptResponse = $client->encrypt($encryptRequest);
printf('Ciphertext: %s' . PHP_EOL, $encryptResponse->getCiphertext());

return $encryptResponse;
Expand Down
Loading

0 comments on commit dac2f06

Please sign in to comment.