Target subtypes must now implement another repository hook to validate any CredentialLibraries that are being added/set. This allows for subtypes to determine the valid set of CredentialLibraries. For tcp.Targets only CredentialLibaries with credential.ApplicationPurpose are allowed. If any other purpose is provided an error is returned.
127 lines
3.1 KiB
Go
127 lines
3.1 KiB
Go
package target
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/hashicorp/boundary/internal/types/subtypes"
|
|
)
|
|
|
|
// AllocFunc is a function that creates an in-memory Target.
|
|
type AllocFunc func() Target
|
|
|
|
// VetFunc is a function that checks the given Target to ensure it can
|
|
// be used by the Repository.
|
|
type VetFunc func(context.Context, Target) error
|
|
|
|
// VetCredentialLibrariesFunc is a function that checks the given CredentialLibraries
|
|
// to ensure that they are valid for a Target subtype.
|
|
type VetCredentialLibrariesFunc func(context.Context, []*CredentialLibrary) error
|
|
|
|
type registryEntry struct {
|
|
alloc AllocFunc
|
|
vet VetFunc
|
|
vetCredentialLibraries VetCredentialLibrariesFunc
|
|
prefix string
|
|
}
|
|
|
|
type registry struct {
|
|
m map[subtypes.Subtype]*registryEntry
|
|
subtypes *subtypes.Registry
|
|
|
|
sync.RWMutex
|
|
}
|
|
|
|
func (r *registry) set(s subtypes.Subtype, entry *registryEntry) {
|
|
r.Lock()
|
|
defer r.Unlock()
|
|
|
|
_, previouslySet := r.m[s]
|
|
if previouslySet {
|
|
panic(fmt.Sprintf("target subtype %s already registered", s))
|
|
}
|
|
|
|
if err := r.subtypes.Register(s, entry.prefix); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
r.m[s] = entry
|
|
}
|
|
|
|
func (r *registry) get(s subtypes.Subtype) (*registryEntry, bool) {
|
|
r.RLock()
|
|
defer r.RUnlock()
|
|
|
|
entry, ok := r.m[s]
|
|
if ok {
|
|
return entry, ok
|
|
}
|
|
return nil, ok
|
|
}
|
|
|
|
func (r *registry) allocFunc(s subtypes.Subtype) (AllocFunc, bool) {
|
|
entry, ok := r.get(s)
|
|
if !ok {
|
|
return nil, ok
|
|
}
|
|
return entry.alloc, ok
|
|
}
|
|
|
|
func (r *registry) vetFunc(s subtypes.Subtype) (VetFunc, bool) {
|
|
entry, ok := r.get(s)
|
|
if !ok {
|
|
return nil, ok
|
|
}
|
|
return entry.vet, ok
|
|
}
|
|
|
|
func (r *registry) vetCredentialLibrariesFunc(s subtypes.Subtype) (VetCredentialLibrariesFunc, bool) {
|
|
entry, ok := r.get(s)
|
|
if !ok {
|
|
return nil, ok
|
|
}
|
|
return entry.vetCredentialLibraries, ok
|
|
}
|
|
|
|
func (r *registry) idPrefix(s subtypes.Subtype) (string, bool) {
|
|
entry, ok := r.get(s)
|
|
if !ok {
|
|
return "", ok
|
|
}
|
|
return entry.prefix, ok
|
|
}
|
|
|
|
var subtypeRegistry = registry{
|
|
m: make(map[subtypes.Subtype]*registryEntry),
|
|
subtypes: subtypes.NewRegistry(),
|
|
}
|
|
|
|
// SubtypeFromType returns the Subtype from the provided string or if
|
|
// no Subtype was registered with that string Unknown is returned.
|
|
func SubtypeFromType(t string) subtypes.Subtype {
|
|
subtypeRegistry.RLock()
|
|
defer subtypeRegistry.RUnlock()
|
|
return subtypeRegistry.subtypes.SubtypeFromType(t)
|
|
}
|
|
|
|
// SubtypeFromId returns the Subtype from the provided id if the id's prefix
|
|
// was registered with a Subtype. Otherwise Unknown is returned.
|
|
func SubtypeFromId(id string) subtypes.Subtype {
|
|
subtypeRegistry.RLock()
|
|
defer subtypeRegistry.RUnlock()
|
|
return subtypeRegistry.subtypes.SubtypeFromId(id)
|
|
}
|
|
|
|
// Register registers repository hooks and the prefixes for a provided Subtype. Register
|
|
// panics if the subtype has already been registered or if any of the
|
|
// prefixes are associated with another subtype.
|
|
func Register(s subtypes.Subtype, af AllocFunc, vf VetFunc, vclf VetCredentialLibrariesFunc, prefix string) {
|
|
subtypeRegistry.set(s, ®istryEntry{
|
|
alloc: af,
|
|
vet: vf,
|
|
vetCredentialLibraries: vclf,
|
|
prefix: prefix,
|
|
})
|
|
}
|