Files
zeekayandHanzo Dev 1ab6e799c1 cd: the module is github.com/hanzoai/cd, where it is actually hosted
go.mod still declared `module github.com/hanzoai/deploy` after the repo was
renamed. A Go module path is not cosmetic — it is the resolvable location. So
`go get github.com/hanzoai/cd` could not work, and anything importing this
module had to spell a name that no longer exists at that address. The build log
of v3.6.0 is where it surfaced: the compiler was still emitting
github.com/hanzoai/deploy/cmd/cd/commands.

3541 import paths across 690 .go, 19 .proto, 4 .yaml, 3 .sh, 2 .mod, Procfile
and Makefile. Plus the 62 ghcr.io/hanzoai/deploy image references, moved to
ghcr.io/hanzoai/cd to match hanzo.yml's build target — a distinct string from
the module path, replaced separately so neither could clobber the other.

go mod tidy clean, CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./... clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-28 21:16:24 -07:00

220 lines
5.8 KiB
Go

package application
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
"github.com/hanzoai/cd/common"
"github.com/hanzoai/cd/util/assets"
"github.com/hanzoai/cd/util/rbac"
"github.com/golang-jwt/jwt/v5"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func newTestTerminalSession(w http.ResponseWriter, r *http.Request) terminalSession {
upgrader := websocket.Upgrader{}
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return terminalSession{}
}
return terminalSession{wsConn: c}
}
func newEnforcer() *rbac.Enforcer {
additionalConfig := make(map[string]string, 0)
kubeclientset := fake.NewClientset(&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: testNamespace,
Name: "cd-cm",
Labels: map[string]string{
"app.kubernetes.io/part-of": "cd",
},
},
Data: additionalConfig,
}, &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "cd-secret",
Namespace: testNamespace,
},
Data: map[string][]byte{
"admin.password": []byte("test"),
"server.secretkey": []byte("test"),
},
})
enforcer := rbac.NewEnforcer(kubeclientset, testNamespace, common.ArgoCDRBACConfigMapName, nil)
return enforcer
}
func reconnect(w http.ResponseWriter, r *http.Request) {
ts := newTestTerminalSession(w, r)
_, _ = ts.reconnect()
}
func TestReconnect(t *testing.T) {
t.Parallel()
s := httptest.NewServer(http.HandlerFunc(reconnect))
defer s.Close()
u := "ws" + strings.TrimPrefix(s.URL, "http")
// Connect to the server
ws, _, err := websocket.DefaultDialer.Dial(u, nil)
require.NoError(t, err)
defer ws.Close()
_, p, _ := ws.ReadMessage()
var message TerminalMessage
err = json.Unmarshal(p, &message)
require.NoError(t, err)
assert.Equal(t, ReconnectMessage, message.Data)
}
func testServerConnection(t *testing.T, testFunc func(w http.ResponseWriter, r *http.Request), expectPermissionDenied bool) {
t.Helper()
s := httptest.NewServer(http.HandlerFunc(testFunc))
defer s.Close()
u := "ws" + strings.TrimPrefix(s.URL, "http")
// Connect to the server
ws, _, err := websocket.DefaultDialer.Dial(u, nil)
require.NoError(t, err)
defer ws.Close()
if expectPermissionDenied {
_, p, _ := ws.ReadMessage()
var message TerminalMessage
err = json.Unmarshal(p, &message)
require.NoError(t, err)
assert.Equal(t, "Permission denied", message.Data)
}
}
func TestVerifyAndReconnectDisableAuthTrue(t *testing.T) {
t.Parallel()
validate := func(w http.ResponseWriter, r *http.Request) {
ts := newTestTerminalSession(w, r)
// Currently testing only the usecase of disableAuth: true since the disableAuth: false case
// requires a valid token to be passed in the request.
// Note that running with disableAuth: false will surprisingly succeed as well, because
// the underlying token nil pointer dereference is swallowed in a location I didn't find,
// or even swallowed by the test framework.
ts.terminalOpts = &TerminalOptions{DisableAuth: true}
code, err := ts.performValidationsAndReconnect([]byte{})
assert.Equal(t, 0, code)
require.NoError(t, err)
}
testServerConnection(t, validate, false)
}
func TestValidateWithAdminPermissions(t *testing.T) {
t.Parallel()
validate := func(w http.ResponseWriter, r *http.Request) {
enf := newEnforcer()
_ = enf.SetBuiltinPolicy(assets.BuiltinPolicyCSV)
enf.SetDefaultRole("role:admin")
enf.SetClaimsEnforcerFunc(func(_ jwt.Claims, _ ...any) bool {
return true
})
ts := newTestTerminalSession(w, r)
ts.terminalOpts = &TerminalOptions{Enf: enf}
ts.appRBACName = "test"
//nolint:staticcheck
ts.ctx = context.WithValue(t.Context(), "claims", &jwt.MapClaims{"groups": []string{"admin"}})
_, err := ts.validatePermissions([]byte{})
require.NoError(t, err)
}
testServerConnection(t, validate, false)
}
func TestValidateWithoutPermissions(t *testing.T) {
t.Parallel()
validate := func(w http.ResponseWriter, r *http.Request) {
enf := newEnforcer()
_ = enf.SetBuiltinPolicy(assets.BuiltinPolicyCSV)
enf.SetDefaultRole("role:test")
enf.SetClaimsEnforcerFunc(func(_ jwt.Claims, _ ...any) bool {
return false
})
ts := newTestTerminalSession(w, r)
ts.terminalOpts = &TerminalOptions{Enf: enf}
ts.appRBACName = "test"
//nolint:staticcheck
ts.ctx = context.WithValue(t.Context(), "claims", &jwt.MapClaims{"groups": []string{"test"}})
_, err := ts.validatePermissions([]byte{})
require.Error(t, err)
assert.EqualError(t, err, common.PermissionDeniedAPIError.Error())
}
testServerConnection(t, validate, true)
}
func TestTerminalSession_Write(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
upgrader := websocket.Upgrader{}
conn, err := upgrader.Upgrade(w, r, nil)
require.NoError(t, err)
defer conn.Close()
for {
// Read the message from the WebSocket connection
messageType, message, err := conn.ReadMessage()
if err != nil {
return
}
// Respond back the same message
err = conn.WriteMessage(messageType, message)
require.NoError(t, err)
}
}))
defer server.Close()
u := "ws" + strings.TrimPrefix(server.URL, "http")
wsConn, _, err := websocket.DefaultDialer.Dial(u, nil)
require.NoError(t, err)
defer wsConn.Close()
ts := terminalSession{
wsConn: wsConn,
}
testData := []byte("hello world")
expectedMessage, err := json.Marshal(TerminalMessage{
Operation: "stdout",
Data: string(testData),
})
require.NoError(t, err)
n, err := ts.Write(testData)
require.NoError(t, err)
assert.Equal(t, len(testData), n)
_, receivedMessage, err := wsConn.ReadMessage()
require.NoError(t, err)
assert.Equal(t, expectedMessage, receivedMessage)
}