Repository navigation
feat(secretmanager): add Cloud SQL managed rotation and secret type samples #3347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
suvidha-malaviya
wants to merge
5
commits into
GoogleCloudPlatform:main
Choose a base branch
from
suvidha-malaviya:cloudsql-managed-rotation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aa7cb97
feat(secretmanager): add Cloud SQL managed rotation and secret type s…
suvidha-malaviya 94baf51
test(secretmanager): guarantee cleanup of secrets and IAM grants on f…
suvidha-malaviya 0e7c884
docs(secretmanager): address review feedback on Cloud SQL managed-rot…
suvidha-malaviya 61a5b10
fix(secretmanager): address review feedback on Cloud SQL managed-rota…
suvidha-malaviya eaa79f4
test(secretmanager): skip Cloud SQL managed-rotation tests when env v…
suvidha-malaviya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
...nager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithCloudSqlCredentialsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } | ||
| } | ||
57 changes: 57 additions & 0 deletions
57
secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithTypeTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } | ||
|
YashSaraf11 marked this conversation as resolved.
Resolved
|
||
| } | ||
79 changes: 79 additions & 0 deletions
79
secretmanager/api/SecretManager.Samples.Tests/EnableRegionalSecretManagedRotationTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
YashSaraf11 marked this conversation as resolved.
Resolved
|
||
| } | ||
54 changes: 54 additions & 0 deletions
54
secretmanager/api/SecretManager.Samples.Tests/GetRegionalSecretTypeTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
secretmanager/api/SecretManager.Samples.Tests/GetSecretTypeTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.