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

Adding validation for name attribute in gcs bucket #12183

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
3 changes: 3 additions & 0 deletions .changelog/6250.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
storage: Added name validation for `google_storage_bucket`
```
3 changes: 3 additions & 0 deletions google/resource_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error

// Get the bucket and location
bucket := d.Get("name").(string)
if err := checkGCSName(bucket); err != nil {
return err
}
location := d.Get("location").(string)

// Create a bucket, setting the labels, location and name.
Expand Down
26 changes: 26 additions & 0 deletions google/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"os"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -525,3 +526,28 @@ func fake404(reasonResourceType, resourceName string) *googleapi.Error {
Message: fmt.Sprintf("%v object %v not found", reasonResourceType, resourceName),
}
}

// validate name of the gcs bucket.
func checkGCSName(name string) error {
MAX_LENGTH := 63
var err error
for _, str := range strings.Split(name, ".") {
strLen := len(str)
fmt.Println(str)
if strLen > MAX_LENGTH {
return fmt.Errorf("error: maximum length exceeded %v\n", str)
}
valid, _ := regexp.MatchString("^[a-z0-9_]+(-[a-z0-9]+)*$", str)

Choose a reason for hiding this comment

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

why this regex. It will not allow user to have "_" in the middle of bucket name

Choose a reason for hiding this comment

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

Here a good starting point for a new regex

^([a-z0-9]+[-\._a-z0-9]+[a-z0-9]+)$

Cases it DOES NOT catch by the naming guidelines: https://cloud.google.com/storage/docs/naming-buckets

  • Bucket names cannot be represented as an IP address in dotted-decimal notation (for example, 192.168.5.4).
  • Bucket names cannot begin with the "goog" prefix.
  • Bucket names cannot contain "google" or close misspellings, such as "g00gle"

if !valid {
return fmt.Errorf("error: string validation failed %v\n", str)
}
gPrefix := strings.HasPrefix(str, "goog")
if gPrefix {
return fmt.Errorf("error: string cannot start with %q %v\n", "goog", str)
}
if err != nil {
break
}
}
return nil
}