(client *http.Client, endpoint string)
| 343 | } |
| 344 | |
| 345 | func fetchIMDSToken(client *http.Client, endpoint string) (string, error) { |
| 346 | ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 347 | defer cancel() |
| 348 | |
| 349 | req, err := http.NewRequestWithContext(ctx, http.MethodPut, endpoint+TokenPath, nil) |
| 350 | if err != nil { |
| 351 | return "", err |
| 352 | } |
| 353 | req.Header.Add(TokenRequestTTLHeader, TokenTTL) |
| 354 | resp, err := client.Do(req) |
| 355 | if err != nil { |
| 356 | return "", err |
| 357 | } |
| 358 | defer resp.Body.Close() |
| 359 | data, err := io.ReadAll(resp.Body) |
| 360 | if err != nil { |
| 361 | return "", err |
| 362 | } |
| 363 | if resp.StatusCode != http.StatusOK { |
| 364 | return "", errors.New(resp.Status) |
| 365 | } |
| 366 | return string(data), nil |
| 367 | } |
| 368 | |
| 369 | // getCredentials - obtains the credentials from the IAM role name associated with |
| 370 | // the current EC2 service. |
no test coverage detected