Skip to content

Commit

Permalink
Goreleaser work
Browse files Browse the repository at this point in the history
- Split client commands into distinct packages
- Adding in goreleaser w/ docker build
- Adding in additional Makefile targets
  • Loading branch information
Justin Nauman committed May 1, 2018
1 parent 4819714 commit 6e9426f
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 113 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
dist/
k8-kms-enc-provider
30 changes: 30 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
project_name: k8-kms-enc-provider

builds:
- binary: k8-kms-enc-provider
main: .
ldflags: -s -w -X github.com/jrnt30/k8-kms-enc-provider/cmd.GitCommit={{.Commit}}
lang: go
env:
- CGO_ENABLED=0
goos:
- linux
- darwin
goarch:
- amd64

dockers:
- image: jrnt30/k8-kms-enc-provider
dockerfile: Dockerfile.gorelease
tag_templates:
- "{{ .Tag }}"
- latest

release:
github:
owner: jrnt30
name: k8-kms-enc-provider
name_template: '{{.Tag}}'

git:
short_hash: true
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ RUN dep ensure && \
CGO_ENABLED=0 go build --ldflags '-extldflags "-static"' -o k8-kms-enc-provider .

FROM alpine:3.7
RUN adduser -D -u 10000 k8-kms-enc-provider
RUN apk add --no-cache ca-certificates
COPY --from=builder /go/src/github.com/jrnt30/k8-kms-enc-provider/k8-kms-enc-provider /usr/local/bin/k8-kms-enc-provider

ENTRYPOINT ["/usr/local/bin/k8-kms-enc-provider"]
COPY --from=builder /go/src/github.com/jrnt30/k8-kms-enc-provider/k8-kms-enc-provider /
USER k8-kms-enc-provider
ENTRYPOINT ["/k8-kms-enc-provider"]
8 changes: 8 additions & 0 deletions Dockerfile.gorelease
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM alpine:3.7

RUN adduser -D -u 10000 k8-kms-enc-provider
RUN apk add --no-cache ca-certificates

COPY k8-kms-enc-provider /
USER k8-kms-enc-provider
ENTRYPOINT ["/k8-kms-enc-provider"]
43 changes: 35 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
default: ; @true
# Heavily inspired by https://github.com/heptio/authenticator/ Makefile/GoReleaser
default: build
GORELEASER := $(shell command -v goreleaser 2> /dev/null)
PROTOC := $(shell command -v protoc 2> /dev/null)

# TODO
# For each .proto file, ensure there is a correpsonding .db.go file present
.PHONY: protos
protos:
protoc --go_out plugins=grpc:generated/ --proto_path proto/ proto/*.proto
.PHONY: build test format check-formatting check-tools generate-protobufs

docker-build:
docker build -t kms-server .
check: check-formatting check-tools

check-tools:
ifndef GORELEASER
$(error "goreleaser not found (`go get -u -v github.com/goreleaser/goreleaser` to fix)")
endif
ifndef PROTOC
$(error "PROTOC not found (`go get -u -v github.com/goreleaser/goreleaser` to fix)")
endif
@true

check-formatting:
@if [ ! `find . -path ./vendor -prune -type f -o -name '*.go' -exec gofmt -l {} + | wc -l` -eq 0 ]; then \
echo "Changes present in go files. Run 'make format' to clean"; \
exit 127; \
fi

format:
find . -path ./vendor -prune -type f -o -name '*.go' -exec gofmt -w {} +;

release: check-tools
$(GORELEASER) --rm-dist

generate-protobufs: check-tools
$(PROTOC) --go_out plugins=grpc:v1beta1/ --proto_path proto/ proto/*.proto

build-local: check-tools
$(GORELEASER) release --skip-publish

build: check-tools generate-protobufs format
$(GORELEASER) --skip-publish --rm-dist --snapshot
28 changes: 8 additions & 20 deletions cmd/client.go → cmd/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,23 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package cmd
package client

import (
"github.com/spf13/cobra"

"github.com/jrnt30/k8-kms-enc-provider/cmd"
)

var socketPath string

// clientCmd represents the client command
var clientCmd = &cobra.Command{
Use: "client",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "Client commands to test interactions with the server",
}

func init() {
RootCmd.AddCommand(clientCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// clientCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// clientCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

cmd.RootCmd.AddCommand(clientCmd)
clientCmd.PersistentFlags().StringVar(&socketPath, "socket", "/tmp/kms-grpc", "path to the socket to use")
}
20 changes: 3 additions & 17 deletions cmd/decrypt.go → cmd/client/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,18 @@ import (
"net"
"time"

"github.com/jrnt30/k8-kms-enc-provider/v1beta1"
"github.com/spf13/cobra"
"google.golang.org/grpc"

"github.com/jrnt30/k8-kms-enc-provider/v1beta1"
)

var cipherText string

// decryptCmd represents the decrypt command
var decryptCmd = &cobra.Command{
Use: "decrypt",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "Allows for the decryption of cipher-text that was encrypted previously",
Run: func(cmd *cobra.Command, args []string) {
var err error

Expand Down Expand Up @@ -73,15 +68,6 @@ to quickly create a Cobra application.`,

func init() {
clientCmd.AddCommand(decryptCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// decryptCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
decryptCmd.Flags().StringVar(&cipherText, "cipher-text", "", "Ciphertext To Decrypt")
decryptCmd.MarkFlagRequired("cipher-text")
}
11 changes: 3 additions & 8 deletions cmd/encrypt.go → cmd/client/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,18 @@ import (
"net"
"time"

"github.com/jrnt30/k8-kms-enc-provider/v1beta1"
"github.com/spf13/cobra"
"google.golang.org/grpc"

"github.com/jrnt30/k8-kms-enc-provider/v1beta1"
)

var plainText string

// encryptCmd represents the encrypt command
var encryptCmd = &cobra.Command{
Use: "encrypt",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "Allows for the encryption of plain-text",
Run: func(cmd *cobra.Command, args []string) {
var err error

Expand Down
64 changes: 64 additions & 0 deletions cmd/client/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright © 2018 Justin Nauman <justin@spantree.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package cmd

import (
"context"
"fmt"
"log"
"net"
"time"

"github.com/spf13/cobra"
"google.golang.org/grpc"

"github.com/jrnt30/k8-kms-enc-provider/v1beta1"
)

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Executes the server's Version endpoint",
Run: func(cmd *cobra.Command, args []string) {
var err error

gc, err := grpc.Dial(socketPath,
grpc.WithInsecure(),
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("unix", addr, timeout)
}))

if err != nil {
log.Fatal(err)
}
client := v1beta1.NewKeyManagementServiceClient(gc)

resp, err := client.Version(context.Background(), &v1beta1.VersionRequest{})
if err != nil {
log.Fatal(err)
}
fmt.Println("Got response: ", resp)
},
}

func init() {
clientCmd.AddCommand(versionCmd)
}
24 changes: 8 additions & 16 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,18 @@ import (
"github.com/spf13/viper"
)

var socketPath string
// GitCommit is injected via the normal build process
// with the git commit hash for the `version` command
var GitCommit string
var cfgFile string

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "k8-kms-enc-provider",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Short: "Provides an implementation of the K8 KMS provider",
Long: `Provides an implemenation of the K8 KMS that leverages AWS KMS as the encryption provider.
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Additionally there is a test Client available to test some of the functionality of the server manually.`,
}

// Execute adds all child commands to the root command sets flags appropriately.
Expand All @@ -58,11 +54,7 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags, which, if defined here,
// will be global for your application.
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "configuration file to use (default is ~/.k8-kms-enc-provider)")
RootCmd.PersistentFlags().StringVar(&socketPath, "socket", "/tmp/kms-grpc", "path to the socket to use")
}

// initConfig reads in config file and ENV variables if set.
Expand All @@ -72,8 +64,8 @@ func initConfig() {
}

viper.SetConfigName(".k8-kms-enc-provider") // name of config file (without extension)
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
Expand Down
15 changes: 6 additions & 9 deletions cmd/server.go → cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package cmd
package server

import (
"log"
Expand All @@ -32,23 +32,19 @@ import (
"github.com/spf13/cobra"
"google.golang.org/grpc"

"github.com/jrnt30/k8-kms-enc-provider/cmd"
"github.com/jrnt30/k8-kms-enc-provider/pkg"
"github.com/jrnt30/k8-kms-enc-provider/v1beta1"
)

var awsRegion string
var keyID string
var socketPath string

// serverCmd represents the server command
var serverCmd = &cobra.Command{
Use: "server",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "Launches the K8 KMS server component that listens on a socket",
Run: func(cmd *cobra.Command, args []string) {
keyProviderServer, err := pkg.NewAwsKmsProvider(&pkg.AwsKmsProviderConfiguration{
AwsRegion: aws.String(awsRegion),
Expand Down Expand Up @@ -95,8 +91,9 @@ to quickly create a Cobra application.`,
}

func init() {
RootCmd.AddCommand(serverCmd)
cmd.RootCmd.AddCommand(serverCmd)

serverCmd.Flags().StringVar(&socketPath, "socket", "/tmp/kms-grpc", "path to the socket to use")
serverCmd.Flags().StringVar(&awsRegion, "region", "", "Region to load the associated KMS Key from")
serverCmd.Flags().StringVar(&keyID, "key-id", "", "KMS Key Identifier (ID or ARN) to be used for encryption")
serverCmd.MarkFlagRequired("region")
Expand Down
Loading

0 comments on commit 6e9426f

Please sign in to comment.