Files
teleport/lib/tbot/cli/start_workload_identity_api.go
T
rosstimothy 58126ad723 Remove lib/client as a dependency of lib/config (#67257)
Relocates client.ParseLabelSpec to lib/utils/parse. This results in
a net 5MB reduction in the weight of lib/config. This may not
have a correlation in binary size since lib/client is heavily consumed
by other packages.

```bash
$ goda cut './lib/config:all' | rg '^github.com/gravitational/teleport/lib/client\b|^ID'
github.com/gravitational/teleport/lib/client   InDegree=1   Cut.PackageCount=75   Cut.AllFiles.Size=5.3MB   Cut.Go.Lines=129512
```
2026-05-29 15:42:50 +00:00

108 lines
3.6 KiB
Go

// Teleport
// Copyright (C) 2025 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 cli
import (
"fmt"
"log/slog"
"github.com/alecthomas/kingpin/v2"
"github.com/gravitational/trace"
"github.com/gravitational/teleport/lib/tbot/config"
"github.com/gravitational/teleport/lib/tbot/services/workloadidentity"
"github.com/gravitational/teleport/lib/utils/parse"
)
// WorkloadIdentityAPICommand implements `tbot start workload-identity-api` and
// `tbot configure workload-identity-api`.
type WorkloadIdentityAPICommand struct {
*sharedStartArgs
*genericMutatorHandler
// Listen configures where the workload identity API should listen. This
// should be prefixed with a scheme e.g unix:// or tcp://.
Listen string
// NameSelector is the name of the workload identity to use.
// --name-selector foo
NameSelector string
// LabelSelector is the labels of the workload identity to use.
// --label-selector x=y,z=a
LabelSelector string
}
// NewWorkloadIdentityAPICommand initializes the command and flags for the
// `workload-identity-api` service and returns a struct that will contain the
// parse result.
func NewWorkloadIdentityAPICommand(parentCmd *kingpin.CmdClause, action MutatorAction, mode CommandMode) *WorkloadIdentityAPICommand {
cmd := parentCmd.Command(
"workload-identity-api",
fmt.Sprintf("%s tbot with a workload identity API listener. Compatible with the SPIFFE Workload API and Envoy SDS.", mode),
)
c := &WorkloadIdentityAPICommand{}
c.sharedStartArgs = newSharedStartArgs(cmd)
c.genericMutatorHandler = newGenericMutatorHandler(cmd, c, action)
cmd.Flag(
"name-selector",
"The name of the workload identity to issue. Mutually exclusive with --label-selector.",
).StringVar(&c.NameSelector)
cmd.Flag(
"label-selector",
"A label-based selector for which workload identities to issue. Multiple labels can be provided using ','. Mutually exclusive with --name-selector.",
).StringVar(&c.LabelSelector)
cmd.Flag(
"listen",
"The address on which the workload identity API should listen. This should either be prefixed with 'unix://' or 'tcp://'.",
).Required().StringVar(&c.Listen)
return c
}
func (c *WorkloadIdentityAPICommand) ApplyConfig(cfg *config.BotConfig, l *slog.Logger) error {
if err := c.sharedStartArgs.ApplyConfig(cfg, l); err != nil {
return trace.Wrap(err)
}
svc := &workloadidentity.WorkloadAPIConfig{
Listen: c.Listen,
}
switch {
case c.NameSelector != "" && c.LabelSelector != "":
return trace.BadParameter("name-selector and label-selector flags are mutually exclusive")
case c.NameSelector != "":
svc.Selector.Name = c.NameSelector
case c.LabelSelector != "":
labels, err := parse.LabelSelectorSpec(c.LabelSelector)
if err != nil {
return trace.Wrap(err, "parsing label-selector")
}
svc.Selector.Labels = map[string][]string{}
for k, v := range labels {
svc.Selector.Labels[k] = []string{v}
}
default:
return trace.BadParameter("name-selector and label-selector must be specified")
}
cfg.Services = append(cfg.Services, svc)
return nil
}