|
| 1 | +package awsconfig |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "gopkg.in/ini.v1" |
| 12 | + |
| 13 | + "github.com/localstack/lstk/internal/endpoint" |
| 14 | + "github.com/localstack/lstk/internal/output" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + profileName = "localstack" |
| 19 | + configSectionName = "profile localstack" // ~/.aws/config uses "profile <name>" as section header |
| 20 | + credsSectionName = "localstack" // ~/.aws/credentials uses just the profile name |
| 21 | + // TODO: make region configurable (e.g. from container env or lstk config) |
| 22 | + defaultRegion = "us-east-1" |
| 23 | +) |
| 24 | + |
| 25 | +var credentialsKeys = map[string]string{ |
| 26 | + "aws_access_key_id": "test", |
| 27 | + "aws_secret_access_key": "test", |
| 28 | +} |
| 29 | + |
| 30 | +// isValidLocalStackEndpoint returns true if the URL points to a known LocalStack host with a port. |
| 31 | +func isValidLocalStackEndpoint(url string) bool { |
| 32 | + for _, prefix := range []string{ |
| 33 | + "http://" + endpoint.Hostname + ":", |
| 34 | + "https://" + endpoint.Hostname + ":", |
| 35 | + "http://127.0.0.1:", |
| 36 | + "https://127.0.0.1:", |
| 37 | + "http://localhost:", |
| 38 | + "https://localhost:", |
| 39 | + } { |
| 40 | + if strings.HasPrefix(url, prefix) { |
| 41 | + return true |
| 42 | + } |
| 43 | + } |
| 44 | + return false |
| 45 | +} |
| 46 | + |
| 47 | +func awsPaths() (configPath, credentialsPath string, err error) { |
| 48 | + home, err := os.UserHomeDir() |
| 49 | + if err != nil { |
| 50 | + return "", "", err |
| 51 | + } |
| 52 | + return filepath.Join(home, ".aws", "config"), filepath.Join(home, ".aws", "credentials"), nil |
| 53 | +} |
| 54 | + |
| 55 | +// profileStatus holds which AWS profile files need to be written or updated. |
| 56 | +type profileStatus struct { |
| 57 | + configNeeded bool |
| 58 | + credsNeeded bool |
| 59 | +} |
| 60 | + |
| 61 | +func (s profileStatus) anyNeeded() bool { |
| 62 | + return s.configNeeded || s.credsNeeded |
| 63 | +} |
| 64 | + |
| 65 | +func (s profileStatus) filesToModify() []string { |
| 66 | + var files []string |
| 67 | + if s.configNeeded { |
| 68 | + files = append(files, "~/.aws/config") |
| 69 | + } |
| 70 | + if s.credsNeeded { |
| 71 | + files = append(files, "~/.aws/credentials") |
| 72 | + } |
| 73 | + return files |
| 74 | +} |
| 75 | + |
| 76 | +// checkProfileStatus determines which AWS profile files need to be written or updated. |
| 77 | +func checkProfileStatus(configPath, credsPath string) (profileStatus, error) { |
| 78 | + configNeeded, err := configNeedsWrite(configPath) |
| 79 | + if err != nil { |
| 80 | + return profileStatus{}, err |
| 81 | + } |
| 82 | + credsNeeded, err := credsNeedWrite(credsPath) |
| 83 | + if err != nil { |
| 84 | + return profileStatus{}, err |
| 85 | + } |
| 86 | + return profileStatus{configNeeded: configNeeded, credsNeeded: credsNeeded}, nil |
| 87 | +} |
| 88 | + |
| 89 | +func configNeedsWrite(path string) (bool, error) { |
| 90 | + f, err := ini.Load(path) |
| 91 | + if errors.Is(err, os.ErrNotExist) { |
| 92 | + return true, nil |
| 93 | + } |
| 94 | + if err != nil { |
| 95 | + return false, err |
| 96 | + } |
| 97 | + section, err := f.GetSection(configSectionName) |
| 98 | + if err != nil { |
| 99 | + return true, nil // section doesn't exist |
| 100 | + } |
| 101 | + endpointKey, err := section.GetKey("endpoint_url") |
| 102 | + if err != nil || !isValidLocalStackEndpoint(endpointKey.Value()) { |
| 103 | + return true, nil |
| 104 | + } |
| 105 | + if !section.HasKey("region") { |
| 106 | + return true, nil |
| 107 | + } |
| 108 | + return false, nil |
| 109 | +} |
| 110 | + |
| 111 | +func credsNeedWrite(path string) (bool, error) { |
| 112 | + f, err := ini.Load(path) |
| 113 | + if errors.Is(err, os.ErrNotExist) { |
| 114 | + return true, nil |
| 115 | + } |
| 116 | + if err != nil { |
| 117 | + return false, err |
| 118 | + } |
| 119 | + section, err := f.GetSection(credsSectionName) |
| 120 | + if err != nil { |
| 121 | + return true, nil // section doesn't exist |
| 122 | + } |
| 123 | + for k, expected := range credentialsKeys { |
| 124 | + key, err := section.GetKey(k) |
| 125 | + if err != nil || key.Value() != expected { |
| 126 | + return true, nil |
| 127 | + } |
| 128 | + } |
| 129 | + return false, nil |
| 130 | +} |
| 131 | + |
| 132 | +// profileExists reports whether the localstack profile section is present in both |
| 133 | +// ~/.aws/config and ~/.aws/credentials. |
| 134 | +func profileExists() (bool, error) { |
| 135 | + configPath, credsPath, err := awsPaths() |
| 136 | + if err != nil { |
| 137 | + return false, err |
| 138 | + } |
| 139 | + configOK, err := sectionExists(configPath, configSectionName) |
| 140 | + if err != nil { |
| 141 | + return false, err |
| 142 | + } |
| 143 | + credsOK, err := sectionExists(credsPath, credsSectionName) |
| 144 | + if err != nil { |
| 145 | + return false, err |
| 146 | + } |
| 147 | + return configOK && credsOK, nil |
| 148 | +} |
| 149 | + |
| 150 | +// writeProfile writes the localstack profile to ~/.aws/config and ~/.aws/credentials, |
| 151 | +// creating or updating sections as needed. |
| 152 | +func writeProfile(host string) error { |
| 153 | + configPath, credsPath, err := awsPaths() |
| 154 | + if err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + configKeys := map[string]string{ |
| 158 | + "region": defaultRegion, |
| 159 | + "output": "json", |
| 160 | + "endpoint_url": "http://" + host, |
| 161 | + } |
| 162 | + if err := upsertSection(configPath, configSectionName, configKeys); err != nil { |
| 163 | + return fmt.Errorf("failed to write %s: %w", configPath, err) |
| 164 | + } |
| 165 | + if err := upsertSection(credsPath, credsSectionName, credentialsKeys); err != nil { |
| 166 | + return fmt.Errorf("failed to write %s: %w", credsPath, err) |
| 167 | + } |
| 168 | + return nil |
| 169 | +} |
| 170 | + |
| 171 | +func writeConfigProfile(configPath, host string) error { |
| 172 | + keys := map[string]string{ |
| 173 | + "region": defaultRegion, |
| 174 | + "output": "json", |
| 175 | + "endpoint_url": "http://" + host, |
| 176 | + } |
| 177 | + return upsertSection(configPath, configSectionName, keys) |
| 178 | +} |
| 179 | + |
| 180 | +func writeCredsProfile(credsPath string) error { |
| 181 | + return upsertSection(credsPath, credsSectionName, credentialsKeys) |
| 182 | +} |
| 183 | + |
| 184 | +// Setup checks for the localstack AWS profile and prompts to create or update it if needed. |
| 185 | +// In non-interactive mode, emits a note instead of prompting. |
| 186 | +func Setup(ctx context.Context, sink output.Sink, interactive bool, port string, localStackHost string) error { |
| 187 | + configPath, credsPath, err := awsPaths() |
| 188 | + if err != nil { |
| 189 | + output.EmitWarning(sink, fmt.Sprintf("could not determine AWS config paths: %v", err)) |
| 190 | + return nil |
| 191 | + } |
| 192 | + |
| 193 | + var dnsOK bool |
| 194 | + localStackHost, dnsOK = endpoint.ResolveHost(port, localStackHost) |
| 195 | + if !dnsOK { |
| 196 | + output.EmitNote(sink, `Could not resolve "localhost.localstack.cloud" — your system may have DNS rebind protection enabled. Using 127.0.0.1 as the endpoint.`) |
| 197 | + } |
| 198 | + |
| 199 | + status, err := checkProfileStatus(configPath, credsPath) |
| 200 | + if err != nil { |
| 201 | + output.EmitWarning(sink, fmt.Sprintf("could not check AWS profile: %v", err)) |
| 202 | + return nil |
| 203 | + } |
| 204 | + if !status.anyNeeded() { |
| 205 | + return nil |
| 206 | + } |
| 207 | + |
| 208 | + if !interactive { |
| 209 | + output.EmitNote(sink, fmt.Sprintf("No complete LocalStack AWS profile found. Run lstk interactively to configure one, or add a [profile %s] section to ~/.aws/config manually.", profileName)) |
| 210 | + return nil |
| 211 | + } |
| 212 | + |
| 213 | + files := strings.Join(status.filesToModify(), " and ") |
| 214 | + responseCh := make(chan output.InputResponse, 1) |
| 215 | + output.EmitUserInputRequest(sink, output.UserInputRequestEvent{ |
| 216 | + Prompt: fmt.Sprintf("Set up LocalStack AWS profile in %s?", files), |
| 217 | + Options: []output.InputOption{{Key: "y", Label: "Y"}, {Key: "n", Label: "n"}}, |
| 218 | + ResponseCh: responseCh, |
| 219 | + }) |
| 220 | + |
| 221 | + select { |
| 222 | + case resp := <-responseCh: |
| 223 | + if resp.Cancelled || resp.SelectedKey == "n" { |
| 224 | + return nil |
| 225 | + } |
| 226 | + if status.configNeeded { |
| 227 | + if err := writeConfigProfile(configPath, localStackHost); err != nil { |
| 228 | + output.EmitWarning(sink, fmt.Sprintf("could not update ~/.aws/config: %v", err)) |
| 229 | + return nil |
| 230 | + } |
| 231 | + } |
| 232 | + if status.credsNeeded { |
| 233 | + if err := writeCredsProfile(credsPath); err != nil { |
| 234 | + output.EmitWarning(sink, fmt.Sprintf("could not update ~/.aws/credentials: %v", err)) |
| 235 | + return nil |
| 236 | + } |
| 237 | + } |
| 238 | + output.EmitSuccess(sink, fmt.Sprintf("LocalStack AWS profile written to %s", files)) |
| 239 | + output.EmitNote(sink, fmt.Sprintf("Try: aws s3 mb s3://test --profile %s", profileName)) |
| 240 | + case <-ctx.Done(): |
| 241 | + return ctx.Err() |
| 242 | + } |
| 243 | + |
| 244 | + return nil |
| 245 | +} |
| 246 | + |
0 commit comments