|
1 | 1 | // Package s3 is a minimal local fork of an AWS SDK package for the Runtime |
2 | | -// Conditions demo. It intentionally mirrors the shape of an SDK client while |
3 | | -// keeping runtime behavior inert and dependency-free. |
| 2 | +// Conditions demo. It mirrors the shape of an SDK client and performs the |
| 3 | +// S3 PutObject call needed by the demo without pulling in the full AWS SDK. |
4 | 4 | package s3 |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "bytes" |
7 | 8 | "context" |
| 9 | + "crypto/hmac" |
| 10 | + "crypto/sha256" |
| 11 | + "encoding/hex" |
8 | 12 | "errors" |
| 13 | + "fmt" |
9 | 14 | "io" |
| 15 | + "net/http" |
| 16 | + "net/url" |
10 | 17 | "os" |
| 18 | + "strings" |
| 19 | + "time" |
11 | 20 | ) |
12 | 21 |
|
13 | 22 | // Config represents SDK configuration. |
@@ -39,10 +48,124 @@ func (c *Client) PutObject(ctx context.Context, input *PutObjectInput, optFns .. |
39 | 48 | if input == nil || input.Bucket == nil || *input.Bucket == "" { |
40 | 49 | return nil, errors.New("s3 bucket is required") |
41 | 50 | } |
42 | | - for _, key := range []string{"AWS_REGION", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY"} { |
43 | | - if os.Getenv(key) == "" { |
44 | | - return nil, errors.New(key + " is not set") |
| 51 | + if input.Key == nil || *input.Key == "" { |
| 52 | + return nil, errors.New("s3 object key is required") |
| 53 | + } |
| 54 | + region, accessKeyID, secretAccessKey, err := credentialsFromEnv() |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + var body []byte |
| 59 | + if input.Body == nil { |
| 60 | + body = nil |
| 61 | + } else { |
| 62 | + body, err = io.ReadAll(input.Body) |
| 63 | + if err != nil { |
| 64 | + return nil, fmt.Errorf("read s3 body: %w", err) |
45 | 65 | } |
46 | 66 | } |
| 67 | + |
| 68 | + host := fmt.Sprintf("%s.s3.%s.amazonaws.com", *input.Bucket, region) |
| 69 | + canonicalURI := "/" + encodeObjectKey(*input.Key) |
| 70 | + endpoint := "https://" + host + canonicalURI |
| 71 | + request, err := http.NewRequestWithContext(ctx, http.MethodPut, endpoint, bytes.NewReader(body)) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + signS3Request(request, body, host, region, accessKeyID, secretAccessKey, time.Now().UTC()) |
| 76 | + |
| 77 | + response, err := http.DefaultClient.Do(request) |
| 78 | + if err != nil { |
| 79 | + return nil, fmt.Errorf("s3 PutObject failed: %w", err) |
| 80 | + } |
| 81 | + defer response.Body.Close() |
| 82 | + if response.StatusCode < 200 || response.StatusCode > 299 { |
| 83 | + responseBody, _ := io.ReadAll(io.LimitReader(response.Body, 4096)) |
| 84 | + return nil, fmt.Errorf("s3 PutObject returned %s: %s", response.Status, strings.TrimSpace(string(responseBody))) |
| 85 | + } |
47 | 86 | return &PutObjectOutput{}, nil |
48 | 87 | } |
| 88 | + |
| 89 | +func credentialsFromEnv() (string, string, string, error) { |
| 90 | + region := os.Getenv("AWS_REGION") |
| 91 | + accessKeyID := os.Getenv("AWS_ACCESS_KEY_ID") |
| 92 | + secretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY") |
| 93 | + for key, value := range map[string]string{ |
| 94 | + "AWS_REGION": region, |
| 95 | + "AWS_ACCESS_KEY_ID": accessKeyID, |
| 96 | + "AWS_SECRET_ACCESS_KEY": secretAccessKey, |
| 97 | + } { |
| 98 | + if value == "" { |
| 99 | + return "", "", "", errors.New(key + " is not set") |
| 100 | + } |
| 101 | + } |
| 102 | + return region, accessKeyID, secretAccessKey, nil |
| 103 | +} |
| 104 | + |
| 105 | +func signS3Request(request *http.Request, body []byte, host, region, accessKeyID, secretAccessKey string, now time.Time) { |
| 106 | + payloadHash := sha256Hex(body) |
| 107 | + amzDate := now.Format("20060102T150405Z") |
| 108 | + shortDate := now.Format("20060102") |
| 109 | + credentialScope := shortDate + "/" + region + "/s3/aws4_request" |
| 110 | + signedHeaders := "host;x-amz-content-sha256;x-amz-date" |
| 111 | + |
| 112 | + request.Header.Set("X-Amz-Content-Sha256", payloadHash) |
| 113 | + request.Header.Set("X-Amz-Date", amzDate) |
| 114 | + |
| 115 | + canonicalHeaders := "host:" + host + "\n" + |
| 116 | + "x-amz-content-sha256:" + payloadHash + "\n" + |
| 117 | + "x-amz-date:" + amzDate + "\n" |
| 118 | + canonicalRequest := strings.Join( |
| 119 | + []string{ |
| 120 | + request.Method, |
| 121 | + request.URL.EscapedPath(), |
| 122 | + "", |
| 123 | + canonicalHeaders, |
| 124 | + signedHeaders, |
| 125 | + payloadHash, |
| 126 | + }, |
| 127 | + "\n", |
| 128 | + ) |
| 129 | + stringToSign := strings.Join( |
| 130 | + []string{ |
| 131 | + "AWS4-HMAC-SHA256", |
| 132 | + amzDate, |
| 133 | + credentialScope, |
| 134 | + sha256Hex([]byte(canonicalRequest)), |
| 135 | + }, |
| 136 | + "\n", |
| 137 | + ) |
| 138 | + signature := hex.EncodeToString(hmacSHA256(signingKey(secretAccessKey, shortDate, region), []byte(stringToSign))) |
| 139 | + request.Header.Set( |
| 140 | + "Authorization", |
| 141 | + "AWS4-HMAC-SHA256 Credential="+accessKeyID+"/"+credentialScope+ |
| 142 | + ", SignedHeaders="+signedHeaders+ |
| 143 | + ", Signature="+signature, |
| 144 | + ) |
| 145 | +} |
| 146 | + |
| 147 | +func signingKey(secretAccessKey, shortDate, region string) []byte { |
| 148 | + dateKey := hmacSHA256([]byte("AWS4"+secretAccessKey), []byte(shortDate)) |
| 149 | + regionKey := hmacSHA256(dateKey, []byte(region)) |
| 150 | + serviceKey := hmacSHA256(regionKey, []byte("s3")) |
| 151 | + return hmacSHA256(serviceKey, []byte("aws4_request")) |
| 152 | +} |
| 153 | + |
| 154 | +func hmacSHA256(key, value []byte) []byte { |
| 155 | + hash := hmac.New(sha256.New, key) |
| 156 | + hash.Write(value) |
| 157 | + return hash.Sum(nil) |
| 158 | +} |
| 159 | + |
| 160 | +func sha256Hex(value []byte) string { |
| 161 | + sum := sha256.Sum256(value) |
| 162 | + return hex.EncodeToString(sum[:]) |
| 163 | +} |
| 164 | + |
| 165 | +func encodeObjectKey(key string) string { |
| 166 | + parts := strings.Split(key, "/") |
| 167 | + for index, part := range parts { |
| 168 | + parts[index] = url.PathEscape(part) |
| 169 | + } |
| 170 | + return strings.Join(parts, "/") |
| 171 | +} |
0 commit comments