Skip to content

Commit 7ea0baf

Browse files
backport of commit e4fdccd (#15936)
This pull request was automerged via backport-assistant
1 parent 74bd098 commit 7ea0baf

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

.changelog/15928.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
consul: Fixed a bug where acceptable service identity on Consul token was not accepted
3+
```

command/agent/consul/acl_testing.go

+11
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ const (
128128
ExampleOperatorTokenID3 = "6177d1b9-c0f6-4118-b891-d818a3cb80b1"
129129
ExampleOperatorTokenID4 = "754ae26c-f3cc-e088-d486-9c0d20f5eaea"
130130
ExampleOperatorTokenID5 = "097cbb45-506b-c79c-ec38-82eb0dc0794a"
131+
ExampleOperatorTokenID6 = "6268bd42-6f72-4c90-9c83-90ed6336dcf9"
131132
)
132133

133134
// Example Consul ACL tokens for use in tests that match the policies as the
@@ -214,6 +215,16 @@ var (
214215
Namespace: "",
215216
}
216217

218+
ExampleOperatorToken6 = &api.ACLToken{
219+
SecretID: ExampleOperatorTokenID6,
220+
AccessorID: "93786935-8856-6e17-0488-c5370a1f044e",
221+
Description: "Operator Token 6",
222+
ServiceIdentities: []*api.ACLServiceIdentity{
223+
{ServiceName: "service1"},
224+
},
225+
Namespace: "",
226+
}
227+
217228
// In Consul namespace "banana"
218229

219230
ExampleOperatorToken10 = &api.ACLToken{

nomad/consul_policy.go

+8
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ func (c *consulACLsAPI) canWriteService(namespace, service string, token *api.AC
160160
// treat that like an exact match to preserve backwards compatibility
161161
matches := (namespace == token.Namespace) || (namespace == "" && token.Namespace == "default")
162162

163+
// check each service identity attached to the token -
164+
// the virtual policy for service identities enables service:write
165+
for _, si := range token.ServiceIdentities {
166+
if si.ServiceName == service {
167+
return true, nil
168+
}
169+
}
170+
163171
// check each policy directly attached to the token
164172
for _, policyRef := range token.Policies {
165173
if allowable, err := c.policyAllowsServiceWrite(matches, namespace, service, policyRef.ID); err != nil {

nomad/consul_policy_oss_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build !ent
2-
// +build !ent
32

43
package nomad
54

@@ -10,7 +9,7 @@ import (
109
"github.com/hashicorp/nomad/ci"
1110
"github.com/hashicorp/nomad/command/agent/consul"
1211
"github.com/hashicorp/nomad/helper/testlog"
13-
"github.com/stretchr/testify/require"
12+
"github.com/shoenig/test/must"
1413
)
1514

1615
func TestConsulACLsAPI_hasSufficientPolicy_oss(t *testing.T) {
@@ -23,8 +22,8 @@ func TestConsulACLsAPI_hasSufficientPolicy_oss(t *testing.T) {
2322
logger: logger,
2423
}
2524
result, err := cAPI.canWriteService(namespace, task, token)
26-
require.NoError(t, err)
27-
require.Equal(t, exp, result)
25+
must.NoError(t, err)
26+
must.Eq(t, exp, result)
2827
}
2928

3029
// In Nomad OSS, group consul namespace will always be empty string.
@@ -41,5 +40,9 @@ func TestConsulACLsAPI_hasSufficientPolicy_oss(t *testing.T) {
4140
t.Run("working role only", func(t *testing.T) {
4241
try(t, "", "service1", consul.ExampleOperatorToken4, true)
4342
})
43+
44+
t.Run("working service identity only", func(t *testing.T) {
45+
try(t, "", "service1", consul.ExampleOperatorToken6, true)
46+
})
4447
})
4548
}

0 commit comments

Comments
 (0)