Skip to content

Commit

Permalink
Extract out common code
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalnayak committed Aug 21, 2016
1 parent 1a62fb6 commit 7d772e4
Showing 1 changed file with 6 additions and 29 deletions.
35 changes: 6 additions & 29 deletions builtin/credential/approle/path_role.go
Expand Up @@ -800,12 +800,16 @@ func (b *backend) pathRoleSecretIDSecretIDRead(req *logical.Request, data *frame
// Create the index at which the secret_id would've been stored
entryIndex := fmt.Sprintf("secret_id/%s/%s", roleNameHMAC, secretIDHMAC)

return b.secretIDCommon(req.Storage, entryIndex, secretIDHMAC)
}

func (b *backend) secretIDCommon(s logical.Storage, entryIndex, secretIDHMAC string) (*logical.Response, error) {
lock := b.secretIDLock(secretIDHMAC)
lock.RLock()
defer lock.RUnlock()

result := secretIDStorageEntry{}
if entry, err := req.Storage.Get(entryIndex); err != nil {
if entry, err := s.Get(entryIndex); err != nil {
return nil, err
} else if entry == nil {
return nil, nil
Expand Down Expand Up @@ -928,34 +932,7 @@ func (b *backend) pathRoleSecretIDAccessorRead(req *logical.Request, data *frame

entryIndex := fmt.Sprintf("secret_id/%s/%s", roleNameHMAC, accessorEntry.SecretIDHMAC)

lock := b.secretIDLock(accessorEntry.SecretIDHMAC)
lock.RLock()
defer lock.RUnlock()

result := secretIDStorageEntry{}
if entry, err := req.Storage.Get(entryIndex); err != nil {
return nil, err
} else if entry == nil {
return nil, nil
} else if err := entry.DecodeJSON(&result); err != nil {
return nil, err
}

result.SecretIDTTL /= time.Second
d := structs.New(result).Map()

// Converting the time values to RFC3339Nano format.
//
// Map() from 'structs' package formats time in RFC3339Nano.
// In order to not break the API due to a modification in the
// third party package, converting the time values again.
d["creation_time"] = (d["creation_time"].(time.Time)).Format(time.RFC3339Nano)
d["expiration_time"] = (d["expiration_time"].(time.Time)).Format(time.RFC3339Nano)
d["last_updated_time"] = (d["last_updated_time"].(time.Time)).Format(time.RFC3339Nano)

return &logical.Response{
Data: d,
}, nil
return b.secretIDCommon(req.Storage, entryIndex, accessorEntry.SecretIDHMAC)
}

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

0 comments on commit 7d772e4

Please sign in to comment.