Files
zeekay 49b38f1da6 util: drop remaining Argo wording, fix stale test fixtures
Reworded comments and renamed local identifiers across db, dex, git,
oidc, session, settings, and cli to our own voice. Fixed real stale
test fixtures found along the way: util/db's TestRepoURLToSecretName
had hand-typed hash values that no longer matched the URLs they were
paired with; util/git's TestEnsurePrefix had a corrupted
already-prefixed-passes-through case; util/oidc's discovery-doc
testdata used placeholder hostnames. Left the OAuth static-client
constants (common.ClientAppID and its util/dex/config.go sibling
"argo-cd-pkce"), the generated ArgoprojV1alpha1 accessor, and every
kept upstream issue citation untouched.
2026-09-05 01:22:37 -07:00

185 lines
5.0 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.Settings, 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)
}
cdStaticClient := map[string]any{
"id": common.ClientAppID,
"name": common.ClientAppName,
"secret": cdSettings.DexOAuth2ClientSecret(),
"redirectURIs": append([]string{redirectURL}, additionalRedirectURLs...),
}
cdPKCEStaticClient := map[string]any{
"id": "argo-cd-pkce",
"name": "Hanzo CD PKCE",
"redirectURIs": []string{
"http://localhost:4000/auth/callback",
},
"public": true,
}
cdCLIStaticClient := map[string]any{
"id": common.CLIClientAppID,
"name": common.CLIClientAppName,
"public": true,
"redirectURIs": []string{
"http://localhost",
"http://localhost:8085/auth/callback",
},
}
staticClients, ok := dexCfg["staticClients"].([]any)
if ok {
dexCfg["staticClients"] = append([]any{cdStaticClient, cdCLIStaticClient, cdPKCEStaticClient}, staticClients...)
} else {
dexCfg["staticClients"] = []any{cdStaticClient, cdCLIStaticClient, cdPKCEStaticClient}
}
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
}