Skip to content

Commit d7f51a8

Browse files
Validate base64 fields in the Google Provider (#4338) (#2906)
Co-authored-by: upodroid <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: upodroid <[email protected]>
1 parent 8d114d3 commit d7f51a8

File tree

6 files changed

+33
-13
lines changed

6 files changed

+33
-13
lines changed

.changelog/4338.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
provider: added plan time validations for fields that expect base64 values.
3+
```

google-beta/resource_api_gateway_api_config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ func resourceApiGatewayApiConfig() *schema.Resource {
6363
Elem: &schema.Resource{
6464
Schema: map[string]*schema.Schema{
6565
"contents": {
66-
Type: schema.TypeString,
67-
Required: true,
68-
ForceNew: true,
69-
Description: `Base64 encoded content of the file.`,
66+
Type: schema.TypeString,
67+
Required: true,
68+
ForceNew: true,
69+
ValidateFunc: validateBase64String,
70+
Description: `Base64 encoded content of the file.`,
7071
},
7172
"path": {
7273
Type: schema.TypeString,

google-beta/resource_cloud_scheduler_job.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,9 @@ By default, the job is sent to the version which is the default version when the
171171
},
172172
},
173173
"body": {
174-
Type: schema.TypeString,
175-
Optional: true,
174+
Type: schema.TypeString,
175+
Optional: true,
176+
ValidateFunc: validateBase64String,
176177
Description: `HTTP request body.
177178
A request body is allowed only if the HTTP method is POST or PUT.
178179
It will result in invalid argument error to set a body on a job with an incompatible HttpMethod.
@@ -232,8 +233,9 @@ send a request to the targeted url`,
232233
Description: `The full URI path that the request will be sent to.`,
233234
},
234235
"body": {
235-
Type: schema.TypeString,
236-
Optional: true,
236+
Type: schema.TypeString,
237+
Optional: true,
238+
ValidateFunc: validateBase64String,
237239
Description: `HTTP request body.
238240
A request body is allowed only if the HTTP method is POST, PUT, or PATCH.
239241
It is an error to set body on a job with an incompatible HttpMethod.
@@ -330,10 +332,13 @@ Pubsub message must contain either non-empty data, or at least one attribute.`,
330332
Elem: &schema.Schema{Type: schema.TypeString},
331333
},
332334
"data": {
333-
Type: schema.TypeString,
334-
Optional: true,
335+
Type: schema.TypeString,
336+
Optional: true,
337+
ValidateFunc: validateBase64String,
335338
Description: `The message payload for PubsubMessage.
336-
Pubsub message must contain either non-empty data, or at least one attribute.`,
339+
Pubsub message must contain either non-empty data, or at least one attribute.
340+
341+
A base64-encoded string.`,
337342
},
338343
},
339344
},

google-beta/resource_healthcare_hl7_v2_store.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,9 @@ store if schematized parsing is desired.`,
160160
AtLeastOneOf: []string{"parser_config.0.allow_null_header", "parser_config.0.segment_terminator", "parser_config.0.schema", "parser_config.0.version"},
161161
},
162162
"segment_terminator": {
163-
Type: schema.TypeString,
164-
Optional: true,
163+
Type: schema.TypeString,
164+
Optional: true,
165+
ValidateFunc: validateBase64String,
165166
Description: `Byte(s) to be used as the segment terminator. If this is unset, '\r' will be used as segment terminator.
166167
167168
A base64-encoded string.`,

google-beta/validation.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package google
22

33
import (
4+
"encoding/base64"
45
"fmt"
56
"net"
67
"regexp"
@@ -265,6 +266,14 @@ func validateIpAddress(i interface{}, val string) ([]string, []error) {
265266
return nil, nil
266267
}
267268

269+
func validateBase64String(i interface{}, val string) ([]string, []error) {
270+
_, err := base64.StdEncoding.DecodeString(i.(string))
271+
if err != nil {
272+
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)}
273+
}
274+
return nil, nil
275+
}
276+
268277
// StringNotInSlice returns a SchemaValidateFunc which tests if the provided value
269278
// is of type string and that it matches none of the element in the invalid slice.
270279
// if ignorecase is true, case is ignored.

website/docs/r/cloud_scheduler_job.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ The `pubsub_target` block supports:
302302
(Optional)
303303
The message payload for PubsubMessage.
304304
Pubsub message must contain either non-empty data, or at least one attribute.
305+
A base64-encoded string.
305306

306307
* `attributes` -
307308
(Optional)

0 commit comments

Comments
 (0)