Skip to content

Commit 5bd4b0b

Browse files
authored
Merge pull request #4 from hooksie1/new-micro
New micro
2 parents 745d5d4 + 5f216d0 commit 5bd4b0b

File tree

1,118 files changed

+413349
-811
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,118 files changed

+413349
-811
lines changed

.github/workflows/release.yaml

+16-46
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,28 @@
1-
name: release
2-
on: [push, pull_request]
1+
name: tagged release
2+
on:
3+
push:
4+
tags:
5+
- '*'
6+
permissions:
7+
id-token: write
8+
contents: read
39
jobs:
410
test:
5-
strategy:
6-
matrix:
7-
go-version: [ 1.22.x ]
8-
os: [ ubuntu-latest ]
9-
runs-on: ${{ matrix.os }}
10-
steps:
11-
- name: Install Go
12-
uses: actions/setup-go@v2
13-
with:
14-
go-version: ${{ matrix.go-version }}
15-
- name: Checkout code
16-
uses: actions/checkout@v2
17-
- uses: actions/cache@v2
18-
with:
19-
path: |
20-
~/go/pkg/mod
21-
~/.cache/go-build
22-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
23-
restore-keys: |
24-
${{ runner.os }}-go-
25-
- name: Test
26-
run: go test ./...
11+
uses: ./.github/workflows/test.yaml
2712
release:
13+
needs: [test]
2814
permissions:
2915
id-token: write
30-
contents: write
16+
contents: write
3117
runs-on: ubuntu-latest
32-
needs: test
33-
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
3418
steps:
35-
- name: Checkout
19+
- name: Checkout code
3620
uses: actions/checkout@v2
37-
with:
38-
fetch-depth: 0
39-
- name: Set up Go
40-
uses: actions/setup-go@v2
41-
with:
42-
go-version: 1.22
43-
- uses: actions/cache@v2
44-
with:
45-
path: |
46-
~/go/pkg/mod
47-
~/.cache/go-build
48-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
49-
restore-keys: |
50-
${{ runner.os }}-go-
5121
- name: Run GoReleaser
52-
uses: goreleaser/goreleaser-action@v2
22+
uses: goreleaser/goreleaser-action@v5
5323
with:
5424
distribution: goreleaser
55-
version: 'v1.2.5'
56-
args: release --rm-dist
25+
version: latest
26+
args: release --clean
5727
env:
58-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28+
GITHUB_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}

.github/workflows/test.yaml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: test
2+
on:
3+
push:
4+
paths:
5+
- '**.go'
6+
workflow_call:
7+
jobs:
8+
test:
9+
strategy:
10+
matrix:
11+
go-version: [ 1.22.x ]
12+
os: [ ubuntu-latest ]
13+
runs-on: ${{ matrix.os }}
14+
steps:
15+
- name: Install Go
16+
uses: actions/setup-go@v2
17+
with:
18+
go-version: ${{ matrix.go-version }}
19+
- name: Checkout code
20+
uses: actions/checkout@v2
21+
- name: Test
22+
run: make test
23+
- name: Coverage
24+
run: make coverage
25+
- name: store coverage
26+
uses: actions/upload-artifact@v2
27+
with:
28+
name: test-coverage
29+
path: ./coverage.html

Dockerfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM golang:alpine as builder
2+
WORKDIR /app
3+
ENV IMAGE_TAG=dev
4+
RUN apk update && apk upgrade && apk add --no-cache ca-certificates git
5+
RUN update-ca-certificates
6+
ADD . /app/
7+
RUN CGO_ENABLED=0 GOOS=linux go build -mod=vendor -a -ldflags="-s -w -X 'github.com/hooksie1/piggybank/cmd.Version=$(printf $(git describe --tags | cut -d '-' -f 1)-$(git rev-parse --short HEAD))'" -installsuffix cgo -o piggybankctl .
8+
9+
10+
FROM scratch
11+
12+
COPY --from=builder /app/piggybankctl .
13+
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
14+
15+
ENTRYPOINT ["./piggybankctl"]

Makefile

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
PROJECT_NAME := "piggybank"
2+
PKG := "github.com/hooksie1/piggybank"
3+
PKG_LIST := $(shell go list ${PKG}/... | grep -v /vendor/)
4+
GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v _test.go)
5+
VERSION := $(shell if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then git describe --exact-match --tags HEAD 2>/dev/null || echo "dev-$(shell git rev-parse --short HEAD)"; else echo "dev"; fi)
6+
GOOS=$(shell go env GOOS)
7+
GOARCH=$(shell go env GOARCH)
8+
9+
.PHONY: all build docker deps clean test coverage lint docker-local edgedb k8s-up k8s-down docker-delete docs update-local deploy-local
10+
11+
all: build
12+
13+
deps: ## Get dependencies
14+
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
15+
16+
lint: deps ## Lint the files
17+
go vet
18+
gocyclo -over 10 -ignore "generated" ./
19+
20+
test: lint ## Run unittests
21+
go test -v ./...
22+
23+
coverage: ## Create test coverage report
24+
go test -cover ./...
25+
go test ./... -coverprofile=cover.out && go tool cover -html=cover.out -o coverage.html
26+
27+
goreleaser: tidy ## Creates local multiarch releases with GoReleaser
28+
goreleaser release --snapshot --rm-dist
29+
30+
tidy: ## Pull in dependencies
31+
go mod tidy && go mod vendor
32+
33+
fmt: ## Format All files
34+
go fmt ./...
35+
36+
piggybankctl: ## Builds the binary on the current platform
37+
go build -mod=vendor -a -ldflags "-w -X '$(PKG)/cmd.Version=$(VERSION)'" -o $(PROJECT_NAME)ctl
38+
39+
docs: ## Builds the cli documentation
40+
mkdir -p docs
41+
./piggybankctl docs
42+
43+
schema: ## Generates boilerplate code from the graph/schema.graphqls file
44+
go run github.com/99designs/gqlgen update
45+
46+
clean: ## Remove previous build
47+
git clean -fd
48+
git clean -fx
49+
git reset --hard
50+
51+
help: ## Display this help screen
52+
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

README.md

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Piggy Bank
22

3-
Piggy Bank is a secrets storage tool for applications that works with NATS. Secrets are stored encrypted in JetStream and can be retrieved as long as the requestor has access to the subject.
3+
Piggy Bank is a secrets storage tool for applications that works with NATS. Secrets are stored encrypted in a JetStream KV and can be retrieved as long as the requestor has access to the subject.
44

55
A decryption key is returned from the initialization phase. If this key is lost, all of the data is unrecoverable.
66

@@ -10,18 +10,23 @@ Be sure to add the KV bucket to NATS: `nats kv add piggybank`
1010

1111
## Example Usage
1212

13-
1. Start piggybank `piggybank start`
14-
2. Initialize the database `nats req piggybankdb.initialize ""`
15-
3. Unlock the database with key sent from step 1 `nats req piggybankdb.unlock '{"database_key": "foobar"}'`
16-
4. Add a secret for an application `nats req -H method:post piggybank.myapplication.registrySecret "somesecrettext"`
17-
5. Retrieve a secret `nats req -H method:get piggybank.myapplication.registrySecret ""`
18-
6. Lock the database `nats req piggybankdb.lock ""`
19-
7. Try to retrieve the secret again `nats req -H method:get piggybank.myapplication.registrySecret ""`
13+
1. Start piggybank `piggybank service start`
14+
2. Initialize the database `piggybank client database initialize`
15+
3. Unlock the database with key sent from step 1 `piggybank client database unlock --key foo`
16+
4. Add a secret for an application `piggybank client secret add --id foo --value bar`
17+
5. Retrieve a secret `piggybank client secret get --id foo`
18+
6. Lock the database `piggybank client database lock`
19+
7. Try to retrieve the secret again `piggybank client secret get --id foo`
2020

2121
## Permissions
22-
Permissions are defined as normal NATS subject permissions. If you have access to a subject, then you can retrieve the secrets. This means the permissions can be as granular as desired.
22+
Permissions are defined as normal NATS subject permissions. If you have access to a subject, then you can retrieve the secrets. This means the permissions can be as granular as desired.
2323

24-
## Config
25-
Piggy Bank requires a config file. It uses Cue to read the configs, but the configs can also be in json or yaml format.
24+
NOTE: Please ensure to set proper permissions for inbox responses. It is recommended to not use the default _INBOX subject for responses and to set granular inboxes for requests to piggybank.
2625

27-
The Cue schema is in `cmd/schema.cue`.
26+
## NATS Connection
27+
28+
Piggybank supports multiple auth methods for NATS.
29+
30+
1. Your current NATS context
31+
2. A path to a credentials file
32+
3. Env vars for the JWT and SEED

cmd/client.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
var clientCmd = &cobra.Command{
8+
Use: "client",
9+
Short: "Client interactions with the service",
10+
PersistentPreRun: bindClientCmdFlags,
11+
}
12+
13+
func init() {
14+
rootCmd.AddCommand(clientCmd)
15+
natsFlags(clientCmd)
16+
}
17+
18+
func bindClientCmdFlags(cmd *cobra.Command, args []string) {
19+
bindNatsFlags(cmd)
20+
}

cmd/config.go

-73
This file was deleted.

cmd/database.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package cmd
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"time"
7+
8+
"github.com/hooksie1/piggybank/service"
9+
"github.com/spf13/cobra"
10+
"github.com/spf13/viper"
11+
)
12+
13+
var databaseCmd = &cobra.Command{
14+
Use: "database",
15+
Short: "Interact with the piggybank db, valid args are init, lock, unlock",
16+
RunE: database,
17+
Args: cobra.MatchAll(cobra.MinimumNArgs(1), cobra.OnlyValidArgs),
18+
ValidArgs: []string{"init", "lock", "unlock"},
19+
SilenceUsage: true,
20+
}
21+
22+
func init() {
23+
clientCmd.AddCommand(databaseCmd)
24+
databaseCmd.Flags().String("key", "", "Database key")
25+
viper.BindPFlag("key", databaseCmd.Flags().Lookup("key"))
26+
}
27+
28+
func database(cmd *cobra.Command, args []string) error {
29+
nc, err := newNatsConnection("piggy-client")
30+
if err != nil {
31+
return err
32+
}
33+
key := viper.GetString("key")
34+
35+
switch args[0] {
36+
case "init":
37+
msg, err := nc.Request("piggybank.database.initialize", nil, 1*time.Second)
38+
if err != nil {
39+
return err
40+
}
41+
42+
fmt.Println(string(msg.Data))
43+
return nil
44+
case "unlock":
45+
if key == "" {
46+
return fmt.Errorf("database key required")
47+
}
48+
49+
req := service.DatabaseKey{DBKey: key}
50+
51+
data, err := json.Marshal(req)
52+
if err != nil {
53+
return err
54+
}
55+
msg, err := nc.Request("piggybank.database.unlock", data, 1*time.Second)
56+
if err != nil {
57+
return err
58+
}
59+
60+
fmt.Println(string(msg.Data))
61+
62+
case "lock":
63+
msg, err := nc.Request("piggybank.database.lock", nil, 1*time.Second)
64+
if err != nil {
65+
return err
66+
}
67+
68+
fmt.Println(string(msg.Data))
69+
}
70+
71+
return nil
72+
}

0 commit comments

Comments
 (0)