-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcredentials.go
More file actions
176 lines (168 loc) · 8.12 KB
/
Copy pathcredentials.go
File metadata and controls
176 lines (168 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package engine
import (
"context"
"crypto/sha256"
"errors"
"fmt"
"os"
"strings"
"github.com/GrayCodeAI/eyrie/catalog/registry"
"github.com/GrayCodeAI/eyrie/config"
"github.com/GrayCodeAI/eyrie/credentials"
)
// SaveCredential validates, stores, and probes a provider credential. The
// secret is persisted before the probe so a transient network failure does not
// discard user input. The returned error states that persistence occurred.
func (e *Engine) SaveCredential(ctx context.Context, providerID, secret string) (CredentialStatus, error) {
ctx = nonNilContext(ctx)
providerID = strings.TrimSpace(providerID)
if providerID == "" {
return CredentialStatus{}, invalid("save_credential", "eyrie engine: provider id is required")
}
if gateway, ok := e.customGateway(providerID); ok {
return e.saveCustomGatewayCredential(ctx, gateway, secret)
}
providerCfg, err := e.loadProviderConfigStrict()
if err != nil {
return CredentialStatus{}, &Error{Code: ErrorInternal, Operation: "save_credential", Provider: providerID, Message: err.Error(), Cause: err}
}
inference, err := config.InferenceForProvider(providerID)
if err != nil {
return CredentialStatus{}, &Error{Code: ErrorInvalidRequest, Operation: "save_credential", Provider: providerID, Message: err.Error(), Cause: err}
}
prepared, err := config.PrepareCredentialForSave(inference, secret)
if err != nil {
return CredentialStatus{}, &Error{Code: ErrorInvalidRequest, Operation: "save_credential", Provider: providerID, Message: err.Error(), Cause: err}
}
envKey := strings.TrimSpace(inference.EnvVar)
if envKey == "" {
return CredentialStatus{}, invalid("save_credential", "eyrie engine: provider has no credential target")
}
if err := e.secretStore.Set(ctx, credentials.AccountForEnv(envKey), prepared); err != nil {
return CredentialStatus{}, &Error{Code: ErrorInternal, Operation: "save_credential", Provider: providerID, Message: "eyrie engine: could not save credential", Cause: err}
}
status := CredentialStatus{ProviderID: providerID, EnvVar: envKey, Configured: true}
if spec, ok := registry.DefaultRegistry.Get(providerID); ok && !spec.RequiresKey {
if err := config.ProbeLocalCredential(ctx, envKey, prepared); err != nil {
return status, &Error{Code: ErrorProviderUnavailable, Operation: "probe_credential", Provider: providerID, Message: fmt.Sprintf("%v (value saved in keychain)", err), Cause: err}
}
status.Verified = true
return status, nil
}
if err := config.ProbeCredentialWithProviderConfig(ctx, envKey, prepared, providerCfg); err != nil {
return status, &Error{Code: ErrorAuthentication, Operation: "probe_credential", Provider: providerID, Message: fmt.Sprintf("%v (key saved in keychain)", err), Cause: err}
}
status.Verified = true
return status, nil
}
// RemoveCredential deletes a provider credential from the configured store.
func (e *Engine) RemoveCredential(ctx context.Context, providerID string) error {
ctx = nonNilContext(ctx)
if gateway, ok := e.customGateway(providerID); ok {
if gateway.CredentialEnv == "" {
return nil
}
if err := e.secretStore.Delete(ctx, credentials.AccountForEnv(gateway.CredentialEnv)); err != nil && !errors.Is(err, credentials.ErrNotFound) {
return &Error{Code: ErrorInternal, Operation: "remove_credential", Provider: gateway.ID, Message: "eyrie engine: could not remove credential", Cause: err}
}
return nil
}
if _, err := config.InferenceForProvider(strings.TrimSpace(providerID)); err != nil {
return &Error{Code: ErrorInvalidRequest, Operation: "remove_credential", Provider: providerID, Message: err.Error(), Cause: err}
}
for _, envKey := range e.CredentialEnvKeys(providerID) {
if err := e.secretStore.Delete(ctx, credentials.AccountForEnv(envKey)); err != nil && !errors.Is(err, credentials.ErrNotFound) {
return &Error{Code: ErrorInternal, Operation: "remove_credential", Provider: providerID, Message: "eyrie engine: could not remove credential", Cause: err}
}
}
return nil
}
// CredentialStatus reports whether a provider credential is configured.
func (e *Engine) CredentialStatus(ctx context.Context, providerID string) (CredentialStatus, error) {
ctx = nonNilContext(ctx)
providerID = strings.TrimSpace(providerID)
if gateway, ok := e.customGateway(providerID); ok {
if gateway.CredentialEnv == "" {
return CredentialStatus{ProviderID: gateway.ID, Configured: true}, nil
}
secret, err := e.credentialValue(ctx, gateway.CredentialEnv)
if err != nil {
return CredentialStatus{}, &Error{Code: ErrorInternal, Operation: "credential_status", Provider: gateway.ID, Message: "eyrie engine: could not read credential status", Cause: err}
}
configured := strings.TrimSpace(secret) != "" && !config.LooksLikePlaceholderSecret(secret)
return credentialStatusWithEnvironment(CredentialStatus{
ProviderID: gateway.ID, EnvVar: gateway.CredentialEnv,
Configured: configured, Masked: maskedCredentialIf(configured, secret),
}, secret, gateway.CredentialEnv), nil
}
inference, err := config.InferenceForProvider(providerID)
if err != nil {
return CredentialStatus{}, &Error{Code: ErrorInvalidRequest, Operation: "credential_status", Provider: providerID, Message: err.Error(), Cause: err}
}
for _, envKey := range e.CredentialEnvKeys(providerID) {
secret, err := e.credentialValue(ctx, envKey)
if err != nil {
return CredentialStatus{}, &Error{Code: ErrorInternal, Operation: "credential_status", Provider: providerID, Message: "eyrie engine: could not read credential status", Cause: err}
}
if strings.TrimSpace(secret) != "" && !config.LooksLikePlaceholderSecret(secret) {
return credentialStatusWithEnvironment(CredentialStatus{
ProviderID: providerID, EnvVar: inference.EnvVar,
Configured: true, Masked: maskedCredential(secret),
}, secret, e.CredentialEnvKeys(providerID)...), nil
}
}
return credentialStatusWithEnvironment(
CredentialStatus{ProviderID: providerID, EnvVar: inference.EnvVar},
"",
e.CredentialEnvKeys(providerID)...,
), nil
}
// credentialStatusWithEnvironment adds safe process-environment metadata to a
// credential status. Secret values are compared only as fixed-size digests and
// are never retained in or returned from the status DTO.
func credentialStatusWithEnvironment(status CredentialStatus, storedSecret string, envVars ...string) CredentialStatus {
for _, envVar := range envVars {
envVar = strings.TrimSpace(envVar)
environmentSecret, ok := os.LookupEnv(envVar)
environmentSecret = strings.TrimSpace(environmentSecret)
if !ok || environmentSecret == "" {
continue
}
status.EnvironmentVariable = envVar
storedSecret = strings.TrimSpace(storedSecret)
if storedSecret != "" {
status.EnvironmentConflict = sha256.Sum256([]byte(environmentSecret)) != sha256.Sum256([]byte(storedSecret))
}
break
}
return status
}
func (e *Engine) saveCustomGatewayCredential(ctx context.Context, gateway CustomGateway, secret string) (CredentialStatus, error) {
if gateway.CredentialEnv == "" {
return CredentialStatus{ProviderID: gateway.ID, Configured: true, Verified: true}, nil
}
secret = strings.TrimSpace(secret)
if err := config.ValidateCredentialSecret(gateway.CredentialEnv, secret); err != nil {
return CredentialStatus{}, &Error{Code: ErrorInvalidRequest, Operation: "save_credential", Provider: gateway.ID, Message: err.Error(), Cause: err}
}
if err := e.secretStore.Set(ctx, credentials.AccountForEnv(gateway.CredentialEnv), secret); err != nil {
return CredentialStatus{}, &Error{Code: ErrorInternal, Operation: "save_credential", Provider: gateway.ID, Message: "eyrie engine: could not save credential", Cause: err}
}
status := CredentialStatus{ProviderID: gateway.ID, EnvVar: gateway.CredentialEnv, Configured: true}
if err := e.probeCustomGateway(ctx, gateway, secret); err != nil {
code := ErrorAuthentication
var typed *Error
if errors.As(err, &typed) && typed.Code != "" {
code = typed.Code
}
return status, &Error{Code: code, Operation: "probe_credential", Provider: gateway.ID, Message: fmt.Sprintf("%v (key saved in keychain)", err), Cause: err}
}
status.Verified = true
return status, nil
}
func maskedCredentialIf(configured bool, secret string) string {
if !configured {
return ""
}
return maskedCredential(secret)
}