forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
// Package objectstore adapts the S3 protocol to bounded application operations.
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/observation"
|
||||
)
|
||||
|
||||
const operationTimeout = 15 * time.Second
|
||||
|
||||
// Client owns a reusable S3 client and its private bucket scope.
|
||||
type Client struct {
|
||||
tracer observation.Tracer
|
||||
client *minio.Client
|
||||
bucket string
|
||||
}
|
||||
|
||||
// New configures a lazy client with explicit service credentials.
|
||||
func New(cfg *config.ObjectStore) (*Client, error) {
|
||||
client, err := minio.New(cfg.Endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretKey, ""),
|
||||
Secure: cfg.Secure,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Client{client: client, bucket: cfg.Bucket}, nil
|
||||
}
|
||||
|
||||
// Put uses one bounded PUT for an already validated original.
|
||||
func (c *Client) Put(ctx context.Context, key, mime string, reader io.Reader, size int64) (result error) {
|
||||
ctx, end := c.start(ctx, "s3.put")
|
||||
defer func() { end(result) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, operationTimeout)
|
||||
defer cancel()
|
||||
_, err := c.client.PutObject(ctx, c.bucket, key, reader, size, minio.PutObjectOptions{
|
||||
ContentType: mime,
|
||||
DisableMultipart: true,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// Get verifies object availability before handing its bounded stream to the caller.
|
||||
func (c *Client) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
ctx, end := c.start(ctx, "s3.get")
|
||||
ctx, cancel := context.WithTimeout(ctx, operationTimeout)
|
||||
object, err := c.client.GetObject(ctx, c.bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
end(err)
|
||||
cancel()
|
||||
return nil, err
|
||||
}
|
||||
if _, err := object.Stat(); err != nil {
|
||||
end(err)
|
||||
_ = object.Close()
|
||||
cancel()
|
||||
return nil, err
|
||||
}
|
||||
return &stream{ReadCloser: object, cancel: cancel, end: end}, nil
|
||||
}
|
||||
|
||||
type stream struct {
|
||||
io.ReadCloser
|
||||
cancel context.CancelFunc
|
||||
end func(error)
|
||||
}
|
||||
|
||||
// Close releases the connection and its operation deadline.
|
||||
func (s *stream) Close() error {
|
||||
defer s.cancel()
|
||||
err := s.ReadCloser.Close()
|
||||
if s.end != nil {
|
||||
s.end(err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Ping verifies the bucket is accessible with the configured scoped identity.
|
||||
func (c *Client) Ping(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, operationTimeout)
|
||||
defer cancel()
|
||||
exists, err := c.client.BucketExists(ctx, c.bucket)
|
||||
if err == nil && !exists {
|
||||
return fmt.Errorf("private bucket missing")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Exists distinguishes a missing object from a failed dependency.
|
||||
func (c *Client) Exists(ctx context.Context, key string) (bool, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, operationTimeout)
|
||||
defer cancel()
|
||||
_, err := c.client.StatObject(ctx, c.bucket, key, minio.StatObjectOptions{})
|
||||
if minio.ToErrorResponse(err).Code == "NoSuchKey" {
|
||||
return false, nil
|
||||
}
|
||||
return err == nil, err
|
||||
}
|
||||
|
||||
// Remove deletes one explicitly claimed immutable object.
|
||||
func (c *Client) Remove(ctx context.Context, key string) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, operationTimeout)
|
||||
defer cancel()
|
||||
return c.client.RemoveObject(ctx, c.bucket, key, minio.RemoveObjectOptions{})
|
||||
}
|
||||
|
||||
// Bootstrap creates a bucket idempotently; no anonymous policy is installed.
|
||||
func (c *Client) Bootstrap(ctx context.Context) error {
|
||||
exists, err := c.client.BucketExists(ctx, c.bucket)
|
||||
if err != nil || exists {
|
||||
return err
|
||||
}
|
||||
return c.client.MakeBucket(ctx, c.bucket, minio.MakeBucketOptions{})
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
|
||||
)
|
||||
|
||||
// testObjects creates a scoped client against an explicitly isolated S3 server.
|
||||
func testObjects(t *testing.T) *Client {
|
||||
t.Helper()
|
||||
if os.Getenv("S3_ENDPOINT") == "" {
|
||||
t.Skip("isolated S3_ENDPOINT required")
|
||||
}
|
||||
cfg, err := config.NewObjectStore()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
client, err := New(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
// TestOriginalRoundTrip checks real S3 publication, streaming, listing and explicit deletion.
|
||||
func TestOriginalRoundTrip(t *testing.T) {
|
||||
client := testObjects(t)
|
||||
key := "original/" + uuid.NewString()
|
||||
original := []byte("synthetic original bytes")
|
||||
if err := client.Put(t.Context(), key, "application/octet-stream", bytes.NewReader(original), int64(len(original))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if err := client.Remove(context.Background(), key); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
reader, err := client.Get(t.Context(), key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := io.ReadAll(reader)
|
||||
if closeErr := reader.Close(); closeErr != nil {
|
||||
t.Fatal(closeErr)
|
||||
}
|
||||
if err != nil || !bytes.Equal(data, original) {
|
||||
t.Fatalf("original changed: %v", err)
|
||||
}
|
||||
found, err := client.Exists(t.Context(), key)
|
||||
if err != nil || !found {
|
||||
t.Fatalf("existence: %v", err)
|
||||
}
|
||||
entries, err := client.List(t.Context(), "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found = false
|
||||
for _, entry := range entries {
|
||||
found = found || entry.Key == key
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("original absent from inventory")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMissingOriginal checks absence is distinct from unavailable S3.
|
||||
func TestMissingOriginal(t *testing.T) {
|
||||
client := testObjects(t)
|
||||
key := "original/" + uuid.NewString()
|
||||
exists, err := client.Exists(t.Context(), key)
|
||||
if err != nil || exists {
|
||||
t.Fatalf("missing: %v", err)
|
||||
}
|
||||
if reader, err := client.Get(t.Context(), key); err == nil {
|
||||
_ = reader.Close()
|
||||
t.Fatal("missing stream accepted")
|
||||
}
|
||||
}
|
||||
|
||||
// TestUnavailableS3 returns bounded failures instead of converting them into missing objects.
|
||||
func TestUnavailableS3(t *testing.T) {
|
||||
client, err := New(&config.ObjectStore{Endpoint: "127.0.0.1:1", Bucket: "attachments", AccessKey: "test", SecretKey: "test"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 50*time.Millisecond)
|
||||
defer cancel()
|
||||
if err := client.Put(ctx, "original/test", "image/png", bytes.NewReader(nil), 0); err == nil {
|
||||
t.Fatal("unavailable PUT succeeded")
|
||||
}
|
||||
if _, err := client.Get(ctx, "original/test"); err == nil {
|
||||
t.Fatal("unavailable GET succeeded")
|
||||
}
|
||||
if _, err := client.Exists(ctx, "original/test"); err == nil {
|
||||
t.Fatal("unavailable HEAD classified as missing")
|
||||
}
|
||||
if _, err := client.List(ctx, ""); err == nil {
|
||||
t.Fatal("unavailable LIST succeeded")
|
||||
}
|
||||
if err := client.Ping(ctx); err == nil {
|
||||
t.Fatal("unavailable bucket ready")
|
||||
}
|
||||
if err := client.Bootstrap(ctx); err == nil {
|
||||
t.Fatal("unavailable bootstrap succeeded")
|
||||
}
|
||||
}
|
||||
|
||||
// TestInvalidEndpoint rejects malformed S3 configuration before a network request.
|
||||
func TestInvalidEndpoint(t *testing.T) {
|
||||
if _, err := New(&config.ObjectStore{Endpoint: "http://invalid/path"}); err == nil {
|
||||
t.Fatal("invalid endpoint accepted")
|
||||
}
|
||||
}
|
||||
|
||||
// TestBucketBootstrap creates an isolated private bucket twice and verifies scoped credentials cannot access it.
|
||||
func TestBucketBootstrap(t *testing.T) {
|
||||
scoped := testObjects(t)
|
||||
if err := scoped.Ping(t.Context()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg, err := config.NewObjectStore()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
secret := os.Getenv("S3_ADMIN_SECRET")
|
||||
if secret == "" {
|
||||
t.Skip("isolated S3_ADMIN_SECRET required for bootstrap test")
|
||||
}
|
||||
cfg.AccessKey = "operator"
|
||||
cfg.SecretKey = secret
|
||||
cfg.Bucket = "test-" + uuid.NewString()
|
||||
admin, err := New(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if err := admin.client.RemoveBucket(context.Background(), cfg.Bucket); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
if err := admin.Bootstrap(t.Context()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := admin.Bootstrap(t.Context()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
scoped.bucket = cfg.Bucket
|
||||
if _, err := scoped.List(t.Context(), ""); err == nil {
|
||||
t.Fatal("service identity accessed another bucket")
|
||||
}
|
||||
}
|
||||
|
||||
// TestPrivateBucketRejectsAnonymous verifies object ACL independently of application authorization.
|
||||
func TestPrivateBucketRejectsAnonymous(t *testing.T) {
|
||||
client := testObjects(t)
|
||||
key := "original/" + uuid.NewString()
|
||||
if err := client.Put(t.Context(), key, "application/octet-stream", bytes.NewReader([]byte("private")), 7); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if err := client.Remove(context.Background(), key); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
cfg, err := config.NewObjectStore()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
scheme := "https://"
|
||||
if !cfg.Secure {
|
||||
scheme = "http://"
|
||||
}
|
||||
httpClient := &http.Client{Timeout: time.Second}
|
||||
response, err := httpClient.Get(scheme + cfg.Endpoint + "/" + cfg.Bucket + "/" + key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := response.Body.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.StatusCode != http.StatusForbidden {
|
||||
t.Fatalf("anonymous access status=%d", response.StatusCode)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/media"
|
||||
)
|
||||
|
||||
// List bounds each reconciliation page and cancels the SDK producer before returning.
|
||||
func (c *Client) List(ctx context.Context, after string) ([]media.ObjectInfo, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, operationTimeout)
|
||||
defer cancel()
|
||||
result := make([]media.ObjectInfo, 0, 1000)
|
||||
for object := range c.client.ListObjects(ctx, c.bucket, minio.ListObjectsOptions{Prefix: "", StartAfter: after, Recursive: true, MaxKeys: 1000}) {
|
||||
if object.Err != nil {
|
||||
return nil, object.Err
|
||||
}
|
||||
result = append(result, media.ObjectInfo{Key: object.Key, ModifiedAt: object.LastModified})
|
||||
if len(result) == 1000 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/observation"
|
||||
)
|
||||
|
||||
// WithTracer binds optional operation tracing without exposing credentials or object keys.
|
||||
func (c *Client) WithTracer(tracer observation.Tracer) *Client {
|
||||
c.tracer = tracer
|
||||
return c
|
||||
}
|
||||
|
||||
// start records only the stable S3 operation name.
|
||||
func (c *Client) start(ctx context.Context, operation string) (context.Context, func(error)) {
|
||||
if c.tracer != nil {
|
||||
return c.tracer.Start(ctx, operation, "client")
|
||||
}
|
||||
return ctx, func(error) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type tracerFixture struct {
|
||||
operation string
|
||||
finished bool
|
||||
}
|
||||
|
||||
// Start records a stable operation name and closes it through the returned callback.
|
||||
func (f *tracerFixture) Start(ctx context.Context, name, _ string) (context.Context, func(error)) {
|
||||
f.operation = name
|
||||
return ctx, func(error) {
|
||||
f.finished = true
|
||||
}
|
||||
}
|
||||
|
||||
// TestObjectObservationOmitsCoordinates keeps bucket keys and credentials outside the tracer boundary.
|
||||
func TestObjectObservationOmitsCoordinates(t *testing.T) {
|
||||
tracer := &tracerFixture{}
|
||||
client := (&Client{}).WithTracer(tracer)
|
||||
_, finish := client.start(t.Context(), "s3.put")
|
||||
finish(nil)
|
||||
if tracer.operation != "s3.put" || !tracer.finished {
|
||||
t.Fatal("operation span missing")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user