-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…50029) * Add basic tctl commands for WorkloadIdentity resource kind * Add `tctl workload-identity ls` * Add `tctl workload-identity ls` * Fix message strings to be consistent * Add tests for `tctl workload-identity` functionality
- Loading branch information
1 parent
624e259
commit 7a51453
Showing
10 changed files
with
441 additions
and
0 deletions.
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.