From de646865cdee3dd43f6d126a753e73fd4289ff2a Mon Sep 17 00:00:00 2001 From: Noah Stride Date: Wed, 9 Oct 2024 17:21:35 +0100 Subject: [PATCH] Add ability to List/Get AMRs to `tctl` (#47388) --- tool/tctl/common/collection.go | 30 ++++++++++++++++++++++++++++ tool/tctl/common/resource_command.go | 24 ++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/tool/tctl/common/collection.go b/tool/tctl/common/collection.go index b70c51d0d6be1..d74bd31ceba2f 100644 --- a/tool/tctl/common/collection.go +++ b/tool/tctl/common/collection.go @@ -31,6 +31,7 @@ import ( "github.com/gravitational/trace" "github.com/gravitational/teleport/api/constants" + accessmonitoringrulesv1pb "github.com/gravitational/teleport/api/gen/proto/go/teleport/accessmonitoringrules/v1" autoupdatev1pb "github.com/gravitational/teleport/api/gen/proto/go/teleport/autoupdate/v1" crownjewelv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/crownjewel/v1" dbobjectv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/dbobject/v1" @@ -1864,3 +1865,32 @@ func (c *autoUpdateVersionCollection) writeText(w io.Writer, verbose bool) error _, err := t.AsBuffer().WriteTo(w) return trace.Wrap(err) } + +type accessMonitoringRuleCollection struct { + items []*accessmonitoringrulesv1pb.AccessMonitoringRule +} + +func (c *accessMonitoringRuleCollection) resources() []types.Resource { + r := make([]types.Resource, 0, len(c.items)) + for _, resource := range c.items { + r = append(r, types.Resource153ToLegacy(resource)) + } + return r +} + +// writeText formats the user tasks into a table and writes them into w. +// If verbose is disabled, labels column can be truncated to fit into the console. +func (c *accessMonitoringRuleCollection) writeText(w io.Writer, verbose bool) error { + var rows [][]string + for _, item := range c.items { + labels := common.FormatLabels(item.GetMetadata().GetLabels(), verbose) + rows = append(rows, []string{item.Metadata.GetName(), labels}) + } + headers := []string{"Name", "Labels"} + t := asciitable.MakeTable(headers, rows...) + + // stable sort by name. + t.SortRowsBy([]int{0}, true) + _, err := t.AsBuffer().WriteTo(w) + return trace.Wrap(err) +} diff --git a/tool/tctl/common/resource_command.go b/tool/tctl/common/resource_command.go index 71e1ad1cf09d6..7975a80a7298f 100644 --- a/tool/tctl/common/resource_command.go +++ b/tool/tctl/common/resource_command.go @@ -42,6 +42,7 @@ import ( apiclient "github.com/gravitational/teleport/api/client" "github.com/gravitational/teleport/api/client/proto" apidefaults "github.com/gravitational/teleport/api/defaults" + accessmonitoringrulesv1pb "github.com/gravitational/teleport/api/gen/proto/go/teleport/accessmonitoringrules/v1" autoupdatev1pb "github.com/gravitational/teleport/api/gen/proto/go/teleport/autoupdate/v1" clusterconfigpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/clusterconfig/v1" crownjewelv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/crownjewel/v1" @@ -3080,6 +3081,29 @@ func (rc *ResourceCommand) getCollection(ctx context.Context, client *authclient return nil, trace.Wrap(err) } return &autoUpdateVersionCollection{version}, nil + case types.KindAccessMonitoringRule: + if rc.ref.Name != "" { + rule, err := client.AccessMonitoringRuleClient().GetAccessMonitoringRule(ctx, rc.ref.Name) + if err != nil { + return nil, trace.Wrap(err) + } + return &accessMonitoringRuleCollection{items: []*accessmonitoringrulesv1pb.AccessMonitoringRule{rule}}, nil + } + + var rules []*accessmonitoringrulesv1pb.AccessMonitoringRule + nextToken := "" + for { + resp, token, err := client.AccessMonitoringRuleClient().ListAccessMonitoringRules(ctx, 0, nextToken) + if err != nil { + return nil, trace.Wrap(err) + } + rules = append(rules, resp...) + if token == "" { + break + } + nextToken = token + } + return &accessMonitoringRuleCollection{items: rules}, nil } return nil, trace.BadParameter("getting %q is not supported", rc.ref.String()) }