Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare KMS for GA #1637

Merged
merged 2 commits into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Kms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ assert($secret === $plainText);

### Version

This component is considered beta. As such, it should be expected to be mostly
stable and we're working towards a release candidate. We will address issues
and requests with a higher priority.
This component is considered GA (generally available). As such, it will not introduce backwards-incompatible changes in
any minor or patch releases. We will address issues and requests with the highest priority.

### Next Steps

Expand Down
138 changes: 69 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This client supports the following Google Cloud Platform services at a [General
* [Cloud Spanner](#cloud-spanner-ga) (GA)
* [Google BigQuery](#google-bigquery-ga) (GA)
* [Google Cloud Datastore](#google-cloud-datastore-ga) (GA)
* [Google Cloud KMS](#google-cloud-kms-ga) (GA)
* [Google Cloud Pub/Sub](#google-cloud-pubsub-ga) (GA)
* [Google Cloud Storage](#google-cloud-storage-ga) (GA)
* [Google Cloud Translation](#google-cloud-translation-ga) (GA)
Expand All @@ -28,7 +29,6 @@ This client supports the following Google Cloud Platform services at a [Beta](#v
* [Google Cloud Asset](#google-cloud-asset-beta) (Beta)
* [Google Cloud Container](#google-cloud-container-beta) (Beta)
* [Google Cloud Dataproc](#google-cloud-dataproc-beta) (Beta)
* [Google Cloud KMS](#google-cloud-kms-beta) (Beta)
* [Google Cloud Natural Language](#google-cloud-natural-language-beta) (Beta)
* [Google Cloud OsLogin](#google-cloud-oslogin-beta) (Beta)
* [Google Cloud Scheduler](#google-cloud-scheduler-beta) (Beta)
Expand Down Expand Up @@ -231,6 +231,74 @@ $entity = $datastore->lookup($key);
$ composer require google/cloud-datastore
```

## Google Cloud KMS (GA)

- [API Documentation](http://googleapis.github.io/google-cloud-php/#/docs/latest/kms/readme)
- [Official Documentation](https://cloud.google.com/kms/docs/reference/rest/)

```php
require __DIR__ . '/vendor/autoload.php';

use Google\ApiCore\ApiException;
use Google\Cloud\Kms\V1\CryptoKey;
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\KeyRing;

$client = new KeyManagementServiceClient();

$projectId = 'example-project';
$location = 'global';

// Create a keyring
$keyRingId = 'example-keyring';
$locationName = $client::locationName($projectId, $location);
$keyRingName = $client::keyRingName($projectId, $location, $keyRingId);

try {
$keyRing = $client->getKeyRing($keyRingName);
} catch (ApiException $e) {
if ($e->getStatus() === 'NOT_FOUND') {
$keyRing = new KeyRing();
$keyRing->setName($keyRingName);
$client->createKeyRing($locationName, $keyRingId, $keyRing);
}
}

// Create a cryptokey
$keyId = 'example-key';
$keyName = $client::cryptoKeyName($projectId, $location, $keyRingId, $keyId);

try {
$cryptoKey = $client->getCryptoKey($keyName);
} catch (ApiException $e) {
if ($e->getStatus() === 'NOT_FOUND') {
$cryptoKey = new CryptoKey();
$cryptoKey->setPurpose(CryptoKeyPurpose::ENCRYPT_DECRYPT);
$cryptoKey = $client->createCryptoKey($keyRingName, $keyId, $cryptoKey);
}
}

// Encrypt and decrypt
$secret = 'My secret text';
$response = $client->encrypt($keyName, $secret);
$cipherText = $response->getCiphertext();

$response = $client->decrypt($keyName, $cipherText);

$plainText = $response->getPlaintext();

assert($secret === $plainText);
```

#### google/cloud-kms

[Google Cloud KMS](https://github.com/googleapis/google-cloud-php-kms) can be installed separately by requiring the [`google/cloud-kms`](https://packagist.org/packages/google/cloud-kms) composer package:

```
$ composer require google/cloud-kms
```

## Google Cloud Pub/Sub (GA)

- [API Documentation](http://googleapis.github.io/google-cloud-php/#/docs/latest/pubsub/pubsubclient)
Expand Down Expand Up @@ -627,74 +695,6 @@ $submittedJob = $jobControllerClient->submitJob($projectId, $region, $job);
$ composer require google/cloud-dataproc
```

## Google Cloud KMS (Beta)

- [API Documentation](http://googleapis.github.io/google-cloud-php/#/docs/latest/kms/readme)
- [Official Documentation](https://cloud.google.com/kms/docs/reference/rest/)

```php
require __DIR__ . '/vendor/autoload.php';

use Google\ApiCore\ApiException;
use Google\Cloud\Kms\V1\CryptoKey;
use Google\Cloud\Kms\V1\CryptoKey\CryptoKeyPurpose;
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\KeyRing;

$client = new KeyManagementServiceClient();

$projectId = 'example-project';
$location = 'global';

// Create a keyring
$keyRingId = 'example-keyring';
$locationName = $client::locationName($projectId, $location);
$keyRingName = $client::keyRingName($projectId, $location, $keyRingId);

try {
$keyRing = $client->getKeyRing($keyRingName);
} catch (ApiException $e) {
if ($e->getStatus() === 'NOT_FOUND') {
$keyRing = new KeyRing();
$keyRing->setName($keyRingName);
$client->createKeyRing($locationName, $keyRingId, $keyRing);
}
}

// Create a cryptokey
$keyId = 'example-key';
$keyName = $client::cryptoKeyName($projectId, $location, $keyRingId, $keyId);

try {
$cryptoKey = $client->getCryptoKey($keyName);
} catch (ApiException $e) {
if ($e->getStatus() === 'NOT_FOUND') {
$cryptoKey = new CryptoKey();
$cryptoKey->setPurpose(CryptoKeyPurpose::ENCRYPT_DECRYPT);
$cryptoKey = $client->createCryptoKey($keyRingName, $keyId, $cryptoKey);
}
}

// Encrypt and decrypt
$secret = 'My secret text';
$response = $client->encrypt($keyName, $secret);
$cipherText = $response->getCiphertext();

$response = $client->decrypt($keyName, $cipherText);

$plainText = $response->getPlaintext();

assert($secret === $plainText);
```

#### google/cloud-kms

[Google Cloud KMS](https://github.com/googleapis/google-cloud-php-kms) can be installed separately by requiring the [`google/cloud-kms`](https://packagist.org/packages/google/cloud-kms) composer package:

```
$ composer require google/cloud-kms
```

## Google Cloud Natural Language (Beta)

- [API Documentation](http://googleapis.github.io/google-cloud-php/#/docs/latest/language/languageclient)
Expand Down