diff --git a/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithCloudSqlCredentialsTests.cs b/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithCloudSqlCredentialsTests.cs new file mode 100644 index 00000000000..75690e847b5 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithCloudSqlCredentialsTests.cs @@ -0,0 +1,55 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using Xunit; + +[Collection(nameof(RegionalSecretManagerFixture))] +public class CreateRegionalSecretWithCloudSqlCredentialsTests +{ + private readonly RegionalSecretManagerFixture _fixture; + private readonly CreateRegionalSecretWithCloudSqlCredentialsSample _sample; + + public CreateRegionalSecretWithCloudSqlCredentialsTests(RegionalSecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new CreateRegionalSecretWithCloudSqlCredentialsSample(); + } + + [Fact] + public void CreatesRegionalSecretWithCloudSqlCredentials() + { + SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId()); + + try + { + // Run the sample code. + Secret result = _sample.CreateRegionalSecretWithCloudSqlCredentials( + projectId: secretName.ProjectId, locationId: secretName.LocationId, secretId: secretName.SecretId); + + // Assert that the secret was created with the Cloud SQL secret type. + Assert.Equal(secretName.SecretId, result.SecretName.SecretId); + Assert.Equal(Secret.Types.SecretType.CloudSqlDbCredentials, result.SecretType); + Assert.NotNull(result.PolicyMember); + Assert.False(string.IsNullOrEmpty(result.PolicyMember.IamPolicyUidPrincipal)); + } + finally + { + // Clean the created secret. + _fixture.DeleteSecret(secretName); + } + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithTypeTests.cs b/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithTypeTests.cs new file mode 100644 index 00000000000..b0a8705f745 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithTypeTests.cs @@ -0,0 +1,57 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using Xunit; + +[Collection(nameof(SecretManagerFixture))] +public class CreateSecretWithTypeTests +{ + private readonly SecretManagerFixture _fixture; + private readonly CreateSecretWithTypeSample _sample; + + public CreateSecretWithTypeTests(SecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new CreateSecretWithTypeSample(); + } + + [Theory] + [InlineData(Secret.Types.SecretType.AccessKey)] + [InlineData(Secret.Types.SecretType.Certificate)] + [InlineData(Secret.Types.SecretType.OtherDbCredentials)] + [InlineData(Secret.Types.SecretType.Other)] + public void CreatesSecretWithType(Secret.Types.SecretType secretType) + { + SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); + + try + { + // Run the sample code. + Secret result = _sample.CreateSecretWithType( + projectId: secretName.ProjectId, secretId: secretName.SecretId, secretType: secretType); + + // Assert that the secret was created with the requested type. + Assert.Equal(secretName.SecretId, result.SecretName.SecretId); + Assert.Equal(secretType, result.SecretType); + } + finally + { + // Clean the created secret. + _fixture.DeleteSecret(secretName); + } + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/EnableRegionalSecretManagedRotationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/EnableRegionalSecretManagedRotationTests.cs new file mode 100644 index 00000000000..12ab299dd69 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/EnableRegionalSecretManagedRotationTests.cs @@ -0,0 +1,79 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using System; +using Xunit; + +[Collection(nameof(RegionalSecretManagerFixture))] +public class EnableRegionalSecretManagedRotationTests +{ + private readonly RegionalSecretManagerFixture _fixture; + private readonly EnableRegionalSecretManagedRotationSample _sample; + + public EnableRegionalSecretManagedRotationTests(RegionalSecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new EnableRegionalSecretManagedRotationSample(); + } + + [Fact] + public void EnablesRegionalSecretManagedRotation() + { + // Skip when no Cloud SQL instance/user is configured (e.g. in CI). + if (!_fixture.HasCloudSqlConfig) + { + return; + } + + Secret secret = null; + string member = null; + try + { + secret = _fixture.CreateCloudSqlCredentialsSecret(_fixture.RandomId()); + member = secret.PolicyMember.IamPolicyUidPrincipal; + + // Run the sample code. + SecretVersion version = _sample.EnableRegionalSecretManagedRotation( + projectId: secret.SecretName.ProjectId, + locationId: secret.SecretName.LocationId, + secretId: secret.SecretName.SecretId, + instanceId: _fixture.CloudSqlInstanceId, + username: _fixture.CloudSqlUsername); + + // Assert that the first version was created and is enabled. + Assert.Contains(secret.SecretName.SecretId, version.Name); + Assert.Equal(SecretVersion.Types.State.Enabled, version.State); + } + finally + { + try + { + if (member != null) + { + _fixture.RevokeCloudSqlRole(member); + } + } + finally + { + if (secret != null) + { + _fixture.DeleteSecret(secret.SecretName); + } + } + } + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/GetRegionalSecretTypeTests.cs b/secretmanager/api/SecretManager.Samples.Tests/GetRegionalSecretTypeTests.cs new file mode 100644 index 00000000000..29c35315fba --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/GetRegionalSecretTypeTests.cs @@ -0,0 +1,54 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using Xunit; + +[Collection(nameof(RegionalSecretManagerFixture))] +public class GetRegionalSecretTypeTests +{ + private readonly RegionalSecretManagerFixture _fixture; + private readonly GetRegionalSecretTypeSample _sample; + + public GetRegionalSecretTypeTests(RegionalSecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new GetRegionalSecretTypeSample(); + } + + [Fact] + public void GetsRegionalSecretType() + { + SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId()); + _fixture.Client.CreateSecret( + new Google.Api.Gax.ResourceNames.LocationName(_fixture.ProjectId, _fixture.LocationId), + secretName.SecretId, + new Secret { SecretType = Secret.Types.SecretType.CloudSqlDbCredentials }); + try + { + // Run the sample code. + Secret result = _sample.GetRegionalSecretType( + projectId: secretName.ProjectId, locationId: secretName.LocationId, secretId: secretName.SecretId); + + Assert.Equal(secretName.SecretId, result.SecretName.SecretId); + Assert.Equal(Secret.Types.SecretType.CloudSqlDbCredentials, result.SecretType); + } + finally + { + _fixture.DeleteSecret(secretName); + } + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/GetSecretTypeTests.cs b/secretmanager/api/SecretManager.Samples.Tests/GetSecretTypeTests.cs new file mode 100644 index 00000000000..ba7468ffe6b --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/GetSecretTypeTests.cs @@ -0,0 +1,57 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using Xunit; + +[Collection(nameof(SecretManagerFixture))] +public class GetSecretTypeTests +{ + private readonly SecretManagerFixture _fixture; + private readonly GetSecretTypeSample _sample; + + public GetSecretTypeTests(SecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new GetSecretTypeSample(); + } + + [Fact] + public void GetsSecretType() + { + SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); + _fixture.Client.CreateSecret( + _fixture.ProjectName, + secretName.SecretId, + new Secret + { + Replication = new Replication { Automatic = new Replication.Types.Automatic() }, + SecretType = Secret.Types.SecretType.Certificate, + }); + try + { + // Run the sample code. + Secret result = _sample.GetSecretType(projectId: secretName.ProjectId, secretId: secretName.SecretId); + + Assert.Equal(secretName.SecretId, result.SecretName.SecretId); + Assert.Equal(Secret.Types.SecretType.Certificate, result.SecretType); + } + finally + { + _fixture.DeleteSecret(secretName); + } + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs b/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs index 6451b6c97cc..0f3d545909e 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs @@ -13,11 +13,15 @@ // the License. using Google.Api.Gax.ResourceNames; +using Google.Cloud.Iam.V1; +using Google.Cloud.ResourceManager.V3; using Google.Cloud.SecretManager.V1; using Google.Protobuf; using Google.Protobuf.WellKnownTypes; +using Grpc.Core; using System; using System.Text; +using System.Threading; using Xunit; [CollectionDefinition(nameof(RegionalSecretManagerFixture))] @@ -32,6 +36,22 @@ public class RegionalSecretManagerFixture : IDisposable, ICollectionFixture Environment.GetEnvironmentVariable("CLOUD_SQL_INSTANCE"); + + // Username of an existing database user on that instance. Null when + // CLOUD_SQL_USER is not set, in which case managed-rotation tests are skipped. + public string CloudSqlUsername => Environment.GetEnvironmentVariable("CLOUD_SQL_USER"); + + // True when both Cloud SQL environment variables are set. + public bool HasCloudSqlConfig => + !string.IsNullOrEmpty(CloudSqlInstanceId) && !string.IsNullOrEmpty(CloudSqlUsername); + public RegionalSecretManagerFixture() { // Get the Google Cloud ProjectId @@ -59,6 +79,9 @@ public RegionalSecretManagerFixture() LabelValue = "my-label-value"; // Required for testing regional samples + // Create the Projects client, used to grant Cloud SQL IAM permissions. + ProjectsClient = ProjectsClient.Create(); + Secret = CreateSecret(RandomId()); SecretVersion = AddSecretVersion(Secret); } @@ -105,6 +128,115 @@ public Secret CreateSecretWithDelayedDestroy() return Client.CreateSecret(locationName, RandomId(), secret); } + /// + /// Creates a Cloud SQL DB credentials secret and grants its own built-in identity + /// Cloud SQL IAM permissions, which enabling managed rotation requires. The grant is + /// per-secret, so callers must call RevokeCloudSqlRole once they are done. + /// + public Secret CreateCloudSqlCredentialsSecret(string secretId) + { + LocationName locationName = new LocationName(ProjectId, LocationId); + Secret secret = Client.CreateSecret(locationName, secretId, new Secret + { + SecretType = Secret.Types.SecretType.CloudSqlDbCredentials, + }); + + try + { + if (secret.PolicyMember?.IamPolicyUidPrincipal == null) + { + throw new InvalidOperationException("The created regional secret does not have a valid PolicyMember or IamPolicyUidPrincipal."); + } + GrantCloudSqlRole(secret.PolicyMember.IamPolicyUidPrincipal); + } + catch + { + DeleteSecret(secret.SecretName); + throw; + } + + // IAM grants are eventually consistent; give it a moment before a caller + // tries to use it for managed rotation. + Thread.Sleep(TimeSpan.FromSeconds(10)); + return secret; + } + + /// + /// Grants the Cloud SQL role to the member on the project. SetIamPolicy replaces the + /// whole policy, so this reads the current policy, adds the member, and writes it back + /// with the same etag, retrying the read-modify-write if another writer raced us. + /// + public void GrantCloudSqlRole(string member) + { + UpdateProjectPolicy(policy => + { + Binding binding = null; + foreach (Binding candidate in policy.Bindings) + { + if (candidate.Role == CloudSqlRole && candidate.Condition == null) + { + binding = candidate; + break; + } + } + if (binding == null) + { + binding = new Binding { Role = CloudSqlRole }; + policy.Bindings.Add(binding); + } + if (binding.Members.Contains(member)) + { + return false; + } + binding.Members.Add(member); + return true; + }); + } + + /// Removes the member from the Cloud SQL role, added by GrantCloudSqlRole. + public void RevokeCloudSqlRole(string member) + { + UpdateProjectPolicy(policy => + { + bool changed = false; + foreach (Binding binding in policy.Bindings) + { + if (binding.Role == CloudSqlRole) + { + changed |= binding.Members.Remove(member); + } + } + return changed; + }); + } + + private void UpdateProjectPolicy(Func modify) + { + ProjectName projectName = new ProjectName(ProjectId); + for (int attempt = 1; ; attempt++) + { + Policy policy = ProjectsClient.GetIamPolicy(projectName.ToString()); + if (!modify(policy)) + { + return; + } + try + { + ProjectsClient.SetIamPolicy(new SetIamPolicyRequest + { + Resource = projectName.ToString(), + Policy = policy, + }); + return; + } + catch (RpcException e) when (e.StatusCode == StatusCode.Aborted && attempt < 5) + { + // Etag mismatch: another writer changed the policy; retry. + Thread.Sleep(TimeSpan.FromSeconds(attempt)); + } + } + } + public SecretVersion AddSecretVersion(Secret secret) { SecretPayload payload = new SecretPayload diff --git a/secretmanager/api/SecretManager.Samples.Tests/RotateRegionalSecretTests.cs b/secretmanager/api/SecretManager.Samples.Tests/RotateRegionalSecretTests.cs new file mode 100644 index 00000000000..8eb4d1793b1 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/RotateRegionalSecretTests.cs @@ -0,0 +1,83 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using Xunit; + +[Collection(nameof(RegionalSecretManagerFixture))] +public class RotateRegionalSecretTests +{ + private readonly RegionalSecretManagerFixture _fixture; + private readonly EnableRegionalSecretManagedRotationSample _enableSample; + private readonly RotateRegionalSecretSample _sample; + + public RotateRegionalSecretTests(RegionalSecretManagerFixture fixture) + { + _fixture = fixture; + _enableSample = new EnableRegionalSecretManagedRotationSample(); + _sample = new RotateRegionalSecretSample(); + } + + [Fact] + public void RotatesRegionalSecret() + { + // Skip when no Cloud SQL instance/user is configured (e.g. in CI). + if (!_fixture.HasCloudSqlConfig) + { + return; + } + + Secret secret = null; + string member = null; + try + { + secret = _fixture.CreateCloudSqlCredentialsSecret(_fixture.RandomId()); + member = secret.PolicyMember.IamPolicyUidPrincipal; + + SecretVersion firstVersion = _enableSample.EnableRegionalSecretManagedRotation( + secret.SecretName.ProjectId, secret.SecretName.LocationId, secret.SecretName.SecretId, + _fixture.CloudSqlInstanceId, _fixture.CloudSqlUsername); + + // Run the sample code. + SecretVersion rotatedVersion = _sample.RotateRegionalSecret( + projectId: secret.SecretName.ProjectId, + locationId: secret.SecretName.LocationId, + secretId: secret.SecretName.SecretId); + + // Assert that rotation created a new, enabled version. + Assert.Contains(secret.SecretName.SecretId, rotatedVersion.Name); + Assert.NotEqual(firstVersion.Name, rotatedVersion.Name); + Assert.Equal(SecretVersion.Types.State.Enabled, rotatedVersion.State); + } + finally + { + try + { + if (member != null) + { + _fixture.RevokeCloudSqlRole(member); + } + } + finally + { + if (secret != null) + { + _fixture.DeleteSecret(secret.SecretName); + } + } + } + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretWithManagedRotationScheduleTests.cs b/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretWithManagedRotationScheduleTests.cs new file mode 100644 index 00000000000..d6dc0c61a49 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretWithManagedRotationScheduleTests.cs @@ -0,0 +1,89 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; +using Xunit; + +[Collection(nameof(RegionalSecretManagerFixture))] +public class UpdateRegionalSecretWithManagedRotationScheduleTests +{ + private readonly RegionalSecretManagerFixture _fixture; + private readonly EnableRegionalSecretManagedRotationSample _enableSample; + private readonly UpdateRegionalSecretWithManagedRotationScheduleSample _sample; + + public UpdateRegionalSecretWithManagedRotationScheduleTests(RegionalSecretManagerFixture fixture) + { + _fixture = fixture; + _enableSample = new EnableRegionalSecretManagedRotationSample(); + _sample = new UpdateRegionalSecretWithManagedRotationScheduleSample(); + } + + [Fact] + public void UpdatesRegionalSecretWithManagedRotationSchedule() + { + // Skip when no Cloud SQL instance/user is configured (e.g. in CI). + if (!_fixture.HasCloudSqlConfig) + { + return; + } + + Secret secret = null; + string member = null; + try + { + secret = _fixture.CreateCloudSqlCredentialsSecret(_fixture.RandomId()); + member = secret.PolicyMember.IamPolicyUidPrincipal; + + _enableSample.EnableRegionalSecretManagedRotation( + secret.SecretName.ProjectId, secret.SecretName.LocationId, secret.SecretName.SecretId, + _fixture.CloudSqlInstanceId, _fixture.CloudSqlUsername); + + long rotationPeriodSeconds = 3600; + DateTimeOffset before = DateTimeOffset.UtcNow; + + // Run the sample code. + Secret updated = _sample.UpdateRegionalSecretWithManagedRotationSchedule( + projectId: secret.SecretName.ProjectId, + locationId: secret.SecretName.LocationId, + secretId: secret.SecretName.SecretId, + rotationPeriodSeconds: rotationPeriodSeconds); + + // Assert that the schedule was applied. + Assert.Equal(rotationPeriodSeconds, updated.Rotation.RotationPeriod.Seconds); + Assert.True( + updated.Rotation.NextRotationTime.ToDateTimeOffset() >= before.AddSeconds(rotationPeriodSeconds).AddSeconds(-1)); + } + finally + { + try + { + if (member != null) + { + _fixture.RevokeCloudSqlRole(member); + } + } + finally + { + if (secret != null) + { + _fixture.DeleteSecret(secret.SecretName); + } + } + } + } +} diff --git a/secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithCloudSqlCredentials.cs b/secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithCloudSqlCredentials.cs new file mode 100644 index 00000000000..ab47682268e --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithCloudSqlCredentials.cs @@ -0,0 +1,60 @@ +/* + * Copyright 2026 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 secretmanager_create_regional_secret_with_cloud_sql_credentials] + +using Google.Api.Gax.ResourceNames; +using Google.Cloud.SecretManager.V1; +using System; + +public class CreateRegionalSecretWithCloudSqlCredentialsSample +{ + /// + /// Creates a new regional secret with type CLOUD_SQL_DB_CREDENTIALS. + /// + public Secret CreateRegionalSecretWithCloudSqlCredentials( + string projectId = "my-project", + string locationId = "my-location", + string secretId = "my-secret" + ) + { + // Create the Regional Secret Manager Client. + SecretManagerServiceClient client = new SecretManagerServiceClientBuilder + { + Endpoint = $"secretmanager.{locationId}.rep.googleapis.com" + }.Build(); + + // Build the parent resource name. + LocationName location = new LocationName(projectId, locationId); + + // Build the secret. + Secret secret = new Secret + { + SecretType = Secret.Types.SecretType.CloudSqlDbCredentials, + }; + + // Call the API. + Secret createdSecret = client.CreateSecret(location, secretId, secret); + Console.WriteLine($"Created secret: {createdSecret.Name}"); + + Console.WriteLine( + "Grant the Cloud SQL User rotate IAM permissions to enable managed rotation to: " + + createdSecret.PolicyMember.IamPolicyUidPrincipal); + + return createdSecret; + } +} +// [END secretmanager_create_regional_secret_with_cloud_sql_credentials] diff --git a/secretmanager/api/SecretManager.Samples/CreateSecretWithType.cs b/secretmanager/api/SecretManager.Samples/CreateSecretWithType.cs new file mode 100644 index 00000000000..a4dc8e19353 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/CreateSecretWithType.cs @@ -0,0 +1,57 @@ +/* + * Copyright 2026 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 secretmanager_create_secret_with_type] + +using Google.Api.Gax.ResourceNames; +using Google.Cloud.SecretManager.V1; +using System; + +public class CreateSecretWithTypeSample +{ + /// + /// Creates a new secret with the given secret type. + /// Note: CLOUD_SQL_DB_CREDENTIALS is only supported in the regional secret. + /// + public Secret CreateSecretWithType( + string projectId = "my-project", + string secretId = "my-secret", + Secret.Types.SecretType secretType = Secret.Types.SecretType.AccessKey + ) + { + // Create the client. + SecretManagerServiceClient client = SecretManagerServiceClient.Create(); + + // Build the parent resource name. + ProjectName projectName = new ProjectName(projectId); + + // Build the secret. + Secret secret = new Secret + { + Replication = new Replication + { + Automatic = new Replication.Types.Automatic(), + }, + SecretType = secretType, + }; + + // Call the API. + Secret createdSecret = client.CreateSecret(projectName, secretId, secret); + Console.WriteLine($"Created secret with secret type: {createdSecret.Name}"); + return createdSecret; + } +} +// [END secretmanager_create_secret_with_type] diff --git a/secretmanager/api/SecretManager.Samples/EnableRegionalSecretManagedRotation.cs b/secretmanager/api/SecretManager.Samples/EnableRegionalSecretManagedRotation.cs new file mode 100644 index 00000000000..2fea7ac7ce9 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/EnableRegionalSecretManagedRotation.cs @@ -0,0 +1,63 @@ +/* + * Copyright 2026 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 secretmanager_enable_regional_secret_managed_rotation] + +using Google.Cloud.SecretManager.V1; +using System; + +public class EnableRegionalSecretManagedRotationSample +{ + /// + /// Enables managed rotation of a CLOUD_SQL_DB_CREDENTIALS typed secret. + /// It validates and enables the rotation, adding a version and sets the + /// passed password (optional). + /// Note: AddSecretVersion is disabled on the CLOUD_SQL_DB_CREDENTIALS + /// currently and for any necessary manual rotations please trigger + /// rotate_secret. + /// + public SecretVersion EnableRegionalSecretManagedRotation( + string projectId = "my-project", + string locationId = "my-location", + string secretId = "my-secret", + string instanceId = "my-instance", + string username = "my-user" + ) + { + // Create the Regional Secret Manager Client. + SecretManagerServiceClient client = new SecretManagerServiceClientBuilder + { + Endpoint = $"secretmanager.{locationId}.rep.googleapis.com" + }.Build(); + + // Build the request. + EnableManagedRotationRequest request = new EnableManagedRotationRequest + { + ParentAsSecretName = SecretName.FromProjectLocationSecret(projectId, locationId, secretId), + CloudSqlSingleUserCredentials = new EnableManagedRotationRequest.Types.CloudSQLSingleUserCredentials + { + InstanceId = instanceId, + Username = username, + }, + }; + + // Enable managed rotation. + SecretVersion version = client.EnableManagedRotation(request); + Console.WriteLine($"Enabled managed rotation, created secret version: {version.Name}"); + return version; + } +} +// [END secretmanager_enable_regional_secret_managed_rotation] diff --git a/secretmanager/api/SecretManager.Samples/GetRegionalSecretType.cs b/secretmanager/api/SecretManager.Samples/GetRegionalSecretType.cs new file mode 100644 index 00000000000..f41f9577ed9 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/GetRegionalSecretType.cs @@ -0,0 +1,48 @@ +/* + * Copyright 2026 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 secretmanager_get_regional_secret_type] + +using Google.Cloud.SecretManager.V1; +using System; + +public class GetRegionalSecretTypeSample +{ + /// + /// Gets the secret type of the given regional secret. + /// + public Secret GetRegionalSecretType( + string projectId = "my-project", + string locationId = "my-location", + string secretId = "my-secret" + ) + { + // Create the Regional Secret Manager Client. + SecretManagerServiceClient client = new SecretManagerServiceClientBuilder + { + Endpoint = $"secretmanager.{locationId}.rep.googleapis.com" + }.Build(); + + // Build the resource name. + SecretName secretName = SecretName.FromProjectLocationSecret(projectId, locationId, secretId); + + // Call the API. + Secret secret = client.GetSecret(secretName); + Console.WriteLine($"Found regional secret {secret.Name} with secret type {secret.SecretType}"); + return secret; + } +} +// [END secretmanager_get_regional_secret_type] diff --git a/secretmanager/api/SecretManager.Samples/GetSecretType.cs b/secretmanager/api/SecretManager.Samples/GetSecretType.cs new file mode 100644 index 00000000000..458f85e7fa6 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/GetSecretType.cs @@ -0,0 +1,41 @@ +/* + * Copyright 2026 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 secretmanager_get_secret_type] + +using Google.Cloud.SecretManager.V1; +using System; + +public class GetSecretTypeSample +{ + /// + /// Gets the secret type of the given secret. + /// + public Secret GetSecretType(string projectId = "my-project", string secretId = "my-secret") + { + // Create the client. + SecretManagerServiceClient client = SecretManagerServiceClient.Create(); + + // Build the resource name. + SecretName secretName = new SecretName(projectId, secretId); + + // Call the API. + Secret secret = client.GetSecret(secretName); + Console.WriteLine($"Found secret {secret.Name} with secret type {secret.SecretType}"); + return secret; + } +} +// [END secretmanager_get_secret_type] diff --git a/secretmanager/api/SecretManager.Samples/RotateRegionalSecret.cs b/secretmanager/api/SecretManager.Samples/RotateRegionalSecret.cs new file mode 100644 index 00000000000..06a870f8b98 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/RotateRegionalSecret.cs @@ -0,0 +1,52 @@ +/* + * Copyright 2026 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 secretmanager_rotate_regional_secret] + +using Google.Cloud.SecretManager.V1; +using System; + +public class RotateRegionalSecretSample +{ + /// + /// Triggers an adhoc rotation for the managed CLOUD_SQL_DB_CREDENTIALS typed + /// secret. + /// + public SecretVersion RotateRegionalSecret( + string projectId = "my-project", + string locationId = "my-location", + string secretId = "my-secret" + ) + { + // Create the Regional Secret Manager Client. + SecretManagerServiceClient client = new SecretManagerServiceClientBuilder + { + Endpoint = $"secretmanager.{locationId}.rep.googleapis.com" + }.Build(); + + // Build the request. + RotateSecretRequest request = new RotateSecretRequest + { + ParentAsSecretName = SecretName.FromProjectLocationSecret(projectId, locationId, secretId), + }; + + // Call the API. + SecretVersion version = client.RotateSecret(request); + Console.WriteLine($"Rotated secret, created secret version: {version.Name}"); + return version; + } +} +// [END secretmanager_rotate_regional_secret] diff --git a/secretmanager/api/SecretManager.Samples/SecretManager.Samples.csproj b/secretmanager/api/SecretManager.Samples/SecretManager.Samples.csproj index 85fcc1a2b63..ca707ff5a5f 100644 --- a/secretmanager/api/SecretManager.Samples/SecretManager.Samples.csproj +++ b/secretmanager/api/SecretManager.Samples/SecretManager.Samples.csproj @@ -5,6 +5,6 @@ - + diff --git a/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretWithManagedRotationSchedule.cs b/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretWithManagedRotationSchedule.cs new file mode 100644 index 00000000000..c726f8e18f4 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretWithManagedRotationSchedule.cs @@ -0,0 +1,71 @@ +/* + * Copyright 2026 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 secretmanager_update_regional_secret_with_managed_rotation_schedule] + +using Google.Cloud.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; + +public class UpdateRegionalSecretWithManagedRotationScheduleSample +{ + /// + /// Updates the rotation schedule of a CLOUD_SQL_DB_CREDENTIALS typed secret. + /// + public Secret UpdateRegionalSecretWithManagedRotationSchedule( + string projectId = "my-project", + string locationId = "my-location", + string secretId = "my-secret", + long rotationPeriodSeconds = 3600 + ) + { + // Create the Regional Secret Manager Client. + SecretManagerServiceClient client = new SecretManagerServiceClientBuilder + { + Endpoint = $"secretmanager.{locationId}.rep.googleapis.com" + }.Build(); + + // The rotation schedule of a CLOUD_SQL_DB_CREDENTIALS secret can be set + // before or after enabling managed rotation; EnableManagedRotation does not + // need to be called first. Other secret types also support a rotation + // schedule, but only when Pub/Sub topics are configured. Pub/Sub topics are + // not required for CLOUD_SQL_DB_CREDENTIALS. + // NextRotationTime and RotationPeriod must be set together. + Timestamp nextRotationTime = Timestamp.FromDateTimeOffset( + DateTimeOffset.UtcNow.AddSeconds(rotationPeriodSeconds)); + + // Build the secret with updated fields. + Secret secret = new Secret + { + SecretName = SecretName.FromProjectLocationSecret(projectId, locationId, secretId), + Rotation = new Rotation + { + NextRotationTime = nextRotationTime, + RotationPeriod = new Duration { Seconds = rotationPeriodSeconds }, + }, + }; + + // Build the field mask. Mask only the rotation subfields being set, not the + // whole "rotation" submessage. + FieldMask fieldMask = FieldMask.FromString("rotation.next_rotation_time,rotation.rotation_period"); + + // Call the API. + Secret updatedSecret = client.UpdateSecret(secret, fieldMask); + Console.WriteLine($"Updated regional secret rotation schedule: {updatedSecret.Name}"); + return updatedSecret; + } +} +// [END secretmanager_update_regional_secret_with_managed_rotation_schedule]