Repository: codenotary/immudb
File: embedded/remotestorage/s3/s3.go (Go)

Code before:
	}()

	return nil
}

func (s *Storage) requestCredentials() (string, string, string, error) {
	if s.fargateCredentials {
		// Use Fargate credentials
		fargateCredentialsRelativeURI := os.Getenv("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI")
		if fargateCredentialsRelativeURI == "" {
      return "", "", "", errors.New(fmt.Sprintf("environment variable %s is not set or empty", "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"))
    }
		fargateCredentialsURL := fmt.Sprintf("%s%s", "http://169.254.170.2", fargateCredentialsRelativeURI)

		fargateReq, err := http.NewRequest("GET", fargateCredentialsURL, nil)
		if err != nil {
			return "", "", "", errors.New("cannot form fargate credentials request")
		}

		fargateResp, err := http.DefaultClient.Do(fargateReq)
		if err != nil {
			return "", "", "", errors.New("cannot get fargate credentials")
		}
		defer fargateResp.Body.Close()

		creds, err := ioutil.ReadAll(fargateResp.Body)
		if err != nil {
			return "", "", "", errors.New("cannot read fargate credentials")
		}

		var credentials struct {
			AccessKeyID     string `json:"AccessKeyId"`
			SecretAccessKey string `json:"SecretAccessKey"`
			SessionToken    string `json:"Token"`
		}
		if err := json.Unmarshal(creds, &credentials); err != nil {
			return "", "", "", errors.New("cannot parse fargate credentials")
		}

		return credentials.AccessKeyID, credentials.SecretAccessKey, credentials.SessionToken, nil
	}

	tokenReq, err := http.NewRequest("PUT", fmt.Sprintf("%s%s",
		s.awsInstanceMetadataURL,
		"/latest/api/token",
	), nil)
	if err != nil {
		return "", "", "", errors.New("cannot form metadata token request")
	}

	tokenReq.Header.Set("X-aws-ec2-metadata-token-ttl-seconds", "21600")

Review comment (suggestion):
`ioutil.ReadAll` should be deprecated. Use `io.ReadAll`.

Code after:
	}()

	return nil
}

func (s *Storage) requestCredentials() (string, string, string, error) {
	if s.useFargateCredentials {
		// Use Fargate credentials

		const fargateMetadataEndpoint = "http://169.254.170.2"

		fargateCredentialsRelativeURI := os.Getenv("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI")
		if fargateCredentialsRelativeURI == "" {
			return "", "", "", errors.New("environment variable AWS_CONTAINER_CREDENTIALS_RELATIVE_URI is not set or empty")
		}
		fargateCredentialsURL := fargateMetadataEndpoint + fargateCredentialsRelativeURI

		fargateReq, err := http.NewRequest("GET", fargateCredentialsURL, nil)
		if err != nil {
			return "", "", "", errors.New("cannot form fargate credentials request")
		}

		fargateResp, err := http.DefaultClient.Do(fargateReq)
		if err != nil {
			return "", "", "", errors.New("cannot get fargate credentials")
		}
		defer fargateResp.Body.Close()

		creds, err := io.ReadAll(fargateResp.Body)
		if err != nil {
			return "", "", "", errors.New("cannot read fargate credentials")
		}

		var credentials struct {
			AccessKeyID     string `json:"AccessKeyId"`
			SecretAccessKey string `json:"SecretAccessKey"`
			SessionToken    string `json:"Token"`
		}
		if err := json.Unmarshal(creds, &credentials); err != nil {
			return "", "", "", errors.New("cannot parse fargate credentials")
		}

		return credentials.AccessKeyID, credentials.SecretAccessKey, credentials.SessionToken, nil
	}

	tokenReq, err := http.NewRequest("PUT", fmt.Sprintf("%s%s",
		s.awsInstanceMetadataURL,
		"/latest/api/token",
	), nil)
	if err != nil {
		return "", "", "", errors.New("cannot form metadata token request")