forked from hashicorp/vault-plugin-auth-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
330 lines (271 loc) · 8.62 KB
/
cli.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package jwtauth
import (
"errors"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"path"
"regexp"
"strconv"
"strings"
"time"
"github.com/hashicorp/cap/util"
"github.com/hashicorp/go-secure-stdlib/base62"
"github.com/hashicorp/vault/api"
)
const (
defaultMount = "oidc"
defaultListenAddress = "localhost"
defaultPort = "8250"
defaultCallbackHost = "localhost"
defaultCallbackMethod = "http"
FieldCallbackHost = "callbackhost"
FieldCallbackMethod = "callbackmethod"
FieldListenAddress = "listenaddress"
FieldPort = "port"
FieldCallbackPort = "callbackport"
FieldSkipBrowser = "skip_browser"
FieldAbortOnError = "abort_on_error"
)
var errorRegex = regexp.MustCompile(`(?s)Errors:.*\* *(.*)`)
type CLIHandler struct{}
// loginResp implements vault's command.LoginHandler interface, but we do not check
// the implementation as that'd cause an import loop.
type loginResp struct {
secret *api.Secret
err error
}
func (h *CLIHandler) Auth(c *api.Client, m map[string]string) (*api.Secret, error) {
// handle ctrl-c while waiting for the callback
sigintCh := make(chan os.Signal, 1)
signal.Notify(sigintCh, authHalts...)
defer signal.Stop(sigintCh)
mount, ok := m["mount"]
if !ok {
mount = defaultMount
}
listenAddress, ok := m[FieldListenAddress]
if !ok {
listenAddress = defaultListenAddress
}
port, ok := m[FieldPort]
if !ok {
port = defaultPort
}
callbackHost, ok := m[FieldCallbackHost]
if !ok {
callbackHost = defaultCallbackHost
}
callbackMethod, ok := m[FieldCallbackMethod]
if !ok {
callbackMethod = defaultCallbackMethod
}
callbackPort, ok := m[FieldCallbackPort]
if !ok {
callbackPort = port
}
parseBool := func(f string, d bool) (bool, error) {
s, ok := m[f]
if !ok {
return d, nil
}
v, err := strconv.ParseBool(s)
if err != nil {
return false, fmt.Errorf(
"failed to parse value for %q, err=%w", f, err)
}
return v, nil
}
var skipBrowserLaunch bool
if v, err := parseBool(FieldSkipBrowser, false); err != nil {
return nil, err
} else {
skipBrowserLaunch = v
}
var abortOnError bool
if v, err := parseBool(FieldAbortOnError, false); err != nil {
return nil, err
} else {
abortOnError = v
}
role := m["role"]
authURL, clientNonce, err := fetchAuthURL(c, role, mount, callbackPort, callbackMethod, callbackHost)
if err != nil {
return nil, err
}
// Set up callback handler
doneCh := make(chan loginResp)
http.HandleFunc("/oidc/callback", callbackHandler(c, mount, clientNonce, doneCh))
listener, err := net.Listen("tcp", listenAddress+":"+port)
if err != nil {
return nil, err
}
defer listener.Close()
// Open the default browser to the callback URL.
if !skipBrowserLaunch {
fmt.Fprintf(os.Stderr, "Complete the login via your OIDC provider. Launching browser to:\n\n %s\n\n\n", authURL)
if err := util.OpenURL(authURL); err != nil {
if abortOnError {
return nil, fmt.Errorf("failed to launch the browser %s=%t, err=%w", FieldAbortOnError, abortOnError, err)
}
fmt.Fprintf(os.Stderr, "Error attempting to automatically open browser: '%s'.\nPlease visit the authorization URL manually.", err)
}
} else {
fmt.Fprintf(os.Stderr, "Complete the login via your OIDC provider. Open the following link in your browser:\n\n %s\n\n\n", authURL)
}
fmt.Fprintf(os.Stderr, "Waiting for OIDC authentication to complete...\n")
// Start local server
go func() {
err := http.Serve(listener, nil)
if err != nil && err != http.ErrServerClosed {
doneCh <- loginResp{nil, err}
}
}()
// Wait for either the callback to finish, or a halt signal (e.g., SIGKILL, SIGINT, SIGTSTP) to be received or up to 2 minutes
select {
case s := <-doneCh:
return s.secret, s.err
case <-sigintCh:
return nil, errors.New("Interrupted")
case <-time.After(2 * time.Minute):
return nil, errors.New("Timed out waiting for response from provider")
}
}
func callbackHandler(c *api.Client, mount string, clientNonce string, doneCh chan<- loginResp) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
var response string
var secret *api.Secret
var err error
defer func() {
w.Write([]byte(response))
doneCh <- loginResp{secret, err}
}()
// Pull any parameters from either the body or query parameters.
// FormValue prioritizes body values, if found.
data := map[string][]string{
"state": {req.FormValue("state")},
"code": {req.FormValue("code")},
"id_token": {req.FormValue("id_token")},
"client_nonce": {clientNonce},
}
// If this is a POST, then the form_post response_mode is being used and the flow
// involves an extra step. First POST the data to Vault, and then issue a GET with
// the same state/code to complete the auth as normal.
if req.Method == http.MethodPost {
url := c.Address() + path.Join("/v1/auth", mount, "oidc/callback")
resp, err := http.PostForm(url, data)
if err != nil {
summary, detail := parseError(err)
response = errorHTML(summary, detail)
return
}
defer resp.Body.Close()
// An id_token will never be part of a redirect GET, so remove it here too.
delete(data, "id_token")
}
secret, err = c.Logical().ReadWithData(fmt.Sprintf("auth/%s/oidc/callback", mount), data)
if err != nil {
summary, detail := parseError(err)
response = errorHTML(summary, detail)
} else {
response = successHTML
}
}
}
func fetchAuthURL(c *api.Client, role, mount, callbackPort string, callbackMethod string, callbackHost string) (string, string, error) {
var authURL string
clientNonce, err := base62.Random(20)
if err != nil {
return "", "", err
}
redirectURI := fmt.Sprintf("%s://%s:%s/oidc/callback", callbackMethod, callbackHost, callbackPort)
data := map[string]interface{}{
"role": role,
"redirect_uri": redirectURI,
"client_nonce": clientNonce,
}
secret, err := c.Logical().Write(fmt.Sprintf("auth/%s/oidc/auth_url", mount), data)
if err != nil {
return "", "", err
}
if secret != nil {
authURL = secret.Data["auth_url"].(string)
}
if authURL == "" {
return "", "", fmt.Errorf("Unable to authorize role %q with redirect_uri %q. Check Vault logs for more information.", role, redirectURI)
}
return authURL, clientNonce, nil
}
// parseError converts error from the API into summary and detailed portions.
// This is used to present a nicer UI by splitting up *known* prefix sentences
// from the rest of the text. e.g.
//
// "No response from provider. Gateway timeout from upstream proxy."
//
// becomes:
//
// "No response from provider.", "Gateway timeout from upstream proxy."
func parseError(err error) (string, string) {
headers := []string{errNoResponse, errLoginFailed, errTokenVerification}
summary := "Login error"
detail := ""
errorParts := errorRegex.FindStringSubmatch(err.Error())
switch len(errorParts) {
case 0:
summary = ""
case 1:
detail = errorParts[0]
case 2:
for _, h := range headers {
if strings.HasPrefix(errorParts[1], h) {
summary = h
detail = strings.TrimSpace(errorParts[1][len(h):])
break
}
}
if detail == "" {
detail = errorParts[1]
}
}
return summary, detail
}
// Help method for OIDC cli
func (h *CLIHandler) Help() string {
help := fmt.Sprintf(`
Usage: vault login -method=oidc [CONFIG K=V...]
The OIDC auth method allows users to authenticate using an OIDC provider.
The provider must be configured as part of a role by the operator.
Authenticate using role "engineering":
$ vault login -method=oidc role=engineering
Complete the login via your OIDC provider. Launching browser to:
https://accounts.google.com/o/oauth2/v2/...
The default browser will be opened for the user to complete the login. Alternatively,
the user may visit the provided URL directly.
Configuration:
role=<string>
Vault role of type "OIDC" to use for authentication.
%s=<string>
Optional address to bind the OIDC callback listener to (default: localhost).
%s=<string>
Optional localhost port to use for OIDC callback (default: 8250).
%s=<string>
Optional method to to use in OIDC redirect_uri (default: http).
%s=<string>
Optional callback host address to use in OIDC redirect_uri (default: localhost).
%s=<string>
Optional port to to use in OIDC redirect_uri (default: the value set for port).
%s=<bool>
Toggle the automatic launching of the default browser to the login URL. (default: false).
%s=<bool>
Abort on any error. (default: false).
`,
FieldListenAddress, FieldPort, FieldCallbackMethod,
FieldCallbackHost, FieldCallbackPort, FieldSkipBrowser,
FieldAbortOnError,
)
return strings.TrimSpace(help)
}