Skip to content

Commit

Permalink
Add GCP region validation (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ohkinozomu authored Feb 16, 2022
1 parent cfaeff6 commit c9192fc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.17
require (
github.com/docker/distribution v2.8.0+incompatible
github.com/jsonmaur/aws-regions/v2 v2.3.1
github.com/ohkinozomu/gcp-regions v0.0.1
)

require github.com/opencontainers/go-digest v1.0.0 // indirect
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug=
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/distribution v2.8.0+incompatible h1:l9EaZDICImO1ngI+uTifW+ZYvvz7fKISBAKpg+MbWbY=
github.com/docker/distribution v2.8.0+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/jsonmaur/aws-regions/v2 v2.3.1 h1:WWt452LyhjI4ZCRKBSULVHqIGE8/9UqVQOSAzuc2woE=
github.com/jsonmaur/aws-regions/v2 v2.3.1/go.mod h1:NqtmZ2wG5HkrTYFQ+II3BDysj0yek59yjtZjAaCn8lE=
github.com/ohkinozomu/gcp-regions v0.0.1 h1:OCOCPyhwIGMR6ao0bwIMQwz5rDMDRLN9O2SH3kvzuyw=
github.com/ohkinozomu/gcp-regions v0.0.1/go.mod h1:a69vJkNVbFBMhvI68qO36vYNC76eG9eWWBtoXIz5AaY=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
24 changes: 21 additions & 3 deletions google_artifact_registry.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
package which_registry

import "regexp"
import (
"errors"
"regexp"
"strings"

regions "github.com/ohkinozomu/gcp-regions"
)

// https://cloud.google.com/artifact-registry/docs/repo-locations#location-mr
func isMultiRegion(l string) bool {
return l == "asia" || l == "europe" || l == "us"
}

// https://cloud.google.com/artifact-registry/docs/docker/pushing-and-pulling#tag
func isGoogleArtifactRegistry(d string) (bool, error) {
// TODO: location check
match, err := regexp.MatchString(".*-docker.pkg.dev", d)
if err != nil {
return false, err
}
return match, nil

if match {
location := strings.Replace(d, "-docker.pkg.dev", "", 1)
if !regions.IsValid(location) && !isMultiRegion(location) {
return false, errors.New("Invalid location: " + location)
}
return true, nil
}
return false, nil
}
15 changes: 13 additions & 2 deletions google_container_registry.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
package which_registry

import "regexp"
import (
"errors"
"regexp"
"strings"
)

// https://cloud.google.com/container-registry/docs/overview#registries
func isGoogleContainerRegistry(d string) (bool, error) {
// TODO: location check
match, err := regexp.MatchString(".*gcr.io", d)
if err != nil {
return false, err
}

if match {
location := strings.Replace(d, "gcr.io", "", 1)
if location == "" || location == "us." || location == "eu." || location == "asia." {
return true, nil
}
return false, errors.New("Invalid location: " + location)
}
return match, nil
}
4 changes: 4 additions & 0 deletions which_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func TestWhich(t *testing.T) {
image: "us-east1-docker.pkg.dev/my-project/my-repo/test-image",
registry: GOOGLE_ARTIFACT_REGISTRY,
},
{
image: "us-docker.pkg.dev/my-project/my-repo/test-image",
registry: GOOGLE_ARTIFACT_REGISTRY,
},
{
image: "gcr.io/example.com/my-project/image-name",
registry: GOOGLE_CONTAINER_REGISTRY,
Expand Down

0 comments on commit c9192fc

Please sign in to comment.