Skip to content

Commit

Permalink
added context propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
ror6ax committed Mar 21, 2018
1 parent f92dd14 commit 7ac8c6f
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 103 deletions.
10 changes: 6 additions & 4 deletions plugin/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keystoneauth
import (
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
"context"
)

// New returns a new backend as an interface. This func
Expand All @@ -12,9 +13,9 @@ func New() (interface{}, error) {
}

// Factory returns a new backend as logical.Backend.
func Factory(conf *logical.BackendConfig) (logical.Backend, error) {
func Factory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
b := Backend()
if err := b.Setup(conf); err != nil {
if err := b.Setup(ctx, conf); err != nil {
return nil, err
}
return b, nil
Expand All @@ -25,8 +26,9 @@ func Factory(conf *logical.BackendConfig) (logical.Backend, error) {
func FactoryType(backendType logical.BackendType) func(*logical.BackendConfig) (logical.Backend, error) {
return func(conf *logical.BackendConfig) (logical.Backend, error) {
b := Backend()
ctx := context.Background()
b.BackendType = backendType
if err := b.Setup(conf); err != nil {
if err := b.Setup(ctx, conf); err != nil {
return nil, err
}
return b, nil
Expand Down Expand Up @@ -80,7 +82,7 @@ type backend struct {
internal string
}

func (b *backend) invalidate(key string) {
func (b *backend) invalidate(ctx context.Context, key string) {
switch key {
case "internal":
b.internal = ""
Expand Down
9 changes: 5 additions & 4 deletions plugin/path_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keystoneauth

import (
"fmt"
"context"
"github.com/fatih/structs"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
Expand All @@ -28,8 +29,8 @@ func pathConfig(b *backend) *framework.Path {
}

func (b *backend) pathConnectionRead(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
entry, err := req.Storage.Get("config/connection")
ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
entry, err := req.Storage.Get(ctx, "config/connection")
if err != nil {
return nil, fmt.Errorf("configure the Keystone connection with config/connection first")
}
Expand All @@ -47,7 +48,7 @@ func (b *backend) pathConnectionRead(
}

func (b *backend) pathConnectionWrite(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
connURL := data.Get("connection_url").(string)
adminAuthToken := data.Get("admin_auth_token").(string)

Expand All @@ -61,7 +62,7 @@ func (b *backend) pathConnectionWrite(
return nil, err
}

if err := req.Storage.Put(entry); err != nil {
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}

Expand Down
19 changes: 10 additions & 9 deletions plugin/path_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keystoneauth

import (
"fmt"
"context"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
Expand Down Expand Up @@ -35,8 +36,8 @@ func pathCredentials(b *backend) *framework.Path {
}
}

func (b *backend) Credential(s logical.Storage, n string) (*credentialEntry, error) {
entry, err := s.Get("credential/" + n)
func (b *backend) Credential(ctx context.Context, s logical.Storage, n string) (*credentialEntry, error) {
entry, err := s.Get(ctx, "credential/" + n)
if err != nil {
return nil, err
}
Expand All @@ -54,22 +55,22 @@ func (b *backend) Credential(s logical.Storage, n string) (*credentialEntry, err
}

func (b *backend) pathCredentialRead(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {

user_id := data.Get("user_id").(string)
blob := data.Get("blob").(string)
thetype := data.Get("type").(string)
project_id := data.Get("project_id").(string)

credential, err := b.Credential(req.Storage, blob)
credential, err := b.Credential(ctx, req.Storage, blob)
if err != nil {
return nil, err
}
if credential == nil {
return logical.ErrorResponse(fmt.Sprintf("unknown credential: %s", blob)), nil
}

conf, err2 := getconfig(req)
conf, err2 := getconfig(ctx, req)
if err2 != nil {
return nil, fmt.Errorf("configure the Keystone connection with config/connection first")
}
Expand All @@ -94,8 +95,8 @@ func (b *backend) pathCredentialRead(
}

func (b *backend) pathCredentialList(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
entries, err := req.Storage.List("credential/")
ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
entries, err := req.Storage.List(ctx, "credential/")
if err != nil {
return nil, err
}
Expand All @@ -104,7 +105,7 @@ func (b *backend) pathCredentialList(
}

func (b *backend) pathCredentialWrite(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {

user_id := data.Get("user_id").(string)
blob := data.Get("blob").(string)
Expand All @@ -126,7 +127,7 @@ func (b *backend) pathCredentialWrite(
if err != nil {
return nil, err
}
if err := req.Storage.Put(entry); err != nil {
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}

Expand Down
19 changes: 10 additions & 9 deletions plugin/path_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keystoneauth

import (
"fmt"
"context"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
"github.com/hashicorp/vault/plugins/helper/database/credsutil"
Expand Down Expand Up @@ -43,8 +44,8 @@ func pathDomains(b *backend) *framework.Path {
}
}

func (b *backend) Domain(s logical.Storage, n string) (*domainEntry, error) {
entry, err := s.Get("domain/" + n)
func (b *backend) Domain(ctx context.Context, s logical.Storage, n string) (*domainEntry, error) {
entry, err := s.Get(ctx, "domain/" + n)
if err != nil {
return nil, err
}
Expand All @@ -62,21 +63,21 @@ func (b *backend) Domain(s logical.Storage, n string) (*domainEntry, error) {
}

func (b *backend) pathDomainRead(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {

name := data.Get("name").(string)
description := data.Get("description").(string)
enabled := data.Get("enabled").(bool)

domain, err := b.Domain(req.Storage, name)
domain, err := b.Domain(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if domain == nil {
return logical.ErrorResponse(fmt.Sprintf("unknown domain: %s", name)), nil
}

conf, err2 := getconfig(req)
conf, err2 := getconfig(ctx, req)
if err2 != nil {
return nil, fmt.Errorf("configure the Keystone connection with config/connection first")
}
Expand Down Expand Up @@ -105,8 +106,8 @@ func (b *backend) pathDomainRead(
}

func (b *backend) pathDomainList(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
entries, err := req.Storage.List("domain/")
ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
entries, err := req.Storage.List(ctx, "domain/")
if err != nil {
return nil, err
}
Expand All @@ -115,7 +116,7 @@ func (b *backend) pathDomainList(
}

func (b *backend) pathDomainWrite(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {

name := data.Get("name").(string)
description := data.Get("description").(string)
Expand All @@ -135,7 +136,7 @@ func (b *backend) pathDomainWrite(
if err != nil {
return nil, err
}
if err := req.Storage.Put(entry); err != nil {
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}

Expand Down
23 changes: 12 additions & 11 deletions plugin/path_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keystoneauth

import (
"fmt"
"context"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
"github.com/hashicorp/vault/plugins/helper/database/credsutil"
Expand Down Expand Up @@ -62,8 +63,8 @@ func pathGroupsUsers(b *backend) *framework.Path {
}
}

func (b *backend) Group(s logical.Storage, n string) (*groupEntry, error) {
entry, err := s.Get("group/" + n)
func (b *backend) Group(ctx context.Context, s logical.Storage, n string) (*groupEntry, error) {
entry, err := s.Get(ctx, "group/" + n)
if err != nil {
return nil, err
}
Expand All @@ -81,23 +82,23 @@ func (b *backend) Group(s logical.Storage, n string) (*groupEntry, error) {
}

func (b *backend) pathGroupRead(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {

var namepostfix string
namepostfix, _ = credsutil.RandomAlphaNumeric(20, true)
name := data.Get("name").(string)
description := data.Get("description").(string)
domain_id := data.Get("domain_id").(string)

group, err := b.Group(req.Storage, name)
group, err := b.Group(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if group == nil {
return logical.ErrorResponse(fmt.Sprintf("unknown group: %s", name)), nil
}

conf, err2 := getconfig(req)
conf, err2 := getconfig(ctx, req)

if err2 != nil {
return nil, fmt.Errorf("configure the Keystone connection with config/connection first")
Expand Down Expand Up @@ -127,8 +128,8 @@ func (b *backend) pathGroupRead(
}

func (b *backend) pathGroupList(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
entries, err := req.Storage.List("group/")
ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
entries, err := req.Storage.List(ctx, "group/")
if err != nil {
return nil, err
}
Expand All @@ -137,7 +138,7 @@ func (b *backend) pathGroupList(
}

func (b *backend) pathGroupWrite(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {

name := data.Get("name").(string)
description := data.Get("description").(string)
Expand All @@ -157,7 +158,7 @@ func (b *backend) pathGroupWrite(
if err != nil {
return nil, err
}
if err := req.Storage.Put(entry); err != nil {
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}

Expand All @@ -170,11 +171,11 @@ func (b *backend) pathGroupWrite(
}

func (b *backend) pathGroupAddUser(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
user_id := data.Get("user_id").(string)
group_id := data.Get("group_id").(string)

conf, err2 := getconfig(req)
conf, err2 := getconfig(ctx, req)

if err2 != nil {
return nil, fmt.Errorf("configure the Keystone connection with config/connection first")
Expand Down
3 changes: 2 additions & 1 deletion plugin/path_internal.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keystoneauth

import (
"context"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
Expand All @@ -17,7 +18,7 @@ func pathInternal(b *backend) *framework.Path {
}

func (b *backend) pathTestingReadInternal(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
// Return the secret
return &logical.Response{
Data: map[string]interface{}{
Expand Down
Loading

0 comments on commit 7ac8c6f

Please sign in to comment.