/
auth.go
120 lines (106 loc) · 2.85 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package plural
import (
"fmt"
"strings"
"github.com/pluralsh/gqlclient"
"github.com/urfave/cli"
"github.com/pluralsh/plural-cli/pkg/config"
"github.com/pluralsh/plural-cli/pkg/utils"
)
func (p *Plural) authCommands() []cli.Command {
return []cli.Command{
{
Name: "oidc",
ArgsUsage: "PROVIDER",
Usage: "logs in using an exchange from a given oidc id token",
Flags: []cli.Flag{
cli.StringFlag{
Name: "token",
Usage: "the oidc id token to use",
},
cli.StringFlag{
Name: "email",
Usage: "the plural email you want to log in as",
},
},
Action: p.handleOidcToken,
},
{
Name: "trust",
Usage: "commands to manage oidc trust relationships",
Subcommands: p.trustCommands(),
},
}
}
func (p *Plural) trustCommands() []cli.Command {
return []cli.Command{
{
Name: "list",
Usage: "lists all trust relationships attached to the current user",
Action: p.handleListTrusts,
},
{
Name: "create",
Flags: []cli.Flag{
cli.StringFlag{
Name: "issuer",
Usage: "the oidc issuer for this trust relationship",
},
cli.StringFlag{
Name: "trust",
Usage: "a regex to establish the claims to trust from this issuer, eg {repo-name}:{ref}:{workflow} for github actions",
},
},
Action: p.handleCreateTrust,
},
{
Name: "delete",
ArgsUsage: "ID",
Usage: "deletes an existing oidc trust relationship by id",
Action: p.handleDeleteTrust,
},
}
}
func (p *Plural) handleListTrusts(c *cli.Context) error {
p.InitPluralClient()
me, err := p.Client.Me()
if err != nil {
return err
}
headers := []string{"ID", "Issuer", "Trust", "Created On"}
return utils.PrintTable(me.TrustRelationships, headers, func(t *gqlclient.OidcTrustRelationshipFragment) ([]string, error) {
return []string{t.ID, t.Issuer, t.Trust, *t.InsertedAt}, nil
})
}
func (p *Plural) handleCreateTrust(c *cli.Context) error {
trustShortcuts := map[string]string{
"github_actions": "https://token.actions.githubusercontent.com",
}
p.InitPluralClient()
issuer, trust := c.String("issuer"), c.String("trust")
if val, ok := trustShortcuts[issuer]; ok {
issuer = val
}
return p.Client.CreateTrust(issuer, trust)
}
func (p *Plural) handleDeleteTrust(c *cli.Context) error {
p.InitPluralClient()
id := c.Args().Get(0)
return p.Client.DeleteTrust(id)
}
func (p *Plural) handleOidcToken(c *cli.Context) error {
p.InitPluralClient()
prov := c.Args().Get(0)
token, email := c.String("token"), c.String("email")
provider := gqlclient.ExternalOidcProvider(strings.ToUpper(prov))
if !provider.IsValid() {
return fmt.Errorf("Invalid oidc provider %s", prov)
}
token, err := p.Client.OidcToken(provider, token, email)
if err != nil {
return err
}
conf := config.Config{Token: token, Email: email}
utils.Success("Logged in as %s", email)
return conf.Flush()
}