mirror of
https://github.com/kerberos-io/agent.git
synced 2026-11-23 23:10:50 +00:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
33cc8a46ed | ||
|
|
7d70361870 | ||
|
|
9a9b5d051b | ||
|
|
1492eb6d6c | ||
|
|
b9cf130762 | ||
|
|
88c2166133 |
@@ -25,6 +25,27 @@ Directory decryption also processes dated subdirectories and writes each output
|
||||
to a sibling `decrypted/` directory; those outputs are excluded from recording
|
||||
lists and automatic cleanup.
|
||||
|
||||
## Recording encryption format
|
||||
|
||||
When recording encryption is enabled, recordings are written in a versioned
|
||||
format:
|
||||
|
||||
| Bytes | Field |
|
||||
|---|---|
|
||||
| 8 | Magic `KRBREC1\0` |
|
||||
| 4 | PBKDF2 iteration count (big-endian) |
|
||||
| 16 | Salt |
|
||||
| 12 | Nonce |
|
||||
| rest | AES-256-GCM ciphertext and 16-byte tag |
|
||||
|
||||
- **Key derivation:** the key is derived from the configured symmetric key with
|
||||
PBKDF2-HMAC-SHA256, currently 600,000 iterations.
|
||||
- **Authentication:** the header is authenticated together with the payload, so
|
||||
any change to a recording is detected.
|
||||
- **Older recordings:** recordings from releases before 3.14.5 use the
|
||||
OpenSSL/CryptoJS `Salted__` format. Playback and `-action decrypt` still read
|
||||
them, so no migration is needed.
|
||||
|
||||
## Direct S3 recording layout
|
||||
|
||||
Direct S3 uploads store recordings as
|
||||
|
||||
@@ -452,7 +452,7 @@ func HandleRecordStream(queue *packets.Queue, configDirectory string, configurat
|
||||
contents, err := os.ReadFile(fullName)
|
||||
if err == nil {
|
||||
// encrypt
|
||||
encryptedContents, err := encryption.AesEncrypt(contents, config.Encryption.SymmetricKey)
|
||||
encryptedContents, err := encryption.EncryptRecording(contents, config.Encryption.SymmetricKey)
|
||||
if err == nil {
|
||||
// write back to file
|
||||
err := os.WriteFile(fullName, []byte(encryptedContents), 0644)
|
||||
@@ -567,7 +567,7 @@ func HandleRecordStream(queue *packets.Queue, configDirectory string, configurat
|
||||
contents, err := os.ReadFile(fullName)
|
||||
if err == nil {
|
||||
// encrypt
|
||||
encryptedContents, err := encryption.AesEncrypt(contents, config.Encryption.SymmetricKey)
|
||||
encryptedContents, err := encryption.EncryptRecording(contents, config.Encryption.SymmetricKey)
|
||||
if err == nil {
|
||||
// write back to file
|
||||
err := os.WriteFile(fullName, []byte(encryptedContents), 0644)
|
||||
@@ -793,7 +793,7 @@ func HandleRecordStream(queue *packets.Queue, configDirectory string, configurat
|
||||
contents, err := os.ReadFile(fullName)
|
||||
if err == nil {
|
||||
// encrypt
|
||||
encryptedContents, err := encryption.AesEncrypt(contents, config.Encryption.SymmetricKey)
|
||||
encryptedContents, err := encryption.EncryptRecording(contents, config.Encryption.SymmetricKey)
|
||||
if err == nil {
|
||||
// write back to file
|
||||
err := os.WriteFile(fullName, []byte(encryptedContents), 0644)
|
||||
|
||||
@@ -26,6 +26,9 @@ func SignWithPrivateKey(data []byte, privateKey *rsa.PrivateKey) ([]byte, error)
|
||||
return signature, err
|
||||
}
|
||||
|
||||
// AesEncrypt writes the OpenSSL/CryptoJS "Salted__" format (MD5-based key
|
||||
// derivation). It is kept for MQTT messages with Kerberos Hub, whose keys are
|
||||
// random. Recordings use EncryptRecording instead.
|
||||
func AesEncrypt(content []byte, password string) ([]byte, error) {
|
||||
salt := make([]byte, 8)
|
||||
_, err := rand.Read(salt)
|
||||
@@ -33,6 +36,9 @@ func AesEncrypt(content []byte, password string) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
key, iv, err := DefaultEvpKDF([]byte(password), salt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
@@ -50,13 +56,18 @@ func AesEncrypt(content []byte, password string) ([]byte, error) {
|
||||
return cipherText, nil
|
||||
}
|
||||
|
||||
// AesDecrypt reads the OpenSSL/CryptoJS "Salted__" format used for MQTT
|
||||
// messages with Kerberos Hub and by recordings from older releases.
|
||||
func AesDecrypt(cipherText []byte, password string) ([]byte, error) {
|
||||
if string(cipherText[:8]) != "Salted__" {
|
||||
if len(cipherText) < 16+aes.BlockSize || string(cipherText[:8]) != "Salted__" {
|
||||
return nil, errors.New("invalid crypto js aes encryption")
|
||||
}
|
||||
if (len(cipherText)-16)%aes.BlockSize != 0 {
|
||||
return nil, errLegacyCiphertext
|
||||
}
|
||||
|
||||
salt := cipherText[8:16]
|
||||
cipherBytes := cipherText[16:]
|
||||
cipherBytes := append([]byte(nil), cipherText[16:]...)
|
||||
key, iv, err := DefaultEvpKDF([]byte(password), salt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -70,8 +81,7 @@ func AesDecrypt(cipherText []byte, password string) ([]byte, error) {
|
||||
mode := cipher.NewCBCDecrypter(block, iv)
|
||||
mode.CryptBlocks(cipherBytes, cipherBytes)
|
||||
|
||||
result := PKCS5UnPadding(cipherBytes)
|
||||
return result, nil
|
||||
return pkcs7Unpad(cipherBytes, aes.BlockSize)
|
||||
}
|
||||
|
||||
func EvpKDF(password []byte, salt []byte, keySize int, iterations int, hashAlgorithm string) ([]byte, error) {
|
||||
@@ -113,6 +123,25 @@ func DefaultEvpKDF(password []byte, salt []byte) (key []byte, iv []byte, err err
|
||||
return derivedKeyBytes[:keySize*4], derivedKeyBytes[keySize*4:], nil
|
||||
}
|
||||
|
||||
// pkcs7Unpad validates the padding instead of trusting the last byte, so a
|
||||
// wrong key or a truncated file returns an error rather than panicking.
|
||||
func pkcs7Unpad(src []byte, blockSize int) ([]byte, error) {
|
||||
length := len(src)
|
||||
if length == 0 || length%blockSize != 0 {
|
||||
return nil, errLegacyCiphertext
|
||||
}
|
||||
padding := int(src[length-1])
|
||||
if padding == 0 || padding > blockSize || padding > length {
|
||||
return nil, errLegacyCiphertext
|
||||
}
|
||||
for _, b := range src[length-padding:] {
|
||||
if int(b) != padding {
|
||||
return nil, errLegacyCiphertext
|
||||
}
|
||||
}
|
||||
return src[:length-padding], nil
|
||||
}
|
||||
|
||||
func PKCS5UnPadding(src []byte) []byte {
|
||||
length := len(src)
|
||||
unpadding := int(src[length-1])
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
package encryption
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/pbkdf2"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"strconv"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Recording encryption format, version 1:
|
||||
//
|
||||
// magic 8 bytes "KRBREC1\x00"
|
||||
// iterations 4 bytes big-endian PBKDF2-HMAC-SHA256 iteration count
|
||||
// salt 16 bytes
|
||||
// nonce 12 bytes
|
||||
// payload AES-256-GCM ciphertext followed by the 16-byte tag
|
||||
//
|
||||
// The header (magic through nonce) is authenticated as additional data, so
|
||||
// any change to it or to the payload is detected on decryption.
|
||||
//
|
||||
// Recordings encrypted by older releases use the OpenSSL/CryptoJS "Salted__"
|
||||
// format (a single MD5 round to derive the key). DecryptRecording still reads
|
||||
// those, so existing recordings remain playable.
|
||||
const (
|
||||
recordingMagic = "KRBREC1\x00"
|
||||
recordingSaltSize = 16
|
||||
recordingNonceSize = 12
|
||||
recordingHeaderSize = len(recordingMagic) + 4 + recordingSaltSize + recordingNonceSize
|
||||
recordingKeySize = 32
|
||||
recordingIterations = 600_000
|
||||
recordingMaxIterations = 10_000_000
|
||||
recordingKeyCacheLimit = 16
|
||||
legacyOpenSSLSaltHeader = "Salted__"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrRecordingFormat = errors.New("unknown recording encryption format")
|
||||
ErrRecordingDecrypt = errors.New("recording could not be decrypted: wrong key or corrupted file")
|
||||
errLegacyCiphertext = errors.New("invalid legacy ciphertext")
|
||||
recordingKeyCacheMu sync.Mutex
|
||||
recordingKeyCache = map[string][]byte{}
|
||||
recordingSaltOnce sync.Once
|
||||
recordingProcessSalt []byte
|
||||
recordingProcessError error
|
||||
)
|
||||
|
||||
// processSalt returns a random salt generated once per process. Deriving the
|
||||
// key with PBKDF2 is deliberately slow, so encryption reuses one salt and the
|
||||
// cached key for every recording instead of paying that cost per file. Each
|
||||
// recording still gets a unique random nonce.
|
||||
func processSalt() ([]byte, error) {
|
||||
recordingSaltOnce.Do(func() {
|
||||
salt := make([]byte, recordingSaltSize)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
recordingProcessError = err
|
||||
return
|
||||
}
|
||||
recordingProcessSalt = salt
|
||||
})
|
||||
return recordingProcessSalt, recordingProcessError
|
||||
}
|
||||
|
||||
func recordingKey(password string, salt []byte, iterations int) ([]byte, error) {
|
||||
cacheKey := string(salt) + "\x00" + strconv.Itoa(iterations) + "\x00" + password
|
||||
recordingKeyCacheMu.Lock()
|
||||
if key, ok := recordingKeyCache[cacheKey]; ok {
|
||||
recordingKeyCacheMu.Unlock()
|
||||
return key, nil
|
||||
}
|
||||
recordingKeyCacheMu.Unlock()
|
||||
|
||||
key, err := pbkdf2.Key(sha256.New, password, salt, iterations, recordingKeySize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
recordingKeyCacheMu.Lock()
|
||||
if len(recordingKeyCache) >= recordingKeyCacheLimit {
|
||||
recordingKeyCache = map[string][]byte{}
|
||||
}
|
||||
recordingKeyCache[cacheKey] = key
|
||||
recordingKeyCacheMu.Unlock()
|
||||
return key, nil
|
||||
}
|
||||
|
||||
// EncryptRecording encrypts a recording with a key derived from password.
|
||||
func EncryptRecording(content []byte, password string) ([]byte, error) {
|
||||
if password == "" {
|
||||
return nil, errors.New("empty encryption key")
|
||||
}
|
||||
salt, err := processSalt()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
key, err := recordingKey(password, salt, recordingIterations)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gcm, err := newRecordingGCM(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
out := make([]byte, recordingHeaderSize, recordingHeaderSize+len(content)+gcm.Overhead())
|
||||
copy(out, recordingMagic)
|
||||
binary.BigEndian.PutUint32(out[len(recordingMagic):], uint32(recordingIterations))
|
||||
copy(out[len(recordingMagic)+4:], salt)
|
||||
nonce := out[len(recordingMagic)+4+recordingSaltSize : recordingHeaderSize]
|
||||
if _, err := rand.Read(nonce); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
header := out[:recordingHeaderSize]
|
||||
return gcm.Seal(out, nonce, content, header), nil
|
||||
}
|
||||
|
||||
// DecryptRecording decrypts a recording written by EncryptRecording, or by
|
||||
// older releases in the legacy OpenSSL "Salted__" format.
|
||||
func DecryptRecording(data []byte, password string) ([]byte, error) {
|
||||
switch {
|
||||
case bytes.HasPrefix(data, []byte(recordingMagic)):
|
||||
return decryptRecordingV1(data, password)
|
||||
case bytes.HasPrefix(data, []byte(legacyOpenSSLSaltHeader)):
|
||||
return AesDecrypt(data, password)
|
||||
default:
|
||||
return nil, ErrRecordingFormat
|
||||
}
|
||||
}
|
||||
|
||||
func decryptRecordingV1(data []byte, password string) ([]byte, error) {
|
||||
if len(data) < recordingHeaderSize+16 {
|
||||
return nil, ErrRecordingDecrypt
|
||||
}
|
||||
iterations := int(binary.BigEndian.Uint32(data[len(recordingMagic):]))
|
||||
if iterations < 1 || iterations > recordingMaxIterations {
|
||||
return nil, ErrRecordingDecrypt
|
||||
}
|
||||
salt := data[len(recordingMagic)+4 : len(recordingMagic)+4+recordingSaltSize]
|
||||
nonce := data[len(recordingMagic)+4+recordingSaltSize : recordingHeaderSize]
|
||||
|
||||
key, err := recordingKey(password, salt, iterations)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gcm, err := newRecordingGCM(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
plain, err := gcm.Open(nil, nonce, data[recordingHeaderSize:], data[:recordingHeaderSize])
|
||||
if err != nil {
|
||||
return nil, ErrRecordingDecrypt
|
||||
}
|
||||
return plain, nil
|
||||
}
|
||||
|
||||
func newRecordingGCM(key []byte) (cipher.AEAD, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cipher.NewGCM(block)
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package encryption
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/pbkdf2"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const testRecordingKey = "correct horse battery staple"
|
||||
|
||||
func TestEncryptRecordingRoundTrip(t *testing.T) {
|
||||
content := bytes.Repeat([]byte("mp4-bytes"), 1000)
|
||||
encrypted, err := EncryptRecording(content, testRecordingKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.HasPrefix(encrypted, []byte(recordingMagic)) {
|
||||
t.Fatalf("missing format header: %q", encrypted[:8])
|
||||
}
|
||||
if got := binary.BigEndian.Uint32(encrypted[len(recordingMagic):]); got != recordingIterations {
|
||||
t.Fatalf("iterations in header = %d, want %d", got, recordingIterations)
|
||||
}
|
||||
decrypted, err := DecryptRecording(encrypted, testRecordingKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(decrypted, content) {
|
||||
t.Fatal("round trip changed the content")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRecordingFormatMatchesSpecification decrypts with only the documented
|
||||
// layout and the standard library, so the format is pinned independently of
|
||||
// the implementation.
|
||||
func TestRecordingFormatMatchesSpecification(t *testing.T) {
|
||||
encrypted, err := EncryptRecording([]byte("payload"), testRecordingKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
iterations := int(binary.BigEndian.Uint32(encrypted[8:12]))
|
||||
salt, nonce := encrypted[12:28], encrypted[28:40]
|
||||
key, err := pbkdf2.Key(sha256.New, testRecordingKey, salt, iterations, 32)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
block, _ := aes.NewCipher(key)
|
||||
gcm, _ := cipher.NewGCM(block)
|
||||
plain, err := gcm.Open(nil, nonce, encrypted[40:], encrypted[:40])
|
||||
if err != nil || string(plain) != "payload" {
|
||||
t.Fatalf("independent PBKDF2-SHA256 + AES-256-GCM decryption failed: %q, %v", plain, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecryptRecordingReadsLegacyFormat(t *testing.T) {
|
||||
legacy, err := AesEncrypt([]byte("old recording"), testRecordingKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := DecryptRecording(legacy, testRecordingKey)
|
||||
if err != nil || string(got) != "old recording" {
|
||||
t.Fatalf("legacy recording not readable: %q, %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptRecordingUsesFreshNonces(t *testing.T) {
|
||||
a, _ := EncryptRecording([]byte("same"), testRecordingKey)
|
||||
b, _ := EncryptRecording([]byte("same"), testRecordingKey)
|
||||
if bytes.Equal(a[28:40], b[28:40]) {
|
||||
t.Fatal("two recordings share a nonce")
|
||||
}
|
||||
if bytes.Equal(a, b) {
|
||||
t.Fatal("identical plaintexts produced identical ciphertexts")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptRecordingCachesDerivedKey(t *testing.T) {
|
||||
if _, err := EncryptRecording([]byte("warm"), testRecordingKey); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
start := time.Now()
|
||||
for i := 0; i < 20; i++ {
|
||||
if _, err := EncryptRecording([]byte("frame"), testRecordingKey); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// 20 fresh PBKDF2 derivations at 600k iterations would take seconds.
|
||||
if elapsed := time.Since(start); elapsed > 500*time.Millisecond {
|
||||
t.Fatalf("20 encryptions took %v; the derived key is not being reused", elapsed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecryptRecordingRejectsWrongKeyAndTampering(t *testing.T) {
|
||||
encrypted, err := EncryptRecording([]byte("secret footage"), testRecordingKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := DecryptRecording(encrypted, "wrong key"); !errors.Is(err, ErrRecordingDecrypt) {
|
||||
t.Fatalf("wrong key: err = %v", err)
|
||||
}
|
||||
|
||||
payload := append([]byte(nil), encrypted...)
|
||||
payload[len(payload)-1] ^= 0x01
|
||||
if _, err := DecryptRecording(payload, testRecordingKey); !errors.Is(err, ErrRecordingDecrypt) {
|
||||
t.Fatalf("modified payload: err = %v", err)
|
||||
}
|
||||
|
||||
header := append([]byte(nil), encrypted...)
|
||||
header[30] ^= 0x01 // inside the nonce, which is authenticated
|
||||
if _, err := DecryptRecording(header, testRecordingKey); !errors.Is(err, ErrRecordingDecrypt) {
|
||||
t.Fatalf("modified header: err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecryptRecordingRejectsExcessiveIterations(t *testing.T) {
|
||||
encrypted, _ := EncryptRecording([]byte("x"), testRecordingKey)
|
||||
crafted := append([]byte(nil), encrypted...)
|
||||
binary.BigEndian.PutUint32(crafted[8:12], 0xFFFFFFFF)
|
||||
start := time.Now()
|
||||
if _, err := DecryptRecording(crafted, testRecordingKey); !errors.Is(err, ErrRecordingDecrypt) {
|
||||
t.Fatalf("crafted iteration count: err = %v", err)
|
||||
}
|
||||
if time.Since(start) > 100*time.Millisecond {
|
||||
t.Fatal("crafted iteration count was not rejected before key derivation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecryptRecordingNeverPanicsOnMalformedInput(t *testing.T) {
|
||||
legacy, _ := AesEncrypt([]byte("old recording"), testRecordingKey)
|
||||
current, _ := EncryptRecording([]byte("new recording"), testRecordingKey)
|
||||
inputs := [][]byte{nil, {}, []byte("Salted__"), []byte("KRBREC1\x00"), []byte("not a recording")}
|
||||
for n := 0; n < len(legacy); n++ {
|
||||
inputs = append(inputs, legacy[:n])
|
||||
}
|
||||
for n := 0; n < 60 && n < len(current); n++ {
|
||||
inputs = append(inputs, current[:n])
|
||||
}
|
||||
for _, input := range inputs {
|
||||
func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("DecryptRecording panicked on %d bytes: %v", len(input), r)
|
||||
}
|
||||
}()
|
||||
_, _ = DecryptRecording(input, testRecordingKey)
|
||||
_, _ = DecryptRecording(input, "wrong key")
|
||||
}()
|
||||
}
|
||||
if _, err := DecryptRecording([]byte("plain mp4"), testRecordingKey); !errors.Is(err, ErrRecordingFormat) {
|
||||
t.Fatalf("unknown format: err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLegacyDecryptWithWrongKeyReturnsError(t *testing.T) {
|
||||
legacy, _ := AesEncrypt([]byte("old recording"), testRecordingKey)
|
||||
// With the wrong key the padding is garbage; it used to be trusted and
|
||||
// could slice out of range.
|
||||
for i := 0; i < 50; i++ {
|
||||
if out, err := AesDecrypt(legacy, "wrong key "+string(rune('a'+i))); err == nil && string(out) == "old recording" {
|
||||
t.Fatal("wrong key decrypted the recording")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,11 @@ func JWTMiddleWare(configDirectory string) jwt.GinJWTMiddleware {
|
||||
// Kerberos Factory) be validated by the agent. Without it, a random secret
|
||||
// is generated once and stored with the configuration; see loadJWTSecret.
|
||||
myKey := loadJWTSecret(configDirectory)
|
||||
// With an explicitly shared secret, Kerberos Factory signs its own tokens
|
||||
// for the agent under the "factory" service identity. Only holders of that
|
||||
// secret can produce them. With the generated per-install secret, only the
|
||||
// agent's own login issues tokens, so that identity is not accepted.
|
||||
sharedSecret := os.Getenv("AGENT_JWT_SECRET") != ""
|
||||
|
||||
m := jwt.GinJWTMiddleware{
|
||||
Realm: "kerberosio",
|
||||
@@ -94,7 +99,10 @@ func JWTMiddleWare(configDirectory string) jwt.GinJWTMiddleware {
|
||||
// Only the configured account may use the API, even with a
|
||||
// validly signed token.
|
||||
user, ok := data.(*models.User)
|
||||
return ok && user.Username != "" && user.Username == agentUsername()
|
||||
if !ok || user.Username == "" {
|
||||
return false
|
||||
}
|
||||
return user.Username == agentUsername() || (sharedSecret && user.Username == factoryServiceUsername)
|
||||
},
|
||||
Unauthorized: func(c *gin.Context, code int, message string) {
|
||||
c.AbortWithStatusJSON(code, gin.H{
|
||||
|
||||
@@ -21,6 +21,9 @@ const (
|
||||
// legacyJWTSecret was the hard-coded fallback in earlier releases. It is
|
||||
// public, so it is never used implicitly.
|
||||
legacyJWTSecret = "TOBECHANGED"
|
||||
// factoryServiceUsername is the identity Kerberos Factory puts in the
|
||||
// tokens it signs with the shared AGENT_JWT_SECRET.
|
||||
factoryServiceUsername = "factory"
|
||||
)
|
||||
|
||||
// loadJWTSecret returns the key used to sign and verify API tokens.
|
||||
|
||||
@@ -14,11 +14,10 @@ import (
|
||||
func TestFilesServesFlatAndDatedRecordings(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
for _, layout := range []string{"flat", "dated"} {
|
||||
for _, encrypted := range []bool{false, true} {
|
||||
name := layout + "-plain"
|
||||
if encrypted {
|
||||
name = layout + "-encrypted"
|
||||
}
|
||||
// "legacy" is the OpenSSL format written by older releases; "v1" is
|
||||
// the current PBKDF2 + AES-GCM recording format.
|
||||
for _, format := range []string{"plain", "legacy", "v1"} {
|
||||
name := layout + "-" + format
|
||||
t.Run(name, func(t *testing.T) {
|
||||
const fileName = "1767355200123-device-key.mp4"
|
||||
directory := t.TempDir()
|
||||
@@ -32,14 +31,18 @@ func TestFilesServesFlatAndDatedRecordings(t *testing.T) {
|
||||
}
|
||||
config := &models.Configuration{Config: models.Config{Encryption: &models.Encryption{}}}
|
||||
content := []byte("recording-payload")
|
||||
if encrypted {
|
||||
if format != "plain" {
|
||||
config.Config.Encryption.Recordings = "true"
|
||||
config.Config.Encryption.SymmetricKey = "0123456789abcdef0123456789abcdef"
|
||||
ciphertext, err := encryption.AesEncrypt(content, config.Config.Encryption.SymmetricKey)
|
||||
encrypt := encryption.EncryptRecording
|
||||
if format == "legacy" {
|
||||
encrypt = encryption.AesEncrypt
|
||||
}
|
||||
ciphertext, err := encrypt(content, config.Config.Encryption.SymmetricKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
content = []byte(ciphertext)
|
||||
content = ciphertext
|
||||
}
|
||||
if err := os.WriteFile(filePath, content, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -70,9 +70,16 @@ func TestLoadJWTSecretReplacesShortStoredSecret(t *testing.T) {
|
||||
|
||||
// securityRouter wires the real middleware and routes the way StartServer does.
|
||||
func securityRouter(t *testing.T) (*gin.Engine, []byte) {
|
||||
t.Helper()
|
||||
return securityRouterWithSecret(t, "")
|
||||
}
|
||||
|
||||
// securityRouterWithSecret is securityRouter with AGENT_JWT_SECRET set to
|
||||
// secret ("" means the generated per-install secret).
|
||||
func securityRouterWithSecret(t *testing.T, secret string) (*gin.Engine, []byte) {
|
||||
t.Helper()
|
||||
gin.SetMode(gin.TestMode)
|
||||
t.Setenv("AGENT_JWT_SECRET", "")
|
||||
t.Setenv("AGENT_JWT_SECRET", secret)
|
||||
t.Setenv("AGENT_USERNAME", "")
|
||||
t.Setenv("AGENT_PASSWORD", "")
|
||||
|
||||
@@ -202,3 +209,29 @@ func TestProfilerRequiresTokenWhenEnabled(t *testing.T) {
|
||||
t.Fatalf("enabled pprof with token: status %d, want 200", code)
|
||||
}
|
||||
}
|
||||
|
||||
// Kerberos Factory signs tokens for the agent with the shared AGENT_JWT_SECRET
|
||||
// under the "factory" identity (uug-ai/factory api/k8sproxy/jwt.go).
|
||||
func TestFactoryTokenAcceptedWithSharedSecret(t *testing.T) {
|
||||
router, key := securityRouterWithSecret(t, "shared-with-factory")
|
||||
if string(key) != "shared-with-factory" {
|
||||
t.Fatalf("middleware key = %q, want the shared secret", key)
|
||||
}
|
||||
token := signedToken(t, key, "factory")
|
||||
|
||||
// An authorised request reaches the handler, which reports the missing file.
|
||||
if code := request(router, "GET", "/file/missing.mp4", token); code != stdhttp.StatusNotFound {
|
||||
t.Fatalf("factory token with shared secret: status %d, want 404 from the handler", code)
|
||||
}
|
||||
if code := request(router, "GET", "/file/missing.mp4", signedToken(t, key, "someone-else")); code != stdhttp.StatusForbidden {
|
||||
t.Fatalf("other identity with shared secret: status %d, want 403", code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFactoryIdentityRejectedWithoutSharedSecret(t *testing.T) {
|
||||
router, key := securityRouter(t)
|
||||
|
||||
if code := request(router, "GET", "/file/missing.mp4", signedToken(t, key, "factory")); code != stdhttp.StatusForbidden {
|
||||
t.Fatalf("factory identity with the per-install secret: status %d, want 403", code)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,7 +199,7 @@ func Files(c *gin.Context, configDirectory string, configuration *models.Configu
|
||||
}
|
||||
|
||||
// Decrypt file
|
||||
contents, err = encryption.AesDecrypt(contents, symmetricKey)
|
||||
contents, err = encryption.DecryptRecording(contents, symmetricKey)
|
||||
if err != nil {
|
||||
c.JSON(404, gin.H{"error": "File not found"})
|
||||
return
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -58,21 +59,76 @@ func init() {
|
||||
mqtt.ERROR = pahoErrorLogger{}
|
||||
}
|
||||
|
||||
func enableMQTTConnectionDiagnostics(options *mqtt.ClientOptions, brokerURL string) {
|
||||
if !strings.Contains(brokerURL, "://") {
|
||||
options.SetCustomOpenConnectionFn(openMQTTConnection)
|
||||
return
|
||||
// parseMQTTBrokers turns the configured MQTT URI into the list of brokers to
|
||||
// connect to. Besides a single broker URI it accepts an ActiveMQ-style
|
||||
// failover URI, as used by Amazon MQ active/standby brokers:
|
||||
//
|
||||
// failover:(mqtt+ssl://b-1.example:8883,mqtt+ssl://b-2.example:8883)
|
||||
//
|
||||
// and a plain comma-separated list of broker URIs. Transport options after
|
||||
// the closing parenthesis (e.g. "?randomize=false") are ignored: paho tries
|
||||
// the brokers in the order they are listed, on connect and on reconnect.
|
||||
//
|
||||
// A comma only separates brokers when the text after it starts with a URI
|
||||
// scheme ("scheme://"). Any other comma belongs to the current broker URI,
|
||||
// so existing single-broker URIs with commas in their credentials, path or
|
||||
// query string (e.g. "wss://broker/mqtt?token=a,b") keep working.
|
||||
func parseMQTTBrokers(raw string) []string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if len(raw) >= len("failover:") && strings.EqualFold(raw[:len("failover:")], "failover:") {
|
||||
raw = strings.TrimSpace(raw[len("failover:"):])
|
||||
if strings.HasPrefix(raw, "(") {
|
||||
if end := strings.LastIndex(raw, ")"); end > 0 {
|
||||
raw = raw[1:end]
|
||||
} else {
|
||||
raw = raw[1:]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
parsedURL, err := url.Parse(brokerURL)
|
||||
if err != nil {
|
||||
return
|
||||
segments := []string{}
|
||||
for _, segment := range strings.Split(raw, ",") {
|
||||
if len(segments) > 0 && !mqttBrokerSchemePattern.MatchString(strings.TrimSpace(segment)) {
|
||||
segments[len(segments)-1] += "," + segment
|
||||
continue
|
||||
}
|
||||
segments = append(segments, segment)
|
||||
}
|
||||
|
||||
switch strings.ToLower(parsedURL.Scheme) {
|
||||
case "", "mqtt", "tcp", "ssl", "tls", "mqtts", "mqtt+ssl", "tcps":
|
||||
options.SetCustomOpenConnectionFn(openMQTTConnection)
|
||||
brokers := []string{}
|
||||
for _, broker := range segments {
|
||||
if broker = strings.TrimSpace(broker); broker != "" {
|
||||
brokers = append(brokers, broker)
|
||||
}
|
||||
}
|
||||
return brokers
|
||||
}
|
||||
|
||||
// mqttBrokerSchemePattern matches the start of a URI with a scheme (RFC 3986).
|
||||
var mqttBrokerSchemePattern = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9+.-]*://`)
|
||||
|
||||
// enableMQTTConnectionDiagnostics installs our instrumented TCP/TLS dialer.
|
||||
// The custom dialer is used for every broker, so it is only installed when
|
||||
// all brokers use a TCP-based scheme (WebSocket brokers keep paho's dialer).
|
||||
func enableMQTTConnectionDiagnostics(options *mqtt.ClientOptions, brokerURLs []string) {
|
||||
if len(brokerURLs) == 0 {
|
||||
return
|
||||
}
|
||||
for _, brokerURL := range brokerURLs {
|
||||
if !strings.Contains(brokerURL, "://") {
|
||||
continue
|
||||
}
|
||||
parsedURL, err := url.Parse(brokerURL)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
switch strings.ToLower(parsedURL.Scheme) {
|
||||
case "", "mqtt", "tcp", "ssl", "tls", "mqtts", "mqtt+ssl", "tcps":
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
options.SetCustomOpenConnectionFn(openMQTTConnection)
|
||||
}
|
||||
|
||||
func openMQTTConnection(uri *url.URL, options mqtt.ClientOptions) (net.Conn, error) {
|
||||
@@ -270,8 +326,11 @@ func ConfigureMQTT(configDirectory string, configuration *models.Configuration,
|
||||
|
||||
// We will set the MQTT endpoint to which we want to connect
|
||||
// and share and receive messages to/from.
|
||||
mqttURL := config.MQTTURI
|
||||
opts.AddBroker(mqttURL)
|
||||
// The URI may list several brokers (failover), paho tries them in order.
|
||||
mqttBrokers := parseMQTTBrokers(config.MQTTURI)
|
||||
for _, broker := range mqttBrokers {
|
||||
opts.AddBroker(broker)
|
||||
}
|
||||
|
||||
// Our MQTT broker can have username/password credentials
|
||||
// to protect it from the outside.
|
||||
@@ -282,7 +341,8 @@ func ConfigureMQTT(configDirectory string, configuration *models.Configuration,
|
||||
opts.SetPassword(mqtt_password)
|
||||
}
|
||||
log.WithFields(log.Fields{
|
||||
"broker_configured": mqttURL != "",
|
||||
"broker_configured": len(mqttBrokers) > 0,
|
||||
"broker_count": len(mqttBrokers),
|
||||
"component": "routers/mqtt",
|
||||
"event": "credentials_configured",
|
||||
"password_configured": mqtt_password != "",
|
||||
@@ -292,7 +352,7 @@ func ConfigureMQTT(configDirectory string, configuration *models.Configuration,
|
||||
// Some extra options to make sure the connection behaves
|
||||
// properly. More information here: github.com/eclipse/paho.mqtt.golang.
|
||||
//opts.SetCleanSession(true)
|
||||
enableMQTTConnectionDiagnostics(opts, mqttURL)
|
||||
enableMQTTConnectionDiagnostics(opts, mqttBrokers)
|
||||
opts.SetCleanSession(false)
|
||||
opts.SetResumeSubs(true)
|
||||
opts.SetStore(mqtt.NewMemoryStore())
|
||||
@@ -354,7 +414,7 @@ func ConfigureMQTT(configDirectory string, configuration *models.Configuration,
|
||||
"event": "connected",
|
||||
}).Info("MQTT broker connected")
|
||||
log.WithFields(log.Fields{
|
||||
"broker_configured": mqttURL != "",
|
||||
"broker_configured": len(mqttBrokers) > 0,
|
||||
"component": "routers/mqtt",
|
||||
"event": "connection_details",
|
||||
}).Debug("MQTT connection details")
|
||||
|
||||
@@ -23,12 +23,15 @@ func TestEnableMQTTConnectionDiagnostics(t *testing.T) {
|
||||
{name: "TCP", brokerURL: "tcp://broker.example:1883", want: true},
|
||||
{name: "default TCP", brokerURL: "broker.example:1883", want: true},
|
||||
{name: "WebSocket", brokerURL: "wss://broker.example/mqtt", want: false},
|
||||
{name: "ActiveMQ failover", brokerURL: "failover:(mqtt+ssl://b-1.example:8883,mqtt+ssl://b-2.example:8883)", want: true},
|
||||
{name: "mixed TCP and WebSocket", brokerURL: "tcp://b-1.example:1883,wss://b-2.example/mqtt", want: false},
|
||||
{name: "empty", brokerURL: "", want: false},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
options := mqtt.NewClientOptions()
|
||||
enableMQTTConnectionDiagnostics(options, test.brokerURL)
|
||||
enableMQTTConnectionDiagnostics(options, parseMQTTBrokers(test.brokerURL))
|
||||
if got := options.CustomOpenConnectionFn != nil; got != test.want {
|
||||
t.Fatalf("CustomOpenConnectionFn configured = %t, want %t", got, test.want)
|
||||
}
|
||||
@@ -206,3 +209,64 @@ func TestRemoteSessionReservationIsIdempotent(t *testing.T) {
|
||||
t.Fatalf("duplicate reserve() error = %v, want %v", err, errRemoteSessionExists)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMQTTBrokers(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
raw string
|
||||
want []string
|
||||
}{
|
||||
{name: "single", raw: "mqtt+ssl://broker.example:8883", want: []string{"mqtt+ssl://broker.example:8883"}},
|
||||
{name: "empty", raw: " ", want: []string{}},
|
||||
{
|
||||
name: "Amazon MQ failover",
|
||||
raw: "failover:(mqtt+ssl://b-1.mq.eu-west-2.amazonaws.com:8883,mqtt+ssl://b-2.mq.eu-west-2.amazonaws.com:8883)",
|
||||
want: []string{"mqtt+ssl://b-1.mq.eu-west-2.amazonaws.com:8883", "mqtt+ssl://b-2.mq.eu-west-2.amazonaws.com:8883"},
|
||||
},
|
||||
{
|
||||
name: "failover with spaces and transport options",
|
||||
raw: " FAILOVER:( ssl://b-1.example:8883 , ssl://b-2.example:8883 )?randomize=false",
|
||||
want: []string{"ssl://b-1.example:8883", "ssl://b-2.example:8883"},
|
||||
},
|
||||
{name: "failover without parentheses", raw: "failover:tcp://b-1.example:1883", want: []string{"tcp://b-1.example:1883"}},
|
||||
{name: "comma separated", raw: "tcp://b-1.example:1883,tcp://b-2.example:1883", want: []string{"tcp://b-1.example:1883", "tcp://b-2.example:1883"}},
|
||||
{name: "comma in query string", raw: "wss://broker.example/mqtt?token=a,b", want: []string{"wss://broker.example/mqtt?token=a,b"}},
|
||||
{name: "comma in password", raw: "mqtt+ssl://user:p,ss@broker.example:8883", want: []string{"mqtt+ssl://user:p,ss@broker.example:8883"}},
|
||||
{name: "comma in path", raw: "wss://broker.example/a,b/mqtt", want: []string{"wss://broker.example/a,b/mqtt"}},
|
||||
{name: "comma followed by text that looks like a scheme without slashes", raw: "wss://broker.example/mqtt?list=a,b:c", want: []string{"wss://broker.example/mqtt?list=a,b:c"}},
|
||||
{
|
||||
name: "failover with commas inside a broker URI",
|
||||
raw: "failover:(wss://b-1.example/mqtt?token=a,b,wss://b-2.example/mqtt?token=c,d)",
|
||||
want: []string{"wss://b-1.example/mqtt?token=a,b", "wss://b-2.example/mqtt?token=c,d"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got := parseMQTTBrokers(test.raw)
|
||||
if len(got) != len(test.want) {
|
||||
t.Fatalf("parseMQTTBrokers(%q) = %q, want %q", test.raw, got, test.want)
|
||||
}
|
||||
for i := range got {
|
||||
if got[i] != test.want[i] {
|
||||
t.Fatalf("parseMQTTBrokers(%q) = %q, want %q", test.raw, got, test.want)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigureMQTTOptionsRegistersFailoverBrokers(t *testing.T) {
|
||||
options := mqtt.NewClientOptions()
|
||||
for _, broker := range parseMQTTBrokers("failover:(mqtt+ssl://b-1.example:8883,mqtt+ssl://b-2.example:8883)") {
|
||||
options.AddBroker(broker)
|
||||
}
|
||||
if len(options.Servers) != 2 {
|
||||
t.Fatalf("registered %d brokers, want 2", len(options.Servers))
|
||||
}
|
||||
for i, host := range []string{"b-1.example:8883", "b-2.example:8883"} {
|
||||
if options.Servers[i].Scheme != "mqtt+ssl" || options.Servers[i].Host != host {
|
||||
t.Fatalf("broker %d = %s, want mqtt+ssl://%s", i, options.Servers[i], host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -409,7 +409,7 @@ func Decrypt(directoryOrFile string, symmetricKey []byte) {
|
||||
return
|
||||
}
|
||||
// Decrypt using AES key
|
||||
decrypted, err := encryption.AesDecrypt(content, string(symmetricKey))
|
||||
decrypted, err := encryption.DecryptRecording(content, string(symmetricKey))
|
||||
if err != nil {
|
||||
log.Fatal("Something went wrong while decrypting: " + err.Error())
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user