|
| 1 | +// Copyright (c) 2026 Super Durable |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +// Package connectorconfiguration validates the platform-owned connector snapshot boundary. |
| 5 | +package connectorconfiguration |
| 6 | + |
| 7 | +import ( |
| 8 | + "crypto/sha256" |
| 9 | + "encoding/hex" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "net/url" |
| 13 | + "os" |
| 14 | + "path/filepath" |
| 15 | + "regexp" |
| 16 | + "strings" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + maximumConfigurationBytes = 32 << 20 |
| 21 | + maximumCredentialBytes = 16 << 10 |
| 22 | +) |
| 23 | + |
| 24 | +// Mode identifies the active connector configuration authority. |
| 25 | +type Mode string |
| 26 | + |
| 27 | +const ( |
| 28 | + // ModeLocal reads the developer-owned Dex connector configuration file. |
| 29 | + ModeLocal Mode = "local" |
| 30 | + // ModeHosted reads a deployment-pinned snapshot and resolves credentials through the broker. |
| 31 | + ModeHosted Mode = "hosted" |
| 32 | +) |
| 33 | + |
| 34 | +// Configuration describes safe connector inputs without loading credential material. |
| 35 | +type Configuration struct { |
| 36 | + Mode Mode |
| 37 | + ConfigurationFile string |
| 38 | + CredentialBrokerURL string |
| 39 | + WorkloadCredentialFile string |
| 40 | + PublicBaseURL string |
| 41 | +} |
| 42 | + |
| 43 | +// Load validates local or hosted connector configuration before the Worker starts. |
| 44 | +func Load(requiresConnections bool) (Configuration, error) { |
| 45 | + hostedFile := strings.TrimSpace(os.Getenv("SUPERVERSE_CONNECTOR_CONFIG_FILE")) |
| 46 | + localFile := strings.TrimSpace(os.Getenv("DEX_CONNECTOR_CONFIG_FILE")) |
| 47 | + if hostedFile != "" || hasHostedEnvironment() { |
| 48 | + if localFile != "" { |
| 49 | + return Configuration{}, fmt.Errorf("local and hosted connector configuration cannot be combined") |
| 50 | + } |
| 51 | + return loadHosted(hostedFile, requiresConnections) |
| 52 | + } |
| 53 | + if localFile == "" { |
| 54 | + if requiresConnections { |
| 55 | + return Configuration{}, fmt.Errorf("DEX_CONNECTOR_CONFIG_FILE is required") |
| 56 | + } |
| 57 | + return Configuration{Mode: ModeLocal}, nil |
| 58 | + } |
| 59 | + if _, err := readBoundedRegularFile(localFile, maximumConfigurationBytes, false); err != nil { |
| 60 | + return Configuration{}, fmt.Errorf("read local connector configuration: %w", err) |
| 61 | + } |
| 62 | + return Configuration{Mode: ModeLocal, ConfigurationFile: localFile}, nil |
| 63 | +} |
| 64 | + |
| 65 | +func loadHosted(configurationFile string, requiresConnections bool) (Configuration, error) { |
| 66 | + if configurationFile == "" || !filepath.IsAbs(configurationFile) { |
| 67 | + return Configuration{}, fmt.Errorf("SUPERVERSE_CONNECTOR_CONFIG_FILE must be an absolute path") |
| 68 | + } |
| 69 | + contents, err := readBoundedRegularFile(configurationFile, maximumConfigurationBytes, false) |
| 70 | + if err != nil { |
| 71 | + return Configuration{}, fmt.Errorf("read hosted connector configuration: %w", err) |
| 72 | + } |
| 73 | + expectedDigest := strings.TrimSpace(os.Getenv("SUPERVERSE_CONNECTOR_CONFIG_DIGEST")) |
| 74 | + if !regexp.MustCompile(`^[0-9a-f]{64}$`).MatchString(expectedDigest) { |
| 75 | + return Configuration{}, fmt.Errorf("SUPERVERSE_CONNECTOR_CONFIG_DIGEST must be lowercase hexadecimal") |
| 76 | + } |
| 77 | + actualDigest := sha256.Sum256(contents) |
| 78 | + if expectedDigest != hex.EncodeToString(actualDigest[:]) { |
| 79 | + return Configuration{}, fmt.Errorf("hosted connector configuration digest does not match") |
| 80 | + } |
| 81 | + publicBaseURL, err := validatedHTTPURL("PUBLIC_BASE_URL", true) |
| 82 | + if err != nil { |
| 83 | + return Configuration{}, err |
| 84 | + } |
| 85 | + brokerURL := strings.TrimSpace(os.Getenv("SUPERVERSE_CONNECTOR_BROKER_URL")) |
| 86 | + credentialFile := strings.TrimSpace(os.Getenv("SUPERVERSE_CONNECTOR_WORKLOAD_CREDENTIAL_FILE")) |
| 87 | + if brokerURL == "" && requiresConnections { |
| 88 | + return Configuration{}, fmt.Errorf("SUPERVERSE_CONNECTOR_BROKER_URL is required") |
| 89 | + } |
| 90 | + if brokerURL != "" { |
| 91 | + if _, err := validatedHTTPURL("SUPERVERSE_CONNECTOR_BROKER_URL", false); err != nil { |
| 92 | + return Configuration{}, err |
| 93 | + } |
| 94 | + } |
| 95 | + if credentialFile == "" && requiresConnections { |
| 96 | + return Configuration{}, fmt.Errorf("SUPERVERSE_CONNECTOR_WORKLOAD_CREDENTIAL_FILE is required") |
| 97 | + } |
| 98 | + if credentialFile != "" { |
| 99 | + if !filepath.IsAbs(credentialFile) { |
| 100 | + return Configuration{}, fmt.Errorf("SUPERVERSE_CONNECTOR_WORKLOAD_CREDENTIAL_FILE must be an absolute path") |
| 101 | + } |
| 102 | + if _, err := readBoundedRegularFile(credentialFile, maximumCredentialBytes, true); err != nil { |
| 103 | + return Configuration{}, fmt.Errorf("read workload credential: %w", err) |
| 104 | + } |
| 105 | + } |
| 106 | + return Configuration{ |
| 107 | + Mode: ModeHosted, |
| 108 | + ConfigurationFile: configurationFile, |
| 109 | + CredentialBrokerURL: brokerURL, |
| 110 | + WorkloadCredentialFile: credentialFile, |
| 111 | + PublicBaseURL: publicBaseURL, |
| 112 | + }, nil |
| 113 | +} |
| 114 | + |
| 115 | +func hasHostedEnvironment() bool { |
| 116 | + for _, name := range []string{ |
| 117 | + "SUPERVERSE_CONNECTOR_CONFIG_DIGEST", |
| 118 | + "SUPERVERSE_CONNECTOR_BROKER_URL", |
| 119 | + "SUPERVERSE_CONNECTOR_WORKLOAD_CREDENTIAL_FILE", |
| 120 | + } { |
| 121 | + if strings.TrimSpace(os.Getenv(name)) != "" { |
| 122 | + return true |
| 123 | + } |
| 124 | + } |
| 125 | + return false |
| 126 | +} |
| 127 | + |
| 128 | +func validatedHTTPURL(name string, allowHTTP bool) (string, error) { |
| 129 | + value := strings.TrimSpace(os.Getenv(name)) |
| 130 | + parsed, err := url.Parse(value) |
| 131 | + if err != nil || parsed.Host == "" || parsed.User != nil || parsed.RawQuery != "" || parsed.Fragment != "" { |
| 132 | + return "", fmt.Errorf("%s must be an absolute URL without credentials, query, or fragment", name) |
| 133 | + } |
| 134 | + if parsed.Scheme != "https" && (!allowHTTP || parsed.Scheme != "http") { |
| 135 | + return "", fmt.Errorf("%s must use HTTPS", name) |
| 136 | + } |
| 137 | + return strings.TrimRight(value, "/"), nil |
| 138 | +} |
| 139 | + |
| 140 | +func readBoundedRegularFile(path string, maximumBytes int64, requirePrivate bool) ([]byte, error) { |
| 141 | + file, err := os.Open(path) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + defer file.Close() |
| 146 | + info, err := file.Stat() |
| 147 | + if err != nil { |
| 148 | + return nil, err |
| 149 | + } |
| 150 | + if !info.Mode().IsRegular() || info.Size() < 1 || info.Size() > maximumBytes { |
| 151 | + return nil, fmt.Errorf("file must be regular and contain between 1 and %d bytes", maximumBytes) |
| 152 | + } |
| 153 | + if requirePrivate && info.Mode().Perm()&0o077 != 0 { |
| 154 | + return nil, fmt.Errorf("file permissions must not grant group or other access") |
| 155 | + } |
| 156 | + contents, err := io.ReadAll(io.LimitReader(file, maximumBytes+1)) |
| 157 | + if err != nil { |
| 158 | + return nil, err |
| 159 | + } |
| 160 | + if int64(len(contents)) != info.Size() { |
| 161 | + return nil, fmt.Errorf("file changed while it was being read") |
| 162 | + } |
| 163 | + return contents, nil |
| 164 | +} |
0 commit comments