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

feat(parametermanager): add samples for global parameter manager kms_key field #2976

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Google.Cloud.Kms.V1;
using Google.Cloud.ParameterManager.V1;

[Collection(nameof(ParameterManagerFixture))]
public class CreateParameterWithKmsKeyTests
{
private readonly ParameterManagerFixture _fixture;
private readonly CreateParameterWithKmsKeySample _sample;

public CreateParameterWithKmsKeyTests(ParameterManagerFixture fixture)
{
_fixture = fixture;
_sample = new CreateParameterWithKmsKeySample();
}

[Fact]
public void CreateParameterWithKmsKey()
{
ParameterName parameterName = _fixture.ParameterName;
Parameter result = _sample.CreateParameterWithKmsKey(
projectId: parameterName.ProjectId, parameterId: parameterName.ParameterId, kmsKey: _fixture.cryptoKey.Name);

Assert.NotNull(result);
Assert.Equal(result.ParameterName.ParameterId, parameterName.ParameterId);
Assert.Equal(result.KmsKey, _fixture.cryptoKey.Name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Google.Cloud.Kms.V1" Version="3.16.0" />
<PackageReference Include="JUnitTestLogger" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ParameterManager.Samples\ParameterManager.Samples.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Copyright(c) 2025 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

using Google.Api.Gax.ResourceNames;
using Google.Cloud.Kms.V1;
using Google.Cloud.ParameterManager.V1;
using Google.Protobuf;
using System.Text;

[CollectionDefinition(nameof(ParameterManagerFixture))]
public class ParameterManagerFixture : IDisposable, ICollectionFixture<ParameterManagerFixture>
{
public string ProjectId { get; }
public const string LocationId = "global";

public ParameterName ParameterName { get; }

public CryptoKeyVersionName CryptoKeyVersionName1 { get; }
public CryptoKeyVersionName CryptoKeyVersionName2 { get; }

public string ParameterId { get; }
public string KeyId { get; }
public string KeyId1 { get; }

public Parameter UpdateParamKmsKey { get; }
public Parameter RemoveParamKmsKey { get; }
public CryptoKey cryptoKey { get; }
public CryptoKey cryptoKey1 { get; }


public ParameterManagerFixture()
{
ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
if (String.IsNullOrEmpty(ProjectId))
{
throw new Exception("missing GOOGLE_PROJECT_ID");
}

ParameterName = new ParameterName(ProjectId, LocationId, RandomId());

KeyRing keyRing = CreateKeyRing(ProjectId, "csharp-test-key-ring");

KeyId = RandomId();
KeyId1 = RandomId();

cryptoKey = CreateHsmKey(ProjectId, KeyId, "csharp-test-key-ring");
cryptoKey1 = CreateHsmKey(ProjectId, KeyId1, "csharp-test-key-ring");

ParameterId = RandomId();
UpdateParamKmsKey = CreateParameterWithKmsKey(ParameterId, ParameterFormat.Unformatted, cryptoKey.Name);

ParameterId = RandomId();
RemoveParamKmsKey = CreateParameterWithKmsKey(ParameterId, ParameterFormat.Unformatted, cryptoKey.Name);

CryptoKeyVersionName1 = new CryptoKeyVersionName(ProjectId, LocationId, "csharp-test-key-ring", KeyId, "1");
CryptoKeyVersionName2 = new CryptoKeyVersionName(ProjectId, LocationId, "csharp-test-key-ring", KeyId1, "1");
}

public void Dispose()
{
DeleteParameter(ParameterName);
DeleteParameter(UpdateParamKmsKey.ParameterName);
DeleteParameter(RemoveParamKmsKey.ParameterName);
DeleteKeyVersion(CryptoKeyVersionName1);
DeleteKeyVersion(CryptoKeyVersionName2);
}

public String RandomId()
{
return $"csharp-{System.Guid.NewGuid()}";
}

public Parameter CreateParameterWithKmsKey(string parameterId, ParameterFormat format, string kmsKey)
{
ParameterManagerClient client = ParameterManagerClient.Create();
LocationName projectName = new LocationName(ProjectId, LocationId);

Parameter parameter = new Parameter
{
Format = format,
KmsKey = kmsKey
};

return client.CreateParameter(projectName, parameter, parameterId);
}

private void DeleteParameter(ParameterName name)
{
ParameterManagerClient client = ParameterManagerClient.Create();
try
{
client.DeleteParameter(name);
}
catch (Grpc.Core.RpcException e) when (e.StatusCode == Grpc.Core.StatusCode.NotFound)
{
// Ignore error - Parameter was already deleted
}
}

public KeyRing CreateKeyRing(string projectId, string keyRingId)
{
KeyManagementServiceClient client = KeyManagementServiceClient.Create();
string name = $"projects/{projectId}/locations/global/keyRings/{keyRingId}";
LocationName parent = new LocationName(projectId, "global");
try
{
KeyRing resp = client.GetKeyRing(name);
return resp;
}
catch (Grpc.Core.RpcException e) when (e.StatusCode == Grpc.Core.StatusCode.NotFound)
{
return client.CreateKeyRing(new CreateKeyRingRequest
{
ParentAsLocationName = parent,
KeyRingId = keyRingId,
});
}
}

public CryptoKey CreateHsmKey(string projectId, string keyId, string keyRingId)
{
KeyManagementServiceClient client = KeyManagementServiceClient.Create();
KeyRingName parent = new KeyRingName(projectId, "global", keyRingId);

CreateCryptoKeyRequest request = new CreateCryptoKeyRequest
{
ParentAsKeyRingName = parent,
CryptoKeyId = keyId,
CryptoKey = new CryptoKey
{
Purpose = CryptoKey.Types.CryptoKeyPurpose.EncryptDecrypt,
VersionTemplate = new CryptoKeyVersionTemplate
{
Algorithm = CryptoKeyVersion.Types.CryptoKeyVersionAlgorithm.GoogleSymmetricEncryption,
ProtectionLevel = ProtectionLevel.Hsm,
},
},
};

try
{
string name = $"projects/{projectId}/locations/global/keyRings/{keyRingId}/cryptoKeys/{keyId}";
CryptoKey resp = client.GetCryptoKey(name);
return resp;
}
catch (Grpc.Core.RpcException e) when (e.StatusCode == Grpc.Core.StatusCode.NotFound)
{
return client.CreateCryptoKey(request);
}
}

public void DeleteKeyVersion(CryptoKeyVersionName name)
{
KeyManagementServiceClient client = KeyManagementServiceClient.Create();
try
{
client.DestroyCryptoKeyVersion(name);
}
catch (Grpc.Core.RpcException e) when (e.StatusCode == Grpc.Core.StatusCode.NotFound)
{
// Ignore error - Parameter was already deleted
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Google.Cloud.Kms.V1;
using Google.Cloud.ParameterManager.V1;

[Collection(nameof(ParameterManagerFixture))]
public class RemoveParameterKmsKeyTests
{
private readonly ParameterManagerFixture _fixture;
private readonly RemoveParameterKmsKeySample _sample;

public RemoveParameterKmsKeyTests(ParameterManagerFixture fixture)
{
_fixture = fixture;
_sample = new RemoveParameterKmsKeySample();
}

[Fact]
public void RemoveParameterKmsKey()
{
ParameterName parameterName = _fixture.ParameterName;
Parameter result = _sample.RemoveParameterKmsKey(
projectId: parameterName.ProjectId, parameterId: parameterName.ParameterId);

Assert.NotNull(result);
Assert.Equal(result.ParameterName.ParameterId, parameterName.ParameterId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Google.Cloud.Kms.V1;
using Google.Cloud.ParameterManager.V1;

[Collection(nameof(ParameterManagerFixture))]
public class UpdateParameterKmsKeyTests
{
private readonly ParameterManagerFixture _fixture;
private readonly UpdateParameterKmsKeySample _sample;

public UpdateParameterKmsKeyTests(ParameterManagerFixture fixture)
{
_fixture = fixture;
_sample = new UpdateParameterKmsKeySample();
}

[Fact]
public void UpdateParameterKmsKey()
{
ParameterName parameterName = _fixture.ParameterName;
Parameter result = _sample.UpdateParameterKmsKey(
projectId: parameterName.ProjectId, parameterId: parameterName.ParameterId, kmsKey: _fixture.cryptoKey1.Name);

Assert.NotNull(result);
Assert.Equal(result.ParameterName.ParameterId, parameterName.ParameterId);
Assert.Equal(result.KmsKey, _fixture.cryptoKey1.Name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START parametermanager_create_param_with_kms_key]

using Google.Api.Gax.ResourceNames;
using Google.Cloud.ParameterManager.V1;

public class CreateParameterWithKmsKeySample
{
/// <summary>
/// This function creates a parameter with kms_key using the Parameter Manager SDK for GCP.
/// </summary>
/// <param name="projectId">The ID of the project where the parameter is to be created.</param>
/// <param name="parameterId">The ID to assign to the new parameter. This ID must be unique within the project.</param>
/// <param name="kmsKey">The ID of the KMS key to be used for encryption.
/// (e.g. "projects/my-project/locations/global/keyRings/my-key-ring/cryptoKeys/my-encryption-key")</param>
/// <returns>The created Parameter object.</returns>
public Parameter CreateParameterWithKmsKey(
string projectId,
string parameterId,
string kmsKey)
{
// Create the client.
ParameterManagerClient client = ParameterManagerClient.Create();

// Build the parent resource name.
LocationName parent = new LocationName(projectId, "global");

// Build the parameter.
Parameter parameter = new Parameter
{
KmsKey = kmsKey
};

// Call the API to create the parameter.
Parameter createdParameter = client.CreateParameter(parent, parameter, parameterId);

// Print the created parameter name with kms_key.
Console.WriteLine($"Created parameter {createdParameter.Name} with kms_key {createdParameter.KmsKey}");

// Return the created parameter.
return createdParameter;
}
}
// [END parametermanager_create_param_with_kms_key]
Loading