feat(ml): fuse two embedding models and protect the ML service with a token
This commit is contained in:
1 parent
bc00d6901f
commit
b24e5ff906
63 files changed
+2287
-276
No files matched your search
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user