Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add tests and ci #9

Merged
merged 2 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: build

on:
push:
branches:
- main
pull_request:

jobs:
Test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

- name: Set up Go
uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # v4.0.1
with:
go-version-file: 'go.mod'

- name: "Run build"
run: make build

- name: "Run unit tests"
run: make test
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ run:
timeout: 10m
concurrency: 4
skip-dirs-use-default: false
tests: false

linters:
disable-all: true
Expand Down
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ fmt:
lint:
golangci-lint run

.PHONY: build clean fmt start enable lint
test:
go test -race -parallel=4 ./...

test-acc:
ACC_TEST=yes go test -race -parallel=4 ./...

.PHONY: build clean fmt start enable lint test test-acc
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ require (
github.com/hashicorp/vault/sdk v0.9.1
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/armon/go-metrics v0.4.1 // indirect
github.com/armon/go-radix v1.0.0 // indirect
Expand Down Expand Up @@ -46,6 +52,7 @@ require (
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/stretchr/testify v1.8.4
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/net v0.8.0 // indirect
Expand Down
7 changes: 6 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww=
github.com/evanphx/json-patch/v5 v5.6.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
Expand Down Expand Up @@ -144,6 +145,7 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=
Expand All @@ -156,6 +158,7 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
Expand Down Expand Up @@ -183,7 +186,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
Expand Down Expand Up @@ -232,6 +236,7 @@ google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
12 changes: 11 additions & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
baseURL = "https://api.vercel.com/v3"
BaseURL = "https://api.vercel.com/v3"
httpTimeout = 60 * time.Second
)

Expand All @@ -20,6 +20,16 @@ type Client struct {
}

func New(apiKey string) *Client {
return &Client{
baseURL: BaseURL,
httpClient: &http.Client{
Timeout: httpTimeout,
},
token: apiKey,
}
}

func NewWithBaseURL(apiKey string, baseURL string) *Client {
return &Client{
baseURL: baseURL,
httpClient: &http.Client{
Expand Down
10 changes: 9 additions & 1 deletion pkg/client/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type DeleteAuthTokenRequest struct {
}

type DeleteAuthTokenResponse struct {
ID string `json:"id"`
ID string `json:"tokenId"`
}

func (c *Client) CreateAuthToken(ctx context.Context, req *CreateAuthTokenRequest) (*CreateAuthTokenResponse, error) {
Expand All @@ -54,6 +54,14 @@ func (c *Client) CreateAuthToken(ctx context.Context, req *CreateAuthTokenReques
return nil, err
}

validStatusAbove := 200
invalidStatusBelow := 300

ok := res.StatusCode >= validStatusAbove && res.StatusCode < invalidStatusBelow
if !ok {
return nil, fmt.Errorf("http error %d with response body '%+v'", res.StatusCode, string(body))
}

if err = json.Unmarshal(body, &resp); err != nil {
return resp, err
}
Expand Down
106 changes: 106 additions & 0 deletions pkg/plugin/backend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package plugin

import (
"context"
"testing"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/sdk/logical"
"github.com/stretchr/testify/require"
)

func TestFactory(t *testing.T) {
t.Parallel()

cases := []struct {
name string
cfg *logical.BackendConfig
wantErr bool
}{
{
name: "Default",
cfg: &logical.BackendConfig{},
wantErr: false,
},
{
name: "MissingConfig",
cfg: nil,
wantErr: true,
},
}

for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
b, err := Factory(ctx, tc.cfg)
if tc.wantErr {
require.Error(t, err)
require.Nil(t, b)
} else {
require.NoError(t, err)
}
})
}
}

func newTestBackend(t *testing.T) (*backend, logical.Storage) {
t.Helper()

config := logical.TestBackendConfig()
config.StorageView = new(logical.InmemStorage)
config.Logger = hclog.NewNullLogger()
b, err := Factory(context.Background(), config)
require.NoError(t, err)
require.NotNil(t, b)

return b.(*backend), config.StorageView
}

func TestBackend_Config(t *testing.T) {
t.Parallel()

cases := []struct {
name string
input []byte
cfg *backendConfig
wantErr bool
}{
{
name: "Default",
input: []byte(`{"api_key": "foo"}`),
cfg: &backendConfig{APIKey: "foo"},
wantErr: false,
},
{
name: "InvalidJSON",
input: []byte(`lorem ipsum`),
cfg: &backendConfig{},
wantErr: true,
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

ctx := context.Background()
b, storage := newTestBackend(t)

if tc.input != nil {
require.NoError(t, storage.Put(ctx, &logical.StorageEntry{
Key: pathPatternConfig,
Value: tc.input,
}))
}

_, err := b.getConfig(ctx, storage)
if tc.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
26 changes: 23 additions & 3 deletions pkg/plugin/path_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (

"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/thevilledev/vault-plugin-secrets-vercel/pkg/client"
)

const (
pathPatternConfig = "config"
pathConfigAPIKey = "api_key"
pathConfigBaseURL = "base_url"
)

var (
Expand All @@ -19,7 +21,8 @@ var (
)

type backendConfig struct {
APIKey string `json:"api_key"`
APIKey string `json:"api_key"`
BaseURL string `json:"base_url"`
}

func (b *backend) pathConfig() []*framework.Path {
Expand All @@ -31,6 +34,11 @@ func (b *backend) pathConfig() []*framework.Path {
pathConfigAPIKey: {
Type: framework.TypeString,
Description: "API key for the Vercel account.",
Required: true,
},
pathConfigBaseURL: {
Type: framework.TypeString,
Description: "Optional API base URL used by this backend.",
},
},

Expand All @@ -54,8 +62,8 @@ func (b *backend) getConfig(ctx context.Context, storage logical.Storage) (*back
return nil, err
}

if e == nil {
return nil, nil
if e == nil || len(e.Value) == 0 {
return &backendConfig{}, nil
}

if err = e.DecodeJSON(&config); err != nil {
Expand All @@ -77,10 +85,22 @@ func (b *backend) pathConfigWrite(ctx context.Context, req *logical.Request,
}
}

if v, ok := data.GetOk(pathConfigBaseURL); ok {
config.BaseURL, ok = v.(string)
if !ok {
b.Logger().Trace("type assertion failed: %+v", v)
return nil, errTypeAssertionFailed
}
}

if config.APIKey == "" {
return nil, errMissingAPIKey
}

if config.BaseURL == "" {
config.BaseURL = client.BaseURL
}

e, err := logical.StorageEntryJSON(pathPatternConfig, config)
if err != nil {
return nil, err
Expand Down
Loading
Loading