From b837ba35a023d746167c6b9f6fdeadb0d0ec1530 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Fri, 30 Jul 2021 18:05:33 -0400 Subject: [PATCH] acl: remove Server.ResolveTokenIdentityAndDefaultMeta This method suffered from similar naming to a couple other methods on Server, and had not great re-use (2 callers). By copying a few of the lines into one of the callers we can move the implementation into the second caller. Once moved, we can see that ResolveTokenAndDefaultMeta is identical in both Client and Server, and likely should be further refactored, possibly into ACLResolver. This change is being made to make ACL resolution easier to trace. --- agent/consul/acl.go | 4 ++++ agent/consul/acl_client.go | 1 + agent/consul/acl_server.go | 13 +++---------- agent/consul/intention_endpoint.go | 11 +++++++---- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/agent/consul/acl.go b/agent/consul/acl.go index 770e1c8e7d..dd90d267e9 100644 --- a/agent/consul/acl.go +++ b/agent/consul/acl.go @@ -1244,6 +1244,10 @@ func (r *ACLResolver) ResolveTokenToIdentityAndAuthorizer(token string) (structs return identity, acl.NewChainedAuthorizer(chain), nil } +// TODO: rename to AccessorIDFromToken. This method is only used to retrieve the +// ACLIdentity.ID, so we don't need to return a full ACLIdentity. We could +// return a much smaller type (instad of just a string) to allow for changes +// in the future. func (r *ACLResolver) ResolveTokenToIdentity(token string) (structs.ACLIdentity, error) { if !r.ACLsEnabled() { return nil, nil diff --git a/agent/consul/acl_client.go b/agent/consul/acl_client.go index 21106f37c4..bc86916eac 100644 --- a/agent/consul/acl_client.go +++ b/agent/consul/acl_client.go @@ -93,6 +93,7 @@ func (c *Client) ResolveTokenToIdentity(token string) (structs.ACLIdentity, erro return c.acls.ResolveTokenToIdentity(token) } +// TODO: Server has an identical implementation, remove duplication func (c *Client) ResolveTokenAndDefaultMeta(token string, entMeta *structs.EnterpriseMeta, authzContext *acl.AuthorizerContext) (acl.Authorizer, error) { identity, authz, err := c.acls.ResolveTokenToIdentityAndAuthorizer(token) if err != nil { diff --git a/agent/consul/acl_server.go b/agent/consul/acl_server.go index 7b753d6801..0351c27249 100644 --- a/agent/consul/acl_server.go +++ b/agent/consul/acl_server.go @@ -235,12 +235,11 @@ func (s *Server) ResolveTokenToIdentity(token string) (structs.ACLIdentity, erro return s.acls.ResolveTokenToIdentity(token) } -// ResolveTokenIdentityAndDefaultMeta retrieves an identity and authorizer for the caller, -// and populates the EnterpriseMeta based on the AuthorizerContext. -func (s *Server) ResolveTokenIdentityAndDefaultMeta(token string, entMeta *structs.EnterpriseMeta, authzContext *acl.AuthorizerContext) (structs.ACLIdentity, acl.Authorizer, error) { +// TODO: Client has an identical implementation, remove duplication +func (s *Server) ResolveTokenAndDefaultMeta(token string, entMeta *structs.EnterpriseMeta, authzContext *acl.AuthorizerContext) (acl.Authorizer, error) { identity, authz, err := s.acls.ResolveTokenToIdentityAndAuthorizer(token) if err != nil { - return nil, nil, err + return nil, err } // Default the EnterpriseMeta based on the Tokens meta or actual defaults @@ -254,12 +253,6 @@ func (s *Server) ResolveTokenIdentityAndDefaultMeta(token string, entMeta *struc // Use the meta to fill in the ACL authorization context entMeta.FillAuthzContext(authzContext) - return identity, authz, err -} - -// ResolveTokenAndDefaultMeta passes through to ResolveTokenIdentityAndDefaultMeta, eliding the identity from its response. -func (s *Server) ResolveTokenAndDefaultMeta(token string, entMeta *structs.EnterpriseMeta, authzContext *acl.AuthorizerContext) (acl.Authorizer, error) { - _, authz, err := s.ResolveTokenIdentityAndDefaultMeta(token, entMeta, authzContext) return authz, err } diff --git a/agent/consul/intention_endpoint.go b/agent/consul/intention_endpoint.go index 247ba85604..67f000f5f3 100644 --- a/agent/consul/intention_endpoint.go +++ b/agent/consul/intention_endpoint.go @@ -100,15 +100,18 @@ func (s *Intention) Apply(args *structs.IntentionRequest, reply *string) error { } // Get the ACL token for the request for the checks below. - var entMeta structs.EnterpriseMeta - ident, authz, err := s.srv.ResolveTokenIdentityAndDefaultMeta(args.Token, &entMeta, nil) + identity, authz, err := s.srv.acls.ResolveTokenToIdentityAndAuthorizer(args.Token) if err != nil { return err } var accessorID string - if ident != nil { - accessorID = ident.ID() + var entMeta structs.EnterpriseMeta + if identity != nil { + entMeta.Merge(identity.EnterpriseMetadata()) + accessorID = identity.ID() + } else { + entMeta.Merge(structs.DefaultEnterpriseMetaInDefaultPartition()) } var (