-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to configure ec2_alias values #5846
Changes from 9 commits
09ca161
79c8e03
fa0b1d9
05a6885
3c8c9a9
1a86187
c456f2a
0126ec1
01e71ce
042f55c
206dbd1
fbdb590
e8645a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,11 +13,16 @@ func pathConfigIdentity(b *backend) *framework.Path { | |
return &framework.Path{ | ||
Pattern: "config/identity$", | ||
Fields: map[string]*framework.FieldSchema{ | ||
"iam_alias": &framework.FieldSchema{ | ||
"iam_alias": { | ||
Type: framework.TypeString, | ||
Default: identityAliasIAMUniqueID, | ||
Description: fmt.Sprintf("Configure how the AWS auth method generates entity aliases when using IAM auth. Valid values are %q and %q", identityAliasIAMUniqueID, identityAliasIAMFullArn), | ||
}, | ||
"ec2_alias": { | ||
Type: framework.TypeString, | ||
Default: identityAliasEC2InstanceID, | ||
Description: fmt.Sprintf("Configure how the AWS auth method generates entity alias when using EC2 auth. Valid values are %q and %q", identityAliasEC2InstanceID, identityAliasEC2ImageID), | ||
}, | ||
}, | ||
|
||
Callbacks: map[logical.Operation]framework.OperationFunc{ | ||
|
@@ -30,27 +35,53 @@ func pathConfigIdentity(b *backend) *framework.Path { | |
} | ||
} | ||
|
||
func pathConfigIdentityRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) { | ||
entry, err := req.Storage.Get(ctx, "config/identity") | ||
func identityConfigEntry(ctx context.Context, s logical.Storage) (*identityConfig, error) { | ||
entryRaw, err := s.Get(ctx, "config/identity") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var entry identityConfig | ||
if entryRaw == nil { | ||
entry.IAMAlias = identityAliasIAMUniqueID | ||
entry.EC2Alias = identityAliasEC2InstanceID | ||
return &entry, nil | ||
} | ||
|
||
err = entryRaw.DecodeJSON(&entry) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if entry == nil { | ||
return nil, nil | ||
|
||
switch { | ||
case entry.IAMAlias == "": | ||
entry.IAMAlias = identityAliasIAMUniqueID | ||
case entry.EC2Alias == "": | ||
entry.EC2Alias = identityAliasEC2InstanceID | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should change this incase both end up being "" for some reason, probably break this into two if statements There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
} | ||
var result identityConfig | ||
if err := entry.DecodeJSON(&result); err != nil { | ||
|
||
return &entry, nil | ||
} | ||
|
||
func pathConfigIdentityRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) { | ||
config, err := identityConfigEntry(ctx, req.Storage) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &logical.Response{ | ||
Data: map[string]interface{}{ | ||
"iam_alias": result.IAMAlias, | ||
"iam_alias": config.IAMAlias, | ||
"ec2_alias": config.EC2Alias, | ||
}, | ||
}, nil | ||
} | ||
|
||
func pathConfigIdentityUpdate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) { | ||
var configEntry identityConfig | ||
config, err := identityConfigEntry(ctx, req.Storage) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
iamAliasRaw, ok := data.GetOk("iam_alias") | ||
if ok { | ||
|
@@ -59,24 +90,41 @@ func pathConfigIdentityUpdate(ctx context.Context, req *logical.Request, data *f | |
if !strutil.StrListContains(allowedIAMAliasValues, iamAlias) { | ||
return logical.ErrorResponse(fmt.Sprintf("iam_alias of %q not in set of allowed values: %v", iamAlias, allowedIAMAliasValues)), nil | ||
} | ||
configEntry.IAMAlias = iamAlias | ||
entry, err := logical.StorageEntryJSON("config/identity", configEntry) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := req.Storage.Put(ctx, entry); err != nil { | ||
return nil, err | ||
config.IAMAlias = iamAlias | ||
} | ||
|
||
ec2AliasRaw, ok := data.GetOk("ec2_alias") | ||
if ok { | ||
ec2Alias := ec2AliasRaw.(string) | ||
allowedEC2AliasValues := []string{identityAliasEC2InstanceID, identityAliasEC2ImageID} | ||
if !strutil.StrListContains(allowedEC2AliasValues, ec2Alias) { | ||
return logical.ErrorResponse(fmt.Sprintf("ec2_alias of %q not in set of allowed values: %v", ec2Alias, allowedEC2AliasValues)), nil | ||
} | ||
config.EC2Alias = ec2Alias | ||
} | ||
|
||
entry, err := logical.StorageEntryJSON("config/identity", config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = req.Storage.Put(ctx, entry) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
type identityConfig struct { | ||
IAMAlias string `json:"iam_alias"` | ||
EC2Alias string `json:"ec2_alias"` | ||
} | ||
|
||
const identityAliasIAMUniqueID = "unique_id" | ||
const identityAliasIAMFullArn = "full_arn" | ||
const identityAliasEC2InstanceID = "instance-id" | ||
const identityAliasEC2ImageID = "image-id" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make these There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
|
||
const pathConfigIdentityHelpSyn = ` | ||
Configure the way the AWS auth method interacts with the identity store | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -589,12 +589,26 @@ func (b *backend) pathLoginUpdateEc2(ctx context.Context, req *logical.Request, | |
} | ||
} | ||
|
||
identityConfigEntry, err := identityConfigEntry(ctx, req.Storage) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
identityAlias := "" | ||
|
||
switch identityConfigEntry.EC2Alias { | ||
case identityAliasEC2InstanceID: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So am I right in thinking that this is the default path if neither have been set? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! The function |
||
identityAlias = identityDocParsed.InstanceID | ||
case identityAliasEC2ImageID: | ||
identityAlias = identityDocParsed.AmiID | ||
} | ||
|
||
// 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, | ||
Name: identityAlias, | ||
}, | ||
}, | ||
}, nil | ||
|
@@ -814,7 +828,7 @@ func (b *backend) pathLoginUpdateEc2(ctx context.Context, req *logical.Request, | |
MaxTTL: shortestMaxTTL, | ||
}, | ||
Alias: &logical.Alias{ | ||
Name: identityDocParsed.InstanceID, | ||
Name: identityAlias, | ||
}, | ||
}, | ||
} | ||
|
@@ -1114,19 +1128,6 @@ func (b *backend) pathLoginRenewEc2(ctx context.Context, req *logical.Request, d | |
} | ||
|
||
func (b *backend) pathLoginUpdateIam(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) { | ||
identityConfigEntryRaw, err := req.Storage.Get(ctx, "config/identity") | ||
if err != nil { | ||
return nil, errwrap.Wrapf("failed to retrieve identity config: {{err}}", err) | ||
} | ||
var identityConfigEntry identityConfig | ||
if identityConfigEntryRaw == nil { | ||
identityConfigEntry.IAMAlias = identityAliasIAMUniqueID | ||
} else { | ||
if err = identityConfigEntryRaw.DecodeJSON(&identityConfigEntry); err != nil { | ||
return nil, errwrap.Wrapf("failed to parse stored config/identity: {{err}}", err) | ||
} | ||
} | ||
|
||
method := data.Get("iam_http_request_method").(string) | ||
if method == "" { | ||
return logical.ErrorResponse("missing iam_http_request_method"), nil | ||
|
@@ -1191,6 +1192,12 @@ func (b *backend) pathLoginUpdateIam(ctx context.Context, req *logical.Request, | |
if err != nil { | ||
return logical.ErrorResponse(fmt.Sprintf("error making upstream request: %v", err)), nil | ||
} | ||
|
||
identityConfigEntry, err := identityConfigEntry(ctx, req.Storage) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// 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] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the above parameter has the old style of declaring the parameter type (
&framework.FieldSchema
), could we rungofmt -s
on this file since we're here anyway? That would clean up the above and any other places that need updating in the file.