-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
233 lines (218 loc) · 5.65 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
Copyright © 2021-2022 Manetu Inc. All Rights Reserved.
*/
package main
import (
"fmt"
"os"
"syscall"
"github.com/urfave/cli/v2" // imports as package "cli"
"golang.org/x/crypto/ssh/terminal"
st "github.com/manetu/security-token/core"
"github.com/manetu/security-token/version"
)
func main() {
defer func() {
if r := recover(); r != nil {
_, _ = fmt.Fprint(os.Stderr, "ERROR: ", r)
}
}()
ctx := st.New()
defer func() {
_ = ctx.Close()
}()
var (
url string
insecure bool
audience string
)
app := &cli.App{
EnableBashCompletion: true,
Commands: []*cli.Command{
{
Name: "version",
Usage: "Report version and build information",
Action: func(c *cli.Context) error {
fmt.Printf("manetu-security-token, git %s, goVersion %s, buildDate %s\n", version.GitCommit, version.GoVersion, version.BuildDate)
return nil
},
},
{
Name: "generate",
Usage: "Generate a new security token",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "realm",
Usage: "Set the realm id",
EnvVars: []string{"MANETU_REALM"},
Required: true,
},
},
Action: func(c *cli.Context) error {
realm := c.String("realm")
cert, err := ctx.Generate(realm)
if err != nil {
return fmt.Errorf("error during generate: %v", err)
}
fmt.Fprintf(os.Stderr, "Serial: %s\n", st.HexEncode(cert.SerialNumber.Bytes()))
fmt.Fprintf(os.Stderr, "MRN: %s\n", st.ComputeMRN(cert))
fmt.Printf("%s\n", st.ExportCert(cert))
return nil
},
},
{
Name: "show",
Usage: "Display the PEM encoded x509 public key for the specified security token",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "serial",
Usage: "Security token serial number",
},
},
Action: func(c *cli.Context) error {
err := ctx.Show(c.String("serial"))
if err != nil {
return fmt.Errorf("error during show: %v", err)
}
return nil
},
},
{
Name: "list",
Usage: "Enumerate available security tokens",
Action: func(c *cli.Context) error {
err := ctx.List()
if err != nil {
return fmt.Errorf("error during list: %v", err)
}
return nil
},
},
{
Name: "delete",
Usage: "Remove a security token",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "serial",
Usage: "Security token serial number",
Required: true,
},
},
Action: func(c *cli.Context) error {
err := ctx.Delete(c.String("serial"))
if err != nil {
return fmt.Errorf("error during delete: %v", err)
}
return nil
},
},
{
Name: "login",
Usage: "Acquires an access token from a security token",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
Usage: "The URL of the Manetu endpoint",
EnvVars: []string{"MANETU_URL"},
Destination: &url,
},
&cli.BoolFlag{
Name: "insecure",
Usage: "Allow insecure TLS",
EnvVars: []string{"MANETU_INSECURE"},
Destination: &insecure,
},
&cli.StringFlag{
Name: "audience",
Usage: "Override the audience claim",
EnvVars: []string{"MANETU_AUDIENCE"},
Destination: &audience,
},
},
Subcommands: []*cli.Command{
{
Name: "hsm",
Usage: "HSM based login",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "serial",
Usage: "HSM serial number",
},
},
Action: func(c *cli.Context) error {
jwt, err := ctx.LoginPKCS11(url, insecure, audience, c.String("serial"))
if err != nil {
return fmt.Errorf("error during HSM login: %v", err)
}
fmt.Printf("%s\n", jwt)
return nil
},
},
{
Name: "pem",
Usage: "non-HSM protected PEM encoded certificate and key-pair",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "key",
Usage: "X509 Key (or path)",
},
&cli.StringFlag{
Name: "cert",
Usage: "X509 cert (or path)",
},
&cli.BoolFlag{
Name: "path",
Usage: "Treat key/cert parameters as paths",
},
&cli.StringFlag{
Name: "p12",
Usage: "PKCS12 Bundled Key/Cert (or path)",
},
&cli.StringFlag{
Name: "password",
Usage: "Password for .p12 file (if not passed in, user will be prompted)",
},
},
Action: func(c *cli.Context) error {
if c.String("p12") != "" {
password := c.String("password")
if password == "" {
fmt.Print("Enter password for PKCS#12 file: ")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return fmt.Errorf("error reading password: %v", err)
}
password = string(bytePassword)
fmt.Println()
}
jwt, err := ctx.LoginPKCS12(url, insecure, audience, c.String("p12"), password, c.Bool("path"))
if err != nil {
return fmt.Errorf("error during PKCS#12 login: %v", err)
}
fmt.Printf("%s\n", jwt)
return nil
}
key := c.String("key")
cert := c.String("cert")
if key == "" || cert == "" {
return fmt.Errorf("both key and cert must be provided for PEM login")
}
jwt, err := ctx.LoginX509(url, insecure, audience, key, cert, c.Bool("path"))
if err != nil {
return fmt.Errorf("error during PEM login: %v", err)
}
fmt.Printf("%s\n", jwt)
return nil
},
},
},
},
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(-1)
}
os.Exit(0)
}