Skip to content

Commit

Permalink
Merge pull request #14 from pteich/support_caddyfile_syntax
Browse files Browse the repository at this point in the history
Support for Caddyfile configuration
  • Loading branch information
pteich authored Oct 8, 2020
2 parents 9b3eabb + 9e9ac15 commit d79c7d6
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 312 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.14 AS builder
FROM golang:1.15 AS builder

WORKDIR /workspace
RUN echo 'package main\n\
Expand All @@ -11,7 +11,7 @@ func main() {\n\
caddycmd.Main()\n\
}' > main.go && \
go env -w GOPROXY="https://goproxy.io,direct" && \
go mod init caddy && go get github.com/caddyserver/caddy/v2@master && go get && \
go mod init caddy && go get github.com/caddyserver/caddy/v2@v2.2.0 && go get && \
CGO_ENABLED=0 go build -trimpath -tags netgo -ldflags '-extldflags "-static" -s -w' -o /usr/bin/caddy


Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
DefaultValuePrefix = "caddy-storage-consul"

// DefaultTimeout is the default timeout for Consul connections
DefaultTimeout = 10 * time.Second
DefaultTimeout = 10

// EnvNameAESKey defines the env variable name to override AES key
EnvNameAESKey = "CADDY_CLUSTERING_CONSUL_AESKEY"
Expand Down
14 changes: 7 additions & 7 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (

func (s *Storage) encrypt(bytes []byte) ([]byte, error) {
// No key? No encrypt
if len(s.config.AESKey) == 0 {
if len(s.AESKey) == 0 {
return bytes, nil
}

c, err := aes.NewCipher(s.config.AESKey)
c, err := aes.NewCipher(s.AESKey)
if err != nil {
return nil, fmt.Errorf("unable to create AES cipher: %w", err)
}
Expand All @@ -42,20 +42,20 @@ func (s *Storage) EncryptStorageData(data *StorageData) ([]byte, error) {
}

// Prefix with simple prefix and then encrypt
bytes = append([]byte(s.config.ValuePrefix), bytes...)
bytes = append([]byte(s.ValuePrefix), bytes...)
return s.encrypt(bytes)
}

func (s *Storage) decrypt(bytes []byte) ([]byte, error) {
// No key? No decrypt
if len(s.config.AESKey) == 0 {
if len(s.AESKey) == 0 {
return bytes, nil
}
if len(bytes) < aes.BlockSize {
return nil, fmt.Errorf("invalid contents")
}

block, err := aes.NewCipher(s.config.AESKey)
block, err := aes.NewCipher(s.AESKey)
if err != nil {
return nil, fmt.Errorf("unable to create AES cipher: %w", err)
}
Expand All @@ -81,13 +81,13 @@ func (s *Storage) DecryptStorageData(bytes []byte) (*StorageData, error) {
}

// Simple sanity check of the beginning of the byte array just to check
if len(bytes) < len(s.config.ValuePrefix) || string(bytes[:len(s.config.ValuePrefix)]) != s.config.ValuePrefix {
if len(bytes) < len(s.ValuePrefix) || string(bytes[:len(s.ValuePrefix)]) != s.ValuePrefix {
return nil, fmt.Errorf("invalid data format")
}

// Now just json unmarshal
data := &StorageData{}
if err := json.Unmarshal(bytes[len(s.config.ValuePrefix):], data); err != nil {
if err := json.Unmarshal(bytes[len(s.ValuePrefix):], data); err != nil {
return nil, fmt.Errorf("unable to unmarshal result: %w", err)
}
return data, nil
Expand Down
28 changes: 15 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ module github.com/pteich/caddy-tlsconsul
go 1.14

require (
github.com/armon/go-metrics v0.3.3 // indirect
github.com/caddyserver/caddy/v2 v2.0.1-0.20200605181936-1dfb11486eac
github.com/caddyserver/certmagic v0.11.1
github.com/cenkalti/backoff/v4 v4.0.2 // indirect
github.com/hashicorp/consul/api v1.4.0
github.com/armon/go-metrics v0.3.4 // indirect
github.com/caddyserver/caddy/v2 v2.2.0
github.com/caddyserver/certmagic v0.12.0
github.com/hashicorp/consul/api v1.7.0
github.com/hashicorp/go-hclog v0.14.1 // indirect
github.com/hashicorp/go-immutable-radix v1.2.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.0 // indirect
github.com/hashicorp/go.net v0.0.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/serf v0.9.2 // indirect
github.com/miekg/dns v1.1.29 // indirect
github.com/mitchellh/mapstructure v1.3.1 // indirect
github.com/hashicorp/serf v0.9.5 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mitchellh/gox v0.4.0 // indirect
github.com/mitchellh/iochan v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.3.3 // indirect
github.com/stretchr/testify v1.5.1
go.uber.org/zap v1.15.0
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 // indirect
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 // indirect
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect
gopkg.in/square/go-jose.v2 v2.5.1 // indirect
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/sys v0.0.0-20201008064518-c1f3e3309c71 // indirect
golang.org/x/tools v0.0.0-20200304193943-95d2e580d8eb // indirect
honnef.co/go/tools v0.0.1-2020.1.3 // indirect
)
Loading

0 comments on commit d79c7d6

Please sign in to comment.