-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add basic tctl commands for WorkloadIdentity resource kind #49828
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9d96c83
Add basic tctl commands for WorkloadIdentity resource kind
strideynet 15f06a5
Add `tctl workload-identity ls`
strideynet b9bb28f
Add `tctl workload-identity ls`
strideynet 40bf610
Fix message strings to be consistent
strideynet e3a074d
Add tests for `tctl workload-identity` functionality
strideynet 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 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 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 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 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 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
4 changes: 4 additions & 0 deletions
4
tool/tctl/common/testdata/TestWorkloadIdentity/workload-identity_ls.golden
This file contains 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,4 @@ | ||
Name SPIFFE ID | ||
---- --------- | ||
test /test | ||
|
1 change: 1 addition & 0 deletions
1
tool/tctl/common/testdata/TestWorkloadIdentity/workload-identity_ls_empty.golden
This file contains 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 @@ | ||
No workload identities configured |
1 change: 1 addition & 0 deletions
1
tool/tctl/common/testdata/TestWorkloadIdentity/workload-identity_rm.golden
This file contains 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 @@ | ||
Workload Identity "test" deleted successfully. | ||
This file contains 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,164 @@ | ||
// Teleport | ||
// Copyright (C) 2024 Gravitational, Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package common | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/alecthomas/kingpin/v2" | ||
"github.com/gravitational/trace" | ||
|
||
"github.com/gravitational/teleport" | ||
workloadidentityv1pb "github.com/gravitational/teleport/api/gen/proto/go/teleport/workloadidentity/v1" | ||
"github.com/gravitational/teleport/lib/asciitable" | ||
"github.com/gravitational/teleport/lib/auth/authclient" | ||
"github.com/gravitational/teleport/lib/service/servicecfg" | ||
"github.com/gravitational/teleport/lib/utils" | ||
) | ||
|
||
// WorkloadIdentityCommand is a group of commands pertaining to Teleport | ||
// Workload Identity. | ||
type WorkloadIdentityCommand struct { | ||
format string | ||
workloadIdentityName string | ||
|
||
listCmd *kingpin.CmdClause | ||
rmCmd *kingpin.CmdClause | ||
|
||
stdout io.Writer | ||
} | ||
|
||
// Initialize sets up the "tctl workload-identity" command. | ||
func (c *WorkloadIdentityCommand) Initialize( | ||
app *kingpin.Application, config *servicecfg.Config, | ||
) { | ||
// TODO(noah): Remove the hidden flag once base functionality is released. | ||
cmd := app.Command( | ||
"workload-identity", | ||
"Manage Teleport Workload Identity.", | ||
).Hidden() | ||
|
||
c.listCmd = cmd.Command( | ||
"ls", | ||
"List workload identity configurations.", | ||
) | ||
c.listCmd. | ||
Flag( | ||
"format", | ||
"Output format, 'text' or 'json'", | ||
). | ||
Hidden(). | ||
Default(teleport.Text). | ||
EnumVar(&c.format, teleport.Text, teleport.JSON) | ||
|
||
c.rmCmd = cmd.Command( | ||
"rm", | ||
"Delete a workload identity configuration.", | ||
) | ||
c.rmCmd. | ||
Arg("name", "Name of the workload identity configuration to delete."). | ||
Required(). | ||
StringVar(&c.workloadIdentityName) | ||
|
||
if c.stdout == nil { | ||
c.stdout = os.Stdout | ||
} | ||
} | ||
|
||
// TryRun attempts to run subcommands. | ||
func (c *WorkloadIdentityCommand) TryRun( | ||
ctx context.Context, cmd string, client *authclient.Client, | ||
) (match bool, err error) { | ||
switch cmd { | ||
case c.listCmd.FullCommand(): | ||
err = c.ListWorkloadIdentities(ctx, client) | ||
case c.rmCmd.FullCommand(): | ||
err = c.DeleteWorkloadIdentity(ctx, client) | ||
default: | ||
return false, nil | ||
} | ||
|
||
return true, trace.Wrap(err) | ||
} | ||
|
||
func (c *WorkloadIdentityCommand) DeleteWorkloadIdentity( | ||
ctx context.Context, | ||
client *authclient.Client, | ||
) error { | ||
workloadIdentityClient := client.WorkloadIdentityResourceServiceClient() | ||
_, err := workloadIdentityClient.DeleteWorkloadIdentity( | ||
ctx, &workloadidentityv1pb.DeleteWorkloadIdentityRequest{ | ||
Name: c.workloadIdentityName, | ||
}) | ||
if err != nil { | ||
return trace.Wrap(err) | ||
} | ||
|
||
fmt.Fprintf( | ||
c.stdout, | ||
"Workload Identity %q deleted successfully.\n", | ||
c.workloadIdentityName, | ||
) | ||
|
||
return nil | ||
} | ||
|
||
// ListWorkloadIdentities writes a listing of the WorkloadIdentity resources | ||
func (c *WorkloadIdentityCommand) ListWorkloadIdentities( | ||
ctx context.Context, client *authclient.Client, | ||
) error { | ||
workloadIdentityClient := client.WorkloadIdentityResourceServiceClient() | ||
var workloadIdentities []*workloadidentityv1pb.WorkloadIdentity | ||
req := &workloadidentityv1pb.ListWorkloadIdentitiesRequest{} | ||
for { | ||
resp, err := workloadIdentityClient.ListWorkloadIdentities(ctx, req) | ||
if err != nil { | ||
return trace.Wrap(err) | ||
} | ||
|
||
workloadIdentities = append( | ||
workloadIdentities, resp.WorkloadIdentities..., | ||
) | ||
if resp.NextPageToken == "" { | ||
break | ||
} | ||
req.PageToken = resp.NextPageToken | ||
} | ||
|
||
if c.format == teleport.Text { | ||
if len(workloadIdentities) == 0 { | ||
fmt.Fprintln(c.stdout, "No workload identities configured") | ||
return nil | ||
} | ||
t := asciitable.MakeTable([]string{"Name", "SPIFFE ID"}) | ||
for _, u := range workloadIdentities { | ||
t.AddRow([]string{ | ||
u.GetMetadata().GetName(), u.GetSpec().GetSpiffe().GetId(), | ||
}) | ||
} | ||
fmt.Fprintln(c.stdout, t.AsBuffer().String()) | ||
} else { | ||
err := utils.WriteJSONArray(c.stdout, workloadIdentities) | ||
if err != nil { | ||
return trace.Wrap(err, "failed to marshal workload identities") | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
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.
ive never heard of this
.golden
file extension in my life. interesting to see it and all over the code base too