/
gcp.go
72 lines (61 loc) · 1.76 KB
/
gcp.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
package authcmd
import (
"context"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.aporeto.io/a3s/pkgs/authlib"
"go.aporeto.io/a3s/pkgs/permissions"
"go.aporeto.io/a3s/pkgs/token"
"go.aporeto.io/manipulate/manipcli"
)
func makeGCPCmd(mmaker manipcli.ManipulatorMaker, restrictions *permissions.Restrictions) *cobra.Command {
cmd := &cobra.Command{
Use: "gcp",
Short: "Use an GCP identity token.",
TraverseChildren: true,
RunE: func(cmd *cobra.Command, args []string) error {
fToken := viper.GetString("access-token")
fTokenAudience := viper.GetString("token-audience")
fAudience := viper.GetStringSlice("audience")
fCloak := viper.GetStringSlice("cloak")
fQRCode := viper.GetBool("qrcode")
fCheck := viper.GetBool("check")
fValidity := viper.GetDuration("validity")
fRefresh := viper.GetBool("refresh")
m, err := mmaker()
if err != nil {
return err
}
client := authlib.NewClient(m)
t, err := client.AuthFromGCP(
context.Background(),
fToken,
fTokenAudience,
authlib.OptAudience(fAudience...),
authlib.OptCloak(fCloak...),
authlib.OptRestrictions(*restrictions),
authlib.OptValidity(fValidity),
authlib.OptRefresh(fRefresh),
)
if err != nil {
return err
}
return token.Fprint(
os.Stdout,
t,
token.PrintOptionDecoded(fCheck),
token.PrintOptionQRCode(fQRCode),
token.PrintOptionRaw(true),
)
},
}
cmd.Flags().String("access-token", "", "Valid GCP token.")
cmd.Flags().String("token-audience", "a3s", "Required GCP token audience.")
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
_ = cmd.Flags().MarkHidden("namespace")
_ = cmd.Flags().MarkHidden("token")
cmd.Parent().HelpFunc()(cmd, args)
})
return cmd
}