Index: go/src/infra/tools/cipd/common.go |
diff --git a/go/src/infra/tools/cipd/common.go b/go/src/infra/tools/cipd/common.go |
index 3bcc9da209552737dc63e307f5fe07c2c64f27a4..e4c2859eb2f84ce73d9bbc8f6d6a23ab3e01a9b2 100644 |
--- a/go/src/infra/tools/cipd/common.go |
+++ b/go/src/infra/tools/cipd/common.go |
@@ -10,6 +10,7 @@ import ( |
"io" |
"io/ioutil" |
"regexp" |
+ "strings" |
"infra/libs/build" |
) |
@@ -20,10 +21,13 @@ const packageServiceDir = ".cipdpkg" |
// Name of the directory inside an installation root reserved for cipd stuff. |
const siteServiceDir = ".cipd" |
-// packageNameRe is a Regular expression for a package name: <word>/<word/<word> |
+// packageNameRe is a regular expression for a package name: <word>/<word/<word> |
// Package names must be lower case. |
var packageNameRe = regexp.MustCompile(`^([a-z0-9_\-]+/)*[a-z0-9_\-]+$`) |
+// instanceTagKeyRe is a regular expression for a tag key. |
+var instanceTagKeyRe = regexp.MustCompile(`^[a-z0-9_\-]+$`) |
+ |
// Name of the manifest file inside the package. |
const manifestName = packageServiceDir + "/manifest.json" |
@@ -58,6 +62,21 @@ func ValidateInstanceID(s string) error { |
return nil |
} |
+// ValidateInstanceTag returns error if a string doesn't look like a valid tag. |
+func ValidateInstanceTag(t string) error { |
+ chunks := strings.SplitN(t, ":", 2) |
+ if len(chunks) != 2 { |
+ return fmt.Errorf("The string %q doesn't look like a tag (a key:value pair)", t) |
+ } |
+ if len(t) > 400 { |
+ return fmt.Errorf("The tag is too long: %q", t) |
+ } |
+ if !instanceTagKeyRe.MatchString(chunks[0]) { |
+ return fmt.Errorf("Invalid tag key in %q. Should be a lowercase word.", t) |
+ } |
+ return nil |
+} |
+ |
// DefaultServiceURL returns URL to a backend to use by default. |
func DefaultServiceURL() string { |
if build.ReleaseBuild { |