Use native request execution and credential masking
This commit is contained in:
@@ -1,18 +1,54 @@
|
||||
package llm
|
||||
|
||||
import "gitea.maximumdirect.net/eric/audita/v3/internal/core/config"
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
// ConfiguredSecrets returns all configured LLM API-key values that should be
|
||||
// redacted from diagnostics and surfaced error payloads.
|
||||
func ConfiguredSecrets(cfg *config.Config) []string {
|
||||
if cfg == nil {
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/redaction"
|
||||
)
|
||||
|
||||
// ConfiguredSecrets obtains only values referenced by captured credential metadata.
|
||||
// It never selects a credential or treats an absent value as an error.
|
||||
func (r *Runtime) ConfiguredSecrets(ctx context.Context, additionalEnvNames ...string) []string {
|
||||
if r == nil || r.Engine == nil {
|
||||
return nil
|
||||
}
|
||||
secrets := make([]string, 0, len(cfg.Backends))
|
||||
for _, backend := range cfg.Backends {
|
||||
if backend.Credential != nil && backend.Credential.Value() != "" {
|
||||
secrets = append(secrets, backend.Credential.Value())
|
||||
ctx = context.WithoutCancel(ctx)
|
||||
names := make(map[string]struct{})
|
||||
add := func(name *string) {
|
||||
if name != nil && *name != "" {
|
||||
names[*name] = struct{}{}
|
||||
}
|
||||
}
|
||||
for _, name := range additionalEnvNames {
|
||||
add(&name)
|
||||
}
|
||||
if ids, err := r.Engine.ListProfiles(ctx); err == nil {
|
||||
for _, id := range ids {
|
||||
if profile, err := r.Engine.InspectProfile(ctx, id); err == nil {
|
||||
add(profile.APIKeyEnv)
|
||||
}
|
||||
}
|
||||
}
|
||||
if ids, err := r.Engine.ListBackends(ctx); err == nil {
|
||||
for _, id := range ids {
|
||||
if backend, err := r.Engine.InspectBackend(ctx, id); err == nil {
|
||||
add(backend.APIKeyEnv)
|
||||
}
|
||||
}
|
||||
}
|
||||
secrets := make([]string, 0, len(names))
|
||||
for name := range names {
|
||||
if value := os.Getenv(name); value != "" {
|
||||
secrets = append(secrets, value)
|
||||
}
|
||||
}
|
||||
return secrets
|
||||
}
|
||||
|
||||
func (r *Runtime) RedactError(ctx context.Context, err error, additionalEnvNames ...string) string {
|
||||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
return redaction.New(r.ConfiguredSecrets(ctx, additionalEnvNames...)).String(err.Error())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user