From 5a6baeaca352abd1b5f409cf86829751ca2e77c3 Mon Sep 17 00:00:00 2001 From: Brian Rodgers Date: Tue, 10 Jan 2017 18:21:31 -0600 Subject: [PATCH 1/2] Added a 'read' for github config --- builtin/credential/github/path_config.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/builtin/credential/github/path_config.go b/builtin/credential/github/path_config.go index b7d957f1ed..f68abba102 100644 --- a/builtin/credential/github/path_config.go +++ b/builtin/credential/github/path_config.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/vault/logical" "github.com/hashicorp/vault/logical/framework" + "github.com/fatih/structs" ) func pathConfig(b *backend) *framework.Path { @@ -36,6 +37,7 @@ API-compatible authentication server.`, Callbacks: map[logical.Operation]framework.OperationFunc{ logical.UpdateOperation: b.pathConfigWrite, + logical.ReadOperation: b.pathConfigRead, }, } } @@ -92,6 +94,20 @@ func (b *backend) pathConfigWrite( return nil, nil } +func (b *backend) pathConfigRead(req *logical.Request, data *framework.FieldData) (*logical.Response, error) { + config, err := b.Config(req.Storage) + if err != nil { + return nil, err + } + config.TTL /= time.Second + config.MaxTTL /= time.Second + + resp := &logical.Response{ + Data: structs.New(config).Map(), + } + return resp, nil +} + // Config returns the configuration for this backend. func (b *backend) Config(s logical.Storage) (*config, error) { entry, err := s.Get("config") @@ -110,8 +126,8 @@ func (b *backend) Config(s logical.Storage) (*config, error) { } type config struct { - Org string `json:"organization"` - BaseURL string `json:"base_url"` - TTL time.Duration `json:"ttl"` - MaxTTL time.Duration `json:"max_ttl"` + Org string `json:"organization" structs:"organization" mapstructure:"organization"` + BaseURL string `json:"base_url" structs:"base_url" mapstructure:"base_url"` + TTL time.Duration `json:"ttl" structs:"ttl" mapstructure:"ttl"` + MaxTTL time.Duration `json:"max_ttl" structs:"max_ttl" mapstructure:"max_ttl"` } From 2615412734e2beaa8f8c29160aefbb69ff544fa8 Mon Sep 17 00:00:00 2001 From: Brian Rodgers Date: Wed, 11 Jan 2017 11:04:15 -0600 Subject: [PATCH 2/2] Added a nil check for config and renamed org field internally. --- builtin/credential/github/path_config.go | 21 +++++++++++++-------- builtin/credential/github/path_login.go | 4 ++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/builtin/credential/github/path_config.go b/builtin/credential/github/path_config.go index f68abba102..9db2e64593 100644 --- a/builtin/credential/github/path_config.go +++ b/builtin/credential/github/path_config.go @@ -77,10 +77,10 @@ func (b *backend) pathConfigWrite( } entry, err := logical.StorageEntryJSON("config", config{ - Org: organization, - BaseURL: baseURL, - TTL: ttl, - MaxTTL: maxTTL, + Organization: organization, + BaseURL: baseURL, + TTL: ttl, + MaxTTL: maxTTL, }) if err != nil { @@ -99,6 +99,11 @@ func (b *backend) pathConfigRead(req *logical.Request, data *framework.FieldData if err != nil { return nil, err } + + if config == nil { + return nil, fmt.Errorf("configuration object not found") + } + config.TTL /= time.Second config.MaxTTL /= time.Second @@ -126,8 +131,8 @@ func (b *backend) Config(s logical.Storage) (*config, error) { } type config struct { - Org string `json:"organization" structs:"organization" mapstructure:"organization"` - BaseURL string `json:"base_url" structs:"base_url" mapstructure:"base_url"` - TTL time.Duration `json:"ttl" structs:"ttl" mapstructure:"ttl"` - MaxTTL time.Duration `json:"max_ttl" structs:"max_ttl" mapstructure:"max_ttl"` + Organization string `json:"organization" structs:"organization" mapstructure:"organization"` + BaseURL string `json:"base_url" structs:"base_url" mapstructure:"base_url"` + TTL time.Duration `json:"ttl" structs:"ttl" mapstructure:"ttl"` + MaxTTL time.Duration `json:"max_ttl" structs:"max_ttl" mapstructure:"max_ttl"` } diff --git a/builtin/credential/github/path_login.go b/builtin/credential/github/path_login.go index 7d0d298302..9836859089 100644 --- a/builtin/credential/github/path_login.go +++ b/builtin/credential/github/path_login.go @@ -107,7 +107,7 @@ func (b *backend) verifyCredentials(req *logical.Request, token string) (*verify if err != nil { return nil, nil, err } - if config.Org == "" { + if config.Organization == "" { return nil, logical.ErrorResponse( "configure the github credential backend first"), nil } @@ -152,7 +152,7 @@ func (b *backend) verifyCredentials(req *logical.Request, token string) (*verify } for _, o := range allOrgs { - if strings.ToLower(*o.Login) == strings.ToLower(config.Org) { + if strings.ToLower(*o.Login) == strings.ToLower(config.Organization) { org = o break }