Skip to content

Commit

Permalink
Add parameter for specific entity to token creation
Browse files Browse the repository at this point in the history
  • Loading branch information
michelvocks committed Feb 20, 2019
1 parent 1e0b6a0 commit 1194954
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/auth_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,5 @@ type TokenCreateRequest struct {
NumUses int `json:"num_uses"`
Renewable *bool `json:"renewable,omitempty"`
Type string `json:"type"`
EntityID string `json:"entity_id"`
}
10 changes: 10 additions & 0 deletions command/token_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type TokenCreateCommand struct {
flagType string
flagMetadata map[string]string
flagPolicies []string
flagEntityID string
}

func (c *TokenCreateCommand) Synopsis() string {
Expand Down Expand Up @@ -176,6 +177,14 @@ func (c *TokenCreateCommand) Flags() *FlagSets {
"specified multiple times to attach multiple policies.",
})

f.StringVar(&StringVar{
Name: "entity-id",
Target: &c.flagEntityID,
Default: "",
Usage: "The id of the entity which will be set for this token. " +
"If set, parent token entity and group policies are not inherited.",
})

return set
}

Expand Down Expand Up @@ -224,6 +233,7 @@ func (c *TokenCreateCommand) Run(args []string) int {
ExplicitMaxTTL: c.flagExplicitMaxTTL.String(),
Period: c.flagPeriod.String(),
Type: c.flagType,
EntityID: c.flagEntityID,
}

var secret *api.Secret
Expand Down
19 changes: 19 additions & 0 deletions vault/token_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2109,6 +2109,7 @@ func (ts *TokenStore) handleCreateCommon(ctx context.Context, req *logical.Reque
NumUses int `mapstructure:"num_uses"`
Period string
Type string `mapstructure:"type"`
EntityID string `mapstructure:"entity_id"`
}
if err := mapstructure.WeakDecode(req.Data, &data); err != nil {
return logical.ErrorResponse(fmt.Sprintf(
Expand Down Expand Up @@ -2441,6 +2442,24 @@ func (ts *TokenStore) handleCreateCommon(ctx context.Context, req *logical.Reque
}
}

// It is allowed to overwrite the corresponding entity of a token.
// This is only possible when client token has sudo/root.
if data.EntityID != "" && ts.core.identityStore != nil {
if !isSudo {
return logical.ErrorResponse("root or sudo privileges required to specify entity id "), logical.ErrInvalidRequest
}
entity, err := ts.core.identityStore.MemDBEntityByID(data.EntityID, false)
if err != nil {
return logical.ErrorResponse(err.Error()), logical.ErrInvalidRequest
}
if entity == nil {
return logical.ErrorResponse("cannot find entity with given entity id"), logical.ErrInvalidRequest
}

// Entity is valid.
te.EntityID = data.EntityID
}

var explicitMaxTTLToUse time.Duration
if data.ExplicitMaxTTL != "" {
dur, err := parseutil.ParseDurationSecond(data.ExplicitMaxTTL)
Expand Down

0 comments on commit 1194954

Please sign in to comment.