mirror of
https://github.com/hanzoai/deploy.git
synced 2026-10-07 08:44:50 +00:00
* feat(api): hard-fork CRD API group argoproj.io -> deploy.hanzo.ai
Hanzo CD hard-fork, phase 1.1. Single source of truth is
pkg/apis/application/register.go's Group constant (Application/AppProject/
ApplicationSet all derive their FullName from it); every other Go call-site
that hardcoded the literal group string is updated to match: the 3 generated
informer GVRs, the app-of-apps nested-Application detector in types.go, the
resource-tree Application-kind cache populator, the 3 NotFound error
constructors in server/application, and the applicationset-controller's
leader-election Lease name/ID (kept in lockstep across Go source + RBAC
resourceNames, or leader election would get RBAC-denied on startup).
CRD YAMLs (application/appproject/applicationset) and every RBAC apiGroups
rule that actually governs our 3 CRDs are repointed to deploy.hanzo.ai, across
both manifests/base/* (namespaced Roles) and manifests/cluster-rbac/* (cluster
scope). Two adjacent things that share the "argoproj.io" string but are NOT
ours are deliberately left untouched: gitops-engine's health-check dispatch
for Argo Workflows' `Workflow` kind, and argocd-server-clusterrole's second
apiGroups block granting read on Argo Workflows' `workflows` resource — both
belong to a separate, unrelated Argoproj-family project we are not forking.
Also untouched, per explicit scope: annotation/label keys (argocd.argoproj.io/*)
and finalizers (*.argoproj.io) that track already-applied live resources, and
argocd-cm.yaml's resource.customizations.ignoreResourceUpdates keys (a narrow
app-of-apps diffing nicety, not build- or RBAC-relevant).
CGO_ENABLED=0 go build ./... verified green.
* ci: wire canonical hanzo.yml + cicd.yml (Hanzo CD image build)
Phase 1.3. Follows the same one-way pattern every hanzo/lux/zoo repo uses:
hanzo.yml declares the image (root Dockerfile -> ghcr.io/hanzoai/deploy) and a
fast go-build sanity test (the multi-stage Dockerfile build is the slow, real
gate); cicd.yml is the ~10-line trigger shim importing hanzoai/ci's reusable
build workflow on the arc pool, secrets from KMS only.
No `deploy:` block yet, matching the hanzoai/cms build-only-first-cutover
pattern: this repo's own live hanzo-cd namespace runs the whole Hanzo fleet's
GitOps, so the prod image/CRD swap is a deliberate separate action (see the
[HELD] universe PR), never an automatic rollout on merge. Every PR/push still
builds and pushes an immutable ghcr.io/hanzoai/deploy:sha-<7>-amd64 image.
* feat(ui): rebrand Argo CD -> Hanzo CD (title, favicon, logo, sidebar)
Phase 1.2. User-facing "Argo CD"/"ArgoCD" text replaced with "Hanzo CD"
across the login page, help modal, settings screens, version-info panel,
sync-options warning, fullscreen-logs title, app.tsx page titles, and
index.html's <title>/noscript text (dropped the now-wrong upstream CLI
doc link rather than invent a hanzo.ai URL that doesn't exist).
Logo/favicon assets replaced with the canonical Hanzo mark (never
hand-drawn, copied from ~/work/hanzo/logo/dist): the two real usages of
Argo's logo collapse onto one Hanzo glyph (mono + white variants) at the
same 5 call sites (login icon + footer, login.scss $logoPath/
$logoNegativePath, sidebar text-logo), preserving the existing CSS
invert() filters so both the light login panel and dark sidebar still
render correctly. Old argologo.svg/argo_o.svg/argo.png deleted (no
longer referenced). favicon-{16x16,32x32}.png added at
ui/src/assets/favicon/ (index.html's links to that path were previously
dead - the directory didn't exist in this checkout). package.json name
argo-cd-ui -> hanzo-cd-ui.
Left alone, deliberately: the two Kubernetes resource-kind icon sets
under ui/src/assets/images/resources/ (unrelated to branding), and
upstream doc/project links (argoproj.io, argo-cd.readthedocs.io) since
we have no equivalent hanzo.ai docs to point to yet - except the login
footer logo's outbound link, which now points to hanzo.ai instead of
argoproj.io (a Hanzo-branded mark linking to the upstream project it
was forked from read as an obvious leftover).
* feat(module): rename Go module github.com/argoproj/argo-cd/v3 -> github.com/hanzoai/deploy
Phase 2. No version suffix on the new path (org policy: never publish our
own Go modules above v1.x.x / with a v2+ suffix) — this fork starts a fresh
v0/v1 module identity rather than carrying the v3 the upstream fork point
had reached.
Mechanical, symmetric rewrite: 704 .go files (import statements only; two
disjoint substitutions applied specific-first so the nested gitops-engine
module path never double-matches), plus go.mod (module line, the
gitops-engine require+replace pair, version reset to v0.0.0-... since a
suffix-less path can't carry a v3 semver), gitops-engine/go.mod, Makefile
(ldflags PACKAGE + 2 TEST_MODULE filters), .golangci.yaml, .goreleaser.yaml,
.mockery.yaml (+ gitops-engine's), Procfile, the codegen/proto generation
scripts (hack/generate-proto.sh, update-codegen.sh, update-openapi.sh), and
20 .proto files' go_package options. Also one OpenTelemetry tracer name
string in gitops-engine/pkg/sync/sync_context.go (a cosmestic label, not an
import, but worth keeping consistent).
CGO_ENABLED=0 go build ./... verified green for both the root module and
the nested gitops-engine module; go vet clean on both; go mod tidy run
(network available) to reconcile go.sum, dependency set unchanged.
Left alone, deliberately: doc-only mentions (docs/snyk/*.html scan reports,
gitops-engine/README.md), a comment-only reference in
.github/workflows/release.yaml, and one Renovate bot config line — none are
build- or codegen-load-bearing. Also left alone: comments citing upstream
GitHub issue URLs (github.com/argoproj/argo-cd/issues/NNNNN) and a handful
of test fixtures using "https://github.com/argoproj/argo-cd.git" as a
representative example repo URL — both are accurate historical/test-data
references, not stale self-references.
---------
Co-authored-by: Hanzo Dev <[email protected]>
640 lines
21 KiB
Go
640 lines
21 KiB
Go
package apiclient_test
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/connectivity"
|
|
"google.golang.org/grpc/credentials"
|
|
"google.golang.org/grpc/health"
|
|
"google.golang.org/grpc/health/grpc_health_v1"
|
|
"google.golang.org/grpc/status"
|
|
|
|
utilstls "github.com/hanzoai/deploy/util/tls"
|
|
|
|
"github.com/hanzoai/deploy/reposerver/apiclient"
|
|
"github.com/hanzoai/deploy/reposerver/apiclient/mocks"
|
|
)
|
|
|
|
func TestNewRepoServerClient_CorrectClientReturned(t *testing.T) {
|
|
t.Parallel()
|
|
mockClientset := &mocks.Clientset{
|
|
RepoServerServiceClient: &mocks.RepoServerServiceClient{},
|
|
}
|
|
|
|
closer, client, err := mockClientset.NewRepoServerClient()
|
|
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, closer)
|
|
assert.NotNil(t, client)
|
|
assert.Equal(t, mockClientset.RepoServerServiceClient, client)
|
|
}
|
|
|
|
func TestNewRepoServerClientset_InvalidInput(t *testing.T) {
|
|
t.Parallel()
|
|
// Call the function with invalid inputs
|
|
invalidClientset := apiclient.NewRepoServerClientset("", -1, utilstls.Configuration{})
|
|
|
|
assert.NotNil(t, invalidClientset)
|
|
assert.Implements(t, (*apiclient.Clientset)(nil), invalidClientset)
|
|
}
|
|
|
|
func TestNewRepoServerClientset_SuccessfulConnection(t *testing.T) {
|
|
t.Parallel()
|
|
// Call the function with valid inputs
|
|
clientset := apiclient.NewRepoServerClientset("localhost:8080", 1, utilstls.Configuration{})
|
|
|
|
assert.NotNil(t, clientset)
|
|
assert.Implements(t, (*apiclient.Clientset)(nil), clientset)
|
|
}
|
|
|
|
func TestNewRepoServerClientset_SuccessfulConnectionWithTLS(t *testing.T) {
|
|
t.Parallel()
|
|
// Call the function with valid inputs
|
|
clientset := apiclient.NewRepoServerClientset("localhost:8080", 1, utilstls.Configuration{
|
|
DisableTLS: false,
|
|
StrictValidation: true,
|
|
Certificates: nil,
|
|
})
|
|
|
|
assert.NotNil(t, clientset)
|
|
assert.Implements(t, (*apiclient.Clientset)(nil), clientset)
|
|
}
|
|
|
|
func TestNewConnection_TLSWithStrictValidation(t *testing.T) {
|
|
t.Parallel()
|
|
tlsConfig := utilstls.Configuration{
|
|
DisableTLS: false,
|
|
StrictValidation: true,
|
|
Certificates: nil,
|
|
}
|
|
|
|
conn, err := apiclient.NewConnection("example.com:443", 10, &tlsConfig)
|
|
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, conn)
|
|
}
|
|
|
|
func TestNewConnection_TLSWithStrictValidationAndCertificates(t *testing.T) {
|
|
t.Parallel()
|
|
tlsConfig := utilstls.Configuration{
|
|
DisableTLS: false,
|
|
StrictValidation: true,
|
|
Certificates: nil,
|
|
}
|
|
|
|
conn, err := apiclient.NewConnection("example.com:443", 10, &tlsConfig)
|
|
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, conn)
|
|
}
|
|
|
|
func TestNewConnection_InsecureConnection(t *testing.T) {
|
|
t.Parallel()
|
|
// Create a TLS configuration with TLS disabled
|
|
tlsConfig := utilstls.Configuration{
|
|
DisableTLS: true,
|
|
StrictValidation: false,
|
|
Certificates: nil,
|
|
}
|
|
|
|
conn, err := apiclient.NewConnection("example.com:80", 10, &tlsConfig)
|
|
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, conn)
|
|
}
|
|
|
|
func TestNewConnection_InMemoryCertificates(t *testing.T) {
|
|
tlsConfig := utilstls.Configuration{
|
|
DisableTLS: false,
|
|
StrictValidation: false,
|
|
ClientCertificates: []tls.Certificate{{}},
|
|
}
|
|
|
|
conn, err := apiclient.NewConnection("example.com:443", 10, &tlsConfig)
|
|
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, conn)
|
|
}
|
|
|
|
func TestNewConnection_ServesFromCacheWhenFilesUnchanged(t *testing.T) {
|
|
t.Cleanup(apiclient.ResetClientCertCache)
|
|
apiclient.ResetClientCertCache()
|
|
|
|
certFile := filepath.Join("..", "..", "util", "tls", "testdata", "valid_tls.crt")
|
|
keyFile := filepath.Join("..", "..", "util", "tls", "testdata", "valid_tls.key")
|
|
|
|
tempCertFile := filepath.Join(t.TempDir(), "client.crt")
|
|
tempKeyFile := filepath.Join(t.TempDir(), "client.key")
|
|
|
|
certBytes, err := os.ReadFile(certFile)
|
|
require.NoError(t, err)
|
|
require.NoError(t, os.WriteFile(tempCertFile, certBytes, 0o600))
|
|
|
|
keyBytes, err := os.ReadFile(keyFile)
|
|
require.NoError(t, err)
|
|
require.NoError(t, os.WriteFile(tempKeyFile, keyBytes, 0o600))
|
|
|
|
tlsConfig := utilstls.Configuration{
|
|
StrictValidation: false,
|
|
ClientCertFile: tempCertFile,
|
|
ClientCertKeyFile: tempKeyFile,
|
|
}
|
|
|
|
// First call: loads from disk and populates the cache.
|
|
conn, err := apiclient.NewConnection("example.com:443", 10, &tlsConfig)
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, conn)
|
|
|
|
// Second call: files are untouched, mtime is unchanged — cert must be served from the cache without re-reading disk.
|
|
conn, err = apiclient.NewConnection("example.com:443", 10, &tlsConfig)
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, conn)
|
|
}
|
|
|
|
func TestNewConnection_ReloadsClientCertificateWhenFileChanges(t *testing.T) {
|
|
t.Cleanup(apiclient.ResetClientCertCache)
|
|
apiclient.ResetClientCertCache()
|
|
|
|
certFile := filepath.Join("..", "..", "util", "tls", "testdata", "valid_tls.crt")
|
|
keyFile := filepath.Join("..", "..", "util", "tls", "testdata", "valid_tls.key")
|
|
|
|
tempCertFile := filepath.Join(t.TempDir(), "client.crt")
|
|
tempKeyFile := filepath.Join(t.TempDir(), "client.key")
|
|
|
|
certBytes, err := os.ReadFile(certFile)
|
|
require.NoError(t, err)
|
|
require.NoError(t, os.WriteFile(tempCertFile, certBytes, 0o600))
|
|
|
|
keyBytes, err := os.ReadFile(keyFile)
|
|
require.NoError(t, err)
|
|
require.NoError(t, os.WriteFile(tempKeyFile, keyBytes, 0o600))
|
|
|
|
tlsConfig := utilstls.Configuration{
|
|
StrictValidation: false,
|
|
ClientCertFile: tempCertFile,
|
|
ClientCertKeyFile: tempKeyFile,
|
|
}
|
|
|
|
// First call: loads and caches the cert.
|
|
conn, err := apiclient.NewConnection("example.com:443", 10, &tlsConfig)
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, conn)
|
|
|
|
// Simulate cert rotation: overwrite with fresh (still valid) cert bytes.
|
|
// Writing updates the mtime, which is enough to trigger a reload.
|
|
require.NoError(t, os.WriteFile(tempCertFile, certBytes, 0o600))
|
|
require.NoError(t, os.WriteFile(tempKeyFile, keyBytes, 0o600))
|
|
|
|
// Second call: mtime changed → cache invalidated → reloads from disk.
|
|
// The new cert is valid, so the call must succeed.
|
|
conn, err = apiclient.NewConnection("example.com:443", 10, &tlsConfig)
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, conn)
|
|
}
|
|
|
|
func TestNewConnection_ErrorWhenRotatedCertIsInvalid(t *testing.T) {
|
|
t.Cleanup(apiclient.ResetClientCertCache)
|
|
apiclient.ResetClientCertCache()
|
|
|
|
certFile := filepath.Join("..", "..", "util", "tls", "testdata", "valid_tls.crt")
|
|
keyFile := filepath.Join("..", "..", "util", "tls", "testdata", "valid_tls.key")
|
|
|
|
tempCertFile := filepath.Join(t.TempDir(), "client.crt")
|
|
tempKeyFile := filepath.Join(t.TempDir(), "client.key")
|
|
|
|
certBytes, err := os.ReadFile(certFile)
|
|
require.NoError(t, err)
|
|
require.NoError(t, os.WriteFile(tempCertFile, certBytes, 0o600))
|
|
|
|
keyBytes, err := os.ReadFile(keyFile)
|
|
require.NoError(t, err)
|
|
require.NoError(t, os.WriteFile(tempKeyFile, keyBytes, 0o600))
|
|
|
|
tlsConfig := utilstls.Configuration{
|
|
StrictValidation: false,
|
|
ClientCertFile: tempCertFile,
|
|
ClientCertKeyFile: tempKeyFile,
|
|
}
|
|
|
|
// First call: succeeds and populates the cache.
|
|
conn, err := apiclient.NewConnection("example.com:443", 10, &tlsConfig)
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, conn)
|
|
|
|
require.NoError(t, os.WriteFile(tempCertFile, []byte("not a valid certificate"), 0o600))
|
|
require.NoError(t, os.WriteFile(tempKeyFile, []byte("not a valid key"), 0o600))
|
|
|
|
// Second call: mtime changed → reload attempted → parse fails → error returned.
|
|
// The stale (valid) cached cert must NOT be silently served.
|
|
_, err = apiclient.NewConnection("example.com:443", 10, &tlsConfig)
|
|
require.Error(t, err,
|
|
"a corrupt cert file must cause an error after cache invalidation, "+
|
|
"not silently serve the stale cached cert")
|
|
assert.Contains(t, err.Error(), "failed to load client certificate",
|
|
"error must identify the cert load as the failure site")
|
|
}
|
|
|
|
func TestNewConnection_DoesNotCacheClientCertificateLoadError(t *testing.T) {
|
|
t.Cleanup(apiclient.ResetClientCertCache)
|
|
apiclient.ResetClientCertCache()
|
|
|
|
tempDir := t.TempDir()
|
|
tempCertFile := filepath.Join(tempDir, "client.crt")
|
|
tempKeyFile := filepath.Join(tempDir, "client.key")
|
|
|
|
tlsConfig := utilstls.Configuration{
|
|
StrictValidation: false,
|
|
ClientCertFile: tempCertFile,
|
|
ClientCertKeyFile: tempKeyFile,
|
|
}
|
|
|
|
conn, err := apiclient.NewConnection("example.com:443", 10, &tlsConfig)
|
|
require.NoError(t, err, "missing cert file must not cause an error — mTLS is skipped gracefully")
|
|
assert.NotNil(t, conn)
|
|
|
|
certFile := filepath.Join("..", "..", "util", "tls", "testdata", "valid_tls.crt")
|
|
keyFile := filepath.Join("..", "..", "util", "tls", "testdata", "valid_tls.key")
|
|
|
|
certBytes, err := os.ReadFile(certFile)
|
|
require.NoError(t, err)
|
|
require.NoError(t, os.WriteFile(tempCertFile, certBytes, 0o600))
|
|
|
|
keyBytes, err := os.ReadFile(keyFile)
|
|
require.NoError(t, err)
|
|
require.NoError(t, os.WriteFile(tempKeyFile, keyBytes, 0o600))
|
|
|
|
conn, err = apiclient.NewConnection("example.com:443", 10, &tlsConfig)
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, conn)
|
|
}
|
|
|
|
func TestMTLSIntegration_NoClientCert_IsRejected(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, parsedClientCert := generateClientCA(t, "test-client")
|
|
|
|
pool := x509.NewCertPool()
|
|
pool.AddCert(parsedClientCert)
|
|
fixture := newMTLSServer(t, pool)
|
|
|
|
conn, err := apiclient.NewConnection(fixture.addr, 5, &utilstls.Configuration{
|
|
StrictValidation: false,
|
|
})
|
|
require.NoError(t, err, "NewConnection is lazy — dial error surfaces on first RPC")
|
|
defer func(conn *grpc.ClientConn) {
|
|
err := conn.Close()
|
|
if err != nil {
|
|
t.Logf("failed to close connection: %v", err)
|
|
}
|
|
}(conn)
|
|
|
|
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
|
|
defer cancel()
|
|
conn.Connect()
|
|
for {
|
|
st := conn.GetState()
|
|
if st == connectivity.Ready || st == connectivity.TransientFailure || st == connectivity.Shutdown {
|
|
break
|
|
}
|
|
if !conn.WaitForStateChange(ctx, st) {
|
|
break
|
|
}
|
|
}
|
|
|
|
err = healthCheck(t, conn)
|
|
require.Error(t, err, "the server enforces mTLS; a client without a certificate must be rejected")
|
|
// gRPC may surface the TLS alert as "certificate required", as a transport-level
|
|
// "broken pipe" when the server closes the connection after detecting the missing cert,
|
|
// or as "connection reset by peer" when the server sends a TCP RST before completing
|
|
// the TLS handshake. All are valid indicators of an mTLS rejection.
|
|
assert.True(t,
|
|
strings.Contains(err.Error(), "certificate") || strings.Contains(err.Error(), "broken pipe") || strings.Contains(err.Error(), "connection reset by peer"),
|
|
"rejection must be a TLS-related error (certificate, broken pipe, or connection reset by peer), got: %v", err,
|
|
)
|
|
}
|
|
|
|
func TestMTLSIntegration_ValidClientCert_IsAccepted(t *testing.T) {
|
|
t.Parallel()
|
|
t.Cleanup(apiclient.ResetClientCertCache)
|
|
|
|
clientCert, parsedClientCert := generateClientCA(t, "test-client")
|
|
|
|
pool := x509.NewCertPool()
|
|
pool.AddCert(parsedClientCert)
|
|
fixture := newMTLSServer(t, pool)
|
|
|
|
conn, err := apiclient.NewConnection(fixture.addr, 5, &utilstls.Configuration{
|
|
StrictValidation: false,
|
|
ClientCertificates: []tls.Certificate{*clientCert},
|
|
})
|
|
require.NoError(t, err)
|
|
defer func(conn *grpc.ClientConn) {
|
|
err := conn.Close()
|
|
if err != nil {
|
|
t.Logf("failed to close connection: %v", err)
|
|
}
|
|
}(conn)
|
|
|
|
require.NoError(t, healthCheck(t, conn), "client presenting a cert trusted by the server's ClientCAs must succeed")
|
|
}
|
|
|
|
func TestMTLSIntegration_UntrustedClientCert_IsRejected(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, trustedParsed := generateClientCA(t, "trusted-ca")
|
|
trustedPool := x509.NewCertPool()
|
|
trustedPool.AddCert(trustedParsed)
|
|
fixture := newMTLSServer(t, trustedPool)
|
|
|
|
untrustedCert, _ := generateClientCA(t, "untrusted-client")
|
|
|
|
conn, err := apiclient.NewConnection(fixture.addr, 5, &utilstls.Configuration{
|
|
StrictValidation: false,
|
|
ClientCertificates: []tls.Certificate{*untrustedCert},
|
|
})
|
|
require.NoError(t, err, "NewConnection is lazy")
|
|
defer func(conn *grpc.ClientConn) {
|
|
err := conn.Close()
|
|
if err != nil {
|
|
t.Logf("failed to close connection: %v", err)
|
|
}
|
|
}(conn)
|
|
|
|
require.Error(t, healthCheck(t, conn), "a cert signed by an unknown CA must be rejected by the server")
|
|
}
|
|
|
|
func TestMTLSIntegration_HealthCheckEphemeralCert_IsAccepted(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
hcCert, err := utilstls.GenerateHealthCheckClientCert()
|
|
require.NoError(t, err, "GenerateHealthCheckClientCert")
|
|
|
|
parsedHCCert, err := x509.ParseCertificate(hcCert.Certificate[0])
|
|
require.NoError(t, err)
|
|
|
|
pool := x509.NewCertPool()
|
|
pool.AddCert(parsedHCCert)
|
|
fixture := newMTLSServer(t, pool)
|
|
|
|
conn, err := apiclient.NewConnection(fixture.addr, 5, &utilstls.Configuration{
|
|
StrictValidation: false, // liveness probe skips server cert verification
|
|
ClientCertificates: []tls.Certificate{*hcCert},
|
|
})
|
|
require.NoError(t, err)
|
|
defer func(conn *grpc.ClientConn) {
|
|
err := conn.Close()
|
|
if err != nil {
|
|
t.Logf("failed to close connection: %v", err)
|
|
}
|
|
}(conn)
|
|
|
|
require.NoError(t, healthCheck(t, conn), "liveness probe using the ephemeral health-check cert must be accepted")
|
|
}
|
|
|
|
// TestMTLSIntegration_DisableTLS_PlaintextConnection verifies that a server
|
|
// running without TLS accepts a plaintext client, establishing the baseline
|
|
// that DisableTLS=true on the client side still works after this PR.
|
|
func TestMTLSIntegration_DisableTLS_PlaintextConnection(t *testing.T) {
|
|
t.Parallel()
|
|
lis, err := new(net.ListenConfig).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
|
require.NoError(t, err)
|
|
|
|
srv := grpc.NewServer() // no TLS credentials
|
|
hsvc := health.NewServer()
|
|
hsvc.SetServingStatus("", grpc_health_v1.HealthCheckResponse_SERVING)
|
|
grpc_health_v1.RegisterHealthServer(srv, hsvc)
|
|
go func() { _ = srv.Serve(lis) }()
|
|
t.Cleanup(srv.Stop)
|
|
|
|
conn, err := apiclient.NewConnection(lis.Addr().String(), 5, &utilstls.Configuration{
|
|
DisableTLS: true,
|
|
})
|
|
require.NoError(t, err)
|
|
defer conn.Close()
|
|
|
|
err = healthCheck(t, conn)
|
|
require.NoError(t, err, "plaintext connection to a plaintext server must succeed")
|
|
}
|
|
|
|
// TestMTLSIntegration_StrictValidation_CertificatesPoolTakesPrecedence
|
|
// exercises the buildTLSClientConfig fix: when Certificates is non-nil,
|
|
// InsecureSkipVerify must be false even if StrictValidation=false in the
|
|
// struct. This is the `strictValidation := tlsConfig.StrictValidation ||
|
|
// tlsConfig.Certificates != nil` guard in buildTLSClientConfig.
|
|
//
|
|
// The test verifies observable behaviour: the connection succeeds only when
|
|
// the client presents the correct CA pool (i.e. server cert verification is
|
|
// actually being performed, not skipped).
|
|
func TestMTLSIntegration_StrictValidation_CertificatesPoolTakesPrecedence(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
clientCert, parsedClientCert := generateClientCA(t, "test-client-strict")
|
|
clientPool := x509.NewCertPool()
|
|
clientPool.AddCert(parsedClientCert)
|
|
fixture := newMTLSServer(t, clientPool)
|
|
|
|
// We need the client to verify the server's certificate, so we must tell
|
|
// it which CA signed the server cert. The server is self-signed, so we
|
|
// extract its cert and build a server-CA pool from it.
|
|
serverTLSState, err := extractServerCert(t, fixture.addr, clientCert)
|
|
require.NoError(t, err)
|
|
serverCAParsed, err := x509.ParseCertificate(serverTLSState.Certificate[0])
|
|
require.NoError(t, err)
|
|
serverCAPool := x509.NewCertPool()
|
|
serverCAPool.AddCert(serverCAParsed)
|
|
|
|
// StrictValidation=false BUT Certificates is non-nil.
|
|
// buildTLSClientConfig must treat this as strict=true (the fix).
|
|
conn, err := apiclient.NewConnection(fixture.addr, 5, &utilstls.Configuration{
|
|
StrictValidation: false, // struct field says "not strict" …
|
|
Certificates: serverCAPool, // … but a pool is present, so strict wins
|
|
ClientCertificates: []tls.Certificate{*clientCert},
|
|
})
|
|
require.NoError(t, err)
|
|
defer conn.Close()
|
|
|
|
err = healthCheck(t, conn)
|
|
require.NoError(t, err,
|
|
"when Certificates pool is set, server cert must be verified and the "+
|
|
"connection must succeed when the pool contains the server's CA")
|
|
}
|
|
|
|
func TestNewConnection_ClientCertFileDoesNotExist_SkipsMTLS(t *testing.T) {
|
|
t.Cleanup(apiclient.ResetClientCertCache)
|
|
apiclient.ResetClientCertCache()
|
|
|
|
tlsConfig := utilstls.Configuration{
|
|
StrictValidation: false,
|
|
ClientCertFile: "/nonexistent/path/client.crt",
|
|
ClientCertKeyFile: "/nonexistent/path/client.key",
|
|
}
|
|
|
|
conn, err := apiclient.NewConnection("example.com:443", 10, &tlsConfig)
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, conn)
|
|
}
|
|
|
|
func TestNewConnection_ClientCertFileStatNonErrNotExist_ReturnsError(t *testing.T) {
|
|
t.Cleanup(apiclient.ResetClientCertCache)
|
|
apiclient.ResetClientCertCache()
|
|
|
|
dir := t.TempDir()
|
|
require.NoError(t, os.Chmod(dir, 0o000))
|
|
t.Cleanup(func() { _ = os.Chmod(dir, 0o700) })
|
|
|
|
tlsConfig := utilstls.Configuration{
|
|
StrictValidation: false,
|
|
ClientCertFile: filepath.Join(dir, "client.crt"),
|
|
ClientCertKeyFile: filepath.Join(dir, "client.key"),
|
|
}
|
|
|
|
_, err := apiclient.NewConnection("example.com:443", 10, &tlsConfig)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "could not stat client cert file")
|
|
}
|
|
|
|
func TestNewConnection_ClientCertCallbackReturnsEmptyCertWhenFileDisappears(t *testing.T) {
|
|
t.Cleanup(apiclient.ResetClientCertCache)
|
|
apiclient.ResetClientCertCache()
|
|
|
|
srcCert := filepath.Join("..", "..", "util", "tls", "testdata", "valid_tls.crt")
|
|
srcKey := filepath.Join("..", "..", "util", "tls", "testdata", "valid_tls.key")
|
|
|
|
dir := t.TempDir()
|
|
tempCert := filepath.Join(dir, "client.crt")
|
|
tempKey := filepath.Join(dir, "client.key")
|
|
|
|
certBytes, err := os.ReadFile(srcCert)
|
|
require.NoError(t, err)
|
|
require.NoError(t, os.WriteFile(tempCert, certBytes, 0o600))
|
|
|
|
keyBytes, err := os.ReadFile(srcKey)
|
|
require.NoError(t, err)
|
|
require.NoError(t, os.WriteFile(tempKey, keyBytes, 0o600))
|
|
|
|
tlsConfig := utilstls.Configuration{
|
|
StrictValidation: false,
|
|
ClientCertFile: tempCert,
|
|
ClientCertKeyFile: tempKey,
|
|
}
|
|
|
|
conn, err := apiclient.NewConnection("example.com:443", 10, &tlsConfig)
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, conn)
|
|
|
|
require.NoError(t, os.Remove(tempCert))
|
|
require.NoError(t, os.Remove(tempKey))
|
|
|
|
apiclient.ResetClientCertCache()
|
|
conn, err = apiclient.NewConnection("example.com:443", 10, &tlsConfig)
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, conn)
|
|
}
|
|
|
|
func extractServerCert(t *testing.T, addr string, clientCert *tls.Certificate) (*tls.Certificate, error) {
|
|
t.Helper()
|
|
cfg := &tls.Config{
|
|
InsecureSkipVerify: true, //nolint:gosec // intentional: only used to fetch the cert bytes
|
|
Certificates: []tls.Certificate{*clientCert},
|
|
}
|
|
d := &tls.Dialer{Config: cfg}
|
|
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
|
|
defer cancel()
|
|
conn, err := d.DialContext(ctx, "tcp", addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer conn.Close()
|
|
tlsConnection, ok := conn.(*tls.Conn)
|
|
if !ok {
|
|
t.Fatal("expected *tls.Conn from tls.Dialer")
|
|
}
|
|
state := tlsConnection.ConnectionState()
|
|
if len(state.PeerCertificates) == 0 {
|
|
t.Fatal("server sent no certificate")
|
|
}
|
|
raw := state.PeerCertificates[0].Raw
|
|
tlsCert := &tls.Certificate{Certificate: [][]byte{raw}}
|
|
return tlsCert, nil
|
|
}
|
|
|
|
type mTLSServerFixture struct {
|
|
addr string
|
|
server *grpc.Server //nolint:unused // for some reason this linter complains
|
|
}
|
|
|
|
func newMTLSServer(t *testing.T, clientCAs *x509.CertPool) *mTLSServerFixture {
|
|
t.Helper()
|
|
|
|
serverCert, err := utilstls.GenerateX509KeyPair(utilstls.CertOptions{
|
|
Hosts: []string{"localhost", "127.0.0.1"},
|
|
Organization: "Argo CD Integration Test Server",
|
|
IsCA: false,
|
|
ECDSACurve: "P256",
|
|
ValidFor: time.Hour,
|
|
})
|
|
require.NoError(t, err, "generating server certificate")
|
|
|
|
serverTLSCfg := &tls.Config{
|
|
Certificates: []tls.Certificate{*serverCert},
|
|
ClientCAs: clientCAs,
|
|
ClientAuth: tls.RequireAndVerifyClientCert,
|
|
MinVersion: tls.VersionTLS12,
|
|
}
|
|
|
|
lis, err := new(net.ListenConfig).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
|
require.NoError(t, err, "binding test listener")
|
|
|
|
srv := grpc.NewServer(grpc.Creds(credentials.NewTLS(serverTLSCfg)))
|
|
hsvc := health.NewServer()
|
|
hsvc.SetServingStatus("", grpc_health_v1.HealthCheckResponse_SERVING)
|
|
grpc_health_v1.RegisterHealthServer(srv, hsvc)
|
|
|
|
go func() { _ = srv.Serve(lis) }()
|
|
t.Cleanup(srv.Stop)
|
|
|
|
return &mTLSServerFixture{addr: lis.Addr().String(), server: srv}
|
|
}
|
|
|
|
func healthCheck(t *testing.T, conn *grpc.ClientConn) error {
|
|
t.Helper()
|
|
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
|
|
defer cancel()
|
|
resp, err := grpc_health_v1.NewHealthClient(conn).Check(ctx, &grpc_health_v1.HealthCheckRequest{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if resp.GetStatus() != grpc_health_v1.HealthCheckResponse_SERVING {
|
|
return status.Errorf(codes.Unavailable, "server status: %v", resp.GetStatus())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func generateClientCA(t *testing.T, cn string) (*tls.Certificate, *x509.Certificate) {
|
|
t.Helper()
|
|
cert, err := utilstls.GenerateX509KeyPair(utilstls.CertOptions{
|
|
Hosts: []string{cn},
|
|
Organization: "Argo CD Integration Test",
|
|
IsCA: true,
|
|
ECDSACurve: "P256",
|
|
ValidFor: time.Hour,
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
|
|
})
|
|
require.NoError(t, err)
|
|
parsed, err := x509.ParseCertificate(cert.Certificate[0])
|
|
require.NoError(t, err)
|
|
return cert, parsed
|
|
}
|