Files
mtusi2026lab7/scripts/s3-control.go

222 lines
7.5 KiB
Go

// Operator-only test/bootstrap utility. Uses the pinned application's native minio-go SDK and SigV4 signer.
package main
import (
"bytes"
"context"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/minio/minio-go/v7/pkg/signer"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
)
type credential struct{ Endpoint, Access, Secret, Bucket string }
type inputs struct{ Root, API, Worker credential }
var receiptDir string
func receipt(stage string) (*os.File, error) {
if receiptDir == "" {
return nil, fmt.Errorf("operator receipt directory required")
}
var nonce [12]byte
if _, e := rand.Read(nonce[:]); e != nil {
return nil, e
}
f, e := os.OpenFile(filepath.Join(receiptDir, hex.EncodeToString(nonce[:])+".json"), os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)
if e != nil {
return nil, e
}
if e = json.NewEncoder(f).Encode(map[string]any{"state": "RUNNING", "stage": stage}); e != nil {
_ = f.Close()
return nil, e
}
return f, nil
}
func finishReceipt(f *os.File, value any) error {
if _, e := f.Seek(0, 0); e != nil {
return e
}
if e := f.Truncate(0); e != nil {
return e
}
return json.NewEncoder(f).Encode(value)
}
func must(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, "S3_CONTROL_OPERATION_FAILED")
os.Exit(1)
}
}
func client(c credential, anonymous bool) *minio.Client {
cred := credentials.NewStaticV4(c.Access, c.Secret, "")
if anonymous {
cred = credentials.NewStaticV4("", "", "")
}
v, e := minio.New(c.Endpoint, &minio.Options{Creds: cred, Region: "us-east-1", Secure: false})
must(e)
return v
}
func admin(ctx context.Context, c credential, path string, value any) error {
record, e := receipt(path)
if e != nil {
return e
}
defer func() { _ = record.Close() }()
body, e := json.Marshal(value)
if e != nil {
return e
}
request, 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)
request.Header.Set("X-Amz-Content-Sha256", hex.EncodeToString(sum[:]))
request.Header.Set("Content-Type", "application/json")
signed := signer.SignV4(*request, c.Access, c.Secret, "", "us-east-1")
response, e := (&http.Client{Timeout: 5 * time.Second}).Do(signed)
if e != nil {
_ = finishReceipt(record, map[string]any{"state": "UNCERTAIN", "stage": path, "error": e.Error()})
return e
}
defer response.Body.Close()
raw, readErr := io.ReadAll(io.LimitReader(response.Body, 65537))
receiptErr := finishReceipt(record, map[string]any{"state": "RESPONSE", "stage": path, "http_status": response.StatusCode, "response_body": raw, "body_read_error": readErr != nil})
if response.StatusCode != 200 && response.StatusCode != 204 {
return fmt.Errorf("admin rejected")
}
if readErr != nil {
return readErr
}
if len(raw) > 65536 {
return fmt.Errorf("admin response too large")
}
return receiptErr
}
func policy(c credential, role string) any {
object := func(actions []string, prefixes ...string) any {
resources := []string{}
for _, prefix := range prefixes {
resources = append(resources, "arn:aws:s3:::"+c.Bucket+"/"+prefix+"/*")
}
return map[string]any{"Effect": "Allow", "Action": actions, "Resource": resources}
}
bucketActions := []string{"s3:GetBucketLocation"}
if role == "api" {
bucketActions = append(bucketActions, "s3:ListBucket")
}
statements := []any{map[string]any{"Effect": "Allow", "Action": bucketActions, "Resource": []string{"arn:aws:s3:::" + c.Bucket}}}
if role == "api" {
statements = append(statements, object([]string{"s3:PutObject", "s3:GetObject", "s3:DeleteObject"}, "staging"), object([]string{"s3:GetObject"}, "originals", "variants"))
} else {
statements = append(statements, object([]string{"s3:GetObject", "s3:DeleteObject"}, "staging"), object([]string{"s3:GetObject", "s3:PutObject"}, "originals", "variants"))
}
return map[string]any{"Version": "2012-10-17", "Statement": statements}
}
func bootstrap(ctx context.Context, c inputs) {
root := client(c.Root, false)
exists, e := root.BucketExists(ctx, c.Root.Bucket)
must(e)
if !exists {
must(root.MakeBucket(ctx, c.Root.Bucket, minio.MakeBucketOptions{Region: "us-east-1"}))
}
for _, item := range []struct {
role string
c credential
}{{"api", c.API}, {"worker", c.Worker}} {
name := "vp7-" + item.role
must(admin(ctx, c.Root, "add-canned-policy?name="+name, policy(item.c, item.role)))
must(admin(ctx, c.Root, "add-user?accessKey="+url.QueryEscape(item.c.Access), map[string]string{"secretKey": item.c.Secret, "status": "enabled"}))
must(admin(ctx, c.Root, "set-user-or-group-policy?policyName="+name+"&userOrGroup="+url.QueryEscape(item.c.Access)+"&isGroup=false", nil))
}
}
func denied(err error) bool { return err != nil && minio.ToErrorResponse(err).Code == "AccessDenied" }
func proof(ctx context.Context, c credential) {
s := client(c, false)
key := "variants/security-probe/" + fmt.Sprint(time.Now().UnixNano())
payload := "worker-scope-probe"
_, e := s.PutObject(ctx, c.Bucket, key, strings.NewReader(payload), int64(len(payload)), minio.PutObjectOptions{DisableMultipart: true})
must(e)
obj, e := s.GetObject(ctx, c.Bucket, key, minio.GetObjectOptions{})
must(e)
data, e := io.ReadAll(io.LimitReader(obj, 100))
must(e)
must(obj.Close())
if string(data) != payload {
must(fmt.Errorf("wrong object"))
}
_, e = s.PutObject(ctx, c.Bucket, "forbidden/"+key, strings.NewReader(payload), int64(len(payload)), minio.PutObjectOptions{DisableMultipart: true})
if !denied(e) {
must(fmt.Errorf("forbidden prefix accepted"))
}
if !denied(s.MakeBucket(ctx, "vp7-worker-forbidden", minio.MakeBucketOptions{})) {
must(fmt.Errorf("bucket admin accepted"))
}
a := client(c, true)
o, e := a.GetObject(ctx, c.Bucket, key, minio.GetObjectOptions{})
if e == nil {
_, e = o.Stat()
_ = o.Close()
}
if !denied(e) {
must(fmt.Errorf("anonymous object accepted"))
}
fmt.Println("PASS_WORKER_S3_ALLOWED_VARIANT_DENIED_PREFIX_ADMIN_ANONYMOUS")
}
func stale(ctx context.Context, c credential) {
s := client(c, false)
_, e := s.PutObject(ctx, c.Bucket, "variants/rotation-secret-probe", strings.NewReader("retired-secret"), 14, minio.PutObjectOptions{DisableMultipart: true})
if e == nil {
must(fmt.Errorf("retired secret still accepted"))
}
code := minio.ToErrorResponse(e).Code
if code != "SignatureDoesNotMatch" && code != "InvalidAccessKeyId" && code != "AccessDenied" {
must(fmt.Errorf("retired secret failed for unrelated reason"))
}
fmt.Println("PASS_RETIRED_S3_SECRET_DENIED_ALLOWED_PREFIX_WRITE")
}
func main() {
action := flag.String("action", "", "bootstrap, prove-worker or prove-stale")
path := flag.String("config", "", "operator-only configuration path")
flag.StringVar(&receiptDir, "receipt-dir", "", "private operator response directory")
flag.Parse()
raw, e := os.ReadFile(*path)
must(e)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
switch *action {
case "bootstrap":
var config inputs
must(json.Unmarshal(raw, &config))
record, e := receipt("bootstrap")
must(e)
bootstrap(ctx, config)
must(finishReceipt(record, map[string]any{"state": "COMPLETE", "stage": "bootstrap"}))
must(record.Close())
fmt.Println("PASS_S3_SEPARATE_PREFIX_SCOPED_PRINCIPALS")
case "prove-worker":
var config credential
must(json.Unmarshal(raw, &config))
proof(ctx, config)
case "prove-stale":
var config credential
must(json.Unmarshal(raw, &config))
stale(ctx, config)
default:
must(fmt.Errorf("unsupported action"))
}
}