Add padded url safe base64 encoding b64_url_pad for random_id outputs #352
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
proposal
I added
b64_url_padfor random_id outputs.Why
When creating a signed URL for CloudCDN, I used
random_id b64_url.google_compute_backend_bucket_signed_url_key | Resources | hashicorp/google | Terraform Registry
I have confirmed that this works.
However, when I created them from the Google Cloud console or
gcloudcommanda, the keys created were URL safe, base64 encoded, and padded with=.Furthermore, the official Google Cloud sample code assumes padded keys, so I could not use the keys created with
b64_urlwithout modification. In addition, when I tried to create a signed URL using thegcloud compute sign-urlcommand, an error occurred because it used a key without padding.failed gcloud command
fixed google cloud sample code for golang
Use signed URLs | Cloud CDN | Google Cloud
// readKeyFile reads the base64url-encoded key file and decodes it. func readKeyFile(path string) ([]byte, error) { b, err := ioutil.ReadFile(path) if err != nil { return nil, fmt.Errorf("failed to read key file: %+v", err) } - d := make([]byte, base64.URLEncoding.DecodedLen(len(b))) - n, err := base64.URLEncoding.Decode(d, b) + d := make([]byte, base64.RawURLEncoding.DecodedLen(len(b))) + n, err := base64.RawURLEncoding.Decode(d, b) if err != nil { return nil, fmt.Errorf("failed to base64url decode: %+v", err) } return d[:n], nil }Change
before
after
Name
At first, I thought of changing the output of b64_url to have padding and creating a new b64_raw_url with no padding.
The reason is that Go code also uses
base64.URLEncodingto encode those with padding andbase64.RawURLEncodingfor those without padding.The reason for using
b64_url_padinstead ofb64_raw_urlis to avoid modifying the already usedb64_url.In the long run, I think it is better to add
b64_raw_url, but if you accept this PR, I would appreciate your consideration.