Skip to content
This repository has been archived by the owner on Mar 30, 2021. It is now read-only.

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Genki Sugawara committed Jun 12, 2016
1 parent beae871 commit cfc9ccb
Show file tree
Hide file tree
Showing 37 changed files with 3,574 additions and 699 deletions.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ GOOS := $(shell go env GOOS)
GOARCH := $(shell go env GOARCH)
RUNTIME_GOPATH := $(GOPATH):$(shell pwd)
SRC := $(wildcard *.go) $(wildcard src/*/*.go) $(wildcard src/*/*/*.go)
TEST_SRC := $(wildcard src/gcredstash/*_test.go)
CMD_TEST_SRC := $(wildcard src/gcredstash/command/*_test.go)

all: gcredstash

gcredstash: go-get $(SRC)
GOPATH=$(RUNTIME_GOPATH) go build

test: $(TEST_SRC) $(CMD_TEST_SRC)
GOPATH=$(RUNTIME_GOPATH) go test $(TEST_SRC)
GOPATH=$(RUNTIME_GOPATH) go test $(CMD_TEST_SRC)

go-get:
go get github.com/mitchellh/cli
go get github.com/aws/aws-sdk-go
go get github.com/ryanuber/go-glob
go get github.com/golang/mock/gomock

clean:
rm -f gcredstash *.gz
Expand All @@ -22,3 +29,8 @@ package: clean gcredstash

deb:
dpkg-buildpackage -us -uc

mock:
go get github.com/golang/mock/mockgen
mockgen -source $(GOPATH)/src/github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface/interface.go -destination src/mockaws/dynamodbmock.go -package mockaws
mockgen -source $(GOPATH)/src/github.com/aws/aws-sdk-go/service/kms/kmsiface/interface.go -destination src/mockaws/kmsmock.go -package mockaws
32 changes: 7 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ Available commands are:
$ gcredstash -h delete
usage: gcredstash delete [-v VERSION] credential
$ gcredstash -h env
usage: gcredstash env [-v VERSION] [-p PREFIX] credential [context [context ...]]
$ gcredstash -h get
usage: gcredstash get [-v VERSION] [-n] credential [context [context ...]]
$ gcredstash -h getall
usage: gcredstash getall [-v VERSION] [context [context ...]]
usage: gcredstash getall [context [context ...]]
$ gcredstash -h list
usage: gcredstash list
Expand All @@ -43,26 +40,6 @@ $ gcredstash -h setup
usage: credstash setup
```

## Set to environment variables

```
$ gcredstash get xxx.*
{
"xxx.xxx": "100",
"xxx.yyy": "200"
}
$ gcredstash env xxx.*
export XXX_YYY=200
export XXX_XXX=100
$ gcredstash env xxx.* -p xxx.
export YYY=200
export XXX=100
$ eval $(gcredstash env xxx.*)
```

## Put from stdin

```
Expand All @@ -85,6 +62,12 @@ brew install https://raw.githubusercontent.com/winebarrel/gcredstash/master/home
wget -q -O- https://github.com/winebarrel/gcredstash/releases/download/vN.N.N/gcredstash_N.N.N_amd64.deb | dpkg -i -
```

## Setup

* `IAM > Encryption Keys`
* Create Encryption Key: `Alias`: `credstash`
* Run `gcredstash setup`

## Environment variables

```sh
Expand All @@ -95,7 +78,6 @@ export AWS_SECRET_ACCESS_KEY=...
# default: credential-store
#export GCREDSTASH_TABLE=...


# default: alias/credstash
#export GCREDSTASH_KMS_KEY"),
```
11 changes: 11 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ package main

import (
"fmt"
"gcredstash"
"gcredstash/command"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/mitchellh/cli"
"os"
)

func Run(args []string) int {
// Meta-option for executables.
// It defines output color and its stdout/stderr stream.

awsSession := session.New()

meta := &command.Meta{
Ui: &cli.ColoredUi{
InfoColor: cli.UiColorBlue,
Expand All @@ -22,6 +29,10 @@ func Run(args []string) int {
},
Table: os.Getenv("GCREDSTASH_TABLE"),
KmsKey: os.Getenv("GCREDSTASH_KMS_KEY"),
Driver: &gcredstash.Driver{
Ddb: dynamodb.New(awsSession),
Kms: kms.New(awsSession),
},
}

if meta.Table == "" {
Expand Down
5 changes: 0 additions & 5 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ func Commands(meta *command.Meta) map[string]cli.CommandFactory {
Meta: *meta,
}, nil
},
"env": func() (cli.Command, error) {
return &command.EnvCommand{
Meta: *meta,
}, nil
},
"get": func() (cli.Command, error) {
return &command.GetCommand{
Meta: *meta,
Expand Down
28 changes: 28 additions & 0 deletions src/gcredstash/base64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gcredstash

import (
"encoding/base64"
)

func B64Decode(encoded string) []byte {
decoded, err := base64.StdEncoding.DecodeString(encoded)

if err != nil {
panic(err)
}

return decoded
}

func B64DecodeStr(encoded string) string {
decoded := B64Decode(encoded)
return string(decoded)
}

func B64Encode(decoded []byte) string {
return base64.StdEncoding.EncodeToString(decoded)
}

func B64EncodeStr(decoded string) string {
return B64Encode([]byte(decoded))
}
24 changes: 24 additions & 0 deletions src/gcredstash/base64_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package gcredstash

import (
. "gcredstash"
"testing"
)

func TestB64DecodeStr(t *testing.T) {
expected := "London Bridge is broken down"
actual := B64DecodeStr("TG9uZG9uIEJyaWRnZSBpcyBicm9rZW4gZG93bg==")

if expected != actual {
t.Errorf("\nexpected: %v\ngot: %v\n", expected, actual)
}
}

func TestB64EncodeStr(t *testing.T) {
expected := "TG9uZG9uIEJyaWRnZSBpcyBicm9rZW4gZG93bg=="
actual := B64EncodeStr("London Bridge is broken down")

if expected != actual {
t.Errorf("\nexpected: %v\ngot: %v\n", expected, actual)
}
}
36 changes: 29 additions & 7 deletions src/gcredstash/command/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,44 @@ type DeleteCommand struct {
Meta
}

func (c *DeleteCommand) Run(args []string) int {
newArgs, version, err := gcredstash.PerseVersion(args)
func (c *DeleteCommand) parseArgs(args []string) (string, string, error) {
newArgs, version, err := gcredstash.ParseVersion(args)

if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
return 1
return "", "", err
}

if len(newArgs) < 1 {
fmt.Fprintf(os.Stderr, "error: too few arguments\n")
return 1
return "", "", fmt.Errorf("too few arguments")
}

if len(newArgs) > 1 {
return "", "", fmt.Errorf("too many arguments")
}

credential := args[0]

err = gcredstash.DeleteSecrets(credential, version, c.Meta.Table)
return credential, version, nil
}

func (c *DeleteCommand) RunImpl(args []string) error {
credential, version, err := c.parseArgs(args)

if err != nil {
return err
}

err = c.Driver.DeleteSecrets(credential, version, c.Meta.Table)

if err != nil {
return err
}

return nil
}

func (c *DeleteCommand) Run(args []string) int {
err := c.RunImpl(args)

if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
Expand Down
67 changes: 67 additions & 0 deletions src/gcredstash/command/detele_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package command

import (
"gcredstash"
. "gcredstash/command"
"gcredstash/testutils"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/golang/mock/gomock"
"mockaws"
"testing"
)

func TestDeleteCommand(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mddb := mockaws.NewMockDynamoDBAPI(ctrl)
mkms := mockaws.NewMockKMSAPI(ctrl)
table := "credential-store"
name := "test.key"
version := "0000000000000000002"

item := map[string]string{
"contents": "eBtO1lgLxIe6Yw==",
"hmac": "b23a3efafd4795e50ca87afd7d764f263e9ae456499a8d40eece70a63ed5da27",
"key": "CiDY1vsR456LEdoL3+0p+PrTCleoqi/sutbDfJZNiUSpphLLAQEBAQB42Nb7EeOeixHaC9/tKfj60wpXqKov7LrWw3yWTYlEqaYAAACiMIGfBgkqhkiG9w0BBwaggZEwgY4CAQAwgYgGCSqGSIb3DQEHATAeBglghkgBZQMEAS4wEQQMy/Oc2pOJsR0y9nbhAgEQgFsHECqku7QZiRjLmmeGyhcsgWdWvi7Op3luJu4soi5sP0pqcsjTrBJqOXHLazgyBS9wb6deP8zpXa/41WT0ZpNY9at4gw7+XRtbz8f4Rlh8WnyFnK5RZ7i0mOlD",
"name": name,
"version": version,
}

mddb.EXPECT().Query(&dynamodb.QueryInput{
TableName: aws.String(table),
ConsistentRead: aws.Bool(true),
KeyConditionExpression: aws.String("#name = :name"),
ExpressionAttributeNames: map[string]*string{"#name": aws.String("name")},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":name": {S: aws.String(name)},
},
}).Return(&dynamodb.QueryOutput{
Count: aws.Int64(1),
Items: []map[string]*dynamodb.AttributeValue{testutils.MapToItem(item)},
}, nil)

mddb.EXPECT().DeleteItem(&dynamodb.DeleteItemInput{
TableName: aws.String(table),
Key: map[string]*dynamodb.AttributeValue{
"name": {S: aws.String(name)},
"version": {S: aws.String(version)},
},
}).Return(nil, nil)

cmd := &DeleteCommand{
Meta: Meta{
Table: table,
KmsKey: "alias/credstash",
Driver: &gcredstash.Driver{Ddb: mddb, Kms: mkms},
},
}

args := []string{name}
err := cmd.RunImpl(args)

if err != nil {
t.Errorf("\nexpected: %v\ngot: %v\n", nil, err)
}
}
Loading

0 comments on commit cfc9ccb

Please sign in to comment.