Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added is_secret_data_base64 field to google_secret_manager_secret_version #8873

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion mmv1/products/secretmanager/SecretVersion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ examples:
data: 'secret-data'
ignore_read_extra:
- 'deletion_policy'
- !ruby/object:Provider::Terraform::Examples
name: 'secret_version_with_base64_string_secret_data'
primary_resource_id: 'secret-version-base64'
vars:
secret_id: 'secret-version'
data: 'secret-data.pfx'
test_vars_overrides:
data: '"./test-fixtures/binary-file.pfx"'
ignore_read_extra:
- 'is_secret_data_base64'
import_format:
['projects/{{%project}}/secrets/{{%secret_id}}/versions/{{%version}}']
custom_code: !ruby/object:Provider::Terraform::CustomCode
Expand All @@ -54,6 +64,11 @@ custom_code: !ruby/object:Provider::Terraform::CustomCode
resource_definition: templates/terraform/resource_definition/secret_version.go.erb
decoder: templates/terraform/decoders/treat_destroyed_state_as_gone.erb
pre_delete: templates/terraform/pre_delete/secret_version_deletion_policy.go.erb
pre_read: templates/terraform/pre_read/secret_version_is_secret_data_base64.go.erb
extra_schema_entry: templates/terraform/extra_schema_entry/secret_version_is_secret_data_base64.go.erb
docs: !ruby/object:Provider::Terraform::Docs
optional_properties: |
* `is_secret_data_base64` - (Optional) If set to 'true', the secret data is expected to be base64-encoded string and would be sent as is.
virtual_fields:
- !ruby/object:Api::Type::Enum
name: 'deletion_policy'
Expand Down Expand Up @@ -123,4 +138,4 @@ properties:
required: true
description: The secret data. Must be no larger than 64KiB.
sensitive: true
custom_expand: templates/terraform/custom_expand/base64.go.erb
custom_expand: templates/terraform/custom_expand/secret_version_secret_data.go.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<%- # the license inside this block applies to this file
# Copyright 2023 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-%>
func expand<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
if v == nil {
return nil, nil
}

if d.Get("is_secret_data_base64").(bool) {
return v, nil
}
return base64.StdEncoding.EncodeToString([]byte(v.(string))), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ func flatten<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d
return err
}

data, err := base64.StdEncoding.DecodeString(accessRes["payload"].(map[string]interface{})["data"].(string))
if err != nil {
return err
if d.Get("is_secret_data_base64").(bool) {
transformed["secret_data"] = accessRes["payload"].(map[string]interface{})["data"].(string)
} else {
data, err := base64.StdEncoding.DecodeString(accessRes["payload"].(map[string]interface{})["data"].(string))
if err != nil {
return err
}
transformed["secret_data"] = string(data)
}
transformed["secret_data"] = string(data)
return []interface{}{transformed}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
resource "google_secret_manager_secret" "secret-basic" {
secret_id = "<%= ctx[:vars]['secret_id'] %>"

replication {
user_managed {
replicas {
location = "us-central1"
}
}
}
}

resource "google_secret_manager_secret_version" "<%= ctx[:primary_resource_id] %>" {
secret = google_secret_manager_secret.secret-basic.id

is_secret_data_base64 = true
secret_data = filebase64("<%= ctx[:vars]['data'] %>")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this field need to be added this way as opposed to being in the yaml properties?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field is a terraform-only field that decides whether the secret_data is to be sent as is or after base64-encoding and whether to decode it while flattening. As this field affects the state value of secret_data, we need to keep it ForceNew. The virtual_field property doesn't provide a way to mark the fields immutable. Hence, I've used extra_schema_entry for this purpose.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<%# The license inside this block applies to this file.
# Copyright 2023 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-%>
"is_secret_data_base64": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: false,
Description: `If set to 'true', the secret data is expected to be base64-encoded string and would be sent as is.`,
},
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<%# The license inside this block applies to this file.
# Copyright 2023 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-%>
// Explicitly set the field to default value if unset
if _, ok := d.GetOkExists("is_secret_data_base64"); !ok {
if err := d.Set("is_secret_data_base64", false); err != nil {
return fmt.Errorf("Error setting is_secret_data_base64: %s", err)
}
}
Binary file not shown.