Skip to content

Commit 5715589

Browse files
VioletHynesGabrielopesantos
authored andcommitted
VAULT-5885: Fix erroneous success message in case of two-phase MFA, and provide MFA information in table format (hashicorp#15428)
* VAULT-5885: Fix erroneous success message in case of two-phase MFA, and provide MFA information in table format * VAULT-5885 Add changelog * VAULT-5885 Update changelog as per PR comments * VAULT-5885 Update changelog category to just 'auth' * VAULT-5885 Hide useless token info in two-phase MFA case * VAULT-5885 Update changelog to reflect token info now no longer present * VAULT-5885 split up changelog into three blocks
1 parent 1633bc1 commit 5715589

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

changelog/15428.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
```release-note:bug
2+
auth: Fixed erroneous success message when using vault login in case of two-phase MFA
3+
```
4+
```release-note:bug
5+
auth: Fixed erroneous token information being displayed when using vault login in case of two-phase MFA
6+
```
7+
```release-note:bug
8+
auth: Fixed two-phase MFA information missing from table format when using vault login
9+
```

command/format.go

+26-15
Original file line numberDiff line numberDiff line change
@@ -385,21 +385,32 @@ func (t TableFormatter) OutputSecret(ui cli.Ui, secret *api.Secret) error {
385385
}
386386

387387
if secret.Auth != nil {
388-
out = append(out, fmt.Sprintf("token %s %s", hopeDelim, secret.Auth.ClientToken))
389-
out = append(out, fmt.Sprintf("token_accessor %s %s", hopeDelim, secret.Auth.Accessor))
390-
// If the lease duration is 0, it's likely a root token, so output the
391-
// duration as "infinity" to clear things up.
392-
if secret.Auth.LeaseDuration == 0 {
393-
out = append(out, fmt.Sprintf("token_duration %s %s", hopeDelim, "∞"))
394-
} else {
395-
out = append(out, fmt.Sprintf("token_duration %s %v", hopeDelim, humanDurationInt(secret.Auth.LeaseDuration)))
396-
}
397-
out = append(out, fmt.Sprintf("token_renewable %s %t", hopeDelim, secret.Auth.Renewable))
398-
out = append(out, fmt.Sprintf("token_policies %s %q", hopeDelim, secret.Auth.TokenPolicies))
399-
out = append(out, fmt.Sprintf("identity_policies %s %q", hopeDelim, secret.Auth.IdentityPolicies))
400-
out = append(out, fmt.Sprintf("policies %s %q", hopeDelim, secret.Auth.Policies))
401-
for k, v := range secret.Auth.Metadata {
402-
out = append(out, fmt.Sprintf("token_meta_%s %s %v", k, hopeDelim, v))
388+
if secret.Auth.MFARequirement != nil {
389+
out = append(out, fmt.Sprintf("mfa_request_id %s %s", hopeDelim, secret.Auth.MFARequirement.MFARequestID))
390+
391+
for k, constraintSet := range secret.Auth.MFARequirement.MFAConstraints {
392+
for _, constraint := range constraintSet.Any {
393+
out = append(out, fmt.Sprintf("mfa_constraint_%s_%s_id %s %s", k, constraint.Type, hopeDelim, constraint.ID))
394+
out = append(out, fmt.Sprintf("mfa_constraint_%s_%s_uses_passcode %s %t", k, constraint.Type, hopeDelim, constraint.UsesPasscode))
395+
}
396+
}
397+
} else { // Token information only makes sense if no further MFA requirement (i.e. if we actually have a token)
398+
out = append(out, fmt.Sprintf("token %s %s", hopeDelim, secret.Auth.ClientToken))
399+
out = append(out, fmt.Sprintf("token_accessor %s %s", hopeDelim, secret.Auth.Accessor))
400+
// If the lease duration is 0, it's likely a root token, so output the
401+
// duration as "infinity" to clear things up.
402+
if secret.Auth.LeaseDuration == 0 {
403+
out = append(out, fmt.Sprintf("token_duration %s %s", hopeDelim, "∞"))
404+
} else {
405+
out = append(out, fmt.Sprintf("token_duration %s %v", hopeDelim, humanDurationInt(secret.Auth.LeaseDuration)))
406+
}
407+
out = append(out, fmt.Sprintf("token_renewable %s %t", hopeDelim, secret.Auth.Renewable))
408+
out = append(out, fmt.Sprintf("token_policies %s %q", hopeDelim, secret.Auth.TokenPolicies))
409+
out = append(out, fmt.Sprintf("identity_policies %s %q", hopeDelim, secret.Auth.IdentityPolicies))
410+
out = append(out, fmt.Sprintf("policies %s %q", hopeDelim, secret.Auth.Policies))
411+
for k, v := range secret.Auth.Metadata {
412+
out = append(out, fmt.Sprintf("token_meta_%s %s %v", k, hopeDelim, v))
413+
}
403414
}
404415
}
405416

command/login.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ func (c *LoginCommand) Run(args []string) int {
240240
c.UI.Warn(wrapAtLength("A login request was issued that is subject to "+
241241
"MFA validation. Please make sure to validate the login by sending another "+
242242
"request to sys/mfa/validate endpoint.") + "\n")
243+
244+
// We return early to prevent success message from being printed
245+
c.checkForAndWarnAboutLoginToken()
246+
return OutputSecret(c.UI, secret)
243247
}
244248

245249
// Unset any previous token wrapping functionality. If the original request
@@ -302,15 +306,7 @@ func (c *LoginCommand) Run(args []string) int {
302306
return 2
303307
}
304308

305-
// Warn if the VAULT_TOKEN environment variable is set, as that will take
306-
// precedence. We output as a warning, so piping should still work since it
307-
// will be on a different stream.
308-
if os.Getenv("VAULT_TOKEN") != "" {
309-
c.UI.Warn(wrapAtLength("WARNING! The VAULT_TOKEN environment variable "+
310-
"is set! This takes precedence over the value set by this command. To "+
311-
"use the value set by this command, unset the VAULT_TOKEN environment "+
312-
"variable or set it to the token displayed below.") + "\n")
313-
}
309+
c.checkForAndWarnAboutLoginToken()
314310
} else if !c.flagTokenOnly {
315311
// If token-only the user knows it won't be stored, so don't warn
316312
c.UI.Warn(wrapAtLength(
@@ -372,3 +368,14 @@ func (c *LoginCommand) extractToken(client *api.Client, secret *api.Secret, unwr
372368
return nil, false, fmt.Errorf("no auth or wrapping info in response")
373369
}
374370
}
371+
372+
// Warn if the VAULT_TOKEN environment variable is set, as that will take
373+
// precedence. We output as a warning, so piping should still work since it
374+
// will be on a different stream.
375+
func (c *LoginCommand) checkForAndWarnAboutLoginToken() {
376+
if os.Getenv("VAULT_TOKEN") != "" {
377+
c.UI.Warn(wrapAtLength("WARNING! The VAULT_TOKEN environment variable "+
378+
"is set! The value of this variable will take precedence; if this is unwanted "+
379+
"please unset VAULT_TOKEN or update its value accordingly.") + "\n")
380+
}
381+
}

0 commit comments

Comments
 (0)