2015-04-01 22:46:21 +00:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-04-01 22:46:21 +00:00
|
|
|
"fmt"
|
2015-08-28 13:28:35 +00:00
|
|
|
"net/url"
|
2015-10-02 17:33:19 +00:00
|
|
|
"time"
|
2015-04-01 22:46:21 +00:00
|
|
|
|
2018-04-05 15:49:21 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2015-04-01 22:46:21 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
2015-10-02 17:33:19 +00:00
|
|
|
func pathConfig(b *backend) *framework.Path {
|
2015-04-01 22:46:21 +00:00
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "config",
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"organization": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "The organization users must be part of",
|
|
|
|
},
|
2015-08-28 13:28:35 +00:00
|
|
|
|
|
|
|
"base_url": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `The API endpoint to use. Useful if you
|
|
|
|
are running GitHub Enterprise or an
|
|
|
|
API-compatible authentication server.`,
|
|
|
|
},
|
2015-10-02 17:33:19 +00:00
|
|
|
"ttl": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `Duration after which authentication will be expired`,
|
|
|
|
},
|
|
|
|
"max_ttl": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `Maximum duration after which authentication will be expired`,
|
|
|
|
},
|
2015-04-01 22:46:21 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-01-07 15:30:47 +00:00
|
|
|
logical.UpdateOperation: b.pathConfigWrite,
|
2017-05-19 12:34:17 +00:00
|
|
|
logical.ReadOperation: b.pathConfigRead,
|
2015-04-01 22:46:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathConfigWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2015-10-02 17:33:19 +00:00
|
|
|
organization := data.Get("organization").(string)
|
2015-08-28 13:28:35 +00:00
|
|
|
baseURL := data.Get("base_url").(string)
|
|
|
|
if len(baseURL) != 0 {
|
|
|
|
_, err := url.Parse(baseURL)
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Error parsing given base_url: %s", err)), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-02 19:41:35 +00:00
|
|
|
var ttl time.Duration
|
|
|
|
var err error
|
|
|
|
ttlRaw, ok := data.GetOk("ttl")
|
2015-10-03 19:36:57 +00:00
|
|
|
if !ok || len(ttlRaw.(string)) == 0 {
|
2015-10-02 19:41:35 +00:00
|
|
|
ttl = 0
|
|
|
|
} else {
|
|
|
|
ttl, err = time.ParseDuration(ttlRaw.(string))
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Invalid 'ttl':%s", err)), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var maxTTL time.Duration
|
|
|
|
maxTTLRaw, ok := data.GetOk("max_ttl")
|
2015-10-03 19:36:57 +00:00
|
|
|
if !ok || len(maxTTLRaw.(string)) == 0 {
|
2015-10-02 19:41:35 +00:00
|
|
|
maxTTL = 0
|
|
|
|
} else {
|
|
|
|
maxTTL, err = time.ParseDuration(maxTTLRaw.(string))
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Invalid 'max_ttl':%s", err)), nil
|
|
|
|
}
|
2015-10-02 17:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
entry, err := logical.StorageEntryJSON("config", config{
|
2017-05-19 12:34:17 +00:00
|
|
|
Organization: organization,
|
|
|
|
BaseURL: baseURL,
|
|
|
|
TTL: ttl,
|
|
|
|
MaxTTL: maxTTL,
|
2015-10-02 17:33:19 +00:00
|
|
|
})
|
|
|
|
|
2015-04-01 22:46:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := req.Storage.Put(ctx, entry); err != nil {
|
2015-04-01 22:46:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathConfigRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
config, err := b.Config(ctx, req.Storage)
|
2017-01-11 00:21:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-11 17:04:15 +00:00
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
return nil, fmt.Errorf("configuration object not found")
|
|
|
|
}
|
|
|
|
|
2017-01-11 00:21:31 +00:00
|
|
|
config.TTL /= time.Second
|
|
|
|
config.MaxTTL /= time.Second
|
|
|
|
|
|
|
|
resp := &logical.Response{
|
2018-01-19 06:44:44 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"organization": config.Organization,
|
|
|
|
"base_url": config.BaseURL,
|
|
|
|
"ttl": config.TTL,
|
|
|
|
"max_ttl": config.MaxTTL,
|
|
|
|
},
|
2017-01-11 00:21:31 +00:00
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2015-04-01 22:46:21 +00:00
|
|
|
// Config returns the configuration for this backend.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) Config(ctx context.Context, s logical.Storage) (*config, error) {
|
|
|
|
entry, err := s.Get(ctx, "config")
|
2015-04-01 22:46:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result config
|
|
|
|
if entry != nil {
|
|
|
|
if err := entry.DecodeJSON(&result); err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("error reading configuration: {{err}}", err)
|
2015-04-01 22:46:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type config struct {
|
2017-01-11 17:04:15 +00:00
|
|
|
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"`
|
2015-04-01 22:46:21 +00:00
|
|
|
}
|