Files
mtusi2026lab4/backend/internal/adapter/storage/bootstrap.go

87 lines
3.0 KiB
Go

package storage
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/signer"
"io"
"net/http"
"net/url"
"os"
"time"
)
type Config struct{ Endpoint, Access, Secret, Bucket string }
func ReadConfig(path string) (Config, error) {
var c Config
b, e := os.ReadFile(path)
if e != nil {
return c, fmt.Errorf("S3 credentials unavailable")
}
if e = json.Unmarshal(b, &c); e != nil {
return c, fmt.Errorf("invalid S3 credentials format")
}
if c.Endpoint == "" || c.Access == "" || c.Secret == "" || c.Bucket == "" {
return c, fmt.Errorf("incomplete S3 configuration")
}
return c, nil
}
func FromConfig(c Config) (*S3, error) { return New(c.Endpoint, c.Access, c.Secret, c.Bucket) }
// RustFS provides S3 SDKs, but its documented administrative API is separate.
// Use the SDK's SigV4 signer; do not implement signing or persist credentials in command arguments.
func admin(ctx context.Context, c Config, path string, body []byte) error {
r, e := http.NewRequestWithContext(ctx, http.MethodPut, "http://"+c.Endpoint+"/rustfs/admin/v3/"+path, bytes.NewReader(body))
if e != nil {
return e
}
sum := sha256.Sum256(body)
r.Header.Set("X-Amz-Content-Sha256", hex.EncodeToString(sum[:]))
r.Header.Set("Content-Type", "application/json")
signed := signer.SignV4(*r, c.Access, c.Secret, "", "us-east-1")
response, e := (&http.Client{Timeout: 5 * time.Second}).Do(signed)
if e != nil {
return fmt.Errorf("S3 admin transport failed")
}
defer response.Body.Close()
_, _ = io.Copy(io.Discard, io.LimitReader(response.Body, 64*1024))
if response.StatusCode != 200 && response.StatusCode != 204 {
return fmt.Errorf("S3 admin operation rejected: HTTP %d", response.StatusCode)
}
return nil
}
func Bootstrap(ctx context.Context, root, app Config) error {
s, e := FromConfig(root)
if e != nil {
return e
}
exists, e := s.Client.BucketExists(ctx, root.Bucket)
if e != nil {
return fmt.Errorf("bucket availability check failed")
}
if !exists {
if e = s.Client.MakeBucket(ctx, root.Bucket, minio.MakeBucketOptions{Region: "us-east-1"}); e != nil {
return fmt.Errorf("bucket creation failed")
}
}
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:::" + app.Bucket}},
map[string]any{"Effect": "Allow", "Action": []string{"s3:PutObject", "s3:GetObject", "s3:DeleteObject"}, "Resource": []string{"arn:aws:s3:::" + app.Bucket + "/originals/*"}},
}}
raw, _ := json.Marshal(policy)
if e = admin(ctx, root, "add-canned-policy?name=messenger", raw); e != nil {
return e
}
raw, _ = json.Marshal(map[string]string{"secretKey": app.Secret, "status": "enabled"})
if e = admin(ctx, root, "add-user?accessKey="+url.QueryEscape(app.Access), raw); e != nil {
return e
}
return admin(ctx, root, "set-user-or-group-policy?policyName=messenger&userOrGroup="+url.QueryEscape(app.Access)+"&isGroup=false", nil)
}