-
Notifications
You must be signed in to change notification settings - Fork 2k
wi: new endpoint for listing workload attached ACL policies #25588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pkazmierczak
wants to merge
23
commits into
main
Choose a base branch
from
f-self-wi-lookup-policy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+476
−15
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
9b27f84
acl: obsolete ResolveToken and use WhoAmI RPC endpoint instead
pkazmierczak 0362ab6
Update command/agent/acl_endpoint.go
pkazmierczak 3aff3b2
better error message
pkazmierczak 0fa3bfb
restore RPC
pkazmierczak 7db690e
WhoAmI RPC should set query meta
pkazmierczak 6e72924
set reply index if there's an ACL token
pkazmierczak 654661d
draft
pkazmierczak a59e92d
url update
pkazmierczak 1454528
typo in the api route
pkazmierczak 605bc14
move SelfPolicy method to ACLPolicies
pkazmierczak 7435e19
add check for WI policies in acl token self CLI
pkazmierczak d8f496a
fix URL handling
pkazmierczak d4e97f6
changelog
pkazmierczak 1683ae7
documentation
pkazmierczak 3ae337a
API doc
pkazmierczak 50686ec
cli unit test
pkazmierczak 8565bf2
acl endpoint test
pkazmierczak 38df7db
oopsie forgot to remove some copy pasta from Schmichael
pkazmierczak 7d8e6ce
more elaborate acl policy self output
pkazmierczak 739ecc6
reformat mdx tables
pkazmierczak 56dfa5f
, n
pkazmierczak d229c19
s/auth methods/policies
pkazmierczak 4053a48
list policies regardless of WI
pkazmierczak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:improvement | ||
wi: new API endpoint for listing workload-attached ACL policies | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package command | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/posener/complete" | ||
) | ||
|
||
type ACLPolicySelfCommand struct { | ||
Meta | ||
|
||
json bool | ||
tmpl string | ||
} | ||
|
||
func (c *ACLPolicySelfCommand) Help() string { | ||
helpText := ` | ||
Usage: nomad acl policy self | ||
|
||
Self is used to fetch information about the policy assigned to the current workload identity. | ||
|
||
General Options: | ||
|
||
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + ` | ||
|
||
ACL List Options: | ||
|
||
-json | ||
Output the ACL policies in a JSON format. | ||
|
||
-t | ||
Format and display the ACL policies using a Go template. | ||
` | ||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
func (c *ACLPolicySelfCommand) AutocompleteFlags() complete.Flags { | ||
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), | ||
complete.Flags{ | ||
"-json": complete.PredictNothing, | ||
"-t": complete.PredictAnything, | ||
}) | ||
} | ||
|
||
func (c *ACLPolicySelfCommand) AutocompleteArgs() complete.Predictor { | ||
return complete.PredictNothing | ||
} | ||
|
||
func (c *ACLPolicySelfCommand) Synopsis() string { | ||
return "Lookup self ACL policy assigned to the workload identity" | ||
} | ||
|
||
func (c *ACLPolicySelfCommand) Name() string { return "acl policy self" } | ||
|
||
func (c *ACLPolicySelfCommand) Run(args []string) int { | ||
flags := c.Meta.FlagSet(c.Name(), FlagSetClient) | ||
flags.Usage = func() { c.Ui.Output(c.Help()) } | ||
flags.BoolVar(&c.json, "json", false, "") | ||
flags.StringVar(&c.tmpl, "t", "", "") | ||
if err := flags.Parse(args); err != nil { | ||
return 1 | ||
} | ||
|
||
// Check that we have no arguments | ||
args = flags.Args() | ||
if l := len(args); l != 0 { | ||
c.Ui.Error("This command takes no arguments") | ||
c.Ui.Error(commandErrorText(c)) | ||
return 1 | ||
} | ||
|
||
// Get the HTTP client | ||
client, err := c.Meta.Client() | ||
if err != nil { | ||
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) | ||
return 1 | ||
} | ||
|
||
policies, _, err := client.ACLPolicies().Self(nil) | ||
if err != nil { | ||
c.Ui.Error(fmt.Sprintf("Error fetching WI policies: %s", err)) | ||
return 1 | ||
} | ||
|
||
if len(policies) == 0 { | ||
c.Ui.Output("No policies found for this identity.") | ||
} else { | ||
if c.json || len(c.tmpl) > 0 { | ||
out, err := Format(c.json, c.tmpl, policies) | ||
if err != nil { | ||
c.Ui.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
c.Ui.Output(out) | ||
return 0 | ||
} | ||
|
||
output := make([]string, 0, len(policies)+1) | ||
output = append(output, "Name|Job ID|Group Name|Task Name") | ||
for _, p := range policies { | ||
var outputString string | ||
if p.JobACL == nil { | ||
outputString = fmt.Sprintf("%s|%s|%s|%s", p.Name, "<unavailable>", "<unavailable>", "<unavailable>") | ||
} else { | ||
outputString = fmt.Sprintf( | ||
"%s|%s|%s|%s", | ||
p.Name, formatJobACL(p.JobACL.JobID), formatJobACL(p.JobACL.Group), formatJobACL(p.JobACL.Task), | ||
) | ||
} | ||
output = append(output, outputString) | ||
} | ||
|
||
c.Ui.Output(formatList(output)) | ||
} | ||
return 0 | ||
} | ||
|
||
func formatJobACL(jobACL string) string { | ||
if jobACL == "" { | ||
return "<not specified>" | ||
} | ||
return jobACL | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package command | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/cli" | ||
"github.com/hashicorp/nomad/command/agent" | ||
"github.com/hashicorp/nomad/nomad/mock" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
"github.com/hashicorp/nomad/testutil" | ||
"github.com/shoenig/test/must" | ||
) | ||
|
||
func TestACLPolicySelfCommand_ViaEnvVar(t *testing.T) { | ||
config := func(c *agent.Config) { | ||
c.ACL.Enabled = true | ||
} | ||
|
||
srv, _, url := testServer(t, true, config) | ||
defer srv.Shutdown() | ||
|
||
state := srv.Agent.Server().State() | ||
|
||
// Bootstrap an initial ACL token | ||
token := srv.RootToken | ||
must.NotNil(t, token) | ||
|
||
// Create a minimal job | ||
job := mock.MinJob() | ||
|
||
// Add a job policy | ||
polArgs := structs.ACLPolicyUpsertRequest{ | ||
Policies: []*structs.ACLPolicy{ | ||
{ | ||
Name: "nw", | ||
Description: "test job can write to nodes", | ||
Rules: `node { policy = "write" }`, | ||
JobACL: &structs.JobACL{ | ||
Namespace: job.Namespace, | ||
JobID: job.ID, | ||
}, | ||
}, | ||
}, | ||
WriteRequest: structs.WriteRequest{ | ||
Region: job.Region, | ||
AuthToken: token.SecretID, | ||
Namespace: job.Namespace, | ||
}, | ||
} | ||
polReply := structs.GenericResponse{} | ||
must.NoError(t, srv.RPC("ACL.UpsertPolicies", &polArgs, &polReply)) | ||
must.NonZero(t, polReply.WriteMeta.Index) | ||
|
||
ui := cli.NewMockUi() | ||
cmd := &ACLPolicySelfCommand{Meta: Meta{Ui: ui, flagAddress: url}} | ||
|
||
allocs := testutil.WaitForRunningWithToken(t, srv.RPC, job, token.SecretID) | ||
must.Len(t, 1, allocs) | ||
|
||
alloc, err := state.AllocByID(nil, allocs[0].ID) | ||
must.NoError(t, err) | ||
must.MapContainsKey(t, alloc.SignedIdentities, "t") | ||
wid := alloc.SignedIdentities["t"] | ||
|
||
// Fetch info on policies with a JWT | ||
t.Setenv("NOMAD_TOKEN", wid) | ||
code := cmd.Run([]string{"-address=" + url}) | ||
must.Zero(t, code) | ||
|
||
// Check the output | ||
out := ui.OutputWriter.String() | ||
must.StrContains(t, out, polArgs.Policies[0].Name) | ||
|
||
// make sure we put the job ACLs in there, too | ||
must.StrContains(t, out, polArgs.Policies[0].JobACL.JobID) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this bool check is reversed, because this is what happens when I use a non-existent token in current
main
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 I don't follow, Daniel.
On
main
you will of course get 404 if you try to querytoken self
with WI. What you're showing above is strange to me, prefixingnomad acl token self
with a bogus token should give you 403 instead.The conditional here is correct, though. For any error other than 404, we return error. In case we get 404, we further check job-attached policies, and only if we cannot find any, we say: no acl token and no policies attached.
Does that make sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what exactly I was thinking back then is lost to the mists of time, alas.
but, while I'm here comparing the two error messages from a bad, but valid (uuid), ACL token:
nomad release 1.10 is clearly about one thing:
this pr:
the new one seems to me like
acl token self
is only for workload ID, because I don't parse "or ACL policies attached to a workload identity" as a separate item? I get lost grammatically somewhere along the way before hitting "found" and I parse it as "No ... workload identity found."this might be resolved for me with a single character:
for bonus clarity, if a bit choppy in rhythm:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
56dfa5f