179 lines
7.5 KiB
Go
179 lines
7.5 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
// Semantic matching is off by default: a stand without the ML service
|
|
// ranks with the neutral Sem, as before.
|
|
defaultSemanticMatchingEnabled = false
|
|
// defaultMLRequestTimeout bounds one embedding request. A whole card is at
|
|
// most a dozen texts; on four CPU threads each takes well under a second.
|
|
defaultMLRequestTimeout = 30 * time.Second
|
|
maxMLRequestTimeout = 5 * time.Minute
|
|
// After this many consecutive failures the client stops calling the ML
|
|
// service for the open duration, so a dead service costs no timeouts.
|
|
defaultMLCircuitFailures = 3
|
|
maxMLCircuitFailures = 100
|
|
defaultMLCircuitOpen = 30 * time.Second
|
|
maxMLCircuitOpen = time.Hour
|
|
// Repository checks call api.github.com; they are off unless enabled.
|
|
defaultGitHubChecksEnabled = false
|
|
defaultGitHubTimeout = 10 * time.Second
|
|
maxGitHubTimeout = time.Minute
|
|
// minMLServiceTokenLength matches the ML service: 32 printable characters
|
|
// of a CSPRNG token are far beyond an online guess.
|
|
minMLServiceTokenLength = 32
|
|
maxMLServiceTokenLength = 512
|
|
// defaultRepositoryCheckInterval is the shortest time between two check
|
|
// requests of one candidate through the refresh operation.
|
|
defaultRepositoryCheckInterval = 10 * time.Minute
|
|
maxRepositoryCheckInterval = 24 * time.Hour
|
|
)
|
|
|
|
// SemanticSettings configure semantic matching and repository checks.
|
|
//
|
|
// String, GoString, Format, and LogValue redact everything, because the ML
|
|
// service token and the GitHub token are secrets of the deployment.
|
|
type SemanticSettings struct {
|
|
// SemanticMatchingEnabled schedules vector jobs on changes and runs them
|
|
// against MLServiceURL. The search reads stored vectors either way.
|
|
SemanticMatchingEnabled bool
|
|
MLServiceURL string
|
|
// MLServiceToken is the shared bearer token of the ML service. It is
|
|
// required while semantic matching is enabled and is a secret: never log it.
|
|
MLServiceToken string
|
|
MLRequestTimeout time.Duration
|
|
MLCircuitFailures int
|
|
MLCircuitOpen time.Duration
|
|
// GitHubChecksEnabled schedules and runs repository checks against the
|
|
// public GitHub API.
|
|
GitHubChecksEnabled bool
|
|
// GitHubToken is an optional read-only token that raises the GitHub rate
|
|
// limit. It is a secret: never log it.
|
|
GitHubToken string
|
|
GitHubTimeout time.Duration
|
|
RepositoryCheckInterval time.Duration
|
|
}
|
|
|
|
const redactedSemanticSettings = "[semantic settings redacted]"
|
|
|
|
func (settings SemanticSettings) String() string { return redactedSemanticSettings }
|
|
|
|
func (settings SemanticSettings) GoString() string { return redactedSemanticSettings }
|
|
|
|
// Format redacts every verb; without it %+v would print the tokens.
|
|
func (settings SemanticSettings) Format(state fmt.State, _ rune) {
|
|
_, _ = state.Write([]byte(redactedSemanticSettings))
|
|
}
|
|
|
|
func (settings SemanticSettings) LogValue() slog.Value {
|
|
return slog.StringValue(redactedSemanticSettings)
|
|
}
|
|
|
|
// LoadSemanticSettings reads the semantic settings; the operator commands
|
|
// that need only them use it directly.
|
|
func LoadSemanticSettings(lookup LookupEnv) (SemanticSettings, error) {
|
|
settings := SemanticSettings{
|
|
MLServiceURL: strings.TrimSpace(valueOrDefault(lookup, "ML_SERVICE_URL", "")),
|
|
MLServiceToken: strings.TrimSpace(valueOrDefault(lookup, "ML_SERVICE_TOKEN", "")),
|
|
GitHubToken: strings.TrimSpace(valueOrDefault(lookup, "GITHUB_TOKEN", "")),
|
|
}
|
|
var err error
|
|
if settings.SemanticMatchingEnabled, err = boolOrDefault(lookup, "SEMANTIC_MATCHING_ENABLED", defaultSemanticMatchingEnabled); err != nil {
|
|
return SemanticSettings{}, err
|
|
}
|
|
if settings.MLRequestTimeout, err = durationOrDefault(lookup, "ML_REQUEST_TIMEOUT", defaultMLRequestTimeout); err != nil {
|
|
return SemanticSettings{}, err
|
|
}
|
|
if settings.MLCircuitFailures, err = intOrDefault(lookup, "ML_CIRCUIT_FAILURES", defaultMLCircuitFailures); err != nil {
|
|
return SemanticSettings{}, err
|
|
}
|
|
if settings.MLCircuitOpen, err = durationOrDefault(lookup, "ML_CIRCUIT_OPEN", defaultMLCircuitOpen); err != nil {
|
|
return SemanticSettings{}, err
|
|
}
|
|
if settings.GitHubChecksEnabled, err = boolOrDefault(lookup, "GITHUB_REPOSITORY_CHECKS_ENABLED", defaultGitHubChecksEnabled); err != nil {
|
|
return SemanticSettings{}, err
|
|
}
|
|
if settings.GitHubTimeout, err = durationOrDefault(lookup, "GITHUB_TIMEOUT", defaultGitHubTimeout); err != nil {
|
|
return SemanticSettings{}, err
|
|
}
|
|
if settings.RepositoryCheckInterval, err = durationOrDefault(lookup, "REPOSITORY_CHECK_INTERVAL", defaultRepositoryCheckInterval); err != nil {
|
|
return SemanticSettings{}, err
|
|
}
|
|
return settings, settings.Validate()
|
|
}
|
|
|
|
// Validate checks the values even while the features are off, so enabling
|
|
// them later cannot surface an old mistake.
|
|
func (settings SemanticSettings) Validate() error {
|
|
if err := settings.validateMLService(); err != nil {
|
|
return err
|
|
}
|
|
return settings.validateLimits()
|
|
}
|
|
|
|
func (settings SemanticSettings) validateMLService() error {
|
|
const location = "SemanticSettings.Validate"
|
|
if settings.SemanticMatchingEnabled && settings.MLServiceURL == "" {
|
|
return errors.New(location + " - ML_SERVICE_URL is required when SEMANTIC_MATCHING_ENABLED is true")
|
|
}
|
|
if settings.SemanticMatchingEnabled && settings.MLServiceToken == "" {
|
|
return errors.New(location + " - ML_SERVICE_TOKEN is required when SEMANTIC_MATCHING_ENABLED is true")
|
|
}
|
|
if err := validateMLServiceToken(settings.MLServiceToken); err != nil {
|
|
return err
|
|
}
|
|
if settings.MLServiceURL != "" {
|
|
parsed, err := url.Parse(settings.MLServiceURL)
|
|
if err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") || parsed.Host == "" ||
|
|
parsed.User != nil || parsed.RawQuery != "" || parsed.Fragment != "" || strings.Trim(parsed.Path, "/") != "" {
|
|
return errors.New(location + " - ML_SERVICE_URL must be an http or https origin without credentials or path")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// validateMLServiceToken checks an optional token; the messages never quote
|
|
// the value.
|
|
func validateMLServiceToken(token string) error {
|
|
const location = "SemanticSettings.Validate"
|
|
if token == "" {
|
|
return nil
|
|
}
|
|
if len(token) < minMLServiceTokenLength || len(token) > maxMLServiceTokenLength {
|
|
return errors.New(location + " - ML_SERVICE_TOKEN must have between 32 and 512 characters")
|
|
}
|
|
for _, character := range token {
|
|
if character < 0x21 || character > 0x7e {
|
|
return errors.New(location + " - ML_SERVICE_TOKEN must be printable ASCII without white space")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (settings SemanticSettings) validateLimits() error {
|
|
const location = "SemanticSettings.Validate"
|
|
switch {
|
|
case settings.MLRequestTimeout <= 0 || settings.MLRequestTimeout > maxMLRequestTimeout:
|
|
return errors.New(location + " - ML_REQUEST_TIMEOUT must be positive and at most 5m")
|
|
case settings.MLCircuitFailures < 1 || settings.MLCircuitFailures > maxMLCircuitFailures:
|
|
return errors.New(location + " - ML_CIRCUIT_FAILURES must be between 1 and 100")
|
|
case settings.MLCircuitOpen <= 0 || settings.MLCircuitOpen > maxMLCircuitOpen:
|
|
return errors.New(location + " - ML_CIRCUIT_OPEN must be positive and at most 1h")
|
|
case settings.GitHubTimeout <= 0 || settings.GitHubTimeout > maxGitHubTimeout:
|
|
return errors.New(location + " - GITHUB_TIMEOUT must be positive and at most 1m")
|
|
case settings.RepositoryCheckInterval <= 0 || settings.RepositoryCheckInterval > maxRepositoryCheckInterval:
|
|
return errors.New(location + " - REPOSITORY_CHECK_INTERVAL must be positive and at most 24h")
|
|
case strings.ContainsAny(settings.GitHubToken, " \t\r\n"):
|
|
return errors.New(location + " - GITHUB_TOKEN must not contain white space")
|
|
}
|
|
return nil
|
|
}
|