135 lines
3.8 KiB
Go
135 lines
3.8 KiB
Go
// Command s3ctl is a small operator/test helper for the private bucket. It is
|
|
// not part of the HTTP API: it uses the same restricted API credentials as the
|
|
// application and is used only to seed failure scenarios for the reconciler
|
|
// and to prove that anonymous access and cross-bucket access are denied.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"mtusi.ru/messenger-lab4/internal/config"
|
|
"mtusi.ru/messenger-lab4/internal/objectstore"
|
|
)
|
|
|
|
func main() {
|
|
op := flag.String("op", "", "put, get, stat, delete or list")
|
|
key := flag.String("key", "", "object key")
|
|
file := flag.String("file", "", "file to upload for -op put")
|
|
contentType := flag.String("content-type", "application/octet-stream", "content type for -op put")
|
|
prefix := flag.String("prefix", "", "prefix for -op list")
|
|
bucketOverride := flag.String("bucket", "", "override the configured bucket (permission test)")
|
|
flag.Parse()
|
|
|
|
cfg, err := config.LoadTool()
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "s3ctl: configuration error:", err)
|
|
os.Exit(2)
|
|
}
|
|
bucket := cfg.S3Bucket
|
|
if *bucketOverride != "" {
|
|
bucket = *bucketOverride
|
|
}
|
|
store, err := objectstore.Open(objectstore.Config{
|
|
Endpoint: cfg.S3Endpoint,
|
|
Region: cfg.S3Region,
|
|
Bucket: bucket,
|
|
AccessKey: cfg.S3AccessKey,
|
|
SecretKey: cfg.S3SecretKey,
|
|
Timeout: cfg.S3Timeout,
|
|
})
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "s3ctl: configuration error:", err)
|
|
os.Exit(2)
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
|
|
switch *op {
|
|
case "put":
|
|
if *key == "" || *file == "" {
|
|
fail("-op put requires -key and -file")
|
|
}
|
|
handle, err := os.Open(*file)
|
|
if err != nil {
|
|
fail("cannot open -file: " + err.Error())
|
|
}
|
|
defer handle.Close()
|
|
info, err := handle.Stat()
|
|
if err != nil {
|
|
fail("cannot stat -file: " + err.Error())
|
|
}
|
|
if err := store.Put(ctx, *key, handle, info.Size(), *contentType); err != nil {
|
|
fail("put failed: " + err.Error())
|
|
}
|
|
print(map[string]any{"operation": "put", "key": *key, "size_bytes": info.Size()})
|
|
case "delete":
|
|
if *key == "" {
|
|
fail("-op delete requires -key")
|
|
}
|
|
if err := store.Remove(ctx, *key); err != nil {
|
|
fail("delete failed: " + err.Error())
|
|
}
|
|
print(map[string]any{"operation": "delete", "key": *key})
|
|
case "stat":
|
|
if *key == "" {
|
|
fail("-op stat requires -key")
|
|
}
|
|
info, err := store.Stat(ctx, *key)
|
|
if err != nil {
|
|
fail("stat failed: " + err.Error())
|
|
}
|
|
print(map[string]any{"operation": "stat", "key": info.Key, "size_bytes": info.Size, "content_type": info.ContentType, "last_modified": info.LastModified.UTC().Format(time.RFC3339Nano)})
|
|
case "list":
|
|
items, err := store.List(ctx, *prefix)
|
|
if err != nil {
|
|
fail("list failed: " + err.Error())
|
|
}
|
|
keys := make([]map[string]any, 0, len(items))
|
|
for _, item := range items {
|
|
keys = append(keys, map[string]any{"key": item.Key, "size_bytes": item.Size, "last_modified": item.LastModified.UTC().Format(time.RFC3339Nano)})
|
|
}
|
|
print(map[string]any{"operation": "list", "bucket": bucket, "prefix": *prefix, "objects": keys})
|
|
case "get":
|
|
if *key == "" {
|
|
fail("-op get requires -key")
|
|
}
|
|
body, info, err := store.Get(ctx, *key)
|
|
if err != nil {
|
|
fail("get failed: " + err.Error())
|
|
}
|
|
defer body.Close()
|
|
buffer := make([]byte, 32*1024)
|
|
written := int64(0)
|
|
for {
|
|
n, readErr := body.Read(buffer)
|
|
if n > 0 {
|
|
if _, err := os.Stdout.Write(buffer[:n]); err != nil {
|
|
fail("write stdout failed")
|
|
}
|
|
written += int64(n)
|
|
}
|
|
if readErr != nil {
|
|
break
|
|
}
|
|
}
|
|
fmt.Fprintf(os.Stderr, "s3ctl: read %d of %d bytes\n", written, info.Size)
|
|
default:
|
|
fail("-op must be one of put, get, stat, delete or list")
|
|
}
|
|
}
|
|
|
|
func print(value any) {
|
|
encoded, _ := json.Marshal(value)
|
|
fmt.Println(string(encoded))
|
|
}
|
|
|
|
func fail(message string) {
|
|
fmt.Fprintln(os.Stderr, "s3ctl:", message)
|
|
os.Exit(1)
|
|
}
|