feat(ml): fuse two embedding models and protect the ML service with a token

This commit is contained in:
shira committed 2026-10-11 14:40:52 +03:00
1 parent bc00d6901f
commit b24e5ff906
63 files changed
+2287 -276

No files matched your search

+57 -5
View File
@@ -2,6 +2,8 @@ package config
import (
"errors"
"fmt"
"log/slog"
"net/url"
"strings"
"time"
@@ -25,6 +27,10 @@ const (
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
@@ -32,14 +38,20 @@ const (
)
// 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
MLRequestTimeout time.Duration
MLCircuitFailures int
MLCircuitOpen time.Duration
// 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
@@ -50,12 +62,28 @@ type SemanticSettings struct {
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", "")),
GitHubToken: strings.TrimSpace(valueOrDefault(lookup, "GITHUB_TOKEN", "")),
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 {
@@ -96,6 +124,12 @@ func (settings SemanticSettings) validateMLService() error {
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 == "" ||
@@ -106,6 +140,24 @@ func (settings SemanticSettings) validateMLService() error {
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 {
+15 -4
View File
@@ -1,6 +1,7 @@
package config
import (
"fmt"
"strings"
"testing"
"time"
@@ -19,14 +20,24 @@ func TestSemanticSettingsDefaultOffAndValidateTheMLOrigin(t *testing.T) {
settings.MLRequestTimeout != 30*time.Second || settings.RepositoryCheckInterval != 10*time.Minute {
t.Fatalf("defaults = %+v, %v", settings, err)
}
const mlToken = "ml-service-token-0123456789abcdefghij"
enabled, err := LoadSemanticSettings(lookupOf(map[string]string{
"SEMANTIC_MATCHING_ENABLED": "true", "ML_SERVICE_URL": "http://ml:8000", "GITHUB_TOKEN": "token-value",
"SEMANTIC_MATCHING_ENABLED": "true", "ML_SERVICE_URL": "http://192.168.10.25:8790", "ML_SERVICE_TOKEN": mlToken,
"GITHUB_TOKEN": "token-value",
}))
if err != nil || !enabled.SemanticMatchingEnabled || enabled.GitHubToken != "token-value" {
t.Fatalf("enabled = %+v, %v", enabled, err)
if err != nil || !enabled.SemanticMatchingEnabled || enabled.GitHubToken != "token-value" || enabled.MLServiceToken != mlToken {
t.Fatalf("enabled = %v, %v", enabled.SemanticMatchingEnabled, err)
}
for _, rendered := range []string{fmt.Sprintf("%v %+v %#v %s", enabled, enabled, enabled, enabled), enabled.LogValue().String()} {
if strings.Contains(rendered, mlToken) || strings.Contains(rendered, "token-value") {
t.Fatalf("formatted settings quote a token: %s", rendered)
}
}
for name, values := range map[string]map[string]string{
"missing URL": {"SEMANTIC_MATCHING_ENABLED": "true"},
"missing URL": {"SEMANTIC_MATCHING_ENABLED": "true", "ML_SERVICE_TOKEN": "ml-service-token-0123456789abcdefghij"},
"missing token": {"SEMANTIC_MATCHING_ENABLED": "true", "ML_SERVICE_URL": "http://ml:8000"},
"short token": {"ML_SERVICE_TOKEN": "secret-but-short"},
"spaced token": {"ML_SERVICE_TOKEN": "secret 0123456789abcdefghijklmnopqrstuvwxyz"},
"credentials": {"ML_SERVICE_URL": "http://user:secret@ml:8000"},
"path": {"ML_SERVICE_URL": "http://ml:8000/v1"},
"scheme": {"ML_SERVICE_URL": "file:///tmp/ml"},