135 lines
4.4 KiB
Go
135 lines
4.4 KiB
Go
package repositories
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
|
|
madmin "github.com/minio/madmin-go/v3"
|
|
"github.com/minio/minio-go/v7"
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
"github.com/minio/minio-go/v7/pkg/lifecycle"
|
|
)
|
|
|
|
func readS3Secret(name string) (string, error) {
|
|
data, err := os.ReadFile(os.Getenv(name))
|
|
if err != nil {
|
|
return "", errors.New("cannot read " + name)
|
|
}
|
|
value := strings.TrimSpace(string(data))
|
|
if value == "" {
|
|
return "", errors.New("empty " + name)
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
// BootstrapS3 is the only application command that needs administrative credentials.
|
|
func BootstrapS3(ctx context.Context) error {
|
|
endpoint, err := url.Parse(os.Getenv("S3_ENDPOINT"))
|
|
if err != nil || endpoint.Host == "" {
|
|
return errors.New("invalid S3_ENDPOINT")
|
|
}
|
|
rootAccess, err := readS3Secret("S3_ROOT_ACCESS_KEY_ID_FILE")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rootSecret, err := readS3Secret("S3_ROOT_SECRET_ACCESS_KEY_FILE")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
client, err := minio.New(endpoint.Host, &minio.Options{Creds: credentials.NewStaticV4(rootAccess, rootSecret, ""), Secure: endpoint.Scheme == "https", Region: os.Getenv("S3_REGION")})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
store := &ObjectStore{client: client, bucket: os.Getenv("S3_BUCKET")}
|
|
if err = store.Bootstrap(ctx); err != nil {
|
|
return err
|
|
}
|
|
if err = bootstrapDirectLifecycle(ctx, client, store.bucket); err != nil {
|
|
return err
|
|
}
|
|
admin, err := madmin.New(endpoint.Host, rootAccess, rootSecret, endpoint.Scheme == "https")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, role := range []string{"api", "worker", "cleanup"} {
|
|
access, err := readS3Secret("S3_" + strings.ToUpper(role) + "_ACCESS_KEY_ID_FILE")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
secret, err := readS3Secret("S3_" + strings.ToUpper(role) + "_SECRET_ACCESS_KEY_FILE")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if access == rootAccess {
|
|
return errors.New("application and root credentials must differ")
|
|
}
|
|
if err := setS3Role(ctx, admin, store.bucket, role, access, secret); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func setS3Role(ctx context.Context, admin *madmin.AdminClient, bucket, role, access, secret string) error {
|
|
resource := "arn:aws:s3:::" + bucket
|
|
statement := func(actions, resources []string) map[string]any {
|
|
return map[string]any{"Effect": "Allow", "Action": actions, "Resource": resources}
|
|
}
|
|
statements := []any{statement([]string{"s3:GetBucketLocation", "s3:ListBucket"}, []string{resource})}
|
|
add := func(action string, prefixes ...string) {
|
|
resources := make([]string, 0, len(prefixes))
|
|
for _, prefix := range prefixes {
|
|
resources = append(resources, resource+"/"+prefix+"/*")
|
|
}
|
|
statements = append(statements, statement([]string{"s3:" + action}, resources))
|
|
}
|
|
switch role {
|
|
case "api":
|
|
add("GetObject", "originals", "variants", "staging/direct")
|
|
add("PutObject", "staging/jobs", "staging/direct", "originals")
|
|
add("DeleteObject", "staging/jobs", "staging/direct")
|
|
case "worker":
|
|
add("GetObject", "staging/jobs", "staging/direct", "originals", "variants")
|
|
add("PutObject", "originals", "variants")
|
|
add("DeleteObject", "staging/jobs", "staging/direct", "variants")
|
|
case "cleanup":
|
|
add("DeleteObject", "originals", "staging/jobs", "staging/direct")
|
|
}
|
|
data, err := json.Marshal(map[string]any{"Version": "2012-10-17", "Statement": statements})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
name := "messenger-attachments-" + role
|
|
if err = admin.AddCannedPolicy(ctx, name, data); err != nil {
|
|
return err
|
|
}
|
|
if err = admin.SetUserReq(ctx, access, madmin.AddOrUpdateUserReq{SecretKey: secret, Status: madmin.AccountEnabled}); err != nil {
|
|
return err
|
|
}
|
|
return admin.SetPolicy(ctx, name, access, false)
|
|
}
|
|
|
|
func bootstrapDirectLifecycle(ctx context.Context, client *minio.Client, bucket string) error {
|
|
config, err := client.GetBucketLifecycle(ctx, bucket)
|
|
if err != nil && minio.ToErrorResponse(err).Code != "NoSuchLifecycleConfiguration" {
|
|
return err
|
|
}
|
|
if config == nil {
|
|
config = lifecycle.NewConfiguration()
|
|
}
|
|
const id = "messenger-direct-staging-expiration"
|
|
rules := make([]lifecycle.Rule, 0, len(config.Rules)+1)
|
|
for _, rule := range config.Rules {
|
|
if rule.ID != id {
|
|
rules = append(rules, rule)
|
|
}
|
|
}
|
|
rules = append(rules, lifecycle.Rule{ID: id, Status: "Enabled", RuleFilter: lifecycle.Filter{Prefix: "staging/direct/"}, Expiration: lifecycle.Expiration{Days: 1}})
|
|
config.Rules = rules
|
|
return client.SetBucketLifecycle(ctx, bucket, config)
|
|
}
|