Files
at-container-registry/pkg/hold/service.go
T

120 lines
3.8 KiB
Go

package hold
import (
"context"
"fmt"
"log"
"net/url"
"atcr.io/pkg/auth"
"github.com/aws/aws-sdk-go/service/s3"
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
"github.com/distribution/distribution/v3/registry/storage/driver/factory"
)
// HoldPDSInterface is the minimal interface needed from the embedded PDS
// This avoids a circular import between pkg/hold and pkg/hold/pds
type HoldPDSInterface interface {
DID() string
}
// HoldService provides presigned URLs for blob storage in a hold
type HoldService struct {
driver storagedriver.StorageDriver
config *Config
s3Client *s3.S3 // S3 client for presigned URLs (nil if not S3 storage)
bucket string // S3 bucket name
s3PathPrefix string // S3 path prefix (if any)
MultipartMgr *MultipartManager // Exported for access in route handlers
pds HoldPDSInterface // Embedded PDS for captain/crew records
authorizer auth.HoldAuthorizer // Authorizer for access control
}
// PresignedURLOperation defines the type of presigned URL operation
type PresignedURLOperation string
const (
OperationGet PresignedURLOperation = "GET"
OperationHead PresignedURLOperation = "HEAD"
OperationPut PresignedURLOperation = "PUT"
)
// NewHoldService creates a new hold service
// holdPDS must be a *pds.HoldPDS but we use any to avoid import cycle
func NewHoldService(cfg *Config, holdPDS any) (*HoldService, error) {
// Create storage driver from config
ctx := context.Background()
driver, err := factory.Create(ctx, cfg.Storage.Type(), cfg.Storage.Parameters())
if err != nil {
return nil, fmt.Errorf("failed to create storage driver: %w", err)
}
// Create local authorizer using the embedded PDS
// This requires casting holdPDS to the concrete type expected by auth
authorizer := auth.NewLocalHoldAuthorizerFromInterface(holdPDS)
// Cast to our interface for storage
pdsInterface, ok := holdPDS.(HoldPDSInterface)
if !ok {
return nil, fmt.Errorf("holdPDS must implement HoldPDSInterface")
}
service := &HoldService{
driver: driver,
config: cfg,
MultipartMgr: NewMultipartManager(),
pds: pdsInterface,
authorizer: authorizer,
}
// Initialize S3 client for presigned URLs (if using S3 storage)
if err := service.initS3Client(); err != nil {
log.Printf("WARNING: S3 presigned URLs disabled: %v", err)
}
return service, nil
}
// GetPresignedURL is a public wrapper around getPresignedURL for use by PDS blob store
func (s *HoldService) GetPresignedURL(ctx context.Context, operation PresignedURLOperation, digest string, did string) (string, error) {
return s.getPresignedURL(ctx, operation, digest, did)
}
// isAuthorizedRead checks if the given DID has read access to this hold
// This is a helper wrapper around the authorizer for internal use
func (s *HoldService) isAuthorizedRead(did string) bool {
ctx := context.Background()
allowed, err := s.authorizer.CheckReadAccess(ctx, s.pds.DID(), did)
if err != nil {
log.Printf("Authorization check failed: %v", err)
return false
}
return allowed
}
// isAuthorizedWrite checks if the given DID has write access to this hold
// This is a helper wrapper around the authorizer for internal use
func (s *HoldService) isAuthorizedWrite(did string) bool {
ctx := context.Background()
allowed, err := s.authorizer.CheckWriteAccess(ctx, s.pds.DID(), did)
if err != nil {
log.Printf("Authorization check failed: %v", err)
return false
}
return allowed
}
// extractHostname extracts the hostname from a URL
func extractHostname(urlStr string) (string, error) {
u, err := url.Parse(urlStr)
if err != nil {
return "", err
}
// Remove port if present
hostname := u.Hostname()
if hostname == "" {
return "", fmt.Errorf("no hostname in URL")
}
return hostname, nil
}