2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-10-03 19:37:53 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2020-06-15 15:01:25 +00:00
|
|
|
"context"
|
2018-10-03 19:37:53 +00:00
|
|
|
"fmt"
|
2022-04-29 17:42:49 +00:00
|
|
|
"net/http"
|
2021-06-25 21:47:47 +00:00
|
|
|
|
2018-10-03 19:37:53 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
|
|
|
cachetype "github.com/hashicorp/consul/agent/cache-types"
|
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
|
|
|
|
2020-10-06 22:09:13 +00:00
|
|
|
// TODO(rb/intentions): this should move back into the agent endpoint since
|
|
|
|
// there is no ext_authz implementation anymore.
|
|
|
|
//
|
2018-10-03 19:37:53 +00:00
|
|
|
// ConnectAuthorize implements the core authorization logic for Connect. It's in
|
|
|
|
// a separate agent method here because we need to re-use this both in our own
|
|
|
|
// HTTP API authz endpoint and in the gRPX xDS/ext_authz API for envoy.
|
|
|
|
//
|
2020-10-06 22:09:13 +00:00
|
|
|
// NOTE: This treats any L7 intentions as DENY.
|
|
|
|
//
|
2018-10-03 19:37:53 +00:00
|
|
|
// The ACL token and the auth request are provided and the auth decision (true
|
2019-03-06 17:13:28 +00:00
|
|
|
// means authorized) and reason string are returned.
|
2018-10-03 19:37:53 +00:00
|
|
|
//
|
2022-04-29 17:42:49 +00:00
|
|
|
// If the request input is invalid the error returned will be a BadRequest HTTPError,
|
2018-10-03 19:37:53 +00:00
|
|
|
// if the token doesn't grant necessary access then an acl.ErrPermissionDenied
|
|
|
|
// error is returned, otherwise error indicates an unexpected server failure. If
|
|
|
|
// access is denied, no error is returned but the first return value is false.
|
|
|
|
func (a *Agent) ConnectAuthorize(token string,
|
2020-01-13 20:51:40 +00:00
|
|
|
req *structs.ConnectAuthorizeRequest) (allowed bool, reason string, m *cache.ResultMeta, err error) {
|
2018-10-03 19:37:53 +00:00
|
|
|
|
|
|
|
// Helper to make the error cases read better without resorting to named
|
|
|
|
// returns which get messy and prone to mistakes in a method this long.
|
|
|
|
returnErr := func(err error) (bool, string, *cache.ResultMeta, error) {
|
|
|
|
return false, "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if req == nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return returnErr(HTTPError{StatusCode: http.StatusBadRequest, Reason: "Invalid request"})
|
2018-10-03 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We need to have a target to check intentions
|
|
|
|
if req.Target == "" {
|
2022-04-29 17:42:49 +00:00
|
|
|
return returnErr(HTTPError{StatusCode: http.StatusBadRequest, Reason: "Target service must be specified"})
|
2018-10-03 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the certificate URI from the client ID
|
|
|
|
uri, err := connect.ParseCertURIFromString(req.ClientCertURI)
|
|
|
|
if err != nil {
|
2022-04-29 17:42:49 +00:00
|
|
|
return returnErr(HTTPError{StatusCode: http.StatusBadRequest, Reason: "ClientCertURI not a valid Connect identifier"})
|
2018-10-03 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uriService, ok := uri.(*connect.SpiffeIDService)
|
|
|
|
if !ok {
|
2022-04-29 17:42:49 +00:00
|
|
|
return returnErr(HTTPError{StatusCode: http.StatusBadRequest, Reason: "ClientCertURI not a valid Service identifier"})
|
2018-10-03 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We need to verify service:write permissions for the given token.
|
|
|
|
// We do this manually here since the RPC request below only verifies
|
|
|
|
// service:read.
|
2020-01-13 20:51:40 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
2021-04-14 16:39:35 +00:00
|
|
|
authz, err := a.delegate.ResolveTokenAndDefaultMeta(token, &req.EnterpriseMeta, &authzContext)
|
2018-10-03 19:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return returnErr(err)
|
|
|
|
}
|
2020-01-13 20:51:40 +00:00
|
|
|
|
2022-03-11 02:48:27 +00:00
|
|
|
if err := authz.ToAllowAuthorizer().ServiceWriteAllowed(req.Target, &authzContext); err != nil {
|
|
|
|
return returnErr(err)
|
2018-10-03 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 21:47:47 +00:00
|
|
|
if !uriService.MatchesPartition(req.TargetPartition()) {
|
|
|
|
reason = fmt.Sprintf("Mismatched partitions: %q != %q",
|
|
|
|
uriService.PartitionOrDefault(),
|
2022-04-05 21:10:06 +00:00
|
|
|
acl.PartitionOrDefault(req.TargetPartition()))
|
2021-06-25 21:47:47 +00:00
|
|
|
return false, reason, nil, nil
|
|
|
|
}
|
|
|
|
|
2018-11-12 20:20:12 +00:00
|
|
|
// Note that we DON'T explicitly validate the trust-domain matches ours. See
|
|
|
|
// the PR for this change for details.
|
2018-10-03 19:37:53 +00:00
|
|
|
|
|
|
|
// TODO(banks): Implement revocation list checking here.
|
|
|
|
|
|
|
|
// Get the intentions for this target service.
|
|
|
|
args := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: a.config.Datacenter,
|
|
|
|
Match: &structs.IntentionQueryMatch{
|
|
|
|
Type: structs.IntentionMatchDestination,
|
|
|
|
Entries: []structs.IntentionMatchEntry{
|
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
Namespace: req.TargetNamespace(),
|
2021-09-16 19:31:19 +00:00
|
|
|
Partition: req.TargetPartition(),
|
2018-10-03 19:37:53 +00:00
|
|
|
Name: req.Target,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
QueryOptions: structs.QueryOptions{Token: token},
|
|
|
|
}
|
|
|
|
|
2020-06-15 15:01:25 +00:00
|
|
|
raw, meta, err := a.cache.Get(context.TODO(), cachetype.IntentionMatchName, args)
|
2018-10-03 19:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return returnErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
reply, ok := raw.(*structs.IndexedIntentionMatches)
|
|
|
|
if !ok {
|
|
|
|
return returnErr(fmt.Errorf("internal error: response type not correct"))
|
|
|
|
}
|
|
|
|
if len(reply.Matches) != 1 {
|
|
|
|
return returnErr(fmt.Errorf("Internal error loading matches"))
|
|
|
|
}
|
|
|
|
|
2020-10-06 22:09:13 +00:00
|
|
|
// Figure out which source matches this request.
|
|
|
|
var ixnMatch *structs.Intention
|
2018-10-03 19:37:53 +00:00
|
|
|
for _, ixn := range reply.Matches[0] {
|
2021-03-16 00:06:04 +00:00
|
|
|
// We match on the intention source because the uriService is the source of the connection to authorize.
|
2021-09-16 19:31:19 +00:00
|
|
|
if _, ok := connect.AuthorizeIntentionTarget(
|
2023-04-20 16:16:04 +00:00
|
|
|
uriService.Service, uriService.Namespace, uriService.Partition, "", ixn, structs.IntentionMatchSource); ok {
|
2020-10-06 22:09:13 +00:00
|
|
|
ixnMatch = ixn
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ixnMatch != nil {
|
|
|
|
if len(ixnMatch.Permissions) == 0 {
|
|
|
|
// This is an L4 intention.
|
|
|
|
reason = fmt.Sprintf("Matched L4 intention: %s", ixnMatch.String())
|
|
|
|
auth := ixnMatch.Action == structs.IntentionActionAllow
|
2018-10-03 19:37:53 +00:00
|
|
|
return auth, reason, &meta, nil
|
|
|
|
}
|
2020-10-06 22:09:13 +00:00
|
|
|
|
|
|
|
// This is an L7 intention, so DENY.
|
|
|
|
reason = fmt.Sprintf("Matched L7 intention: %s", ixnMatch.String())
|
|
|
|
return false, reason, &meta, nil
|
2018-10-03 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reason = "Default behavior configured by ACLs"
|
2020-01-13 20:51:40 +00:00
|
|
|
return authz.IntentionDefaultAllow(nil) == acl.Allow, reason, &meta, nil
|
2018-10-03 19:37:53 +00:00
|
|
|
}
|