diff --git a/tailcfg/tailcfg.go b/tailcfg/tailcfg.go index 57c68fad6..2270b66e1 100644 --- a/tailcfg/tailcfg.go +++ b/tailcfg/tailcfg.go @@ -709,37 +709,82 @@ func isAlpha(b byte) bool { return (b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') } +// BlueprintTagNamespacePrefix is the literal prefix (immediately after +// "tag:") for tags that are auto-generated by the control plane on behalf +// of a Blueprint. The double slash is intentional: it visually marks +// blueprint-managed tags so operators can distinguish them at a glance +// from user-authored tags. Blueprint-managed tags do not require a +// tagOwners entry (the control plane is the implicit owner). +const BlueprintTagNamespacePrefix = "bp//" + // CheckTag validates tag for use as an ACL tag. // For now we allow only ascii alphanumeric tags, and they need to start // with a letter. No unicode shenanigans allowed, and we reserve punctuation // marks other than '-' for a possible future URI scheme. // +// The one exception is the "tag:bp//" namespace, which is reserved for +// blueprint-managed tags. The portion follows the same rules as a +// regular tag name (letter-start, alphanumerics and dashes). +// // Because we're ignoring unicode entirely, we can treat utf-8 as a series of // bytes. Anything >= 128 is disqualified anyway. // // We might relax these rules later. func CheckTag(tag string) error { - var ok bool - tag, ok = strings.CutPrefix(tag, "tag:") + body, ok := strings.CutPrefix(tag, "tag:") if !ok { return errors.New("tags must start with 'tag:'") } - if tag == "" { - return errors.New("tag names must not be empty") - } - if !isAlpha(tag[0]) { - return errors.New("tag names must start with a letter, after 'tag:'") + if rest, isBP := strings.CutPrefix(body, BlueprintTagNamespacePrefix); isBP { + return checkTagBody(rest, "blueprint tag IDs") } + return checkTagBody(body, "tag names") +} - for _, b := range []byte(tag) { +// checkTagBody validates the post-prefix portion of a tag name (whether +// the prefix was "tag:" or "tag:bp//"). errNoun names what's being +// validated for use in human-readable error messages (e.g. "tag names" or +// "blueprint tag IDs"). +func checkTagBody(name, errNoun string) error { + if name == "" { + return fmt.Errorf("%s must not be empty", errNoun) + } + if !isAlpha(name[0]) { + return fmt.Errorf("%s must start with a letter", errNoun) + } + for _, b := range []byte(name) { if !isNum(b) && !isAlpha(b) && b != '-' { - return errors.New("tag names can only contain numbers, letters, or dashes") + return fmt.Errorf("%s can only contain numbers, letters, or dashes", errNoun) } } - return nil } +// IsBlueprintTag reports whether tag is a syntactically valid +// blueprint-managed tag of the form "tag:bp//". It does not check +// whether the referenced blueprint exists. +func IsBlueprintTag(tag string) bool { + rest, ok := strings.CutPrefix(tag, "tag:"+BlueprintTagNamespacePrefix) + if !ok { + return false + } + return checkTagBody(rest, "blueprint tag IDs") == nil +} + +// BlueprintIDFromTag returns the portion of a blueprint-managed tag +// "tag:bp//". If tag is not a syntactically valid blueprint tag, it +// returns "" and false. +func BlueprintIDFromTag(tag string) (id string, ok bool) { + rest, ok := strings.CutPrefix(tag, "tag:"+BlueprintTagNamespacePrefix) + if !ok { + return "", false + } + if checkTagBody(rest, "blueprint tag IDs") != nil { + return "", false + } + return rest, true +} + // CheckRequestTags checks that all of h.RequestTags are valid. func (h *Hostinfo) CheckRequestTags() error { if h == nil { diff --git a/tailcfg/tailcfg_test.go b/tailcfg/tailcfg_test.go index 8dd9191b6..cb047d588 100644 --- a/tailcfg/tailcfg_test.go +++ b/tailcfg/tailcfg_test.go @@ -892,6 +892,28 @@ func TestCheckTag(t *testing.T) { {"bad", "tag:", false}, {"no_leading_num", "tag:1foo", false}, {"no_punctuation", "tag:foa@bar", false}, + + // Blueprint-managed tag namespace (tag:bp//). The double + // slash is intentional; it visually marks tags whose + // provenance is a blueprint rather than user-authored + // tagOwners. The must satisfy the same character rules + // as a regular tag name (letter-start, alphanumerics, and + // dashes). + {"bp_simple", "tag:bp//foo", true}, + {"bp_dashes", "tag:bp//github-connector", true}, + {"bp_alnum", "tag:bp//us-east-1", true}, + {"bp_uppercase_id", "tag:bp//Foo", true}, + {"bp_empty_id", "tag:bp//", false}, + {"bp_id_starts_with_digit", "tag:bp//1foo", false}, + {"bp_id_starts_with_dash", "tag:bp//-foo", false}, + {"bp_id_has_slash", "tag:bp//foo/bar", false}, + {"bp_id_has_punctuation", "tag:bp//foo@bar", false}, + {"bp_triple_slash", "tag:bp///foo", false}, + {"bp_single_slash", "tag:bp/foo", false}, + {"bp_no_slash_just_bp", "tag:bp", true}, + {"bp_uppercase_namespace_is_not_blueprint", "tag:BP//foo", false}, + {"bp_mixed_case_namespace_is_not_blueprint", "tag:Bp//foo", false}, + {"bp_prefix_lookalike_is_not_blueprint", "tag:bpx//foo", false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -905,6 +927,60 @@ func TestCheckTag(t *testing.T) { } } +func TestIsBlueprintTag(t *testing.T) { + tests := []struct { + tag string + want bool + }{ + {"tag:bp//foo", true}, + {"tag:bp//github-connector", true}, + {"tag:bp//us-east-1", true}, + {"tag:bp//", false}, + {"tag:bp//1foo", false}, + {"tag:bp//foo/bar", false}, + {"tag:foo", false}, + {"tag:bp", false}, + {"tag:BP//foo", false}, + {"tag:bpx//foo", false}, + {"", false}, + {"bp//foo", false}, + } + for _, tt := range tests { + t.Run(tt.tag, func(t *testing.T) { + if got := IsBlueprintTag(tt.tag); got != tt.want { + t.Errorf("IsBlueprintTag(%q) = %v; want %v", tt.tag, got, tt.want) + } + }) + } +} + +func TestBlueprintIDFromTag(t *testing.T) { + tests := []struct { + tag string + wantID string + wantOK bool + }{ + {"tag:bp//foo", "foo", true}, + {"tag:bp//github-connector", "github-connector", true}, + {"tag:bp//", "", false}, + {"tag:bp//1foo", "", false}, + {"tag:foo", "", false}, + {"tag:bp", "", false}, + {"", "", false}, + } + for _, tt := range tests { + t.Run(tt.tag, func(t *testing.T) { + id, ok := BlueprintIDFromTag(tt.tag) + if ok != tt.wantOK { + t.Errorf("BlueprintIDFromTag(%q) ok = %v; want %v", tt.tag, ok, tt.wantOK) + } + if id != tt.wantID { + t.Errorf("BlueprintIDFromTag(%q) id = %q; want %q", tt.tag, id, tt.wantID) + } + }) + } +} + func TestDisplayMessageEqual(t *testing.T) { type test struct { name string