Skip to content

Commit

Permalink
plugin api update, dependency update (#12)
Browse files Browse the repository at this point in the history
* plugin api update, dependency update,  moar tests
  • Loading branch information
gites committed Apr 4, 2018
1 parent a8cc98d commit 0bf5027
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 37 deletions.
56 changes: 39 additions & 17 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions authfile/backend.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package authfile

import (
"context"

"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
log "github.com/mgutz/logxi/v1"
)

//Factory function implementation
func Factory(conf *logical.BackendConfig) (logical.Backend, error) {
func Factory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
b := Backend(conf)
err := b.Setup(conf)
err := b.Setup(ctx, conf)
if err != nil {
return nil, err
}
Expand Down
33 changes: 27 additions & 6 deletions authfile/backend_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package authfile

import (
"context"
"testing"

"github.com/hashicorp/vault/logical"
Expand All @@ -12,7 +13,7 @@ func TestBackend_Config(t *testing.T) {
cfg.StorageView = storage

b := Backend(cfg)
err := b.Setup(cfg)
err := b.Setup(context.Background(), cfg)
if err != nil {
t.Fatal(err)
}
Expand All @@ -22,7 +23,7 @@ func TestBackend_Config(t *testing.T) {
"path": "/etc/vault/password-file",
}

_, err = b.HandleRequest(&logical.Request{
_, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "config",
Data: data,
Expand All @@ -32,7 +33,7 @@ func TestBackend_Config(t *testing.T) {
t.Fatal(err)
}

resp, err := b.HandleRequest(&logical.Request{
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: "config",
Storage: storage,
Expand All @@ -50,7 +51,7 @@ func TestBackend_Config(t *testing.T) {
// Missing path
data2 := map[string]interface{}{}

_, err = b.HandleRequest(&logical.Request{
_, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "config",
Data: data2,
Expand All @@ -66,7 +67,7 @@ func TestBackend_Config(t *testing.T) {
"ttl": "auioe",
}

_, err = b.HandleRequest(&logical.Request{
_, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "config",
Data: data3,
Expand All @@ -76,7 +77,7 @@ func TestBackend_Config(t *testing.T) {
t.Fatal("Config accepted bad ttl")
}
}
func TestBackend_Authenticate(t *testing.T) {
func TestBackend_LoginAuth(t *testing.T) {
var user users
user.User = "gites"
user.Hash = "$6$spfjUPN4$6ap3h.6Fac23HO/CFTZpQYdwvZ8zFflZkCQMWVO.13pCFEOjw8sjVljiIU6SgAhRDwwUBK1DYvHmBdoz/3wef0"
Expand All @@ -86,3 +87,23 @@ func TestBackend_Authenticate(t *testing.T) {
t.Fatal("Couldn't authenticate request")
}
}

func TestBackend_LoginFileRead(t *testing.T) {
cfg := logical.TestBackendConfig()
storage := &logical.InmemStorage{}
cfg.StorageView = storage

b := Backend(cfg)
err := b.Setup(context.Background(), cfg)
if err != nil {
t.Fatal(err)
}

userMap, err := getUsers("../test/password-file", 300, b)
if err != nil {
t.Fatal(err)
}
if userMap["wac"].User != "wac" {
t.Fatal("Couldn't correctly read password file -> wac != wac")
}
}
13 changes: 7 additions & 6 deletions authfile/path_config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package authfile

import (
"context"
"fmt"
"time"

Expand Down Expand Up @@ -36,8 +37,8 @@ func pathConfig(b *backend) *framework.Path {
}
}

func (b *backend) pathConfigRead(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
cfg, err := b.Config(req.Storage)
func (b *backend) pathConfigRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
cfg, err := b.Config(ctx, req.Storage)
if err != nil {
return nil, errors.Wrapf(err, "failed to get configuration from storage")
}
Expand All @@ -54,7 +55,7 @@ func (b *backend) pathConfigRead(req *logical.Request, data *framework.FieldData
return resp, nil
}

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

path := data.Get("path").(string)
if path == "" {
Expand All @@ -74,15 +75,15 @@ func (b *backend) pathConfigWrite(req *logical.Request, data *framework.FieldDat
return nil, err
}

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

return nil, nil
}

func (b *backend) Config(s logical.Storage) (*config, error) {
entry, err := s.Get("config")
func (b *backend) Config(ctx context.Context, s logical.Storage) (*config, error) {
entry, err := s.Get(ctx, "config")
if err != nil {
return nil, err
}
Expand Down
11 changes: 6 additions & 5 deletions authfile/path_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package authfile

import (
"bufio"
"context"
"io"
"os"
"strings"
Expand Down Expand Up @@ -55,11 +56,11 @@ func pathLoginUserpass(b *backend) *framework.Path {
}
}

func (b *backend) pathLogin(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
func (b *backend) pathLogin(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
user := data.Get("username").(string)
pass := data.Get("password").(string)

config, err := b.Config(req.Storage)
config, err := b.Config(ctx, req.Storage)

var fileTTL time.Duration = 300
var auth = false
Expand Down Expand Up @@ -95,7 +96,7 @@ func (b *backend) pathLogin(req *logical.Request, data *framework.FieldData) (*l
}, nil
}

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

if req.Auth == nil {
return logical.ErrorResponse("Couldn't authenticate client"), nil
Expand All @@ -110,7 +111,7 @@ func (b *backend) pathLoginRenew(req *logical.Request, data *framework.FieldData
return logical.ErrorResponse("No internal password data in request"), nil
}

config, err := b.Config(req.Storage)
config, err := b.Config(ctx, req.Storage)

var fileTTL time.Duration = 300
var auth = false
Expand All @@ -130,7 +131,7 @@ func (b *backend) pathLoginRenew(req *logical.Request, data *framework.FieldData
if !policyutil.EquivalentPolicies(userMap[user].Policies, req.Auth.Policies) {
return logical.ErrorResponse("Policies have changed, not renewing"), nil
}
return framework.LeaseExtend(config.TTL, config.MaxTTL, b.System())(req, data)
return framework.LeaseExtend(config.TTL, config.MaxTTL, b.System())(ctx, req, data)
}

func authenticate(user users, pass string, b *backend) bool {
Expand Down
3 changes: 2 additions & 1 deletion test/get_vault.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/bin/bash

ARG1=$1
VER=${ARG1:-0.9.3}
VER=${ARG1:-0.9.5}
echo "Downloading v$VER Vault binary"
wget -q https://releases.hashicorp.com/vault/$VER/vault_${VER}_linux_amd64.zip
rm -rf vault
unzip vault_${VER}_linux_amd64.zip
chmod +x vault

0 comments on commit 0bf5027

Please sign in to comment.