From aa7cb9795b9184c598abf45df5f006753f9acc42 Mon Sep 17 00:00:00 2001 From: smalaviya-crest Date: Wed, 30 Sep 2026 11:54:44 +0530 Subject: [PATCH 1/5] feat(secretmanager): add Cloud SQL managed rotation and secret type samples Add C# samples and tests for creating a Cloud SQL credentials secret, enabling, rotating and scheduling managed rotation, and creating/getting secrets by secret type. Bump Google.Cloud.SecretManager.V1 to 2.8.0. --- ...ionalSecretWithCloudSqlCredentialsTests.cs | 49 +++++++ .../CreateSecretWithTypeTests.cs | 52 ++++++++ ...nableRegionalSecretManagedRotationTests.cs | 58 +++++++++ .../GetRegionalSecretTypeTests.cs | 55 ++++++++ .../GetSecretTypeTests.cs | 57 ++++++++ .../RegionalSecretManagerFixture.cs | 123 ++++++++++++++++++ .../RotateRegionalSecretTests.cs | 62 +++++++++ ...lSecretWithManagedRotationScheduleTests.cs | 68 ++++++++++ ...teRegionalSecretWithCloudSqlCredentials.cs | 65 +++++++++ .../CreateSecretWithType.cs | 60 +++++++++ .../EnableRegionalSecretManagedRotation.cs | 68 ++++++++++ .../GetRegionalSecretType.cs | 50 +++++++ .../SecretManager.Samples/GetSecretType.cs | 43 ++++++ .../RotateRegionalSecret.cs | 55 ++++++++ .../SecretManager.Samples.csproj | 2 +- ...gionalSecretWithManagedRotationSchedule.cs | 77 +++++++++++ 16 files changed, 943 insertions(+), 1 deletion(-) create mode 100644 secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithCloudSqlCredentialsTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithTypeTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/EnableRegionalSecretManagedRotationTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/GetRegionalSecretTypeTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/GetSecretTypeTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/RotateRegionalSecretTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretWithManagedRotationScheduleTests.cs create mode 100644 secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithCloudSqlCredentials.cs create mode 100644 secretmanager/api/SecretManager.Samples/CreateSecretWithType.cs create mode 100644 secretmanager/api/SecretManager.Samples/EnableRegionalSecretManagedRotation.cs create mode 100644 secretmanager/api/SecretManager.Samples/GetRegionalSecretType.cs create mode 100644 secretmanager/api/SecretManager.Samples/GetSecretType.cs create mode 100644 secretmanager/api/SecretManager.Samples/RotateRegionalSecret.cs create mode 100644 secretmanager/api/SecretManager.Samples/UpdateRegionalSecretWithManagedRotationSchedule.cs diff --git a/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithCloudSqlCredentialsTests.cs b/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithCloudSqlCredentialsTests.cs new file mode 100644 index 00000000000..698c042e0ae --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithCloudSqlCredentialsTests.cs @@ -0,0 +1,49 @@ +/* + * 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()); + + // 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.False(string.IsNullOrEmpty(result.PolicyMember.IamPolicyUidPrincipal)); + + // 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..5f8d94f6330 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithTypeTests.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. + */ + +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()); + + // 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); + + // 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..61d9e36c2dd --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/EnableRegionalSecretManagedRotationTests.cs @@ -0,0 +1,58 @@ +/* + * 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() + { + Secret secret = _fixture.CreateCloudSqlCredentialsSecret(_fixture.RandomId()); + string member = secret.PolicyMember.IamPolicyUidPrincipal; + try + { + // 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 + { + _fixture.DeleteSecret(secret.SecretName); + _fixture.RevokeCloudSqlRole(member); + } + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/GetRegionalSecretTypeTests.cs b/secretmanager/api/SecretManager.Samples.Tests/GetRegionalSecretTypeTests.cs new file mode 100644 index 00000000000..b4bdaf91585 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/GetRegionalSecretTypeTests.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 GetRegionalSecretTypeTests +{ + private readonly RegionalSecretManagerFixture _fixture; + private readonly GetRegionalSecretTypeSample _sample; + + public GetRegionalSecretTypeTests(RegionalSecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new GetRegionalSecretTypeSample(); + } + + [Fact] + public void GetsRegionalSecretType() + { + // No Cloud SQL instance or IAM grant is needed just to read the type. + 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..8e11f4da962 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,15 @@ public class RegionalSecretManagerFixture : IDisposable, ICollectionFixture RequiredEnvironmentVariable("CLOUD_SQL_INSTANCE"); + + // Username of an existing database user on that instance. + public string CloudSqlUsername => RequiredEnvironmentVariable("CLOUD_SQL_USER"); public RegionalSecretManagerFixture() { // Get the Google Cloud ProjectId @@ -59,6 +72,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 +121,113 @@ public Secret CreateSecretWithDelayedDestroy() return Client.CreateSecret(locationName, RandomId(), secret); } + private static string RequiredEnvironmentVariable(string name) + { + string value = Environment.GetEnvironmentVariable(name); + if (String.IsNullOrEmpty(value)) + { + throw new Exception($"missing {name}"); + } + return value; + } + + /// + /// 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, + }); + + GrantCloudSqlRole(secret.PolicyMember.IamPolicyUidPrincipal); + + // 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..9e6a8f2e478 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/RotateRegionalSecretTests.cs @@ -0,0 +1,62 @@ +/* + * 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() + { + Secret secret = _fixture.CreateCloudSqlCredentialsSecret(_fixture.RandomId()); + string member = secret.PolicyMember.IamPolicyUidPrincipal; + try + { + 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 + { + _fixture.DeleteSecret(secret.SecretName); + _fixture.RevokeCloudSqlRole(member); + } + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretWithManagedRotationScheduleTests.cs b/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretWithManagedRotationScheduleTests.cs new file mode 100644 index 00000000000..36b97c9a9c9 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretWithManagedRotationScheduleTests.cs @@ -0,0 +1,68 @@ +/* + * 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() + { + Secret secret = _fixture.CreateCloudSqlCredentialsSecret(_fixture.RandomId()); + string member = secret.PolicyMember.IamPolicyUidPrincipal; + try + { + _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 + { + _fixture.DeleteSecret(secret.SecretName); + _fixture.RevokeCloudSqlRole(member); + } + } +} diff --git a/secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithCloudSqlCredentials.cs b/secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithCloudSqlCredentials.cs new file mode 100644 index 00000000000..4c8bcec00eb --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithCloudSqlCredentials.cs @@ -0,0 +1,65 @@ +/* + * 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 secret with the Cloud SQL DB credentials secret type. This type is + /// required to enable Secret Manager's managed rotation of Cloud SQL passwords. + /// It can only be set when the secret is created, and the secret's location must + /// match the region of the target Cloud SQL instance. + /// + 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}"); + + // This built-in identity is what you grant Cloud SQL IAM permissions to, + // so that Secret Manager can rotate the database password on its behalf. + Console.WriteLine( + "Grant this identity Cloud SQL IAM permissions to enable rotation: " + + 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..550db231977 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/CreateSecretWithType.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_secret_with_type] + +using Google.Api.Gax.ResourceNames; +using Google.Cloud.SecretManager.V1; +using System; + +public class CreateSecretWithTypeSample +{ + /// + /// Creates a secret with the given secret type restriction (e.g. AccessKey, + /// Certificate, OtherDbCredentials, or Other). CloudSqlDbCredentials is only for + /// regional secrets that go through EnableRegionalSecretManagedRotation. These + /// other secret types are plain metadata tags: they don't require any additional + /// credentials payload at creation time. + /// + public Secret CreateSecretWithType( + string projectId = "my-project", + string secretId = "my-secret", + Secret.Types.SecretType secretType = Secret.Types.SecretType.Other + ) + { + // 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..31a37e2fe1d --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/EnableRegionalSecretManagedRotation.cs @@ -0,0 +1,68 @@ +/* + * 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 for a Cloud SQL DB credentials secret. This links the + /// secret to a Cloud SQL instance and database user, and can only be called once + /// per secret. It adds the secret's first version and sets the matching password + /// on the Cloud SQL user, taking the place of a manually added secret version. + /// Afterwards, use RotateRegionalSecret to trigger further rotations. + /// + /// instanceId is the bare Cloud SQL instance ID (e.g. "my-instance"), not a + /// connection name. Neither the project nor the region should be included: the + /// service already knows the project from the secret's own path. + /// + 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. Parent holds the full secret resource name, not a + // collection parent. Leaving Password unset lets Secret Manager generate a + // secure password itself. + EnableManagedRotationRequest request = new EnableManagedRotationRequest + { + ParentAsSecretName = SecretName.FromProjectLocationSecret(projectId, locationId, secretId), + CloudSqlSingleUserCredentials = new EnableManagedRotationRequest.Types.CloudSQLSingleUserCredentials + { + InstanceId = instanceId, + Username = username, + }, + }; + + // Call the API. + 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..a9f1ff200c3 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/GetRegionalSecretType.cs @@ -0,0 +1,50 @@ +/* + * 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 and prints the secret type (e.g. CloudSqlDbCredentials, AccessKey, + /// Certificate, OtherDbCredentials, Other, or Unspecified for a secret with no + /// type restriction) 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..779d55c45da --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/GetSecretType.cs @@ -0,0 +1,43 @@ +/* + * 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 and prints the secret type (e.g. CloudSqlDbCredentials, AccessKey, + /// Certificate, OtherDbCredentials, Other, or Unspecified for a secret with no + /// type restriction) 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..bf072006b7a --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/RotateRegionalSecret.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. + */ + +// [START secretmanager_rotate_regional_secret] + +using Google.Cloud.SecretManager.V1; +using System; + +public class RotateRegionalSecretSample +{ + /// + /// Triggers a managed rotation for a Cloud SQL DB credentials secret. Managed + /// rotation must already be enabled on the secret (see + /// EnableRegionalSecretManagedRotation). Each call generates a new password, + /// updates the Cloud SQL user, and adds the result as a new secret version. + /// + 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. Parent holds the full secret resource name, not a + // collection parent. + 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..4d7ebe67200 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretWithManagedRotationSchedule.cs @@ -0,0 +1,77 @@ +/* + * 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 +{ + /// + /// Reconfigures the recurring rotation schedule on a secret that already has + /// Cloud SQL managed rotation enabled (see EnableRegionalSecretManagedRotation). + /// This only applies to regional secrets of the CLOUD_SQL_DB_CREDENTIALS type. + /// + /// rotationPeriodSeconds is the interval between rotations, in whole seconds. The + /// service requires it to be at least 3600 (1 hour), and the derived next rotation + /// time (now + rotationPeriodSeconds) must be at least 300 seconds in the future. + /// Both are enforced by the API, not checked client-side here. + /// + 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(); + + // 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 two subfields being set, not the whole + // "rotation" submessage: that would also include the output-only + // managed_rotation_status, and the API rejects it as immutable. + FieldMask fieldMask = new FieldMask + { + Paths = { "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] From 94baf51666deb3238ca11e6fc80201e828389753 Mon Sep 17 00:00:00 2001 From: smalaviya-crest Date: Wed, 30 Sep 2026 12:17:18 +0530 Subject: [PATCH 2/5] test(secretmanager): guarantee cleanup of secrets and IAM grants on failure --- ...ionalSecretWithCloudSqlCredentialsTests.cs | 26 ++++++++++++------- .../CreateSecretWithTypeTests.cs | 23 +++++++++------- ...nableRegionalSecretManagedRotationTests.cs | 17 +++++++++--- .../RegionalSecretManagerFixture.cs | 14 +++++++++- .../RotateRegionalSecretTests.cs | 17 +++++++++--- ...lSecretWithManagedRotationScheduleTests.cs | 17 +++++++++--- 6 files changed, 82 insertions(+), 32 deletions(-) diff --git a/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithCloudSqlCredentialsTests.cs b/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithCloudSqlCredentialsTests.cs index 698c042e0ae..75690e847b5 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithCloudSqlCredentialsTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithCloudSqlCredentialsTests.cs @@ -34,16 +34,22 @@ public void CreatesRegionalSecretWithCloudSqlCredentials() { SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId()); - // Run the sample code. - Secret result = _sample.CreateRegionalSecretWithCloudSqlCredentials( - projectId: secretName.ProjectId, locationId: secretName.LocationId, secretId: secretName.SecretId); + 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.False(string.IsNullOrEmpty(result.PolicyMember.IamPolicyUidPrincipal)); - - // Clean the created secret. - _fixture.DeleteSecret(secretName); + // 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 index 5f8d94f6330..b0a8705f745 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithTypeTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithTypeTests.cs @@ -38,15 +38,20 @@ public void CreatesSecretWithType(Secret.Types.SecretType secretType) { SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); - // Run the sample code. - Secret result = _sample.CreateSecretWithType( - projectId: secretName.ProjectId, secretId: secretName.SecretId, secretType: secretType); + 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); - - // Clean the created secret. - _fixture.DeleteSecret(secretName); + // 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 index 61d9e36c2dd..de106395654 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/EnableRegionalSecretManagedRotationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/EnableRegionalSecretManagedRotationTests.cs @@ -33,10 +33,13 @@ public EnableRegionalSecretManagedRotationTests(RegionalSecretManagerFixture fix [Fact] public void EnablesRegionalSecretManagedRotation() { - Secret secret = _fixture.CreateCloudSqlCredentialsSecret(_fixture.RandomId()); - string member = secret.PolicyMember.IamPolicyUidPrincipal; + 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, @@ -51,8 +54,14 @@ public void EnablesRegionalSecretManagedRotation() } finally { - _fixture.DeleteSecret(secret.SecretName); - _fixture.RevokeCloudSqlRole(member); + if (secret != null) + { + _fixture.DeleteSecret(secret.SecretName); + } + if (member != null) + { + _fixture.RevokeCloudSqlRole(member); + } } } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs b/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs index 8e11f4da962..cf00f5426b4 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs @@ -144,7 +144,19 @@ public Secret CreateCloudSqlCredentialsSecret(string secretId) SecretType = Secret.Types.SecretType.CloudSqlDbCredentials, }); - GrantCloudSqlRole(secret.PolicyMember.IamPolicyUidPrincipal); + 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. diff --git a/secretmanager/api/SecretManager.Samples.Tests/RotateRegionalSecretTests.cs b/secretmanager/api/SecretManager.Samples.Tests/RotateRegionalSecretTests.cs index 9e6a8f2e478..3f77adb98c7 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/RotateRegionalSecretTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/RotateRegionalSecretTests.cs @@ -34,10 +34,13 @@ public RotateRegionalSecretTests(RegionalSecretManagerFixture fixture) [Fact] public void RotatesRegionalSecret() { - Secret secret = _fixture.CreateCloudSqlCredentialsSecret(_fixture.RandomId()); - string member = secret.PolicyMember.IamPolicyUidPrincipal; + 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); @@ -55,8 +58,14 @@ public void RotatesRegionalSecret() } finally { - _fixture.DeleteSecret(secret.SecretName); - _fixture.RevokeCloudSqlRole(member); + if (secret != null) + { + _fixture.DeleteSecret(secret.SecretName); + } + if (member != null) + { + _fixture.RevokeCloudSqlRole(member); + } } } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretWithManagedRotationScheduleTests.cs b/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretWithManagedRotationScheduleTests.cs index 36b97c9a9c9..6029578355a 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretWithManagedRotationScheduleTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretWithManagedRotationScheduleTests.cs @@ -36,10 +36,13 @@ public UpdateRegionalSecretWithManagedRotationScheduleTests(RegionalSecretManage [Fact] public void UpdatesRegionalSecretWithManagedRotationSchedule() { - Secret secret = _fixture.CreateCloudSqlCredentialsSecret(_fixture.RandomId()); - string member = secret.PolicyMember.IamPolicyUidPrincipal; + 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); @@ -61,8 +64,14 @@ public void UpdatesRegionalSecretWithManagedRotationSchedule() } finally { - _fixture.DeleteSecret(secret.SecretName); - _fixture.RevokeCloudSqlRole(member); + if (secret != null) + { + _fixture.DeleteSecret(secret.SecretName); + } + if (member != null) + { + _fixture.RevokeCloudSqlRole(member); + } } } } From 0e7c884753a151d79364bbd190cf44ef5894ae4f Mon Sep 17 00:00:00 2001 From: smalaviya-crest Date: Fri, 9 Oct 2026 16:17:44 +0530 Subject: [PATCH 3/5] docs(secretmanager): address review feedback on Cloud SQL managed-rotation samples Align comments with other samples, simplify summaries and clarify rotation schedule behavior. --- .../GetRegionalSecretTypeTests.cs | 1 - ...teRegionalSecretWithCloudSqlCredentials.cs | 9 ++------ .../CreateSecretWithType.cs | 7 ++----- .../EnableRegionalSecretManagedRotation.cs | 21 +++++++------------ .../GetRegionalSecretType.cs | 4 +--- .../SecretManager.Samples/GetSecretType.cs | 4 +--- .../RotateRegionalSecret.cs | 9 +++----- ...gionalSecretWithManagedRotationSchedule.cs | 19 +++++++---------- 8 files changed, 25 insertions(+), 49 deletions(-) diff --git a/secretmanager/api/SecretManager.Samples.Tests/GetRegionalSecretTypeTests.cs b/secretmanager/api/SecretManager.Samples.Tests/GetRegionalSecretTypeTests.cs index b4bdaf91585..29c35315fba 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/GetRegionalSecretTypeTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/GetRegionalSecretTypeTests.cs @@ -32,7 +32,6 @@ public GetRegionalSecretTypeTests(RegionalSecretManagerFixture fixture) [Fact] public void GetsRegionalSecretType() { - // No Cloud SQL instance or IAM grant is needed just to read the type. SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId()); _fixture.Client.CreateSecret( new Google.Api.Gax.ResourceNames.LocationName(_fixture.ProjectId, _fixture.LocationId), diff --git a/secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithCloudSqlCredentials.cs b/secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithCloudSqlCredentials.cs index 4c8bcec00eb..ab47682268e 100644 --- a/secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithCloudSqlCredentials.cs +++ b/secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithCloudSqlCredentials.cs @@ -23,10 +23,7 @@ public class CreateRegionalSecretWithCloudSqlCredentialsSample { /// - /// Creates a secret with the Cloud SQL DB credentials secret type. This type is - /// required to enable Secret Manager's managed rotation of Cloud SQL passwords. - /// It can only be set when the secret is created, and the secret's location must - /// match the region of the target Cloud SQL instance. + /// Creates a new regional secret with type CLOUD_SQL_DB_CREDENTIALS. /// public Secret CreateRegionalSecretWithCloudSqlCredentials( string projectId = "my-project", @@ -53,10 +50,8 @@ public Secret CreateRegionalSecretWithCloudSqlCredentials( Secret createdSecret = client.CreateSecret(location, secretId, secret); Console.WriteLine($"Created secret: {createdSecret.Name}"); - // This built-in identity is what you grant Cloud SQL IAM permissions to, - // so that Secret Manager can rotate the database password on its behalf. Console.WriteLine( - "Grant this identity Cloud SQL IAM permissions to enable rotation: " + + "Grant the Cloud SQL User rotate IAM permissions to enable managed rotation to: " + createdSecret.PolicyMember.IamPolicyUidPrincipal); return createdSecret; diff --git a/secretmanager/api/SecretManager.Samples/CreateSecretWithType.cs b/secretmanager/api/SecretManager.Samples/CreateSecretWithType.cs index 550db231977..9e815c02381 100644 --- a/secretmanager/api/SecretManager.Samples/CreateSecretWithType.cs +++ b/secretmanager/api/SecretManager.Samples/CreateSecretWithType.cs @@ -23,11 +23,8 @@ public class CreateSecretWithTypeSample { /// - /// Creates a secret with the given secret type restriction (e.g. AccessKey, - /// Certificate, OtherDbCredentials, or Other). CloudSqlDbCredentials is only for - /// regional secrets that go through EnableRegionalSecretManagedRotation. These - /// other secret types are plain metadata tags: they don't require any additional - /// credentials payload at creation time. + /// 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", diff --git a/secretmanager/api/SecretManager.Samples/EnableRegionalSecretManagedRotation.cs b/secretmanager/api/SecretManager.Samples/EnableRegionalSecretManagedRotation.cs index 31a37e2fe1d..2fea7ac7ce9 100644 --- a/secretmanager/api/SecretManager.Samples/EnableRegionalSecretManagedRotation.cs +++ b/secretmanager/api/SecretManager.Samples/EnableRegionalSecretManagedRotation.cs @@ -22,15 +22,12 @@ public class EnableRegionalSecretManagedRotationSample { /// - /// Enables managed rotation for a Cloud SQL DB credentials secret. This links the - /// secret to a Cloud SQL instance and database user, and can only be called once - /// per secret. It adds the secret's first version and sets the matching password - /// on the Cloud SQL user, taking the place of a manually added secret version. - /// Afterwards, use RotateRegionalSecret to trigger further rotations. - /// - /// instanceId is the bare Cloud SQL instance ID (e.g. "my-instance"), not a - /// connection name. Neither the project nor the region should be included: the - /// service already knows the project from the secret's own path. + /// 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", @@ -46,9 +43,7 @@ public SecretVersion EnableRegionalSecretManagedRotation( Endpoint = $"secretmanager.{locationId}.rep.googleapis.com" }.Build(); - // Build the request. Parent holds the full secret resource name, not a - // collection parent. Leaving Password unset lets Secret Manager generate a - // secure password itself. + // Build the request. EnableManagedRotationRequest request = new EnableManagedRotationRequest { ParentAsSecretName = SecretName.FromProjectLocationSecret(projectId, locationId, secretId), @@ -59,7 +54,7 @@ public SecretVersion EnableRegionalSecretManagedRotation( }, }; - // Call the API. + // Enable managed rotation. SecretVersion version = client.EnableManagedRotation(request); Console.WriteLine($"Enabled managed rotation, created secret version: {version.Name}"); return version; diff --git a/secretmanager/api/SecretManager.Samples/GetRegionalSecretType.cs b/secretmanager/api/SecretManager.Samples/GetRegionalSecretType.cs index a9f1ff200c3..f41f9577ed9 100644 --- a/secretmanager/api/SecretManager.Samples/GetRegionalSecretType.cs +++ b/secretmanager/api/SecretManager.Samples/GetRegionalSecretType.cs @@ -22,9 +22,7 @@ public class GetRegionalSecretTypeSample { /// - /// Gets and prints the secret type (e.g. CloudSqlDbCredentials, AccessKey, - /// Certificate, OtherDbCredentials, Other, or Unspecified for a secret with no - /// type restriction) of the given regional secret. + /// Gets the secret type of the given regional secret. /// public Secret GetRegionalSecretType( string projectId = "my-project", diff --git a/secretmanager/api/SecretManager.Samples/GetSecretType.cs b/secretmanager/api/SecretManager.Samples/GetSecretType.cs index 779d55c45da..458f85e7fa6 100644 --- a/secretmanager/api/SecretManager.Samples/GetSecretType.cs +++ b/secretmanager/api/SecretManager.Samples/GetSecretType.cs @@ -22,9 +22,7 @@ public class GetSecretTypeSample { /// - /// Gets and prints the secret type (e.g. CloudSqlDbCredentials, AccessKey, - /// Certificate, OtherDbCredentials, Other, or Unspecified for a secret with no - /// type restriction) of the given secret. + /// Gets the secret type of the given secret. /// public Secret GetSecretType(string projectId = "my-project", string secretId = "my-secret") { diff --git a/secretmanager/api/SecretManager.Samples/RotateRegionalSecret.cs b/secretmanager/api/SecretManager.Samples/RotateRegionalSecret.cs index bf072006b7a..06a870f8b98 100644 --- a/secretmanager/api/SecretManager.Samples/RotateRegionalSecret.cs +++ b/secretmanager/api/SecretManager.Samples/RotateRegionalSecret.cs @@ -22,10 +22,8 @@ public class RotateRegionalSecretSample { /// - /// Triggers a managed rotation for a Cloud SQL DB credentials secret. Managed - /// rotation must already be enabled on the secret (see - /// EnableRegionalSecretManagedRotation). Each call generates a new password, - /// updates the Cloud SQL user, and adds the result as a new secret version. + /// Triggers an adhoc rotation for the managed CLOUD_SQL_DB_CREDENTIALS typed + /// secret. /// public SecretVersion RotateRegionalSecret( string projectId = "my-project", @@ -39,8 +37,7 @@ public SecretVersion RotateRegionalSecret( Endpoint = $"secretmanager.{locationId}.rep.googleapis.com" }.Build(); - // Build the request. Parent holds the full secret resource name, not a - // collection parent. + // Build the request. RotateSecretRequest request = new RotateSecretRequest { ParentAsSecretName = SecretName.FromProjectLocationSecret(projectId, locationId, secretId), diff --git a/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretWithManagedRotationSchedule.cs b/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretWithManagedRotationSchedule.cs index 4d7ebe67200..1aa66e6b151 100644 --- a/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretWithManagedRotationSchedule.cs +++ b/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretWithManagedRotationSchedule.cs @@ -23,14 +23,7 @@ public class UpdateRegionalSecretWithManagedRotationScheduleSample { /// - /// Reconfigures the recurring rotation schedule on a secret that already has - /// Cloud SQL managed rotation enabled (see EnableRegionalSecretManagedRotation). - /// This only applies to regional secrets of the CLOUD_SQL_DB_CREDENTIALS type. - /// - /// rotationPeriodSeconds is the interval between rotations, in whole seconds. The - /// service requires it to be at least 3600 (1 hour), and the derived next rotation - /// time (now + rotationPeriodSeconds) must be at least 300 seconds in the future. - /// Both are enforced by the API, not checked client-side here. + /// Updates the rotation schedule of a CLOUD_SQL_DB_CREDENTIALS typed secret. /// public Secret UpdateRegionalSecretWithManagedRotationSchedule( string projectId = "my-project", @@ -45,6 +38,11 @@ public Secret UpdateRegionalSecretWithManagedRotationSchedule( 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)); @@ -60,9 +58,8 @@ public Secret UpdateRegionalSecretWithManagedRotationSchedule( }, }; - // Build the field mask. Mask only the two subfields being set, not the whole - // "rotation" submessage: that would also include the output-only - // managed_rotation_status, and the API rejects it as immutable. + // Build the field mask. Mask only the rotation subfields being set, not the + // whole "rotation" submessage. FieldMask fieldMask = new FieldMask { Paths = { "rotation.next_rotation_time", "rotation.rotation_period" } From 61a5b105e6a240a4a25afc56dee90825288a3ba3 Mon Sep 17 00:00:00 2001 From: smalaviya-crest Date: Mon, 2 Nov 2026 18:35:35 +0530 Subject: [PATCH 4/5] fix(secretmanager): address review feedback on Cloud SQL managed-rotation samples --- .../EnableRegionalSecretManagedRotationTests.cs | 14 ++++++++++---- .../RotateRegionalSecretTests.cs | 14 ++++++++++---- ...gionalSecretWithManagedRotationScheduleTests.cs | 14 ++++++++++---- .../SecretManager.Samples/CreateSecretWithType.cs | 2 +- ...ateRegionalSecretWithManagedRotationSchedule.cs | 5 +---- 5 files changed, 32 insertions(+), 17 deletions(-) diff --git a/secretmanager/api/SecretManager.Samples.Tests/EnableRegionalSecretManagedRotationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/EnableRegionalSecretManagedRotationTests.cs index de106395654..a5b9ff20474 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/EnableRegionalSecretManagedRotationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/EnableRegionalSecretManagedRotationTests.cs @@ -54,13 +54,19 @@ public void EnablesRegionalSecretManagedRotation() } finally { - if (secret != null) + try { - _fixture.DeleteSecret(secret.SecretName); + if (member != null) + { + _fixture.RevokeCloudSqlRole(member); + } } - if (member != null) + finally { - _fixture.RevokeCloudSqlRole(member); + if (secret != null) + { + _fixture.DeleteSecret(secret.SecretName); + } } } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/RotateRegionalSecretTests.cs b/secretmanager/api/SecretManager.Samples.Tests/RotateRegionalSecretTests.cs index 3f77adb98c7..e5be5de04b5 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/RotateRegionalSecretTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/RotateRegionalSecretTests.cs @@ -58,13 +58,19 @@ public void RotatesRegionalSecret() } finally { - if (secret != null) + try { - _fixture.DeleteSecret(secret.SecretName); + if (member != null) + { + _fixture.RevokeCloudSqlRole(member); + } } - if (member != null) + finally { - _fixture.RevokeCloudSqlRole(member); + 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 index 6029578355a..5ee6fa112e2 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretWithManagedRotationScheduleTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretWithManagedRotationScheduleTests.cs @@ -64,13 +64,19 @@ public void UpdatesRegionalSecretWithManagedRotationSchedule() } finally { - if (secret != null) + try { - _fixture.DeleteSecret(secret.SecretName); + if (member != null) + { + _fixture.RevokeCloudSqlRole(member); + } } - if (member != null) + finally { - _fixture.RevokeCloudSqlRole(member); + if (secret != null) + { + _fixture.DeleteSecret(secret.SecretName); + } } } } diff --git a/secretmanager/api/SecretManager.Samples/CreateSecretWithType.cs b/secretmanager/api/SecretManager.Samples/CreateSecretWithType.cs index 9e815c02381..a4dc8e19353 100644 --- a/secretmanager/api/SecretManager.Samples/CreateSecretWithType.cs +++ b/secretmanager/api/SecretManager.Samples/CreateSecretWithType.cs @@ -29,7 +29,7 @@ public class CreateSecretWithTypeSample public Secret CreateSecretWithType( string projectId = "my-project", string secretId = "my-secret", - Secret.Types.SecretType secretType = Secret.Types.SecretType.Other + Secret.Types.SecretType secretType = Secret.Types.SecretType.AccessKey ) { // Create the client. diff --git a/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretWithManagedRotationSchedule.cs b/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretWithManagedRotationSchedule.cs index 1aa66e6b151..c726f8e18f4 100644 --- a/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretWithManagedRotationSchedule.cs +++ b/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretWithManagedRotationSchedule.cs @@ -60,10 +60,7 @@ public Secret UpdateRegionalSecretWithManagedRotationSchedule( // Build the field mask. Mask only the rotation subfields being set, not the // whole "rotation" submessage. - FieldMask fieldMask = new FieldMask - { - Paths = { "rotation.next_rotation_time", "rotation.rotation_period" } - }; + FieldMask fieldMask = FieldMask.FromString("rotation.next_rotation_time,rotation.rotation_period"); // Call the API. Secret updatedSecret = client.UpdateSecret(secret, fieldMask); From eaa79f4324c19a6392edb548f9cdf3fdbd935c94 Mon Sep 17 00:00:00 2001 From: smalaviya-crest Date: Wed, 18 Nov 2026 16:12:32 +0530 Subject: [PATCH 5/5] test(secretmanager): skip Cloud SQL managed-rotation tests when env vars are unset --- ...nableRegionalSecretManagedRotationTests.cs | 6 +++++ .../RegionalSecretManagerFixture.cs | 25 ++++++++----------- .../RotateRegionalSecretTests.cs | 6 +++++ ...lSecretWithManagedRotationScheduleTests.cs | 6 +++++ 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/secretmanager/api/SecretManager.Samples.Tests/EnableRegionalSecretManagedRotationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/EnableRegionalSecretManagedRotationTests.cs index a5b9ff20474..12ab299dd69 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/EnableRegionalSecretManagedRotationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/EnableRegionalSecretManagedRotationTests.cs @@ -33,6 +33,12 @@ public EnableRegionalSecretManagedRotationTests(RegionalSecretManagerFixture fix [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 diff --git a/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs b/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs index cf00f5426b4..0f3d545909e 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs @@ -40,11 +40,18 @@ public class RegionalSecretManagerFixture : IDisposable, ICollectionFixture RequiredEnvironmentVariable("CLOUD_SQL_INSTANCE"); + // Bare ID of a pre-provisioned Cloud SQL instance in LocationId. Null when + // CLOUD_SQL_INSTANCE is not set, in which case managed-rotation tests are skipped. + public string CloudSqlInstanceId => 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); - // Username of an existing database user on that instance. - public string CloudSqlUsername => RequiredEnvironmentVariable("CLOUD_SQL_USER"); public RegionalSecretManagerFixture() { // Get the Google Cloud ProjectId @@ -121,16 +128,6 @@ public Secret CreateSecretWithDelayedDestroy() return Client.CreateSecret(locationName, RandomId(), secret); } - private static string RequiredEnvironmentVariable(string name) - { - string value = Environment.GetEnvironmentVariable(name); - if (String.IsNullOrEmpty(value)) - { - throw new Exception($"missing {name}"); - } - return value; - } - /// /// 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 diff --git a/secretmanager/api/SecretManager.Samples.Tests/RotateRegionalSecretTests.cs b/secretmanager/api/SecretManager.Samples.Tests/RotateRegionalSecretTests.cs index e5be5de04b5..8eb4d1793b1 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/RotateRegionalSecretTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/RotateRegionalSecretTests.cs @@ -34,6 +34,12 @@ public RotateRegionalSecretTests(RegionalSecretManagerFixture fixture) [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 diff --git a/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretWithManagedRotationScheduleTests.cs b/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretWithManagedRotationScheduleTests.cs index 5ee6fa112e2..d6dc0c61a49 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretWithManagedRotationScheduleTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretWithManagedRotationScheduleTests.cs @@ -36,6 +36,12 @@ public UpdateRegionalSecretWithManagedRotationScheduleTests(RegionalSecretManage [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