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

185 lines
5.1 KiB
Go

package dex
import (
"errors"
"fmt"
"os"
"sigs.k8s.io/yaml"
"github.com/hanzoai/cd/common"
"github.com/hanzoai/cd/util/settings"
log "github.com/sirupsen/logrus"
)
func GenerateDexConfigYAML(cdSettings *settings.ArgoCDSettings, disableTLS bool) ([]byte, error) {
if !cdSettings.IsDexConfigured() {
return nil, nil
}
redirectURL, err := cdSettings.RedirectURL()
if err != nil {
return nil, fmt.Errorf("failed to infer redirect url from config: %w", err)
}
var dexCfg map[string]any
err = yaml.Unmarshal([]byte(cdSettings.DexConfig), &dexCfg)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal dex.config from configmap: %w", err)
}
dexCfg["issuer"] = cdSettings.IssuerURL()
dexCfg["storage"] = map[string]any{
"type": "memory",
}
if disableTLS {
dexCfg["web"] = map[string]any{
"http": "0.0.0.0:5556",
}
} else {
dexCfg["web"] = map[string]any{
"https": "0.0.0.0:5556",
"tlsCert": "/tmp/tls.crt",
"tlsKey": "/tmp/tls.key",
}
}
if loggerCfg, found := dexCfg["logger"].(map[string]any); found {
if _, found := loggerCfg["level"]; !found {
loggerCfg["level"] = slogLevelFromLogrus(os.Getenv(common.EnvLogLevel))
}
if _, found := loggerCfg["format"]; !found {
loggerCfg["format"] = os.Getenv(common.EnvLogFormat)
}
} else {
dexCfg["logger"] = map[string]any{
"level": slogLevelFromLogrus(os.Getenv(common.EnvLogLevel)),
"format": os.Getenv(common.EnvLogFormat),
}
}
dexCfg["grpc"] = map[string]any{
"addr": "0.0.0.0:5557",
}
dexCfg["telemetry"] = map[string]any{
"http": "0.0.0.0:5558",
}
if oauth2Cfg, found := dexCfg["oauth2"].(map[string]any); found {
if _, found := oauth2Cfg["skipApprovalScreen"].(bool); !found {
oauth2Cfg["skipApprovalScreen"] = true
}
} else {
dexCfg["oauth2"] = map[string]any{
"skipApprovalScreen": true,
}
}
additionalRedirectURLs, err := cdSettings.RedirectAdditionalURLs()
if err != nil {
return nil, fmt.Errorf("failed to infer additional redirect urls from config: %w", err)
}
argoCDStaticClient := map[string]any{
"id": common.ArgoCDClientAppID,
"name": common.ArgoCDClientAppName,
"secret": cdSettings.DexOAuth2ClientSecret(),
"redirectURIs": append([]string{redirectURL}, additionalRedirectURLs...),
}
argoCDPKCEStaticClient := map[string]any{
"id": "argo-cd-pkce",
"name": "Hanzo CD PKCE",
"redirectURIs": []string{
"http://localhost:4000/auth/callback",
},
"public": true,
}
argoCDCLIStaticClient := map[string]any{
"id": common.ArgoCDCLIClientAppID,
"name": common.ArgoCDCLIClientAppName,
"public": true,
"redirectURIs": []string{
"http://localhost",
"http://localhost:8085/auth/callback",
},
}
staticClients, ok := dexCfg["staticClients"].([]any)
if ok {
dexCfg["staticClients"] = append([]any{argoCDStaticClient, argoCDCLIStaticClient, argoCDPKCEStaticClient}, staticClients...)
} else {
dexCfg["staticClients"] = []any{argoCDStaticClient, argoCDCLIStaticClient, argoCDPKCEStaticClient}
}
dexRedirectURL, err := cdSettings.DexRedirectURL()
if err != nil {
return nil, err
}
connectors, ok := dexCfg["connectors"].([]any)
if !ok {
return nil, errors.New("malformed Dex configuration found")
}
for i, connectorIf := range connectors {
connector, ok := connectorIf.(map[string]any)
if !ok {
return nil, errors.New("malformed Dex configuration found")
}
connectorType := connector["type"].(string)
if !needsRedirectURI(connectorType) {
continue
}
connectorCfg, ok := connector["config"].(map[string]any)
if !ok {
return nil, errors.New("malformed Dex configuration found")
}
connectorCfg["redirectURI"] = dexRedirectURL
connector["config"] = connectorCfg
connectors[i] = connector
}
dexCfg["connectors"] = connectors
dexCfg = settings.ReplaceMapSecrets(dexCfg, cdSettings.Secrets)
if escapedConnectors, ok := dexCfg["connectors"].([]any); ok {
for i, connectorIf := range escapedConnectors {
connector, ok := connectorIf.(map[string]any)
if !ok {
continue
}
if connectorCfg, ok := connector["config"].(map[string]any); ok {
connector["config"] = settings.EscapeDollarSignsInConnectorConfig(connectorCfg, cdSettings.Secrets)
escapedConnectors[i] = connector
}
}
dexCfg["connectors"] = escapedConnectors
}
return yaml.Marshal(dexCfg)
}
// needsRedirectURI returns whether or not the given connector type needs a redirectURI
// Update this list as necessary, as new connectors are added
// https://dexidp.io/docs/connectors/
func needsRedirectURI(connectorType string) bool {
switch connectorType {
case "oidc", "saml", "microsoft", "linkedin", "gitlab", "github", "bitbucket-cloud", "openshift", "gitea", "google", "oauth":
return true
}
return false
}
func slogLevelFromLogrus(level string) string {
logrusLevel, err := log.ParseLevel(level)
if err != nil {
return level
}
switch logrusLevel {
case log.DebugLevel, log.TraceLevel:
return "DEBUG"
case log.InfoLevel:
return "INFO"
case log.WarnLevel:
return "WARN"
case log.ErrorLevel, log.FatalLevel, log.PanicLevel:
return "ERROR"
}
// return the logrus level and let slog parse it
return level
}