Лабораторная 7

This commit is contained in:
MacBook
2026-11-24 12:20:21 +03:00
parent 20afc2c790
commit b962fa0d5d
99 changed files with 11329 additions and 107 deletions
+59
View File
@@ -0,0 +1,59 @@
package objectstore
import (
"context"
"encoding/json"
"mtusi-messenger/internal/security"
"net/http"
"net/url"
"strings"
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, role 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
}
tc, e := security.ClientTLS()
if e != nil {
return e
}
admin.SetCustomTransport(&http.Transport{TLSClientConfig: tc})
if err = admin.AddUser(ctx, apiAccess, apiSecret); err != nil {
return err
}
statements := []any{map[string]any{"Effect": "Allow", "Action": []string{"s3:GetBucketLocation", "s3:ListBucket"}, "Resource": []string{"arn:aws:s3:::" + bucket}}}
allow := func(actions []string, prefixes ...string) {
resources := []string{}
for _, prefix := range prefixes {
resources = append(resources, "arn:aws:s3:::"+bucket+"/"+prefix+"/*")
}
statements = append(statements, map[string]any{"Effect": "Allow", "Action": actions, "Resource": resources})
}
if strings.HasPrefix(role, "api") {
allow([]string{"s3:PutObject", "s3:GetObject"}, "staging")
allow([]string{"s3:GetObject"}, "originals", "variants")
} else if role == "worker" {
allow([]string{"s3:GetObject"}, "staging")
allow([]string{"s3:GetObject", "s3:PutObject"}, "originals", "variants")
} else if role == "dispatcher" || role == "cleanup" {
allow([]string{"s3:GetObject", "s3:DeleteObject"}, "staging")
}
policy := map[string]any{"Version": "2012-10-17", "Statement": statements}
encoded, err := json.Marshal(policy)
if err != nil {
return err
}
if err = admin.AddCannedPolicy(ctx, "messenger-"+apiAccess, encoded); err != nil {
return err
}
return admin.SetPolicy(ctx, "messenger-"+apiAccess, apiAccess, false)
}