namespaces: enforce a character set for namespaces

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day
2017-06-21 17:24:43 -07:00
parent 753f1a6c16
commit 5380585e21
6 changed files with 168 additions and 2 deletions

View File

@@ -37,7 +37,9 @@ func NamespaceFromEnv(ctx context.Context) context.Context {
return WithNamespace(ctx, namespace)
}
// Namespace returns the namespace from the context
// Namespace returns the namespace from the context.
//
// The namespace is not guaranteed to be valid.
func Namespace(ctx context.Context) (string, bool) {
namespace, ok := ctx.Value(namespaceKey{}).(string)
if !ok {
@@ -52,12 +54,16 @@ func IsNamespaceRequired(err error) bool {
return errors.Cause(err) == errNamespaceRequired
}
// NamespaceRequired returns the namespace or an error
// NamespaceRequired returns the valid namepace from the context or an error.
func NamespaceRequired(ctx context.Context) (string, error) {
namespace, ok := Namespace(ctx)
if !ok || namespace == "" {
return "", errNamespaceRequired
}
if err := Validate(namespace); err != nil {
return "", err
}
return namespace, nil
}