Skip to content

Commit 61b814d

Browse files
add sensitive_params to bigquery_data_transfer_config (#3937) (#2451)
* suppress diff for secret_access_key on bigquery data transfer params * add sensitiveParams for secret access key * add customize diff, fix spelling * add custom import and post create Signed-off-by: Modular Magician <[email protected]>
1 parent daab7f0 commit 61b814d

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

.changelog/3937.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
bigquerydatatransfer: fixed `params.secret_access_key` perma-diff for AWS S3 data transfer config types by adding a `sensitive_params` block with the `secret_access_key` attribute.
3+
```

google-beta/resource_bigquery_data_transfer_config.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ import (
2525
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
2626
)
2727

28+
var sensitiveParams = []string{"secret_access_key"}
29+
30+
func sensitiveParamCustomizeDiff(diff *schema.ResourceDiff, v interface{}) error {
31+
for _, sp := range sensitiveParams {
32+
mapLabel := diff.Get("params." + sp).(string)
33+
authLabel := diff.Get("sensitive_params.0." + sp).(string)
34+
if mapLabel != "" && authLabel != "" {
35+
return fmt.Errorf("Sensitive param [%s] cannot be set in both `params` and the `sensitive_params` block.", sp)
36+
}
37+
}
38+
return nil
39+
}
40+
2841
func resourceBigqueryDataTransferConfig() *schema.Resource {
2942
return &schema.Resource{
3043
Create: resourceBigqueryDataTransferConfigCreate,
@@ -42,6 +55,8 @@ func resourceBigqueryDataTransferConfig() *schema.Resource {
4255
Delete: schema.DefaultTimeout(4 * time.Minute),
4356
},
4457

58+
CustomizeDiff: sensitiveParamCustomizeDiff,
59+
4560
Schema: map[string]*schema.Schema{
4661
"data_source_id": {
4762
Type: schema.TypeString,
@@ -106,6 +121,28 @@ about the format here:
106121
https://cloud.google.com/appengine/docs/flexible/python/scheduling-jobs-with-cron-yaml#the_schedule_format
107122
NOTE: the granularity should be at least 8 hours, or less frequent.`,
108123
},
124+
"sensitive_params": {
125+
Type: schema.TypeList,
126+
Optional: true,
127+
Description: `Different parameters are configured primarily using the the 'params' field on this
128+
resource. This block contains the parameters which contain secrets or passwords so that they can be marked
129+
sensitive and hidden from plan output. The name of the field, eg: secret_access_key, will be the key
130+
in the 'params' map in the api request.
131+
132+
Credentials may not be specified in both locations and will cause an error. Changing from one location
133+
to a different credential configuration in the config will require an apply to update state.`,
134+
MaxItems: 1,
135+
Elem: &schema.Resource{
136+
Schema: map[string]*schema.Schema{
137+
"secret_access_key": {
138+
Type: schema.TypeString,
139+
Required: true,
140+
Description: `The Secret Access Key of the AWS account transferring data from.`,
141+
Sensitive: true,
142+
},
143+
},
144+
},
145+
},
109146
"service_account_name": {
110147
Type: schema.TypeString,
111148
Optional: true,
@@ -186,6 +223,11 @@ func resourceBigqueryDataTransferConfigCreate(d *schema.ResourceData, meta inter
186223
obj["params"] = paramsProp
187224
}
188225

226+
obj, err = resourceBigqueryDataTransferConfigEncoder(d, meta, obj)
227+
if err != nil {
228+
return err
229+
}
230+
189231
url, err := replaceVars(d, config, "{{BigqueryDataTransferBasePath}}projects/{{project}}/locations/{{location}}/transferConfigs?serviceAccountName={{service_account_name}}")
190232
if err != nil {
191233
return err
@@ -267,6 +309,18 @@ func resourceBigqueryDataTransferConfigRead(d *schema.ResourceData, meta interfa
267309
return handleNotFoundError(err, d, fmt.Sprintf("BigqueryDataTransferConfig %q", d.Id()))
268310
}
269311

312+
res, err = resourceBigqueryDataTransferConfigDecoder(d, meta, res)
313+
if err != nil {
314+
return err
315+
}
316+
317+
if res == nil {
318+
// Decoding the object has resulted in it being gone. It may be marked deleted
319+
log.Printf("[DEBUG] Removing BigqueryDataTransferConfig because it no longer exists.")
320+
d.SetId("")
321+
return nil
322+
}
323+
270324
if err := d.Set("project", project); err != nil {
271325
return fmt.Errorf("Error reading Config: %s", err)
272326
}
@@ -351,6 +405,11 @@ func resourceBigqueryDataTransferConfigUpdate(d *schema.ResourceData, meta inter
351405
obj["params"] = paramsProp
352406
}
353407

408+
obj, err = resourceBigqueryDataTransferConfigEncoder(d, meta, obj)
409+
if err != nil {
410+
return err
411+
}
412+
354413
url, err := replaceVars(d, config, "{{BigqueryDataTransferBasePath}}{{name}}")
355414
if err != nil {
356415
return err
@@ -547,3 +606,40 @@ func expandBigqueryDataTransferConfigParams(v interface{}, d TerraformResourceDa
547606
}
548607
return m, nil
549608
}
609+
610+
func resourceBigqueryDataTransferConfigEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
611+
paramMap, ok := obj["params"]
612+
if !ok {
613+
paramMap = make(map[string]string)
614+
}
615+
616+
var params map[string]string
617+
params = paramMap.(map[string]string)
618+
619+
for _, sp := range sensitiveParams {
620+
if auth, _ := d.GetOkExists("sensitive_params.0." + sp); auth != "" {
621+
params[sp] = auth.(string)
622+
}
623+
}
624+
625+
obj["params"] = params
626+
627+
return obj, nil
628+
}
629+
630+
func resourceBigqueryDataTransferConfigDecoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) {
631+
if paramMap, ok := res["params"]; ok {
632+
params := paramMap.(map[string]interface{})
633+
for _, sp := range sensitiveParams {
634+
if _, apiOk := params[sp]; apiOk {
635+
if _, exists := d.GetOkExists("sensitive_params.0." + sp); exists {
636+
delete(params, sp)
637+
} else {
638+
params[sp] = d.Get("params." + sp)
639+
}
640+
}
641+
}
642+
}
643+
644+
return res, nil
645+
}

website/docs/r/bigquery_data_transfer_config.html.markdown

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ To get more information about Config, see:
3232
* How-to Guides
3333
* [Official Documentation](https://cloud.google.com/bigquery/docs/reference/datatransfer/rest/)
3434

35+
~> **Warning:** All arguments including `sensitive_params.secret_access_key` will be stored in the raw
36+
state as plain-text. [Read more about sensitive data in state](/docs/state/sensitive-data.html).
37+
3538
## Example Usage - Bigquerydatatransfer Config Scheduled Query
3639

3740

@@ -122,6 +125,16 @@ The following arguments are supported:
122125
(Optional)
123126
When set to true, no runs are scheduled for a given transfer.
124127

128+
* `sensitive_params` -
129+
(Optional)
130+
Different parameters are configured primarily using the the `params` field on this
131+
resource. This block contains the parameters which contain secrets or passwords so that they can be marked
132+
sensitive and hidden from plan output. The name of the field, eg: secret_access_key, will be the key
133+
in the `params` map in the api request.
134+
Credentials may not be specified in both locations and will cause an error. Changing from one location
135+
to a different credential configuration in the config will require an apply to update state.
136+
Structure is documented below.
137+
125138
* `location` -
126139
(Optional)
127140
The geographic location where the transfer config should reside.
@@ -137,6 +150,13 @@ The following arguments are supported:
137150
If it is not provided, the provider project is used.
138151

139152

153+
The `sensitive_params` block supports:
154+
155+
* `secret_access_key` -
156+
(Required)
157+
The Secret Access Key of the AWS account transferring data from.
158+
**Note**: This property is sensitive and will not be displayed in the plan.
159+
140160
## Attributes Reference
141161

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

0 commit comments

Comments
 (0)