Skip to content

Commit dda38ba

Browse files
committed
ai: fix for irsa
1 parent 2929414 commit dda38ba

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

tools/dz-ai/internal/data/duck/config.go

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import (
1313
)
1414

1515
// LoadS3ConfigFromEnv loads S3 configuration from environment variables.
16-
// Supports both AWS S3 and MinIO configurations.
16+
// Supports both AWS S3 and MinIO configurations, including IRSA (IAM Roles for Service Accounts).
1717
//
1818
// Environment variables:
19-
// - S3_ACCESS_KEY_ID or AWS_ACCESS_KEY_ID (required)
20-
// - S3_SECRET_ACCESS_KEY or AWS_SECRET_ACCESS_KEY (required)
19+
// - S3_ACCESS_KEY_ID or AWS_ACCESS_KEY_ID (optional, for IRSA leave unset to use IAM role)
20+
// - S3_SECRET_ACCESS_KEY or AWS_SECRET_ACCESS_KEY (optional, for IRSA leave unset to use IAM role)
2121
// - S3_ENDPOINT (optional, for MinIO: "http://localhost:9000")
2222
// - S3_REGION or AWS_REGION (optional, defaults to "us-east-1")
2323
// - S3_USE_SSL (optional, "true"/"false", auto-detected if S3_ENDPOINT is set)
@@ -30,23 +30,36 @@ import (
3030
// Otherwise, assumes AWS S3 and sets:
3131
// - UseSSL: true
3232
// - URLStyle: "virtual"
33+
//
34+
// For IRSA (IAM Roles for Service Accounts) in AWS EKS, leave both access key and secret
35+
// unset to use the IAM role credentials automatically.
3336
func LoadS3ConfigFromEnv() (*S3Config, error) {
3437
// Get access key (try S3_ prefix first, then AWS_ prefix)
3538
accessKeyID := os.Getenv("S3_ACCESS_KEY_ID")
3639
if accessKeyID == "" {
3740
accessKeyID = os.Getenv("AWS_ACCESS_KEY_ID")
3841
}
39-
if accessKeyID == "" {
40-
return nil, nil // Not an error, just not configured
41-
}
4242

4343
// Get secret key (try S3_ prefix first, then AWS_ prefix)
4444
secretAccessKey := os.Getenv("S3_SECRET_ACCESS_KEY")
4545
if secretAccessKey == "" {
4646
secretAccessKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
4747
}
48-
if secretAccessKey == "" {
49-
return nil, fmt.Errorf("S3_ACCESS_KEY_ID or AWS_ACCESS_KEY_ID is set but S3_SECRET_ACCESS_KEY or AWS_SECRET_ACCESS_KEY is missing")
48+
49+
// If neither access key nor secret is set, return nil (not configured, will use IRSA/default credentials)
50+
if accessKeyID == "" && secretAccessKey == "" {
51+
return nil, nil // Not an error, just not configured - will use default AWS credentials chain (IRSA)
52+
}
53+
54+
// If only secret is set without access key, that's an error
55+
if accessKeyID == "" && secretAccessKey != "" {
56+
return nil, fmt.Errorf("S3_SECRET_ACCESS_KEY or AWS_SECRET_ACCESS_KEY is set but S3_ACCESS_KEY_ID or AWS_ACCESS_KEY_ID is missing")
57+
}
58+
59+
// If only access key is set without secret, that's also an error (inconsistent state)
60+
// For IRSA, both should be unset to use IAM role credentials
61+
if accessKeyID != "" && secretAccessKey == "" {
62+
return nil, fmt.Errorf("S3_ACCESS_KEY_ID or AWS_ACCESS_KEY_ID is set but S3_SECRET_ACCESS_KEY or AWS_SECRET_ACCESS_KEY is missing (for IRSA, leave both unset)")
5063
}
5164

5265
// Get endpoint (optional, for MinIO)
@@ -127,6 +140,10 @@ func EnsureMinIOBucket(ctx context.Context, log *slog.Logger, storageURI string,
127140
}
128141

129142
// Create S3 client
143+
// MinIO always requires explicit credentials
144+
if s3Config.AccessKeyID == "" || s3Config.SecretAccessKey == "" {
145+
return fmt.Errorf("MinIO requires both S3_ACCESS_KEY_ID and S3_SECRET_ACCESS_KEY to be set")
146+
}
130147
creds := credentials.NewStaticCredentialsProvider(
131148
s3Config.AccessKeyID,
132149
s3Config.SecretAccessKey,
@@ -181,12 +198,28 @@ func PrepareS3ConfigForStorageURI(ctx context.Context, log *slog.Logger, storage
181198
}
182199

183200
// Load S3 config from environment variables
201+
// If nil, that's OK - will use default AWS credentials chain (IRSA)
184202
s3Config, err := LoadS3ConfigFromEnv()
185203
if err != nil {
186204
return nil, fmt.Errorf("failed to load S3 configuration: %w", err)
187205
}
206+
// If s3Config is nil, create a minimal config with just region for IRSA
188207
if s3Config == nil {
189-
return nil, fmt.Errorf("S3 storage URI specified but S3 configuration not found in environment variables (S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY required)")
208+
region := os.Getenv("S3_REGION")
209+
if region == "" {
210+
region = os.Getenv("AWS_REGION")
211+
}
212+
if region == "" {
213+
region = "us-east-1" // Default region
214+
}
215+
s3Config = &S3Config{
216+
AccessKeyID: "", // Empty for IRSA
217+
SecretAccessKey: "", // Empty for IRSA
218+
Endpoint: "", // AWS S3
219+
Region: region,
220+
UseSSL: true, // AWS S3 default
221+
URLStyle: "virtual", // AWS S3 default
222+
}
190223
}
191224

192225
// If using localhost MinIO, ensure the bucket exists

0 commit comments

Comments
 (0)