open-vault/helper/namespace/namespace.go

128 lines
2.7 KiB
Go
Raw Normal View History

2018-08-10 16:13:06 +00:00
package namespace
import (
"context"
"errors"
"strings"
)
type contextValues struct{}
2018-08-22 18:37:40 +00:00
type Namespace struct {
ID string `json:"id"`
Path string `json:"path"`
}
2018-08-10 16:13:06 +00:00
const (
RootNamespaceID = "root"
)
var (
contextNamespace contextValues = struct{}{}
ErrNoNamespace error = errors.New("no namespace")
2018-08-22 18:37:40 +00:00
RootNamespace *Namespace = &Namespace{
ID: RootNamespaceID,
Path: "",
}
2018-08-10 16:13:06 +00:00
)
func (n *Namespace) HasParent(possibleParent *Namespace) bool {
switch {
case n.Path == "":
return false
case possibleParent.Path == "":
return true
default:
return strings.HasPrefix(n.Path, possibleParent.Path)
}
}
func (n *Namespace) TrimmedPath(path string) string {
return strings.TrimPrefix(path, n.Path)
}
func ContextWithNamespace(ctx context.Context, ns *Namespace) context.Context {
2018-08-27 23:38:38 +00:00
return context.WithValue(ctx, contextNamespace, ns)
2018-08-10 16:13:06 +00:00
}
2018-08-22 18:37:40 +00:00
func RootContext(ctx context.Context) context.Context {
if ctx == nil {
return ContextWithNamespace(context.Background(), RootNamespace)
}
return ContextWithNamespace(ctx, RootNamespace)
}
// This function caches the ns to avoid doing a .Value lookup over and over,
// because it's called a *lot* in the request critical path. .Value is
// concurrency-safe so uses some kind of locking/atomicity, but it should never
// be read before first write, plus we don't believe this will be called from
// different goroutines, so it should be safe.
2018-08-10 16:13:06 +00:00
func FromContext(ctx context.Context) (*Namespace, error) {
if ctx == nil {
return nil, errors.New("context was nil")
}
2018-08-22 18:37:40 +00:00
nsRaw := ctx.Value(contextNamespace)
if nsRaw == nil {
return nil, ErrNoNamespace
}
ns := nsRaw.(*Namespace)
2018-08-10 16:13:06 +00:00
if ns == nil {
return nil, ErrNoNamespace
}
2018-08-22 18:37:40 +00:00
return ns, nil
2018-08-10 16:13:06 +00:00
}
// Canonicalize trims any prefix '/' and adds a trailing '/' to the
// provided string
func Canonicalize(nsPath string) string {
if nsPath == "" {
return ""
}
// Canonicalize the path to not have a '/' prefix
nsPath = strings.TrimPrefix(nsPath, "/")
// Canonicalize the path to always having a '/' suffix
if !strings.HasSuffix(nsPath, "/") {
nsPath += "/"
}
return nsPath
}
2018-09-18 03:03:00 +00:00
func SplitIDFromString(input string) (string, string) {
prefix := ""
slashIdx := strings.LastIndex(input, "/")
2018-10-15 16:56:24 +00:00
switch {
case strings.HasPrefix(input, "b."):
prefix = "b."
input = input[2:]
case strings.HasPrefix(input, "s."):
prefix = "s."
input = input[2:]
case slashIdx > 0:
// Leases will never have a b./s. to start
if slashIdx == len(input)-1 {
return input, ""
}
prefix = input[:slashIdx+1]
input = input[slashIdx+1:]
}
2018-09-18 03:03:00 +00:00
idx := strings.LastIndex(input, ".")
if idx == -1 {
return prefix + input, ""
2018-09-18 03:03:00 +00:00
}
if idx == len(input)-1 {
return prefix + input, ""
2018-09-18 03:03:00 +00:00
}
return prefix + input[:idx], input[idx+1:]
2018-09-18 03:03:00 +00:00
}