Skip to content

Commit

Permalink
Final sync
Browse files Browse the repository at this point in the history
  • Loading branch information
jefferai committed Oct 23, 2017
1 parent 47e4097 commit cd6d67d
Show file tree
Hide file tree
Showing 27 changed files with 482 additions and 84 deletions.
20 changes: 19 additions & 1 deletion builtin/credential/app-id/path_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,32 @@ func pathLogin(b *backend) *framework.Path {
},

Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathLogin,
logical.UpdateOperation: b.pathLogin,
logical.AliasLookaheadOperation: b.pathLoginAliasLookahead,
},

HelpSynopsis: pathLoginSyn,
HelpDescription: pathLoginDesc,
}
}

func (b *backend) pathLoginAliasLookahead(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appId := data.Get("app_id").(string)

if appId == "" {
return nil, fmt.Errorf("missing app_id")
}

return &logical.Response{
Auth: &logical.Auth{
Alias: &logical.Alias{
Name: appId,
},
},
}, nil
}

func (b *backend) pathLogin(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
appId := data.Get("app_id").(string)
Expand Down
21 changes: 19 additions & 2 deletions builtin/credential/approle/path_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package approle

import (
"fmt"
"strings"
"time"

"github.com/hashicorp/vault/logical"
Expand All @@ -23,17 +24,33 @@ func pathLogin(b *backend) *framework.Path {
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathLoginUpdate,
logical.UpdateOperation: b.pathLoginUpdate,
logical.AliasLookaheadOperation: b.pathLoginUpdateAliasLookahead,
},
HelpSynopsis: pathLoginHelpSys,
HelpDescription: pathLoginHelpDesc,
}
}

func (b *backend) pathLoginUpdateAliasLookahead(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
roleID := strings.TrimSpace(data.Get("role_id").(string))
if roleID == "" {
return nil, fmt.Errorf("missing role_id")
}

return &logical.Response{
Auth: &logical.Auth{
Alias: &logical.Alias{
Name: roleID,
},
},
}, nil
}

// Returns the Auth object indicating the authentication and authorization information
// if the credentials provided are validated by the backend.
func (b *backend) pathLoginUpdate(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
role, roleName, metadata, err := b.validateCredentials(req, data)
role, roleName, metadata, _, err := b.validateCredentials(req, data)
if err != nil || role == nil {
return logical.ErrorResponse(fmt.Sprintf("failed to validate SecretID: %s", err)), nil
}
Expand Down
27 changes: 14 additions & 13 deletions builtin/credential/approle/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,64 +90,65 @@ func (b *backend) validateRoleID(s logical.Storage, roleID string) (*roleStorage
}

// Validates the supplied RoleID and SecretID
func (b *backend) validateCredentials(req *logical.Request, data *framework.FieldData) (*roleStorageEntry, string, map[string]string, error) {
func (b *backend) validateCredentials(req *logical.Request, data *framework.FieldData) (*roleStorageEntry, string, map[string]string, string, error) {
metadata := make(map[string]string)
// RoleID must be supplied during every login
roleID := strings.TrimSpace(data.Get("role_id").(string))
if roleID == "" {
return nil, "", metadata, fmt.Errorf("missing role_id")
return nil, "", metadata, "", fmt.Errorf("missing role_id")
}

// Validate the RoleID and get the Role entry
role, roleName, err := b.validateRoleID(req.Storage, roleID)
if err != nil {
return nil, "", metadata, err
return nil, "", metadata, "", err
}
if role == nil || roleName == "" {
return nil, "", metadata, fmt.Errorf("failed to validate role_id")
return nil, "", metadata, "", fmt.Errorf("failed to validate role_id")
}

// Calculate the TTL boundaries since this reflects the properties of the token issued
if role.TokenTTL, role.TokenMaxTTL, err = b.SanitizeTTL(role.TokenTTL, role.TokenMaxTTL); err != nil {
return nil, "", metadata, err
return nil, "", metadata, "", err
}

var secretID string
if role.BindSecretID {
// If 'bind_secret_id' was set on role, look for the field 'secret_id'
// to be specified and validate it.
secretID := strings.TrimSpace(data.Get("secret_id").(string))
secretID = strings.TrimSpace(data.Get("secret_id").(string))
if secretID == "" {
return nil, "", metadata, fmt.Errorf("missing secret_id")
return nil, "", metadata, "", fmt.Errorf("missing secret_id")
}

// Check if the SecretID supplied is valid. If use limit was specified
// on the SecretID, it will be decremented in this call.
var valid bool
valid, metadata, err = b.validateBindSecretID(req, roleName, secretID, role.HMACKey, role.BoundCIDRList)
if err != nil {
return nil, "", metadata, err
return nil, "", metadata, "", err
}
if !valid {
return nil, "", metadata, fmt.Errorf("invalid secret_id %q", secretID)
return nil, "", metadata, "", fmt.Errorf("invalid secret_id %q", secretID)
}
}

if role.BoundCIDRList != "" {
// If 'bound_cidr_list' was set, verify the CIDR restrictions
if req.Connection == nil || req.Connection.RemoteAddr == "" {
return nil, "", metadata, fmt.Errorf("failed to get connection information")
return nil, "", metadata, "", fmt.Errorf("failed to get connection information")
}

belongs, err := cidrutil.IPBelongsToCIDRBlocksString(req.Connection.RemoteAddr, role.BoundCIDRList, ",")
if err != nil {
return nil, "", metadata, fmt.Errorf("failed to verify the CIDR restrictions set on the role: %v", err)
return nil, "", metadata, "", fmt.Errorf("failed to verify the CIDR restrictions set on the role: %v", err)
}
if !belongs {
return nil, "", metadata, fmt.Errorf("source address %q unauthorized through CIDR restrictions on the role", req.Connection.RemoteAddr)
return nil, "", metadata, "", fmt.Errorf("source address %q unauthorized through CIDR restrictions on the role", req.Connection.RemoteAddr)
}
}

return role, roleName, metadata, nil
return role, roleName, metadata, secretID, nil
}

// validateBindSecretID is used to determine if the given SecretID is a valid one.
Expand Down
26 changes: 25 additions & 1 deletion builtin/credential/aws/path_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ needs to be supplied along with 'identity' parameter.`,
},

Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathLoginUpdate,
logical.UpdateOperation: b.pathLoginUpdate,
logical.AliasLookaheadOperation: b.pathLoginUpdate,
},

HelpSynopsis: pathLoginSyn,
Expand Down Expand Up @@ -546,6 +547,17 @@ func (b *backend) pathLoginUpdateEc2(
}
}

// If we're just looking up for MFA, return the Alias info
if req.Operation == logical.AliasLookaheadOperation {
return &logical.Response{
Auth: &logical.Auth{
Alias: &logical.Alias{
Name: identityDocParsed.InstanceID,
},
},
}, nil
}

roleName := data.Get("role").(string)

// If roleName is not supplied, a role in the name of the instance's AMI ID will be looked for
Expand Down Expand Up @@ -1157,6 +1169,18 @@ func (b *backend) pathLoginUpdateIam(
// This could either be a "userID:SessionID" (in the case of an assumed role) or just a "userID"
// (in the case of an IAM user).
callerUniqueId := strings.Split(callerID.UserId, ":")[0]

// If we're just looking up for MFA, return the Alias info
if req.Operation == logical.AliasLookaheadOperation {
return &logical.Response{
Auth: &logical.Auth{
Alias: &logical.Alias{
Name: callerUniqueId,
},
},
}, nil
}

entity, err := parseIamArn(callerID.Arn)
if err != nil {
return logical.ErrorResponse(fmt.Sprintf("error parsing arn %q: %v", callerID.Arn, err)), nil
Expand Down
19 changes: 18 additions & 1 deletion builtin/credential/cert/path_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,28 @@ func pathLogin(b *backend) *framework.Path {
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathLogin,
logical.UpdateOperation: b.pathLogin,
logical.AliasLookaheadOperation: b.pathLoginAliasLookahead,
},
}
}

func (b *backend) pathLoginAliasLookahead(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
clientCerts := req.Connection.ConnState.PeerCertificates
if len(clientCerts) == 0 {
return nil, fmt.Errorf("no client certificate found")
}

return &logical.Response{
Auth: &logical.Auth{
Alias: &logical.Alias{
Name: clientCerts[0].Subject.CommonName,
},
},
}, nil
}

func (b *backend) pathLogin(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {

Expand Down
26 changes: 24 additions & 2 deletions builtin/credential/github/path_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,36 @@ func pathLogin(b *backend) *framework.Path {
},

Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathLogin,
logical.UpdateOperation: b.pathLogin,
logical.AliasLookaheadOperation: b.pathLoginAliasLookahead,
},
}
}

func (b *backend) pathLogin(
func (b *backend) pathLoginAliasLookahead(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
token := data.Get("token").(string)

var verifyResp *verifyCredentialsResp
if verifyResponse, resp, err := b.verifyCredentials(req, token); err != nil {
return nil, err
} else if resp != nil {
return resp, nil
} else {
verifyResp = verifyResponse
}

return &logical.Response{
Auth: &logical.Auth{
Alias: &logical.Alias{
Name: *verifyResp.User.Login,
},
},
}, nil
}

func (b *backend) pathLogin(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
token := data.Get("token").(string)

var verifyResp *verifyCredentialsResp
Expand Down
19 changes: 18 additions & 1 deletion builtin/credential/ldap/path_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,31 @@ func pathLogin(b *backend) *framework.Path {
},

Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathLogin,
logical.UpdateOperation: b.pathLogin,
logical.AliasLookaheadOperation: b.pathLoginAliasLookahead,
},

HelpSynopsis: pathLoginSyn,
HelpDescription: pathLoginDesc,
}
}

func (b *backend) pathLoginAliasLookahead(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
username := d.Get("username").(string)
if username == "" {
return nil, fmt.Errorf("missing username")
}

return &logical.Response{
Auth: &logical.Auth{
Alias: &logical.Alias{
Name: username,
},
},
}, nil
}

func (b *backend) pathLogin(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
username := d.Get("username").(string)
Expand Down
19 changes: 18 additions & 1 deletion builtin/credential/okta/path_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,31 @@ func pathLogin(b *backend) *framework.Path {
},

Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathLogin,
logical.UpdateOperation: b.pathLogin,
logical.AliasLookaheadOperation: b.pathLoginAliasLookahead,
},

HelpSynopsis: pathLoginSyn,
HelpDescription: pathLoginDesc,
}
}

func (b *backend) pathLoginAliasLookahead(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
username := d.Get("username").(string)
if username == "" {
return nil, fmt.Errorf("missing username")
}

return &logical.Response{
Auth: &logical.Auth{
Alias: &logical.Alias{
Name: username,
},
},
}, nil
}

func (b *backend) pathLogin(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
username := d.Get("username").(string)
Expand Down
19 changes: 18 additions & 1 deletion builtin/credential/radius/path_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,31 @@ func pathLogin(b *backend) *framework.Path {
},

Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathLogin,
logical.UpdateOperation: b.pathLogin,
logical.AliasLookaheadOperation: b.pathLoginAliasLookahead,
},

HelpSynopsis: pathLoginSyn,
HelpDescription: pathLoginDesc,
}
}

func (b *backend) pathLoginAliasLookahead(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
username := d.Get("username").(string)
if username == "" {
return nil, fmt.Errorf("missing username")
}

return &logical.Response{
Auth: &logical.Auth{
Alias: &logical.Alias{
Name: username,
},
},
}, nil
}

func (b *backend) pathLogin(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
username := d.Get("username").(string)
Expand Down
Loading

0 comments on commit cd6d67d

Please sign in to comment.