Skip to content

Commit 41ed9d6

Browse files
MakeshiftDrJume
andauthored
feat: allow the AWS SDK to get auth from the environment (#88)
Co-authored-by: Julian Meinking <12785972+DrJume@users.noreply.github.com>
1 parent 3e7178a commit 41ed9d6

File tree

5 files changed

+76
-58
lines changed

5 files changed

+76
-58
lines changed

.env

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ STORAGE_FILESYSTEM_PATH=.data/storage/filesystem
88
# s3
99
# STORAGE_DRIVER=s3
1010
# STORAGE_S3_BUCKET=test
11-
# STORAGE_S3_ENDPOINT=localhost
12-
# STORAGE_S3_PORT=9000
13-
# STORAGE_S3_USE_SSL=false
14-
# STORAGE_S3_ACCESS_KEY=minioadmin
15-
# STORAGE_S3_SECRET_KEY=minioadmin
11+
# AWS_ENDPOINT_URL=http://minio:9000
12+
# AWS_ACCESS_KEY_ID=minioadmin
13+
# AWS_SECRET_ACCESS_KEY=minioadmin
1614

1715
# sqlite
1816
DB_DRIVER=sqlite
@@ -32,4 +30,4 @@ DB_SQLITE_PATH=.data/sqlite.db
3230
# DB_MYSQL_HOST=localhost
3331
# DB_MYSQL_USER=root
3432
# DB_MYSQL_PASSWORD=root
35-
# DB_MYSQL_PORT=3306
33+
# DB_MYSQL_PORT=3306

docker-compose.yml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3.9'
2-
31
services:
42
postgres:
53
image: postgres:15
@@ -24,3 +22,25 @@ services:
2422
command: -c 'mkdir -p /data/test && /usr/bin/minio server /data'
2523
ports:
2624
- 9000:9000
25+
environment:
26+
MINIO_ROOT_USER: access_key
27+
MINIO_ROOT_PASSWORD: secret_key
28+
29+
cache-server:
30+
build:
31+
dockerfile: Dockerfile
32+
context: .
33+
ports:
34+
- '3000:3000'
35+
depends_on:
36+
- minio
37+
38+
environment:
39+
API_BASE_URL: http://localhost:3000
40+
41+
STORAGE_DRIVER: s3
42+
STORAGE_S3_BUCKET: test
43+
44+
AWS_ACCESS_KEY_ID: access_key
45+
AWS_SECRET_ACCESS_KEY: secret_key
46+
AWS_ENDPOINT_URL: http://minio:9000

docs/content/2.storage-drivers/s3.md

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ services:
2424

2525
STORAGE_DRIVER: s3
2626
STORAGE_S3_BUCKET: gh-actions-cache
27-
STORAGE_S3_ACCESS_KEY: access_key
28-
STORAGE_S3_SECRET_KEY: secret_key
2927

30-
STORAGE_S3_ENDPOINT: minio
31-
STORAGE_S3_PORT: '9000'
32-
STORAGE_S3_USE_SSL: 'false'
28+
AWS_ACCESS_KEY_ID: access_key
29+
AWS_SECRET_ACCESS_KEY: secret_key
30+
AWS_ENDPOINT_URL: http://minio:9000
3331
volumes:
3432
- cache-data:/app/.data
3533

@@ -47,6 +45,8 @@ volumes:
4745
4846
### `docker-compose` AWS S3 example
4947

48+
This example assumes that credentials are being provided by the environment, e.g. via an [instance profile](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html) or [EKS IRSA](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html).
49+
5050
```yaml [docker-compose.yml]
5151
version: '3.9'
5252
@@ -60,12 +60,7 @@ services:
6060
6161
STORAGE_DRIVER: s3
6262
STORAGE_S3_BUCKET: gh-actions-cache
63-
STORAGE_S3_ACCESS_KEY: access_key
64-
STORAGE_S3_SECRET_KEY: secret_key
6563
66-
STORAGE_S3_ENDPOINT: s3.amazonaws.com
67-
STORAGE_S3_PORT: '443'
68-
STORAGE_S3_USE_SSL: 'true'
6964
volumes:
7065
- cache-data:/app/.data
7166
@@ -75,46 +70,65 @@ volumes:
7570

7671
### Environment Variables
7772

78-
Don't forget to set the `STORAGE_DRIVER` environment variable to `s3` to use the S3 storage driver.
73+
The only required S3-related environment variables are `STORAGE_DRIVER: s3` and `STORAGE_S3_BUCKET`. The rest of the environment variables are optional and depend on your S3-compatible storage provider.
74+
75+
The AWS SDK will automatically use any AWS credentials available in the environment, e.g. `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_REGION`. Outside of AWS, these environment variables can still be used to authenticate with S3-compatible storage, as seen in the Minio example above.
76+
77+
Common environment variables are listed below. For a full list of configuration options, see the [AWS SDK documentation](https://docs.aws.amazon.com/sdkref/latest/guide/settings-reference.html#EVarSettings).
7978

8079
#### `STORAGE_S3_BUCKET`
8180

8281
Example: `gh-actions-cache`
8382

84-
The name of the S3 bucket used for storage.
83+
The name of the S3 bucket used for storage. This environment variable is always required.
8584

86-
#### `STORAGE_S3_ACCESS_KEY`
85+
#### `AWS_REGION`
8786

88-
Example: `access_key`
87+
Example: `us-east-1`
8988

90-
The access key for S3 storage.
89+
The AWS SDK relies on this variable being set. In the cache server, it defaults to `us-east-1` if not provided. This has no effect if you are using a non-AWS S3-compatible storage provider, such as MinIO.
9190

92-
#### `STORAGE_S3_SECRET_KEY`
91+
#### `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`
9392

94-
Example: `secret_key`
93+
Example:
94+
`AWS_ACCESS_KEY_ID: access_key`
95+
`AWS_SECRET_ACCESS_KEY: secret_key`
9596

96-
The secret key for S3 storage.
97+
This is the access key/secret key used to authenticate with S3-compatible storage. If required to authenticate with your provider, these should be provided by the provider. Alternatively, you can use the `AWS_PROFILE` environment variable to specify a profile from your AWS credentials file.
9798

98-
#### `STORAGE_S3_ENDPOINT`
99+
#### `AWS_PROFILE`
99100

100-
Example: `s3.amazonaws.com`, `minio`
101+
Example: `my-profile`
101102

102-
The endpoint hostname for S3 storage.
103+
If you wish to run the cache server locally and utilize a profile from your AWS credentials file or local AWS CLI configuration, you can set the `AWS_PROFILE` environment variable to the name of the profile. Note that this will also require mounting the AWS credentials file into the container in order for the SDK to be able to find it.
103104

104-
#### `STORAGE_S3_REGION`
105+
```yaml [docker-compose.yml]
106+
version: '3.9'
105107
106-
Example: `us-west-1`
108+
services:
109+
cache-server:
110+
image: ghcr.io/falcondev-oss/github-actions-cache-server:latest
111+
ports:
112+
- '3000:3000'
113+
environment:
114+
API_BASE_URL: http://localhost:3000
107115
108-
The region for AWS S3. Not needed with MinIO.
116+
STORAGE_DRIVER: s3
117+
STORAGE_S3_BUCKET: gh-actions-cache
109118
110-
#### `STORAGE_S3_PORT`
119+
AWS_PROFILE: my-profile
111120
112-
Example: `443`, `9000`
121+
volumes:
122+
- cache-data:/app/.data
123+
# Mount the AWS CLI credentials and config into the container
124+
- ~/.aws:/root/.aws:ro
113125
114-
The port S3 storage is running on.
126+
volumes:
127+
cache-data:
128+
```
115129

116-
#### `STORAGE_S3_USE_SSL`
130+
#### `AWS_ENDPOINT_URL`
117131

118-
Example: `false`
132+
Example: `http://minio:9000`
119133

120-
Whether to use SSL for S3 storage connections.
134+
This is the endpoint URL for the S3-compatible storage. This is only required if you are using a non-AWS S3-compatible storage provider, such as MinIO.

lib/storage/drivers/s3.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,13 @@ import { streamToBuffer } from '~/lib/utils'
1818
export const s3Driver = defineStorageDriver({
1919
envSchema: z.object({
2020
STORAGE_S3_BUCKET: z.string().min(1),
21-
STORAGE_S3_ENDPOINT: z.string().min(1),
22-
STORAGE_S3_REGION: z.string().min(1).default('us-east-1'),
23-
STORAGE_S3_PORT: z.coerce.number().positive(),
24-
STORAGE_S3_USE_SSL: z.string().transform((v) => v === 'true'),
25-
STORAGE_S3_ACCESS_KEY: z.string().min(1),
26-
STORAGE_S3_SECRET_KEY: z.string().min(1),
21+
// AWS SDK requires an AWS_REGION to be set, even if you're using a custom endpoint
22+
AWS_REGION: z.string().default('us-east-1'),
2723
}),
2824
async setup(options) {
29-
const protocol = options.STORAGE_S3_USE_SSL ? 'https' : 'http'
30-
const port = options.STORAGE_S3_PORT ? `:${options.STORAGE_S3_PORT}` : ''
31-
3225
const s3 = new S3Client({
33-
credentials: {
34-
secretAccessKey: options.STORAGE_S3_SECRET_KEY,
35-
accessKeyId: options.STORAGE_S3_ACCESS_KEY,
36-
},
37-
endpoint: `${protocol}://${options.STORAGE_S3_ENDPOINT}${port}`,
38-
region: options.STORAGE_S3_REGION,
3926
forcePathStyle: true,
27+
region: options.AWS_REGION,
4028
})
4129

4230
try {

tests/.env.s3.storage

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
STORAGE_DRIVER=s3
22
STORAGE_S3_BUCKET=test
3-
STORAGE_S3_ENDPOINT=localhost
4-
STORAGE_S3_PORT=9000
5-
STORAGE_S3_USE_SSL=false
6-
STORAGE_S3_ACCESS_KEY=minioadmin
7-
STORAGE_S3_SECRET_KEY=minioadmin
3+
AWS_ENDPOINT_URL=http://localhost:9000
4+
AWS_ACCESS_KEY_ID=minioadmin
5+
AWS_SECRET_ACCESS_KEY=minioadmin

0 commit comments

Comments
 (0)