Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6663b268c8 | ||
|
|
6753422f7a | ||
|
|
64101e00d2 |
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
# Reef
|
# Reef
|
||||||
|
|
||||||
A shared library for Hammerhead, Blacktip, and Bamboo
|
A shared library for Hammerhead, Blacktip, Bamboo and Sandbar
|
||||||
|
|
||||||
Shared types and building blocks for the sharks stack: [Hammerhead](https://git.veillax.com/Veillax/hammerhead) (control plane), [Blacktip](https://git.veillax.com/Veillax/blacktip) (edge) and [Bamboo](https://git.veillax.com/Veillax/bamboo) (site agent).
|
Shared types and building blocks for the sharks stack: [Hammerhead](https://git.veillax.com/Veillax/hammerhead) (control plane), [Blacktip](https://git.veillax.com/Veillax/blacktip) (edge) and [Bamboo](https://git.veillax.com/Veillax/bamboo) (site agent).
|
||||||
|
|
||||||
@@ -15,6 +15,7 @@ Reef holds the code all three programs agree on, so each can be built and releas
|
|||||||
| `netguard` | Refuses internal addresses for anything that connects out on a user's behalf (webhooks, mail delivery), and dials the address it checked |
|
| `netguard` | Refuses internal addresses for anything that connects out on a user's behalf (webhooks, mail delivery), and dials the address it checked |
|
||||||
| `docker` | Lists running containers through the Docker Engine API, with no SDK |
|
| `docker` | Lists running containers through the Docker Engine API, with no SDK |
|
||||||
| `dotenv` | Loads a `.env` file into the environment without overriding real variables |
|
| `dotenv` | Loads a `.env` file into the environment without overriding real variables |
|
||||||
|
| `secretbox` | AES-256-GCM sealing of small secrets (`enc1:` values) before they are written to a database |
|
||||||
|
|
||||||
## Using it
|
## Using it
|
||||||
|
|
||||||
|
|||||||
+28
-1
@@ -1,7 +1,10 @@
|
|||||||
// Package model holds the types shared by hammerhead, blacktip and bamboo.
|
// Package model holds the types shared by hammerhead, blacktip and bamboo.
|
||||||
package model
|
package model
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ProtoTCP = "tcp" // raw TCP on ip:port
|
ProtoTCP = "tcp" // raw TCP on ip:port
|
||||||
@@ -19,6 +22,21 @@ func (m Mapping) LastPort() int {
|
|||||||
return m.Port
|
return m.Port
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsRedirect reports whether the mapping answers with a redirect instead of forwarding to a target.
|
||||||
|
func (m Mapping) IsRedirect() bool { return m.RedirectTo != "" }
|
||||||
|
|
||||||
|
// RedirectLocation is where a request for requestURI (path and query) is sent. With KeepPath the
|
||||||
|
// request's path and query follow the destination, so example.com/a?b=1 goes to to/a?b=1.
|
||||||
|
func (m Mapping) RedirectLocation(requestURI string) string {
|
||||||
|
if !m.RedirectKeepPath {
|
||||||
|
return m.RedirectTo
|
||||||
|
}
|
||||||
|
if requestURI == "" || requestURI[0] != '/' {
|
||||||
|
requestURI = "/" + requestURI
|
||||||
|
}
|
||||||
|
return strings.TrimRight(m.RedirectTo, "/") + requestURI
|
||||||
|
}
|
||||||
|
|
||||||
// HostBased reports whether mappings of this protocol are routed by hostname.
|
// HostBased reports whether mappings of this protocol are routed by hostname.
|
||||||
func HostBased(proto string) bool {
|
func HostBased(proto string) bool {
|
||||||
return proto == ProtoHTTP || proto == ProtoHTTPS || proto == ProtoTLS
|
return proto == ProtoHTTP || proto == ProtoHTTPS || proto == ProtoTLS
|
||||||
@@ -61,6 +79,8 @@ type Site struct {
|
|||||||
Local bool `json:"local"`
|
Local bool `json:"local"`
|
||||||
EdgeID string `json:"edgeId,omitempty"` // the edge a local site belongs to
|
EdgeID string `json:"edgeId,omitempty"` // the edge a local site belongs to
|
||||||
|
|
||||||
|
Tags []string `json:"tags,omitempty"` // labels for grouping and filtering in the dashboard
|
||||||
|
|
||||||
TunnelIP string `json:"tunnelIp,omitempty"` // the site's address inside the tunnel network
|
TunnelIP string `json:"tunnelIp,omitempty"` // the site's address inside the tunnel network
|
||||||
PublicKey string `json:"publicKey,omitempty"` // WireGuard public key, reported by the agent
|
PublicKey string `json:"publicKey,omitempty"` // WireGuard public key, reported by the agent
|
||||||
|
|
||||||
@@ -125,10 +145,17 @@ type Mapping struct {
|
|||||||
Hostname string `json:"hostname,omitempty"` // web/tls only; "*.example.com" allowed
|
Hostname string `json:"hostname,omitempty"` // web/tls only; "*.example.com" allowed
|
||||||
Targets []Target `json:"targets"` // udp uses only the first
|
Targets []Target `json:"targets"` // udp uses only the first
|
||||||
Enabled bool `json:"enabled"`
|
Enabled bool `json:"enabled"`
|
||||||
|
Tags []string `json:"tags,omitempty"` // labels for grouping and filtering in the dashboard
|
||||||
// ProxyProtocol ("v1" or "v2") makes the edge start each tcp/tls connection to the
|
// ProxyProtocol ("v1" or "v2") makes the edge start each tcp/tls connection to the
|
||||||
// target with a PROXY protocol header carrying the real client address.
|
// target with a PROXY protocol header carrying the real client address.
|
||||||
ProxyProtocol string `json:"proxyProtocol,omitempty"`
|
ProxyProtocol string `json:"proxyProtocol,omitempty"`
|
||||||
|
|
||||||
|
// A web mapping with RedirectTo set sends visitors there instead of to a target (so it has no
|
||||||
|
// targets, login or health checks). It answers 301 when RedirectPermanent, otherwise 302.
|
||||||
|
RedirectTo string `json:"redirectTo,omitempty"`
|
||||||
|
RedirectPermanent bool `json:"redirectPermanent,omitempty"`
|
||||||
|
RedirectKeepPath bool `json:"redirectKeepPath,omitempty"`
|
||||||
|
|
||||||
// web (http/https) options
|
// web (http/https) options
|
||||||
Auth Auth `json:"auth"`
|
Auth Auth `json:"auth"`
|
||||||
AuthEpoch int64 `json:"authEpoch"` // bumped to sign every visitor out; set by hammerhead only
|
AuthEpoch int64 `json:"authEpoch"` // bumped to sign every visitor out; set by hammerhead only
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestRedirectLocation(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
m Mapping
|
||||||
|
uri string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{Mapping{RedirectTo: "https://new.example.com"}, "/a?b=1", "https://new.example.com"},
|
||||||
|
{Mapping{RedirectTo: "https://new.example.com", RedirectKeepPath: true}, "/a?b=1", "https://new.example.com/a?b=1"},
|
||||||
|
{Mapping{RedirectTo: "https://new.example.com/", RedirectKeepPath: true}, "/", "https://new.example.com/"},
|
||||||
|
{Mapping{RedirectTo: "https://new.example.com/base", RedirectKeepPath: true}, "/x/y", "https://new.example.com/base/x/y"},
|
||||||
|
{Mapping{RedirectTo: "https://new.example.com", RedirectKeepPath: true}, "", "https://new.example.com/"},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
if got := c.m.RedirectLocation(c.uri); got != c.want {
|
||||||
|
t.Errorf("%+v %q: got %q, want %q", c.m, c.uri, got, c.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (Mapping{}).IsRedirect() || !(Mapping{RedirectTo: "https://x"}).IsRedirect() {
|
||||||
|
t.Error("IsRedirect is wrong")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
// Package secretbox encrypts small secrets (tokens, passwords, private keys)
|
||||||
|
// before they are written to the database, using AES-256-GCM.
|
||||||
|
//
|
||||||
|
// Sealed values look like "enc1:<base64>". Opening a value that isn't sealed
|
||||||
|
// returns it unchanged, so data written before encryption was turned on is
|
||||||
|
// still readable and gets sealed the next time it is saved.
|
||||||
|
package secretbox
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const prefix = "enc1:"
|
||||||
|
|
||||||
|
type Box struct{ aead cipher.AEAD }
|
||||||
|
|
||||||
|
// GenerateKey returns a new random key in the form New accepts.
|
||||||
|
func GenerateKey() string {
|
||||||
|
b := make([]byte, 32)
|
||||||
|
rand.Read(b)
|
||||||
|
return base64.StdEncoding.EncodeToString(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// New builds a Box from a key given as base64 or hex of 32 bytes. Any other
|
||||||
|
// text is treated as a passphrase and stretched with SHA-256.
|
||||||
|
func New(key string) (*Box, error) {
|
||||||
|
key = strings.TrimSpace(key)
|
||||||
|
if key == "" {
|
||||||
|
return nil, errors.New("empty key")
|
||||||
|
}
|
||||||
|
raw := decodeKey(key)
|
||||||
|
block, err := aes.NewCipher(raw)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
aead, err := cipher.NewGCM(block)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Box{aead: aead}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeKey(key string) []byte {
|
||||||
|
if b, err := base64.StdEncoding.DecodeString(key); err == nil && len(b) == 32 {
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
if b, err := hex.DecodeString(key); err == nil && len(b) == 32 {
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
sum := sha256.Sum256([]byte(key))
|
||||||
|
return sum[:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Seal encrypts s. The empty string stays empty so unset secrets stay visibly unset.
|
||||||
|
func (b *Box) Seal(s string) string {
|
||||||
|
if s == "" || strings.HasPrefix(s, prefix) {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
nonce := make([]byte, b.aead.NonceSize())
|
||||||
|
rand.Read(nonce)
|
||||||
|
return prefix + base64.StdEncoding.EncodeToString(b.aead.Seal(nonce, nonce, []byte(s), nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open decrypts a sealed value. A wrong key or damaged data is an error, so a
|
||||||
|
// bad key can never silently turn secrets into garbage.
|
||||||
|
func (b *Box) Open(s string) (string, error) {
|
||||||
|
if !strings.HasPrefix(s, prefix) {
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
raw, err := base64.StdEncoding.DecodeString(s[len(prefix):])
|
||||||
|
if err != nil || len(raw) < b.aead.NonceSize() {
|
||||||
|
return "", errors.New("damaged secret")
|
||||||
|
}
|
||||||
|
n := b.aead.NonceSize()
|
||||||
|
plain, err := b.aead.Open(nil, raw[:n], raw[n:], nil)
|
||||||
|
if err != nil {
|
||||||
|
return "", errors.New("can't decrypt a stored secret: is HAMMERHEAD_MASTER_KEY the key it was saved with?")
|
||||||
|
}
|
||||||
|
return string(plain), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package secretbox
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSealOpenRoundTripAndKeys(t *testing.T) {
|
||||||
|
key := GenerateKey()
|
||||||
|
b, err := New(key)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
sealed := b.Seal("hunter2")
|
||||||
|
if !strings.HasPrefix(sealed, "enc1:") || strings.Contains(sealed, "hunter2") {
|
||||||
|
t.Fatalf("not sealed: %q", sealed)
|
||||||
|
}
|
||||||
|
if again := b.Seal("hunter2"); again == sealed {
|
||||||
|
t.Error("two seals of the same value must differ (random nonce)")
|
||||||
|
}
|
||||||
|
if b.Seal(sealed) != sealed {
|
||||||
|
t.Error("sealing an already sealed value must not double-encrypt")
|
||||||
|
}
|
||||||
|
if got, err := b.Open(sealed); err != nil || got != "hunter2" {
|
||||||
|
t.Errorf("Open = %q, %v", got, err)
|
||||||
|
}
|
||||||
|
if got, _ := b.Open("legacy plaintext"); got != "legacy plaintext" {
|
||||||
|
t.Errorf("unsealed legacy data should pass through, got %q", got)
|
||||||
|
}
|
||||||
|
if b.Seal("") != "" {
|
||||||
|
t.Error("empty stays empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
other, _ := New(GenerateKey())
|
||||||
|
if _, err := other.Open(sealed); err == nil {
|
||||||
|
t.Error("the wrong key must fail loudly, not return garbage")
|
||||||
|
}
|
||||||
|
if _, err := b.Open(sealed[:len(sealed)-4] + "AAAA"); err == nil {
|
||||||
|
t.Error("tampered data must fail")
|
||||||
|
}
|
||||||
|
|
||||||
|
// a passphrase works as a key, and is stable
|
||||||
|
p1, _ := New("correct horse battery staple")
|
||||||
|
p2, _ := New("correct horse battery staple")
|
||||||
|
if got, err := p2.Open(p1.Seal("x")); err != nil || got != "x" {
|
||||||
|
t.Errorf("passphrase keys must be reproducible: %q %v", got, err)
|
||||||
|
}
|
||||||
|
if _, err := New(" "); err == nil {
|
||||||
|
t.Error("an empty key must be rejected")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user