From 524ed6db37bbca9d83234d70edcf7e675790c2ec Mon Sep 17 00:00:00 2001 From: vishalnayak Date: Sun, 21 Aug 2016 15:46:11 -0400 Subject: [PATCH] Extract out common code --- builtin/credential/approle/path_role.go | 35 +++++-------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/builtin/credential/approle/path_role.go b/builtin/credential/approle/path_role.go index a738a0072..696396b2f 100644 --- a/builtin/credential/approle/path_role.go +++ b/builtin/credential/approle/path_role.go @@ -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 @@ -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) {