Skip to content

Commit

Permalink
[NET-5329] use acl templated policy under the hood for node/service i…
Browse files Browse the repository at this point in the history
…dentities (#18813)
  • Loading branch information
roncodingenthusiast authored Sep 14, 2023
1 parent e5808d8 commit 8021226
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 42 deletions.
3 changes: 3 additions & 0 deletions .changelog/18813.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
acl: Use templated policy to generate synthetic policies for tokens/roles with node and/or service identities
```
57 changes: 27 additions & 30 deletions agent/structs/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"errors"
"fmt"
"hash"
"hash/fnv"
"sort"
"strings"
"time"

"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/lib/stringslice"

"golang.org/x/crypto/blake2b"
Expand Down Expand Up @@ -182,22 +182,20 @@ func (s *ACLServiceIdentity) EstimateSize() int {
}

func (s *ACLServiceIdentity) SyntheticPolicy(entMeta *acl.EnterpriseMeta) *ACLPolicy {
// use templated policy to generate synthetic policy
templatedPolicy := ACLTemplatedPolicy{
TemplateID: ACLTemplatedPolicyServiceID,
TemplateName: api.ACLTemplatedPolicyServiceName,
Datacenters: s.Datacenters,
TemplateVariables: &ACLTemplatedPolicyVariables{
Name: s.ServiceName,
},
}

// Given that we validate this string name before persisting, we do not
// have to escape it before doing the following interpolation.
rules := aclServiceIdentityRules(s.ServiceName, entMeta)

hasher := fnv.New128a()
hashID := fmt.Sprintf("%x", hasher.Sum([]byte(rules)))

policy := &ACLPolicy{}
policy.ID = hashID
policy.Name = fmt.Sprintf("synthetic-policy-%s", hashID)
sn := NewServiceName(s.ServiceName, entMeta)
policy.Description = fmt.Sprintf("synthetic policy for service identity %q", sn.String())
policy.Rules = rules
policy.Datacenters = s.Datacenters
policy.EnterpriseMeta.Merge(entMeta)
policy.SetHash(true)
// expect any errors from generating the synthetic policy
policy, _ := templatedPolicy.SyntheticPolicy(entMeta)

return policy
}

Expand Down Expand Up @@ -254,21 +252,20 @@ func (s *ACLNodeIdentity) EstimateSize() int {
}

func (s *ACLNodeIdentity) SyntheticPolicy(entMeta *acl.EnterpriseMeta) *ACLPolicy {
// use templated policy to generate synthetic policy
templatedPolicy := ACLTemplatedPolicy{
TemplateID: ACLTemplatedPolicyNodeID,
TemplateName: api.ACLTemplatedPolicyNodeName,
Datacenters: []string{s.Datacenter},
TemplateVariables: &ACLTemplatedPolicyVariables{
Name: s.NodeName,
},
}

// Given that we validate this string name before persisting, we do not
// have to escape it before doing the following interpolation.
rules := aclNodeIdentityRules(s.NodeName, entMeta)

hasher := fnv.New128a()
hashID := fmt.Sprintf("%x", hasher.Sum([]byte(rules)))

policy := &ACLPolicy{}
policy.ID = hashID
policy.Name = fmt.Sprintf("synthetic-policy-%s", hashID)
policy.Description = fmt.Sprintf("synthetic policy for node identity %q", s.NodeName)
policy.Rules = rules
policy.Datacenters = []string{s.Datacenter}
policy.EnterpriseMeta.Merge(entMeta)
policy.SetHash(true)
// expect any errors from generating the synthetic policy
policy, _ := templatedPolicy.SyntheticPolicy(entMeta)

return policy
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Policies:

Service Identities:
Name: gardener (Datacenters: middleearth-northwest)
Description: synthetic policy for service identity "gardener"
Description: synthetic policy generated from templated policy: builtin/service
Rules:
service "gardener" {
policy = "write"
Expand All @@ -43,7 +43,7 @@ Service Identities:

Node Identities:
Name: bagend (Datacenter: middleearth-northwest)
Description: synthetic policy for node identity "bagend"
Description: synthetic policy generated from templated policy: builtin/node
Rules:
node "bagend" {
policy = "write"
Expand Down Expand Up @@ -96,7 +96,7 @@ Roles:

Service Identities:
Name: foo (Datacenters: middleearth-southwest)
Description: synthetic policy for service identity "foo"
Description: synthetic policy generated from templated policy: builtin/service
Rules:
service "foo" {
policy = "write"
Expand Down Expand Up @@ -125,7 +125,7 @@ Roles:

Node Identities:
Name: bar (Datacenter: middleearth-southwest)
Description: synthetic policy for node identity "bar"
Description: synthetic policy generated from templated policy: builtin/node
Rules:
node "bar" {
policy = "write"
Expand Down Expand Up @@ -158,7 +158,7 @@ Namespace Role Defaults:

Service Identities:
Name: web (Datacenters: middleearth-northeast)
Description: synthetic policy for service identity "web"
Description: synthetic policy generated from templated policy: builtin/service
Rules:
service "web" {
policy = "write"
Expand All @@ -175,7 +175,7 @@ Namespace Role Defaults:

Node Identities:
Name: db (Datacenter: middleearth-northwest)
Description: synthetic policy for node identity "db"
Description: synthetic policy generated from templated policy: builtin/node
Rules:
node "db" {
policy = "write"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Policies:

Service Identities:
Name: gardener (Datacenters: middleearth-northwest)
Description: synthetic policy for service identity "gardener"
Description: synthetic policy generated from templated policy: builtin/service
Rules:
service "gardener" {
policy = "write"
Expand All @@ -40,7 +40,7 @@ Service Identities:

Node Identities:
Name: bagend (Datacenter: middleearth-northwest)
Description: synthetic policy for node identity "bagend"
Description: synthetic policy generated from templated policy: builtin/node
Rules:
node "bagend" {
policy = "write"
Expand Down Expand Up @@ -93,7 +93,7 @@ Roles:

Service Identities:
Name: foo (Datacenters: middleearth-southwest)
Description: synthetic policy for service identity "foo"
Description: synthetic policy generated from templated policy: builtin/service
Rules:
service "foo" {
policy = "write"
Expand Down Expand Up @@ -122,7 +122,7 @@ Roles:

Node Identities:
Name: bar (Datacenter: middleearth-southwest)
Description: synthetic policy for node identity "bar"
Description: synthetic policy generated from templated policy: builtin/node
Rules:
node "bar" {
policy = "write"
Expand Down Expand Up @@ -155,7 +155,7 @@ Namespace Role Defaults:

Service Identities:
Name: web (Datacenters: middleearth-northeast)
Description: synthetic policy for service identity "web"
Description: synthetic policy generated from templated policy: builtin/service
Rules:
service "web" {
policy = "write"
Expand All @@ -172,7 +172,7 @@ Namespace Role Defaults:

Node Identities:
Name: db (Datacenter: middleearth-northwest)
Description: synthetic policy for node identity "db"
Description: synthetic policy generated from templated policy: builtin/node
Rules:
node "db" {
policy = "write"
Expand Down

0 comments on commit 8021226

Please sign in to comment.