@@ -5,10 +5,52 @@ import (
55 "fmt"
66
77 "github.com/hashicorp/vault/sdk/framework"
8+ "github.com/hashicorp/vault/sdk/helper/authmetadata"
89 "github.com/hashicorp/vault/sdk/helper/strutil"
910 "github.com/hashicorp/vault/sdk/logical"
1011)
1112
13+ var (
14+ // iamAuthMetadataFields is a list of the default auth metadata
15+ // added to tokens during login. The default alias type used
16+ // by this back-end is the role ID. Subsequently, the default
17+ // fields included are expected to have a low rate of change
18+ // when the role ID is in use.
19+ iamAuthMetadataFields = & authmetadata.Fields {
20+ FieldName : "iam_metadata" ,
21+ Default : []string {
22+ "account_id" ,
23+ "auth_type" ,
24+ },
25+ AvailableToAdd : []string {
26+ "canonical_arn" ,
27+ "client_arn" ,
28+ "client_user_id" ,
29+ "inferred_aws_region" ,
30+ "inferred_entity_id" ,
31+ "inferred_entity_type" ,
32+ },
33+ }
34+
35+ // ec2AuthMetadataFields is a list of the default auth metadata
36+ // added to tokens during login. The default alias type used
37+ // by this back-end is the role ID. Subsequently, the default
38+ // fields included are expected to have a low rate of change
39+ // when the role ID is in use.
40+ ec2AuthMetadataFields = & authmetadata.Fields {
41+ FieldName : "ec2_metadata" ,
42+ Default : []string {
43+ "account_id" ,
44+ "auth_type" ,
45+ },
46+ AvailableToAdd : []string {
47+ "ami_id" ,
48+ "instance_id" ,
49+ "region" ,
50+ },
51+ }
52+ )
53+
1254func (b * backend ) pathConfigIdentity () * framework.Path {
1355 return & framework.Path {
1456 Pattern : "config/identity$" ,
@@ -18,11 +60,13 @@ func (b *backend) pathConfigIdentity() *framework.Path {
1860 Default : identityAliasIAMUniqueID ,
1961 Description : fmt .Sprintf ("Configure how the AWS auth method generates entity aliases when using IAM auth. Valid values are %q, %q, and %q. Defaults to %q." , identityAliasRoleID , identityAliasIAMUniqueID , identityAliasIAMFullArn , identityAliasRoleID ),
2062 },
63+ iamAuthMetadataFields .FieldName : authmetadata .FieldSchema (iamAuthMetadataFields ),
2164 "ec2_alias" : {
2265 Type : framework .TypeString ,
2366 Default : identityAliasEC2InstanceID ,
2467 Description : fmt .Sprintf ("Configure how the AWS auth method generates entity alias when using EC2 auth. Valid values are %q, %q, and %q. Defaults to %q." , identityAliasRoleID , identityAliasEC2InstanceID , identityAliasEC2ImageID , identityAliasRoleID ),
2568 },
69+ ec2AuthMetadataFields .FieldName : authmetadata .FieldSchema (ec2AuthMetadataFields ),
2670 },
2771
2872 Operations : map [logical.Operation ]framework.OperationHandler {
@@ -45,9 +89,12 @@ func identityConfigEntry(ctx context.Context, s logical.Storage) (*identityConfi
4589 return nil , err
4690 }
4791
48- var entry identityConfig
92+ entry := & identityConfig {
93+ IAMAuthMetadataHandler : authmetadata .NewHandler (iamAuthMetadataFields ),
94+ EC2AuthMetadataHandler : authmetadata .NewHandler (ec2AuthMetadataFields ),
95+ }
4996 if entryRaw != nil {
50- if err := entryRaw .DecodeJSON (& entry ); err != nil {
97+ if err := entryRaw .DecodeJSON (entry ); err != nil {
5198 return nil , err
5299 }
53100 }
@@ -60,7 +107,7 @@ func identityConfigEntry(ctx context.Context, s logical.Storage) (*identityConfi
60107 entry .EC2Alias = identityAliasRoleID
61108 }
62109
63- return & entry , nil
110+ return entry , nil
64111}
65112
66113func pathConfigIdentityRead (ctx context.Context , req * logical.Request , _ * framework.FieldData ) (* logical.Response , error ) {
@@ -71,8 +118,10 @@ func pathConfigIdentityRead(ctx context.Context, req *logical.Request, _ *framew
71118
72119 return & logical.Response {
73120 Data : map [string ]interface {}{
74- "iam_alias" : config .IAMAlias ,
75- "ec2_alias" : config .EC2Alias ,
121+ "iam_alias" : config .IAMAlias ,
122+ iamAuthMetadataFields .FieldName : config .IAMAuthMetadataHandler .AuthMetadata (),
123+ "ec2_alias" : config .EC2Alias ,
124+ ec2AuthMetadataFields .FieldName : config .EC2AuthMetadataHandler .AuthMetadata (),
76125 },
77126 }, nil
78127}
@@ -102,6 +151,12 @@ func pathConfigIdentityUpdate(ctx context.Context, req *logical.Request, data *f
102151 }
103152 config .EC2Alias = ec2Alias
104153 }
154+ if err := config .IAMAuthMetadataHandler .ParseAuthMetadata (data ); err != nil {
155+ return logical .ErrorResponse (err .Error ()), logical .ErrInvalidRequest
156+ }
157+ if err := config .EC2AuthMetadataHandler .ParseAuthMetadata (data ); err != nil {
158+ return logical .ErrorResponse (err .Error ()), logical .ErrInvalidRequest
159+ }
105160
106161 entry , err := logical .StorageEntryJSON ("config/identity" , config )
107162 if err != nil {
@@ -117,8 +172,10 @@ func pathConfigIdentityUpdate(ctx context.Context, req *logical.Request, data *f
117172}
118173
119174type identityConfig struct {
120- IAMAlias string `json:"iam_alias"`
121- EC2Alias string `json:"ec2_alias"`
175+ IAMAlias string `json:"iam_alias"`
176+ IAMAuthMetadataHandler * authmetadata.Handler `json:"iam_auth_metadata_handler"`
177+ EC2Alias string `json:"ec2_alias"`
178+ EC2AuthMetadataHandler * authmetadata.Handler `json:"ec2_auth_metadata_handler"`
122179}
123180
124181const identityAliasIAMUniqueID = "unique_id"
0 commit comments