Remove application credential substitution
This commit is contained in:
@@ -25,13 +25,11 @@ type CallHooks struct {
|
||||
}
|
||||
|
||||
type RoleClient struct {
|
||||
runtime *Runtime
|
||||
role string
|
||||
profileID string
|
||||
settings config.LLM
|
||||
hooks CallHooks
|
||||
credentialMu sync.Mutex
|
||||
credentialNames map[string]struct{}
|
||||
runtime *Runtime
|
||||
role string
|
||||
profileID string
|
||||
settings config.LLM
|
||||
hooks CallHooks
|
||||
}
|
||||
|
||||
var _ structuredoutput.Client = (*RoleClient)(nil)
|
||||
@@ -152,14 +150,6 @@ func (c *RoleClient) prepareCall(parent context.Context, req structuredoutput.Re
|
||||
var details pk.PreparedRun
|
||||
capturePrepared := func(_ context.Context, prepared pk.PreparedRun) {
|
||||
details = prepared
|
||||
if name := details.EffectiveModelParams.APIKeyEnv; name != "" {
|
||||
c.credentialMu.Lock()
|
||||
if c.credentialNames == nil {
|
||||
c.credentialNames = make(map[string]struct{})
|
||||
}
|
||||
c.credentialNames[name] = struct{}{}
|
||||
c.credentialMu.Unlock()
|
||||
}
|
||||
response.Prompt = promptMetadata(details)
|
||||
record.Prompt = response.Prompt
|
||||
response.Model = details.EffectiveModelParams.Model
|
||||
@@ -311,17 +301,6 @@ func preparedSnapshot(details pk.PreparedRun) *structuredoutput.PreparedSnapshot
|
||||
return &structuredoutput.PreparedSnapshot{Messages: messages, Prompt: promptMetadata(details)}
|
||||
}
|
||||
|
||||
// ConfiguredSecrets supplies artifact redaction with the engine's referenced credentials.
|
||||
func (c *RoleClient) ConfiguredSecrets(ctx context.Context) []string {
|
||||
c.credentialMu.Lock()
|
||||
names := make([]string, 0, len(c.credentialNames))
|
||||
for name := range c.credentialNames {
|
||||
names = append(names, name)
|
||||
}
|
||||
c.credentialMu.Unlock()
|
||||
return c.runtime.ConfiguredSecrets(ctx, names...)
|
||||
}
|
||||
|
||||
func convertMessages(messages []pk.RenderedMessage) []structuredoutput.Message {
|
||||
result := make([]structuredoutput.Message, len(messages))
|
||||
for i, message := range messages {
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/redaction"
|
||||
)
|
||||
|
||||
var stageSanitizer = regexp.MustCompile(`[^a-zA-Z0-9_.-]+`)
|
||||
@@ -23,13 +22,12 @@ type InteractionArtifacts struct {
|
||||
|
||||
// DiagnosticsWriter writes generic machine-readable LLM interaction artifacts.
|
||||
type DiagnosticsWriter struct {
|
||||
dir string
|
||||
redactor redaction.Redactor
|
||||
dir string
|
||||
}
|
||||
|
||||
// NewDiagnosticsWriter creates a diagnostics writer rooted at dir.
|
||||
func NewDiagnosticsWriter(dir string, secrets []string) *DiagnosticsWriter {
|
||||
return &DiagnosticsWriter{dir: dir, redactor: redaction.New(secrets)}
|
||||
func NewDiagnosticsWriter(dir string) *DiagnosticsWriter {
|
||||
return &DiagnosticsWriter{dir: dir}
|
||||
}
|
||||
|
||||
// WriteInteraction writes request/response/error artifacts for a generic LLM
|
||||
@@ -82,12 +80,12 @@ func sanitizeStageName(stage string) string {
|
||||
}
|
||||
|
||||
func (w *DiagnosticsWriter) writeJSON(path string, payload any) error {
|
||||
redacted, err := w.redactor.JSON(payload)
|
||||
encoded, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal json artifact %q: %w", filepath.Base(path), err)
|
||||
}
|
||||
redacted = append(redacted, '\n')
|
||||
if err := os.WriteFile(path, redacted, 0o644); err != nil {
|
||||
encoded = append(encoded, '\n')
|
||||
if err := os.WriteFile(path, encoded, 0o644); err != nil {
|
||||
return fmt.Errorf("write json artifact %q: %w", filepath.Base(path), err)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -4,13 +4,12 @@ import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDiagnosticsWriterProducesValidJSONArtifacts(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writer := NewDiagnosticsWriter(dir, nil)
|
||||
writer := NewDiagnosticsWriter(dir)
|
||||
|
||||
artifacts, err := writer.WriteInteraction(
|
||||
"validation:batch-1",
|
||||
@@ -31,45 +30,6 @@ func TestDiagnosticsWriterProducesValidJSONArtifacts(t *testing.T) {
|
||||
assertValidJSONFile(t, artifacts.ResponsePayloadPath)
|
||||
}
|
||||
|
||||
func TestDiagnosticsWriterRedactsSecrets(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
secret := "top-secret-token"
|
||||
writer := NewDiagnosticsWriter(dir, []string{secret})
|
||||
|
||||
artifacts, err := writer.WriteInteraction(
|
||||
"module glossary / request",
|
||||
map[string]any{"authorization": "Bearer " + secret},
|
||||
map[string]any{"api_key": secret, "nested": map[string]any{"value": secret}},
|
||||
map[string]any{"echo": secret},
|
||||
map[string]any{"error": "failed with " + secret},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("WriteInteraction: %v", err)
|
||||
}
|
||||
|
||||
for _, path := range []string{
|
||||
artifacts.RequestMetadataPath,
|
||||
artifacts.RequestPayloadPath,
|
||||
artifacts.ResponsePayloadPath,
|
||||
artifacts.ErrorPayloadPath,
|
||||
} {
|
||||
raw, readErr := os.ReadFile(path)
|
||||
if readErr != nil {
|
||||
t.Fatalf("read %s: %v", path, readErr)
|
||||
}
|
||||
content := string(raw)
|
||||
if strings.Contains(content, secret) {
|
||||
t.Fatalf("secret leaked in %s: %s", filepath.Base(path), content)
|
||||
}
|
||||
if !strings.Contains(content, "[REDACTED]") {
|
||||
t.Fatalf("redaction marker missing in %s: %s", filepath.Base(path), content)
|
||||
}
|
||||
if !json.Valid(raw) {
|
||||
t.Fatalf("invalid json in %s", filepath.Base(path))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func assertValidJSONFile(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
raw, err := os.ReadFile(path)
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -26,7 +25,7 @@ type transportFunc func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f transportFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
|
||||
|
||||
func TestRoleClientUsesNativeTargetAndRedactsCredential(t *testing.T) {
|
||||
func TestRoleClientUsesSelectedNativeCredential(t *testing.T) {
|
||||
t.Setenv("AUDITA_TEST_KEY", " synthetic-secret-value\t ")
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "config.yml"), []byte("version: 3\n"), 0644); err != nil {
|
||||
@@ -41,7 +40,7 @@ func TestRoleClientUsesNativeTargetAndRedactsCredential(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
calls := 0
|
||||
providerErr := errors.New("provider echoed synthetic-secret-value")
|
||||
providerErr := errors.New("transport stopped")
|
||||
httpClient := &http.Client{Transport: transportFunc(func(r *http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
if r.Header.Get("Authorization") != "Bearer synthetic-secret-value" {
|
||||
@@ -62,10 +61,6 @@ func TestRoleClientUsesNativeTargetAndRedactsCredential(t *testing.T) {
|
||||
if err == nil || calls != 1 {
|
||||
t.Fatalf("error = %v, calls = %d", err, calls)
|
||||
}
|
||||
masked := runtime.RedactError(context.Background(), providerErr)
|
||||
if strings.Contains(masked, "synthetic-secret-value") {
|
||||
t.Fatal("credential not redacted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoleClientExecutionBudgetsAndFailureClasses(t *testing.T) {
|
||||
@@ -282,10 +277,4 @@ func TestNativeCredentialSelectionAndOptionalAbsence(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
secrets := runtime.ConfiguredSecrets(context.Background())
|
||||
for _, value := range []string{"backend-secret-value", "profile-secret-value"} {
|
||||
if !slices.Contains(secrets, value) {
|
||||
t.Fatalf("missing credential masking value")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/config"
|
||||
@@ -20,12 +19,10 @@ import (
|
||||
|
||||
// Runtime owns the captured engine shared by all processing clients.
|
||||
type Runtime struct {
|
||||
Config config.Config
|
||||
Engine *pk.Engine
|
||||
SourcePaths []string
|
||||
nextCallID atomic.Uint64
|
||||
credentialSourcesOnce sync.Once
|
||||
credentialEnvNames map[string]struct{}
|
||||
Config config.Config
|
||||
Engine *pk.Engine
|
||||
SourcePaths []string
|
||||
nextCallID atomic.Uint64
|
||||
}
|
||||
|
||||
type AssetError struct{ Err error }
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"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
|
||||
}
|
||||
r.credentialSourcesOnce.Do(func() {
|
||||
r.credentialEnvNames = r.discoverCredentialEnvNames(context.WithoutCancel(ctx))
|
||||
})
|
||||
secrets := make([]string, 0, len(r.credentialEnvNames)+len(additionalEnvNames))
|
||||
read := func(name string) {
|
||||
if value := os.Getenv(name); value != "" {
|
||||
secrets = append(secrets, value)
|
||||
// PromptKit trims the value before sending it to the provider.
|
||||
if trimmed := strings.TrimSpace(value); trimmed != "" && trimmed != value {
|
||||
secrets = append(secrets, trimmed)
|
||||
}
|
||||
}
|
||||
}
|
||||
for name := range r.credentialEnvNames {
|
||||
read(name)
|
||||
}
|
||||
if len(additionalEnvNames) > 0 {
|
||||
seen := make(map[string]struct{}, len(additionalEnvNames))
|
||||
for _, name := range additionalEnvNames {
|
||||
if _, known := r.credentialEnvNames[name]; known {
|
||||
continue
|
||||
}
|
||||
if _, duplicate := seen[name]; name == "" || duplicate {
|
||||
continue
|
||||
}
|
||||
seen[name] = struct{}{}
|
||||
read(name)
|
||||
}
|
||||
}
|
||||
return secrets
|
||||
}
|
||||
|
||||
func (r *Runtime) discoverCredentialEnvNames(ctx context.Context) map[string]struct{} {
|
||||
names := make(map[string]struct{})
|
||||
add := func(name *string) {
|
||||
if name != nil && *name != "" {
|
||||
names[*name] = struct{}{}
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/config"
|
||||
ck "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func TestConfiguredSecretsIncludesUnselectedBackends(t *testing.T) {
|
||||
var empty *Runtime
|
||||
if empty.ConfiguredSecrets(context.Background()) != nil {
|
||||
t.Fatal("nil runtime has secrets")
|
||||
}
|
||||
t.Setenv("SELECTED_KEY", "selected-secret")
|
||||
t.Setenv("OTHER_KEY", "unselected-secret")
|
||||
t.Setenv("UNRELATED_KEY", "unrelated-secret")
|
||||
dir := t.TempDir()
|
||||
for name, data := range map[string]string{
|
||||
"config.yml": "version: 3\n",
|
||||
"prompts.yml": "backends:\n - id: selected\n endpoint: https://selected.invalid/v1\n api_key_env: SELECTED_KEY\n - id: other\n endpoint: https://other.invalid/v1\n api_key_env: OTHER_KEY\nprofiles:\n - id: base\n backend: selected\n model: example\n",
|
||||
} {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), []byte(data), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
loaded, err := config.Load(ck.Input{Source: ck.File(filepath.Join(dir, "config.yml"))}, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runtime, err := NewRuntime(context.Background(), loaded, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, value := range runtime.ConfiguredSecrets(context.Background()) {
|
||||
seen[value] = true
|
||||
}
|
||||
if len(seen) != 2 || !seen["selected-secret"] || !seen["unselected-secret"] {
|
||||
t.Fatalf("unexpected resolved backend secrets: %v", seen)
|
||||
}
|
||||
t.Setenv("SELECTED_KEY", "rotated-secret")
|
||||
values := runtime.ConfiguredSecrets(context.Background(), "SELECTED_KEY", "UNRELATED_KEY", "UNRELATED_KEY")
|
||||
slices.Sort(values)
|
||||
if !slices.Equal(values, []string{"rotated-secret", "unrelated-secret", "unselected-secret"}) {
|
||||
t.Fatalf("values did not reflect the new credential and prepared source: %v", values)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user