feat: complete lab 7 grade 3 security controls

This commit is contained in:
Dmitrii Grishko
2026-11-14 20:08:27 +03:00
parent 8986ba6bef
commit c5e3ac3aff
49 changed files with 1670 additions and 149 deletions
+46 -19
View File
@@ -40,17 +40,6 @@ func BootstrapS3(ctx context.Context) error {
if err != nil {
return err
}
access, err := readS3Secret("S3_ACCESS_KEY_ID_FILE")
if err != nil {
return err
}
secret, err := readS3Secret("S3_SECRET_ACCESS_KEY_FILE")
if err != nil {
return err
}
if access == rootAccess {
return errors.New("API and root credentials must differ")
}
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
@@ -66,23 +55,61 @@ func BootstrapS3(ctx context.Context) error {
if 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:::" + store.bucket}},
map[string]any{"Effect": "Allow", "Action": []string{"s3:GetObject", "s3:PutObject", "s3:DeleteObject"}, "Resource": []string{"arn:aws:s3:::" + store.bucket + "/originals/*", "arn:aws:s3:::" + store.bucket + "/staging/direct/*", "arn:aws:s3:::" + store.bucket + "/staging/jobs/*", "arn:aws:s3:::" + store.bucket + "/variants/*"}},
}}
data, err := json.Marshal(policy)
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
}
const name = "messenger-attachments-api"
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
}
// Explicitly assign the managed policy; creating a user alone grants no S3 rights.
// This idempotent API is supported by our pinned MinIO server release.
return admin.SetPolicy(ctx, name, access, false)
}