|
| 1 | +package credentials |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "maps" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// ParseCredentialOverrides parses a string of credential overrides that the user provided as a command line arg. |
| 12 | +// The format of credential overrides can be one of two things: |
| 13 | +// cred1:ENV1,ENV2 (direct mapping of environment variables) |
| 14 | +// cred1:ENV1=VALUE1,ENV2=VALUE2 (key-value pairs) |
| 15 | +// |
| 16 | +// This function turns it into a map[string]map[string]string like this: |
| 17 | +// |
| 18 | +// { |
| 19 | +// "cred1": { |
| 20 | +// "ENV1": "VALUE1", |
| 21 | +// "ENV2": "VALUE2", |
| 22 | +// } |
| 23 | +// } |
| 24 | +func ParseCredentialOverrides(overrides []string) (map[string]map[string]string, error) { |
| 25 | + credentialOverrides := make(map[string]map[string]string) |
| 26 | + |
| 27 | + for _, o := range overrides { |
| 28 | + credName, envs, found := strings.Cut(o, ":") |
| 29 | + if !found { |
| 30 | + return nil, fmt.Errorf("invalid credential override: %s", o) |
| 31 | + } |
| 32 | + envMap, ok := credentialOverrides[credName] |
| 33 | + if !ok { |
| 34 | + envMap = make(map[string]string) |
| 35 | + } |
| 36 | + for _, env := range strings.Split(envs, ",") { |
| 37 | + for _, env := range strings.Split(env, "|") { |
| 38 | + key, value, found := strings.Cut(env, "=") |
| 39 | + if !found { |
| 40 | + // User just passed an env var name as the key, so look up the value. |
| 41 | + value = os.Getenv(key) |
| 42 | + } |
| 43 | + envMap[key] = value |
| 44 | + } |
| 45 | + } |
| 46 | + credentialOverrides[credName] = envMap |
| 47 | + } |
| 48 | + |
| 49 | + return credentialOverrides, nil |
| 50 | +} |
| 51 | + |
| 52 | +type withOverride struct { |
| 53 | + target CredentialStore |
| 54 | + credContext []string |
| 55 | + overrides map[string]map[string]map[string]string |
| 56 | +} |
| 57 | + |
| 58 | +func (w withOverride) Get(ctx context.Context, toolName string) (*Credential, bool, error) { |
| 59 | + for _, credCtx := range w.credContext { |
| 60 | + overrides, ok := w.overrides[credCtx] |
| 61 | + if !ok { |
| 62 | + continue |
| 63 | + } |
| 64 | + override, ok := overrides[toolName] |
| 65 | + if !ok { |
| 66 | + continue |
| 67 | + } |
| 68 | + |
| 69 | + return &Credential{ |
| 70 | + Context: credCtx, |
| 71 | + ToolName: toolName, |
| 72 | + Type: CredentialTypeTool, |
| 73 | + Env: maps.Clone(override), |
| 74 | + }, true, nil |
| 75 | + } |
| 76 | + |
| 77 | + return w.target.Get(ctx, toolName) |
| 78 | +} |
| 79 | + |
| 80 | +func (w withOverride) Add(ctx context.Context, cred Credential) error { |
| 81 | + for _, credCtx := range w.credContext { |
| 82 | + if override, ok := w.overrides[credCtx]; ok { |
| 83 | + if _, ok := override[cred.ToolName]; ok { |
| 84 | + return fmt.Errorf("cannot add credential with context %q and tool %q because it is statically configure", cred.Context, cred.ToolName) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return w.target.Add(ctx, cred) |
| 89 | +} |
| 90 | + |
| 91 | +func (w withOverride) Refresh(ctx context.Context, cred Credential) error { |
| 92 | + if override, ok := w.overrides[cred.Context]; ok { |
| 93 | + if _, ok := override[cred.ToolName]; ok { |
| 94 | + return nil |
| 95 | + } |
| 96 | + } |
| 97 | + return w.target.Refresh(ctx, cred) |
| 98 | +} |
| 99 | + |
| 100 | +func (w withOverride) Remove(ctx context.Context, toolName string) error { |
| 101 | + for _, credCtx := range w.credContext { |
| 102 | + if override, ok := w.overrides[credCtx]; ok { |
| 103 | + if _, ok := override[toolName]; ok { |
| 104 | + return fmt.Errorf("cannot remove credential with context %q and tool %q because it is statically configure", credCtx, toolName) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + return w.target.Remove(ctx, toolName) |
| 109 | +} |
| 110 | + |
| 111 | +func (w withOverride) List(ctx context.Context) ([]Credential, error) { |
| 112 | + creds, err := w.target.List(ctx) |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + |
| 117 | + added := make(map[string]map[string]bool) |
| 118 | + for i, cred := range creds { |
| 119 | + if override, ok := w.overrides[cred.Context]; ok { |
| 120 | + if _, ok := override[cred.ToolName]; ok { |
| 121 | + creds[i].Type = CredentialTypeTool |
| 122 | + creds[i].Env = maps.Clone(override[cred.ToolName]) |
| 123 | + } |
| 124 | + } |
| 125 | + tools, ok := added[cred.Context] |
| 126 | + if !ok { |
| 127 | + tools = make(map[string]bool) |
| 128 | + } |
| 129 | + tools[cred.ToolName] = true |
| 130 | + added[cred.Context] = tools |
| 131 | + } |
| 132 | + |
| 133 | + for _, credCtx := range w.credContext { |
| 134 | + tools := w.overrides[credCtx] |
| 135 | + for toolName := range tools { |
| 136 | + if _, ok := added[credCtx][toolName]; ok { |
| 137 | + continue |
| 138 | + } |
| 139 | + creds = append(creds, Credential{ |
| 140 | + Context: credCtx, |
| 141 | + ToolName: toolName, |
| 142 | + Type: CredentialTypeTool, |
| 143 | + Env: maps.Clone(tools[toolName]), |
| 144 | + }) |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return creds, nil |
| 149 | +} |
0 commit comments