Skip to content

Commit

Permalink
Validate base64 fields in the Google Provider (#4338) (#8304)
Browse files Browse the repository at this point in the history
Co-authored-by: upodroid <[email protected]>
Signed-off-by: Modular Magician <[email protected]>

Co-authored-by: upodroid <[email protected]>
  • Loading branch information
modular-magician and upodroid authored Jan 26, 2021
1 parent 460008a commit 287a89e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .changelog/4338.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
provider: added plan time validations for fields that expect base64 values.
```
19 changes: 12 additions & 7 deletions google/resource_cloud_scheduler_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ By default, the job is sent to the version which is the default version when the
},
},
"body": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateBase64String,
Description: `HTTP request body.
A request body is allowed only if the HTTP method is POST or PUT.
It will result in invalid argument error to set a body on a job with an incompatible HttpMethod.
Expand Down Expand Up @@ -232,8 +233,9 @@ send a request to the targeted url`,
Description: `The full URI path that the request will be sent to.`,
},
"body": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateBase64String,
Description: `HTTP request body.
A request body is allowed only if the HTTP method is POST, PUT, or PATCH.
It is an error to set body on a job with an incompatible HttpMethod.
Expand Down Expand Up @@ -330,10 +332,13 @@ Pubsub message must contain either non-empty data, or at least one attribute.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"data": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateBase64String,
Description: `The message payload for PubsubMessage.
Pubsub message must contain either non-empty data, or at least one attribute.`,
Pubsub message must contain either non-empty data, or at least one attribute.
A base64-encoded string.`,
},
},
},
Expand Down
5 changes: 3 additions & 2 deletions google/resource_healthcare_hl7_v2_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ store if schematized parsing is desired.`,
AtLeastOneOf: []string{"parser_config.0.allow_null_header", "parser_config.0.segment_terminator", "parser_config.0.schema"},
},
"segment_terminator": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateBase64String,
Description: `Byte(s) to be used as the segment terminator. If this is unset, '\r' will be used as segment terminator.
A base64-encoded string.`,
Expand Down
9 changes: 9 additions & 0 deletions google/validation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package google

import (
"encoding/base64"
"fmt"
"net"
"regexp"
Expand Down Expand Up @@ -265,6 +266,14 @@ func validateIpAddress(i interface{}, val string) ([]string, []error) {
return nil, nil
}

func validateBase64String(i interface{}, val string) ([]string, []error) {
_, err := base64.StdEncoding.DecodeString(i.(string))
if err != nil {
return nil, []error{fmt.Errorf("could not decode %q as a valid base64 value. Please use the terraform base64 functions such as base64encode() or filebase64() to supply a valid base64 string", val)}
}
return nil, nil
}

// StringNotInSlice returns a SchemaValidateFunc which tests if the provided value
// is of type string and that it matches none of the element in the invalid slice.
// if ignorecase is true, case is ignored.
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/cloud_scheduler_job.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ The `pubsub_target` block supports:
(Optional)
The message payload for PubsubMessage.
Pubsub message must contain either non-empty data, or at least one attribute.
A base64-encoded string.

* `attributes` -
(Optional)
Expand Down

0 comments on commit 287a89e

Please sign in to comment.