eec8c28fb3
Go Benchmark / Performance Regression Check (push) Has been cancelled
Cerberus Integration / Cerberus Security Stack Integration (push) Has been cancelled
Upload Coverage to Codecov / Backend Codecov Upload (push) Has been cancelled
Upload Coverage to Codecov / Frontend Codecov Upload (push) Has been cancelled
CodeQL - Analyze / CodeQL analysis (go) (push) Has been cancelled
CodeQL - Analyze / CodeQL analysis (javascript-typescript) (push) Has been cancelled
CrowdSec Integration / CrowdSec Bouncer Integration (push) Has been cancelled
Docker Build, Publish & Test / build-and-push (push) Has been cancelled
Quality Checks / Auth Route Protection Contract (push) Has been cancelled
Quality Checks / Codecov Trigger/Comment Parity Guard (push) Has been cancelled
Quality Checks / Backend (Go) (push) Has been cancelled
Quality Checks / Frontend (React) (push) Has been cancelled
Rate Limit integration / Rate Limiting Integration (push) Has been cancelled
Security Scan (PR) / Trivy Binary Scan (push) Has been cancelled
Supply Chain Verification (PR) / Verify Supply Chain (push) Has been cancelled
WAF integration / Coraza WAF Integration (push) Has been cancelled
Docker Build, Publish & Test / Security Scan PR Image (push) Has been cancelled
Repo Health Check / Repo health (push) Has been cancelled
History Rewrite Dry-Run / Dry-run preview for history rewrite (push) Has been cancelled
Prune Renovate Branches / prune (push) Has been cancelled
Renovate / renovate (push) Has been cancelled
Nightly Build & Package / sync-development-to-nightly (push) Has been cancelled
Nightly Build & Package / Trigger Nightly Validation Workflows (push) Has been cancelled
Nightly Build & Package / build-and-push-nightly (push) Has been cancelled
Nightly Build & Package / test-nightly-image (push) Has been cancelled
Nightly Build & Package / verify-nightly-supply-chain (push) Has been cancelled
Update GeoLite2 Checksum / update-checksum (push) Has been cancelled
Container Registry Prune / prune-ghcr (push) Has been cancelled
Container Registry Prune / prune-dockerhub (push) Has been cancelled
Container Registry Prune / summarize (push) Has been cancelled
Supply Chain Verification / Verify SBOM (push) Has been cancelled
Supply Chain Verification / Verify Release Artifacts (push) Has been cancelled
Supply Chain Verification / Verify Docker Image Supply Chain (push) Has been cancelled
Monitor Caddy Major Release / check-caddy-major (push) Has been cancelled
Weekly Nightly to Main Promotion / Verify Nightly Branch Health (push) Has been cancelled
Weekly Nightly to Main Promotion / Create Promotion PR (push) Has been cancelled
Weekly Nightly to Main Promotion / Trigger Missing Required Checks (push) Has been cancelled
Weekly Nightly to Main Promotion / Notify on Failure (push) Has been cancelled
Weekly Nightly to Main Promotion / Workflow Summary (push) Has been cancelled
Weekly Security Rebuild / Security Rebuild & Scan (push) Has been cancelled
167 lines
6.0 KiB
Go
Executable File
167 lines
6.0 KiB
Go
Executable File
package caddy
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"testing"
|
|
|
|
"github.com/Wikid82/charon/backend/internal/crypto"
|
|
"github.com/Wikid82/charon/backend/internal/models"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func newTestEncSvc(t *testing.T) *crypto.EncryptionService {
|
|
t.Helper()
|
|
key := make([]byte, 32)
|
|
for i := range key {
|
|
key[i] = byte(i)
|
|
}
|
|
svc, err := crypto.NewEncryptionService(base64.StdEncoding.EncodeToString(key))
|
|
require.NoError(t, err)
|
|
return svc
|
|
}
|
|
|
|
// Test: encrypted key with encryption service → decrypt success → cert loaded
|
|
func TestGenerateConfig_CustomCert_EncryptedKey(t *testing.T) {
|
|
encSvc := newTestEncSvc(t)
|
|
encKey, err := encSvc.Encrypt([]byte("-----BEGIN PRIVATE KEY-----\nfake-key-data\n-----END PRIVATE KEY-----"))
|
|
require.NoError(t, err)
|
|
|
|
certID := uint(10)
|
|
hosts := []models.ProxyHost{
|
|
{
|
|
UUID: "h-enc", DomainNames: "enc.test", ForwardHost: "127.0.0.1", ForwardPort: 8080, Enabled: true,
|
|
CertificateID: &certID,
|
|
Certificate: &models.SSLCertificate{
|
|
ID: certID, UUID: "c-enc", Name: "EncCert", Provider: "custom",
|
|
Certificate: "-----BEGIN CERTIFICATE-----\nfake-cert\n-----END CERTIFICATE-----",
|
|
PrivateKeyEncrypted: encKey,
|
|
},
|
|
},
|
|
}
|
|
|
|
cfg, err := GenerateConfig(hosts, "/data", "admin@test.com", "/dist", "letsencrypt", true, false, false, false, false, "", nil, nil, nil, nil, nil, encSvc)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, cfg)
|
|
require.NotNil(t, cfg.Apps.TLS)
|
|
require.NotNil(t, cfg.Apps.TLS.Certificates)
|
|
assert.NotEmpty(t, cfg.Apps.TLS.Certificates.LoadPEM)
|
|
}
|
|
|
|
// Test: encrypted key with no encryption service → skip
|
|
func TestGenerateConfig_CustomCert_EncryptedKeyNoEncSvc(t *testing.T) {
|
|
certID := uint(11)
|
|
hosts := []models.ProxyHost{
|
|
{
|
|
UUID: "h-noenc", DomainNames: "noenc.test", ForwardHost: "127.0.0.1", ForwardPort: 8080, Enabled: true,
|
|
CertificateID: &certID,
|
|
Certificate: &models.SSLCertificate{
|
|
ID: certID, UUID: "c-noenc", Name: "NoEncSvcCert", Provider: "custom",
|
|
Certificate: "-----BEGIN CERTIFICATE-----\nfake-cert\n-----END CERTIFICATE-----",
|
|
PrivateKeyEncrypted: "encrypted-data-here",
|
|
},
|
|
},
|
|
}
|
|
|
|
cfg, err := GenerateConfig(hosts, "/data", "admin@test.com", "/dist", "letsencrypt", true, false, false, false, false, "", nil, nil, nil, nil, nil)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, cfg)
|
|
// Cert should be skipped - no TLS certs loaded
|
|
if cfg.Apps.TLS != nil && cfg.Apps.TLS.Certificates != nil {
|
|
assert.Empty(t, cfg.Apps.TLS.Certificates.LoadPEM)
|
|
}
|
|
}
|
|
|
|
// Test: no key at all → skip
|
|
func TestGenerateConfig_CustomCert_NoKey(t *testing.T) {
|
|
certID := uint(12)
|
|
hosts := []models.ProxyHost{
|
|
{
|
|
UUID: "h-nokey", DomainNames: "nokey.test", ForwardHost: "127.0.0.1", ForwardPort: 8080, Enabled: true,
|
|
CertificateID: &certID,
|
|
Certificate: &models.SSLCertificate{
|
|
ID: certID, UUID: "c-nokey", Name: "NoKeyCert", Provider: "custom",
|
|
Certificate: "-----BEGIN CERTIFICATE-----\nfake-cert\n-----END CERTIFICATE-----",
|
|
},
|
|
},
|
|
}
|
|
|
|
cfg, err := GenerateConfig(hosts, "/data", "admin@test.com", "/dist", "letsencrypt", true, false, false, false, false, "", nil, nil, nil, nil, nil)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, cfg)
|
|
if cfg.Apps.TLS != nil && cfg.Apps.TLS.Certificates != nil {
|
|
assert.Empty(t, cfg.Apps.TLS.Certificates.LoadPEM)
|
|
}
|
|
}
|
|
|
|
// Test: missing cert PEM → skip
|
|
func TestGenerateConfig_CustomCert_NoCertPEM(t *testing.T) {
|
|
certID := uint(13)
|
|
hosts := []models.ProxyHost{
|
|
{
|
|
UUID: "h-nocert", DomainNames: "nocert.test", ForwardHost: "127.0.0.1", ForwardPort: 8080, Enabled: true,
|
|
CertificateID: &certID,
|
|
Certificate: &models.SSLCertificate{
|
|
ID: certID, UUID: "c-nocert", Name: "NoCertPEM", Provider: "custom",
|
|
PrivateKey: "some-key",
|
|
},
|
|
},
|
|
}
|
|
|
|
cfg, err := GenerateConfig(hosts, "/data", "admin@test.com", "/dist", "letsencrypt", true, false, false, false, false, "", nil, nil, nil, nil, nil)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, cfg)
|
|
if cfg.Apps.TLS != nil && cfg.Apps.TLS.Certificates != nil {
|
|
assert.Empty(t, cfg.Apps.TLS.Certificates.LoadPEM)
|
|
}
|
|
}
|
|
|
|
// Test: cert with chain → chain concatenated
|
|
func TestGenerateConfig_CustomCert_WithChain(t *testing.T) {
|
|
certID := uint(14)
|
|
hosts := []models.ProxyHost{
|
|
{
|
|
UUID: "h-chain", DomainNames: "chain.test", ForwardHost: "127.0.0.1", ForwardPort: 8080, Enabled: true,
|
|
CertificateID: &certID,
|
|
Certificate: &models.SSLCertificate{
|
|
ID: certID, UUID: "c-chain", Name: "ChainCert", Provider: "custom",
|
|
Certificate: "-----BEGIN CERTIFICATE-----\nleaf-cert\n-----END CERTIFICATE-----",
|
|
PrivateKey: "-----BEGIN PRIVATE KEY-----\nkey-data\n-----END PRIVATE KEY-----",
|
|
CertificateChain: "-----BEGIN CERTIFICATE-----\nca-cert\n-----END CERTIFICATE-----",
|
|
},
|
|
},
|
|
}
|
|
|
|
cfg, err := GenerateConfig(hosts, "/data", "admin@test.com", "/dist", "letsencrypt", true, false, false, false, false, "", nil, nil, nil, nil, nil)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, cfg)
|
|
require.NotNil(t, cfg.Apps.TLS)
|
|
require.NotNil(t, cfg.Apps.TLS.Certificates)
|
|
require.NotEmpty(t, cfg.Apps.TLS.Certificates.LoadPEM)
|
|
assert.Contains(t, cfg.Apps.TLS.Certificates.LoadPEM[0].Certificate, "ca-cert")
|
|
}
|
|
|
|
// Test: decrypt failure → skip
|
|
func TestGenerateConfig_CustomCert_DecryptFailure(t *testing.T) {
|
|
encSvc := newTestEncSvc(t)
|
|
certID := uint(15)
|
|
hosts := []models.ProxyHost{
|
|
{
|
|
UUID: "h-decfail", DomainNames: "decfail.test", ForwardHost: "127.0.0.1", ForwardPort: 8080, Enabled: true,
|
|
CertificateID: &certID,
|
|
Certificate: &models.SSLCertificate{
|
|
ID: certID, UUID: "c-decfail", Name: "DecryptFail", Provider: "custom",
|
|
Certificate: "-----BEGIN CERTIFICATE-----\nfake-cert\n-----END CERTIFICATE-----",
|
|
PrivateKeyEncrypted: "not-valid-encrypted-data",
|
|
},
|
|
},
|
|
}
|
|
|
|
cfg, err := GenerateConfig(hosts, "/data", "admin@test.com", "/dist", "letsencrypt", true, false, false, false, false, "", nil, nil, nil, nil, nil, encSvc)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, cfg)
|
|
if cfg.Apps.TLS != nil && cfg.Apps.TLS.Certificates != nil {
|
|
assert.Empty(t, cfg.Apps.TLS.Certificates.LoadPEM)
|
|
}
|
|
}
|