Skip to content

Commit

Permalink
artifactregistry: implement upstream credentials (#9439)
Browse files Browse the repository at this point in the history
* implemented remote auth

* add IAM settings to test
[upstream:74854089df7d0ba6c81ef3465c2ee3980a78bd85]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician committed Nov 18, 2023
1 parent 3d43ea6 commit 964c424
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/9439.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
artifactregistry: added `remote_repository_config.upstream_credentials` field to `google_artifact_registry_repository` resource
```
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,42 @@ snapshot versions.`,
},
ExactlyOneOf: []string{"remote_repository_config.0.apt_repository", "remote_repository_config.0.docker_repository", "remote_repository_config.0.maven_repository", "remote_repository_config.0.npm_repository", "remote_repository_config.0.python_repository", "remote_repository_config.0.yum_repository"},
},
"upstream_credentials": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `The credentials used to access the remote repository.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"username_password_credentials": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Use username and password to access the remote repository.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"password_secret_version": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The Secret Manager key version that holds the password to access the
remote repository. Must be in the format of
'projects/{project}/secrets/{secret}/versions/{version}'.`,
},
"username": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The username to access the remote repository.`,
},
},
},
},
},
},
},
"yum_repository": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -1233,6 +1269,8 @@ func flattenArtifactRegistryRepositoryRemoteRepositoryConfig(v interface{}, d *s
flattenArtifactRegistryRepositoryRemoteRepositoryConfigPythonRepository(original["pythonRepository"], d, config)
transformed["yum_repository"] =
flattenArtifactRegistryRepositoryRemoteRepositoryConfigYumRepository(original["yumRepository"], d, config)
transformed["upstream_credentials"] =
flattenArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentials(original["upstreamCredentials"], d, config)
return []interface{}{transformed}
}
func flattenArtifactRegistryRepositoryRemoteRepositoryConfigDescription(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
Expand Down Expand Up @@ -1379,6 +1417,42 @@ func flattenArtifactRegistryRepositoryRemoteRepositoryConfigYumRepositoryPublicR
return v
}

func flattenArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentials(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["username_password_credentials"] =
flattenArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentialsUsernamePasswordCredentials(original["usernamePasswordCredentials"], d, config)
return []interface{}{transformed}
}
func flattenArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentialsUsernamePasswordCredentials(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["username"] =
flattenArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentialsUsernamePasswordCredentialsUsername(original["username"], d, config)
transformed["password_secret_version"] =
flattenArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentialsUsernamePasswordCredentialsPasswordSecretVersion(original["passwordSecretVersion"], d, config)
return []interface{}{transformed}
}
func flattenArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentialsUsernamePasswordCredentialsUsername(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentialsUsernamePasswordCredentialsPasswordSecretVersion(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenArtifactRegistryRepositoryCleanupPolicyDryRun(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
Expand Down Expand Up @@ -1760,6 +1834,13 @@ func expandArtifactRegistryRepositoryRemoteRepositoryConfig(v interface{}, d tpg
transformed["yumRepository"] = transformedYumRepository
}

transformedUpstreamCredentials, err := expandArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentials(original["upstream_credentials"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedUpstreamCredentials); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["upstreamCredentials"] = transformedUpstreamCredentials
}

return transformed, nil
}

Expand Down Expand Up @@ -1965,6 +2046,59 @@ func expandArtifactRegistryRepositoryRemoteRepositoryConfigYumRepositoryPublicRe
return v, nil
}

func expandArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentials(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedUsernamePasswordCredentials, err := expandArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentialsUsernamePasswordCredentials(original["username_password_credentials"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedUsernamePasswordCredentials); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["usernamePasswordCredentials"] = transformedUsernamePasswordCredentials
}

return transformed, nil
}

func expandArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentialsUsernamePasswordCredentials(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedUsername, err := expandArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentialsUsernamePasswordCredentialsUsername(original["username"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedUsername); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["username"] = transformedUsername
}

transformedPasswordSecretVersion, err := expandArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentialsUsernamePasswordCredentialsPasswordSecretVersion(original["password_secret_version"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedPasswordSecretVersion); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["passwordSecretVersion"] = transformedPasswordSecretVersion
}

return transformed, nil
}

func expandArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentialsUsernamePasswordCredentialsUsername(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentialsUsernamePasswordCredentialsPasswordSecretVersion(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandArtifactRegistryRepositoryCleanupPolicyDryRun(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,75 @@ resource "google_artifact_registry_repository" "my-repo" {
`, context)
}

func TestAccArtifactRegistryRepository_artifactRegistryRepositoryRemoteCustomExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckArtifactRegistryRepositoryDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccArtifactRegistryRepository_artifactRegistryRepositoryRemoteCustomExample(context),
},
{
ResourceName: "google_artifact_registry_repository.my-repo",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"repository_id", "location", "labels", "terraform_labels"},
},
},
})
}

func testAccArtifactRegistryRepository_artifactRegistryRepositoryRemoteCustomExample(context map[string]interface{}) string {
return acctest.Nprintf(`
data "google_project" "project" {}
resource "google_secret_manager_secret" "tf-test-example-custom-remote-secret%{random_suffix}" {
secret_id = "tf-test-example-secret%{random_suffix}"
replication {
auto {}
}
}
resource "google_secret_manager_secret_version" "tf-test-example-custom-remote-secret%{random_suffix}_version" {
secret = google_secret_manager_secret.tf-test-example-custom-remote-secret%{random_suffix}.id
secret_data = "tf-test-remote-password%{random_suffix}"
}
resource "google_secret_manager_secret_iam_member" "secret-access" {
secret_id = google_secret_manager_secret.tf-test-example-custom-remote-secret%{random_suffix}.id
role = "roles/secretmanager.secretAccessor"
member = "serviceAccount:service-${data.google_project.project.number}@gcp-sa-artifactregistry.iam.gserviceaccount.com"
}
resource "google_artifact_registry_repository" "my-repo" {
location = "us-central1"
repository_id = "tf-test-example-custom-remote%{random_suffix}"
description = "example remote docker repository with credentials%{random_suffix}"
format = "DOCKER"
mode = "REMOTE_REPOSITORY"
remote_repository_config {
description = "docker hub with custom credentials"
docker_repository {
public_repository = "DOCKER_HUB"
}
upstream_credentials {
username_password_credentials {
username = "tf-test-remote-username%{random_suffix}"
password_secret_version = google_secret_manager_secret_version.tf-test-example-custom-remote-secret%{random_suffix}_version.name
}
}
}
}
`, context)
}

func testAccCheckArtifactRegistryRepositoryDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
74 changes: 74 additions & 0 deletions website/docs/r/artifact_registry_repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,55 @@ resource "google_artifact_registry_repository" "my-repo" {
}
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=artifact_registry_repository_remote_custom&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Artifact Registry Repository Remote Custom


```hcl
data "google_project" "project" {}
resource "google_secret_manager_secret" "example-custom-remote-secret" {
secret_id = "example-secret"
replication {
auto {}
}
}
resource "google_secret_manager_secret_version" "example-custom-remote-secret_version" {
secret = google_secret_manager_secret.example-custom-remote-secret.id
secret_data = "remote-password"
}
resource "google_secret_manager_secret_iam_member" "secret-access" {
secret_id = google_secret_manager_secret.example-custom-remote-secret.id
role = "roles/secretmanager.secretAccessor"
member = "serviceAccount:service-${data.google_project.project.number}@gcp-sa-artifactregistry.iam.gserviceaccount.com"
}
resource "google_artifact_registry_repository" "my-repo" {
location = "us-central1"
repository_id = "example-custom-remote"
description = "example remote docker repository with credentials"
format = "DOCKER"
mode = "REMOTE_REPOSITORY"
remote_repository_config {
description = "docker hub with custom credentials"
docker_repository {
public_repository = "DOCKER_HUB"
}
upstream_credentials {
username_password_credentials {
username = "remote-username"
password_secret_version = google_secret_manager_secret_version.example-custom-remote-secret_version.name
}
}
}
}
```

## Argument Reference

Expand Down Expand Up @@ -474,6 +523,11 @@ The following arguments are supported:
Specific settings for an Yum remote repository.
Structure is [documented below](#nested_yum_repository).

* `upstream_credentials` -
(Optional)
The credentials used to access the remote repository.
Structure is [documented below](#nested_upstream_credentials).


<a name="nested_apt_repository"></a>The `apt_repository` block supports:

Expand Down Expand Up @@ -545,6 +599,26 @@ The following arguments are supported:
(Required)
Specific repository from the base, e.g. `"centos/8-stream/BaseOS/x86_64/os"`

<a name="nested_upstream_credentials"></a>The `upstream_credentials` block supports:

* `username_password_credentials` -
(Optional)
Use username and password to access the remote repository.
Structure is [documented below](#nested_username_password_credentials).


<a name="nested_username_password_credentials"></a>The `username_password_credentials` block supports:

* `username` -
(Optional)
The username to access the remote repository.

* `password_secret_version` -
(Optional)
The Secret Manager key version that holds the password to access the
remote repository. Must be in the format of
`projects/{project}/secrets/{secret}/versions/{version}`.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:
Expand Down

0 comments on commit 964c424

Please sign in to comment.