forked from mtusi2026/mtusi2026lab5
Реализована лабораторная работа 5 на оценку 4
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
|
||||
madmin "github.com/minio/madmin-go/v3"
|
||||
)
|
||||
|
||||
// Bootstrap receives root credentials only in the one-shot s3-init process.
|
||||
// The API and cleaner receive a separate user limited to this bucket/prefix.
|
||||
func Bootstrap(ctx context.Context, endpoint, bucket, rootAccess, rootSecret, apiAccess, apiSecret string) error {
|
||||
u, err := url.Parse(endpoint)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
admin, err := madmin.New(u.Host, rootAccess, rootSecret, u.Scheme == "https")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = admin.AddUser(ctx, apiAccess, apiSecret); 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:::" + bucket}},
|
||||
map[string]any{"Effect": "Allow", "Action": []string{"s3:GetObject", "s3:PutObject", "s3:DeleteObject"}, "Resource": []string{"arn:aws:s3:::" + bucket + "/originals/*", "arn:aws:s3:::" + bucket + "/staging/*", "arn:aws:s3:::" + bucket + "/variants/*"}},
|
||||
},
|
||||
}
|
||||
encoded, err := json.Marshal(policy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = admin.AddCannedPolicy(ctx, "messenger-api", encoded); err != nil {
|
||||
return err
|
||||
}
|
||||
return admin.SetPolicy(ctx, "messenger-api", apiAccess, false)
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
Client *minio.Client
|
||||
Bucket string
|
||||
endpoint string
|
||||
}
|
||||
|
||||
func New(endpoint, region, bucket, access, secret string) (*Store, error) {
|
||||
u, err := url.Parse(endpoint)
|
||||
if err != nil || u.Host == "" || (u.Scheme != "http" && u.Scheme != "https") || u.Path != "" {
|
||||
return nil, fmt.Errorf("invalid S3 endpoint")
|
||||
}
|
||||
transport := &http.Transport{
|
||||
DialContext: (&net.Dialer{Timeout: 2 * time.Second, KeepAlive: 30 * time.Second}).DialContext,
|
||||
MaxIdleConns: 16, MaxIdleConnsPerHost: 8, MaxConnsPerHost: 8,
|
||||
ResponseHeaderTimeout: 10 * time.Second, IdleConnTimeout: 30 * time.Second,
|
||||
TLSHandshakeTimeout: 3 * time.Second,
|
||||
}
|
||||
client, err := minio.New(u.Host, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(access, secret, ""), Secure: u.Scheme == "https",
|
||||
Region: region, Transport: transport, MaxRetries: 1,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Store{Client: client, Bucket: bucket, endpoint: endpoint}, nil
|
||||
}
|
||||
|
||||
func (s *Store) Ready(ctx context.Context) error {
|
||||
ok, err := s.Client.BucketExists(ctx, s.Bucket)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !ok {
|
||||
return fmt.Errorf("S3 bucket missing")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) Put(ctx context.Context, key string, r io.Reader, size int64, kind string) error {
|
||||
_, err := s.Client.PutObject(ctx, s.Bucket, key, r, size, minio.PutObjectOptions{
|
||||
ContentType: kind, DisableMultipart: true,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) Get(ctx context.Context, key string) (*minio.Object, error) {
|
||||
object, err := s.Client.GetObject(ctx, s.Bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err = object.Stat(); err != nil {
|
||||
object.Close()
|
||||
return nil, err
|
||||
}
|
||||
return object, nil
|
||||
}
|
||||
|
||||
func (s *Store) Delete(ctx context.Context, key string) error {
|
||||
return s.Client.RemoveObject(ctx, s.Bucket, key, minio.RemoveObjectOptions{})
|
||||
}
|
||||
|
||||
// Audit performs an actual unauthenticated GET and an out-of-scope operation
|
||||
// with API credentials. Neither check prints credentials or object URLs.
|
||||
func (s *Store) Audit(ctx context.Context, key string) error {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", s.endpoint+"/"+s.Bucket+"/"+key, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
response, err := (&http.Client{Timeout: 5 * time.Second}).Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
response.Body.Close()
|
||||
if response.StatusCode != 403 {
|
||||
return fmt.Errorf("anonymous GET returned %d", response.StatusCode)
|
||||
}
|
||||
err = s.Client.MakeBucket(ctx, "forbidden-audit-bucket", minio.MakeBucketOptions{})
|
||||
if minio.ToErrorResponse(err).Code != "AccessDenied" {
|
||||
return fmt.Errorf("API credentials are not restricted: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user