Files
mtusi2026lab5/internal/objectstore/bootstrap.go

41 lines
1.3 KiB
Go

package objectstore
import (
"context"
"encoding/json"
"net/url"
madmin "github.com/minio/madmin-go/v3"
)
// Bootstrap receives root credentials only in the one-shot s3-init process.
// The API and cleaner receive a separate user limited to this bucket/prefix.
func Bootstrap(ctx context.Context, endpoint, bucket, rootAccess, rootSecret, apiAccess, apiSecret string) error {
u, err := url.Parse(endpoint)
if err != nil {
return err
}
admin, err := madmin.New(u.Host, rootAccess, rootSecret, u.Scheme == "https")
if err != nil {
return err
}
if err = admin.AddUser(ctx, apiAccess, apiSecret); err != nil {
return err
}
policy := map[string]any{
"Version": "2012-10-17",
"Statement": []any{
map[string]any{"Effect": "Allow", "Action": []string{"s3:GetBucketLocation", "s3:ListBucket"}, "Resource": []string{"arn:aws:s3:::" + bucket}},
map[string]any{"Effect": "Allow", "Action": []string{"s3:GetObject", "s3:PutObject", "s3:DeleteObject"}, "Resource": []string{"arn:aws:s3:::" + bucket + "/originals/*", "arn:aws:s3:::" + bucket + "/staging/*", "arn:aws:s3:::" + bucket + "/variants/*"}},
},
}
encoded, err := json.Marshal(policy)
if err != nil {
return err
}
if err = admin.AddCannedPolicy(ctx, "messenger-api", encoded); err != nil {
return err
}
return admin.SetPolicy(ctx, "messenger-api", apiAccess, false)
}