Subtype Id registration moved to its own package (#1416)

* Allow repo packages to register subtypes and id prefixes. 
* Refactor out request authentication logic to a new package as a sibling to handler.
This commit is contained in:
Todd Knight authored and GitHub committed 2021-07-27 09:59:52 -07:00
1 parent ab6f3eaeb4
commit 10e8aeae4b
62 files changed
+603 -597

No files matched your search

+83
View File
@@ -0,0 +1,83 @@
// Package subtypes provides helpers to work with boundary resource subtypes.
package subtypes
import (
"fmt"
"strings"
"github.com/hashicorp/boundary/internal/errors"
)
// Subtype variables identify a boundary resource subtype.
type Subtype string
const (
UnknownSubtype Subtype = "unknown"
)
// String returns the string representation of a Subtype
func (t Subtype) String() string {
return string(t)
}
// Registry stores a collection of boundary resource subtypes along with their
// prefixes and allows for translating prefixes back to registered subtypes.
// Registry is not thread safe.
type Registry struct {
subtypesPrefixes map[string]Subtype
knownSubtypes map[Subtype]interface{}
}
// New Registry creates a new boundary resource subtype registry.
func NewRegistry() *Registry {
return &Registry{
subtypesPrefixes: make(map[string]Subtype),
knownSubtypes: make(map[Subtype]interface{}),
}
}
// SubtypeFromType returns the Subtype from the provided string or if
// no Subtype was registered with that string Unknown is returned.
func (r *Registry) SubtypeFromType(t string) Subtype {
st := Subtype(t)
if _, ok := r.knownSubtypes[st]; !ok {
return UnknownSubtype
}
return st
}
// SubtypeFromId returns the Subtype from the provided id if the id's prefix
// was registered with a Subtype. Otherwise Unknown is returned.
func (r *Registry) SubtypeFromId(id string) Subtype {
i := strings.Index(id, "_")
if i == -1 {
return UnknownSubtype
}
prefix := id[:i]
subtype, ok := r.subtypesPrefixes[prefix]
if !ok {
return UnknownSubtype
}
return subtype
}
// Register registers all the prefixes for a provided Subtype. Register returns
// an error if the subtype has already been registered or if any of the
// prefixes are associated with another subtype.
func (r *Registry) Register(subtype Subtype, prefixes ...string) error {
const op = "subtypes.(Registry).Register"
if _, present := r.knownSubtypes[subtype]; present {
return errors.New(errors.SubtypeAlreadyRegistered, op, fmt.Sprintf("subtype %q already registered", subtype))
}
r.knownSubtypes[subtype] = nil
for _, prefix := range prefixes {
prefix = strings.TrimSpace(prefix)
if st, ok := r.subtypesPrefixes[prefix]; ok {
return errors.New(errors.SubtypeAlreadyRegistered, op, fmt.Sprintf("prefix %q is already registered to subtype %q", prefix, st))
}
r.subtypesPrefixes[prefix] = subtype
}
return nil
}
+74
View File
@@ -0,0 +1,74 @@
package subtypes_test
import (
"testing"
"github.com/hashicorp/boundary/internal/types/subtypes"
"github.com/stretchr/testify/assert"
)
func TestSubtypeFromId(t *testing.T) {
testSubtype := subtypes.Subtype("test")
r := subtypes.NewRegistry()
r.Register(testSubtype, "tttst")
tests := []struct {
name string
given string
want subtypes.Subtype
}{
{"empty-string", "", subtypes.UnknownSubtype},
{"no-prefix-delimiter", "tttst1234", subtypes.UnknownSubtype},
{"prefix-first", "_tttst_1234", subtypes.UnknownSubtype},
{"unknown-prefix", "kaz_1234", subtypes.UnknownSubtype},
{"prefix-no-id", "tttst_", testSubtype},
{"vault-prefix", "tttst_1234", testSubtype},
{"prefix-no-delimiter-no-id", "tttst", subtypes.UnknownSubtype},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got := r.SubtypeFromId(tt.given)
assert.Equalf(t, tt.want, got, "given: %s", tt.given)
if got != tt.want {
t.Errorf("(%s): expected %s, actual %s", tt.given, tt.want, got)
}
})
}
}
func TestSubtypeFromType(t *testing.T) {
testSubtype := subtypes.Subtype("test")
r := subtypes.NewRegistry()
r.Register(testSubtype, "tttst")
tests := []struct {
name string
given string
want subtypes.Subtype
}{
{"empty-string", "", subtypes.UnknownSubtype},
{"correct-string", "test", testSubtype},
{"captialized", "TEST", subtypes.UnknownSubtype},
{"typo", "testt", subtypes.UnknownSubtype},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got := r.SubtypeFromType(tt.given)
assert.Equalf(t, tt.want, got, "given: %s", tt.given)
if got != tt.want {
t.Errorf("(%s): expected %s, actual %s", tt.given, tt.want, got)
}
})
}
}
func TestRegister(t *testing.T) {
r := subtypes.NewRegistry()
assert.NoError(t, r.Register("test", "testprefix"))
// registering multiple subtypes should be fine.
assert.NoError(t, r.Register("second", "secondprefix"))
// registering another prefix with a different subtype errors.
assert.Error(t, r.Register("third", "testprefix"))
// Registering the same subtype twice errors.
assert.Error(t, r.Register("test", "repeatedprefix"))
}