从现有密钥中移除轮替时间表。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
C#
如需了解如何安装和使用 Cloud KMS 客户端库,请参阅 Cloud KMS 客户端库。
如需向 Cloud KMS 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
using Google.Cloud.Kms.V1;
using Google.Protobuf.WellKnownTypes;
public class UpdateKeyRemoveRotationSample
{
public CryptoKey UpdateKeyRemoveRotation(string projectId = "my-project", string locationId = "us-east1", string keyRingId = "my-key-ring", string keyId = "my-key")
{
// Create the client.
KeyManagementServiceClient client = KeyManagementServiceClient.Create();
// Build the key.
CryptoKey key = new CryptoKey
{
CryptoKeyName = new CryptoKeyName(projectId, locationId, keyRingId, keyId),
RotationPeriod = null,
NextRotationTime = null,
};
// Build the update mask.
FieldMask fieldMask = new FieldMask
{
Paths = { "rotation_period", "next_rotation_time" },
};
// Call the API.
CryptoKey result = client.UpdateCryptoKey(key, fieldMask);
// Return the updated key.
return result;
}
}
Go
如需了解如何安装和使用 Cloud KMS 客户端库,请参阅 Cloud KMS 客户端库。
如需向 Cloud KMS 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
import (
"context"
"fmt"
"io"
kms "cloud.google.com/go/kms/apiv1"
"cloud.google.com/go/kms/apiv1/kmspb"
fieldmask "google.golang.org/genproto/protobuf/field_mask"
)
// removeRotationSchedule updates a key to remove a rotation schedule, if one
// exists.
func removeRotationSchedule(w io.Writer, name string) error {
// name := "projects/my-project/locations/us-east1/keyRings/my-key-ring/cryptoKeys/my-key"
// Create the client.
ctx := context.Background()
client, err := kms.NewKeyManagementClient(ctx)
if err != nil {
return fmt.Errorf("failed to create kms client: %w", err)
}
defer client.Close()
// Build the request.
req := &kmspb.UpdateCryptoKeyRequest{
CryptoKey: &kmspb.CryptoKey{
// Provide the name of the key to update
Name: name,
// Remove any rotation fields.
RotationSchedule: nil,
NextRotationTime: nil,
},
UpdateMask: &fieldmask.FieldMask{
Paths: []string{"rotation_period", "next_rotation_time"},
},
}
// Call the API.
result, err := client.UpdateCryptoKey(ctx, req)
if err != nil {
return fmt.Errorf("failed to update key: %w", err)
}
fmt.Fprintf(w, "Updated key: %s\n", result.Name)
return nil
}
Java
如需了解如何安装和使用 Cloud KMS 客户端库,请参阅 Cloud KMS 客户端库。
如需向 Cloud KMS 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
import com.google.cloud.kms.v1.CryptoKey;
import com.google.cloud.kms.v1.CryptoKeyName;
import com.google.cloud.kms.v1.KeyManagementServiceClient;
import com.google.protobuf.FieldMask;
import com.google.protobuf.util.FieldMaskUtil;
import java.io.IOException;
public class UpdateKeyRemoveRotation {
public void updateKeyRemoveRotation() throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String locationId = "us-east1";
String keyRingId = "my-key-ring";
String keyId = "my-key";
updateKeyRemoveRotation(projectId, locationId, keyRingId, keyId);
}
// Update a key to remove all labels.
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());
}
}
}
Node.js
如需了解如何安装和使用 Cloud KMS 客户端库,请参阅 Cloud KMS 客户端库。
如需向 Cloud KMS 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-east1';
// const keyRingId = 'my-key-ring';
// const keyId = 'my-key';
// Imports the Cloud KMS library
const {KeyManagementServiceClient} = require('@google-cloud/kms');
// Instantiates a client
const client = new KeyManagementServiceClient();
// Build the key name
const keyName = client.cryptoKeyPath(projectId, locationId, keyRingId, keyId);
async function updateKeyRemoveRotation() {
const [key] = await client.updateCryptoKey({
cryptoKey: {
name: keyName,
rotationPeriod: null,
nextRotationTime: null,
},
updateMask: {
paths: ['rotation_period', 'next_rotation_time'],
},
});
console.log(`Removed rotation for: ${key.name}`);
return key;
}
return updateKeyRemoveRotation();
PHP
如需了解如何安装和使用 Cloud KMS 客户端库,请参阅 Cloud KMS 客户端库。
如需向 Cloud KMS 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
use Google\Cloud\Kms\V1\CryptoKey;
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
use Google\Protobuf\FieldMask;
function update_key_remove_rotation(
string $projectId = 'my-project',
string $locationId = 'us-east1',
string $keyRingId = 'my-key-ring',
string $keyId = 'my-key'
) {
// Create the Cloud KMS client.
$client = new KeyManagementServiceClient();
// Build the key name.
$keyName = $client->cryptoKeyName($projectId, $locationId, $keyRingId, $keyId);
// Build the key.
$key = (new CryptoKey())
->setName($keyName);
// Create the field mask.
$updateMask = (new FieldMask())
->setPaths(['rotation_period', 'next_rotation_time']);
// Call the API.
$updatedKey = $client->updateCryptoKey($key, $updateMask);
printf('Updated key: %s' . PHP_EOL, $updatedKey->getName());
return $updatedKey;
}
Python
如需了解如何安装和使用 Cloud KMS 客户端库,请参阅 Cloud KMS 客户端库。
如需向 Cloud KMS 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
from google.cloud import kms
def update_key_remove_rotation(
project_id: str, location_id: str, key_ring_id: str, key_id: str
) -> kms.CryptoKey:
"""
Remove a rotation schedule from an existing key.
Args:
project_id (string): Google Cloud project ID (e.g. 'my-project').
location_id (string): Cloud KMS location (e.g. 'us-east1').
key_ring_id (string): ID of the Cloud KMS key ring (e.g. 'my-key-ring').
key_id (string): ID of the key to use (e.g. 'my-key').
Returns:
CryptoKey: Updated Cloud KMS key.
"""
# Create the client.
client = kms.KeyManagementServiceClient()
# Build the key name.
key_name = client.crypto_key_path(project_id, location_id, key_ring_id, key_id)
key = {"name": key_name}
# Build the update mask.
update_mask = {"paths": ["rotation_period", "next_rotation_time"]}
# Call the API.
updated_key = client.update_crypto_key(
request={"crypto_key": key, "update_mask": update_mask}
)
print(f"Updated key: {updated_key.name}")
return updated_key
Ruby
如需了解如何安装和使用 Cloud KMS 客户端库,请参阅 Cloud KMS 客户端库。
如需向 Cloud KMS 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the parent key name.
key_name = client.crypto_key_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id
# Build the key.
key = {
name: key_name,
rotation_period: nil,
next_rotation_time: nil
}
# Build the field mask.
update_mask = { paths: ["rotation_period", "next_rotation_time"] }
# Call the API.
updated_key = client.update_crypto_key crypto_key: key, update_mask: update_mask
puts "Updated key: #{updated_key.name}"
后续步骤
如需搜索并过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。