2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-07-13 15:33:48 +00:00
|
|
|
package external
|
2022-04-20 00:24:21 +00:00
|
|
|
|
2023-01-04 12:40:34 +00:00
|
|
|
import (
|
|
|
|
"github.com/hashicorp/go-uuid"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
"github.com/hashicorp/consul/acl/resolver"
|
|
|
|
)
|
2022-04-20 00:24:21 +00:00
|
|
|
|
|
|
|
// We tag logs with a unique identifier to ease debugging. In the future this
|
|
|
|
// should probably be a real Open Telemetry trace ID.
|
|
|
|
func TraceID() string {
|
|
|
|
id, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
2023-01-04 12:40:34 +00:00
|
|
|
|
|
|
|
type ACLResolver interface {
|
|
|
|
ResolveTokenAndDefaultMeta(string, *acl.EnterpriseMeta, *acl.AuthorizerContext) (resolver.Result, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequireAnyValidACLToken checks that the caller provided a valid ACL token
|
|
|
|
// without requiring any specific permissions. This is useful for endpoints
|
|
|
|
// that are used by all/most consumers of our API, such as those called by the
|
|
|
|
// consul-server-connection-manager library when establishing a new connection.
|
|
|
|
//
|
|
|
|
// Note: no token is required if ACLs are disabled.
|
|
|
|
func RequireAnyValidACLToken(resolver ACLResolver, token string) error {
|
|
|
|
authz, err := resolver.ResolveTokenAndDefaultMeta(token, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return status.Error(codes.Unauthenticated, err.Error())
|
|
|
|
}
|
|
|
|
|
2023-01-09 18:28:53 +00:00
|
|
|
if id := authz.ACLIdentity; id != nil && id.ID() == acl.AnonymousTokenID {
|
2023-01-04 12:40:34 +00:00
|
|
|
return status.Error(codes.Unauthenticated, "An ACL token must be provided (via the `x-consul-token` metadata field) to call this endpoint")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|