cache

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package cache implements GitHub Actions cache protocol backed by S3.

Index

Constants

View Source
const (
	// CacheTokenHeader is the HTTP header used to pass the cache authentication token.
	CacheTokenHeader = "X-Cache-Token"
)

Variables

This section is empty.

Functions

func GenerateCacheToken

func GenerateCacheToken(secret, jobID, instanceID, scope string) string

GenerateCacheToken creates a self-contained HMAC token for cache authentication. Token format: base64url(job_id:instance_id:scope).hmac_hex This allows stateless validation - the server can extract the payload and verify the signature. The scope parameter should be in "org/repo" format for repository-scoped caching.

func ParseCacheToken

func ParseCacheToken(token string) (jobID, instanceID, scope string, ok bool)

ParseCacheToken extracts job_id, instance_id, and scope from a self-contained token. Returns empty strings if token is malformed. Scope may be empty for backwards compatibility with older tokens.

func ScopeFromContext added in v1.0.0

func ScopeFromContext(ctx context.Context) string

ScopeFromContext extracts the cache scope from a request context. Returns empty string if no scope was set.

func ValidateCacheToken

func ValidateCacheToken(secret, token string) bool

ValidateCacheToken verifies that the provided token has a valid HMAC signature. This is stateless - it extracts the payload from the token and recomputes the HMAC.

Types

type AuthMiddleware

type AuthMiddleware struct {
	// contains filtered or unexported fields
}

AuthMiddleware wraps an http.Handler to require cache token authentication. This is completely stateless - no token registration required.

func NewAuthMiddleware

func NewAuthMiddleware(secret string) *AuthMiddleware

NewAuthMiddleware creates authentication middleware for cache endpoints. If secret is empty, authentication is disabled (for backwards compatibility).

func (*AuthMiddleware) IsEnabled

func (m *AuthMiddleware) IsEnabled() bool

IsEnabled returns whether authentication is enabled.

func (*AuthMiddleware) Wrap

func (m *AuthMiddleware) Wrap(next http.Handler) http.Handler

Wrap returns an http.Handler that validates the cache token before calling the next handler. If valid, it extracts the scope from the token and stores it in the request context.

func (*AuthMiddleware) WrapFunc

func (m *AuthMiddleware) WrapFunc(next http.HandlerFunc) http.Handler

WrapFunc is a convenience method for wrapping http.HandlerFunc.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler implements HTTP endpoints for GitHub Actions cache protocol.

func NewHandler

func NewHandler(server *Server) *Handler

NewHandler creates a new cache handler.

func NewHandlerWithAuth

func NewHandlerWithAuth(server *Server, metrics Metrics, cacheSecret string) *Handler

NewHandlerWithAuth creates a new cache handler with authentication and metrics. Authentication is stateless - no token registration required on the server.

func NewHandlerWithMetrics

func NewHandlerWithMetrics(server *Server, metrics Metrics) *Handler

NewHandlerWithMetrics creates a new cache handler with metrics support.

func (*Handler) CommitCacheEntry

func (h *Handler) CommitCacheEntry(w http.ResponseWriter, r *http.Request, cacheID string)

CommitCacheEntry commits a cache entry after upload via PATCH /_apis/artifactcache/caches/{id}.

func (*Handler) DownloadCacheArtifact

func (h *Handler) DownloadCacheArtifact(w http.ResponseWriter, r *http.Request, cacheID string)

DownloadCacheArtifact redirects to pre-signed S3 URL via GET /_artifacts/{id}.

func (*Handler) GetCacheEntry

func (h *Handler) GetCacheEntry(w http.ResponseWriter, r *http.Request)

GetCacheEntry retrieves cache metadata via GET /_apis/artifactcache/cache.

func (*Handler) IsAuthEnabled

func (h *Handler) IsAuthEnabled() bool

IsAuthEnabled returns whether cache authentication is enabled.

func (*Handler) RegisterRoutes

func (h *Handler) RegisterRoutes(mux *http.ServeMux)

RegisterRoutes registers all cache API routes with the HTTP mux. If authentication is enabled, all cache endpoints require a valid token.

func (*Handler) ReserveCacheEntry

func (h *Handler) ReserveCacheEntry(w http.ResponseWriter, r *http.Request)

ReserveCacheEntry reserves a cache entry for upload via POST /_apis/artifactcache/caches.

type Metrics

type Metrics interface {
	PublishCacheHit(ctx context.Context) error
	PublishCacheMiss(ctx context.Context) error
}

Metrics defines the interface for publishing cache metrics.

type PresignAPI

type PresignAPI interface {
	PresignPutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.PresignOptions)) (*v4.PresignedHTTPRequest, error)
	PresignGetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.PresignOptions)) (*v4.PresignedHTTPRequest, error)
}

PresignAPI defines pre-signing operations for S3 URLs.

type S3API

type S3API interface {
	HeadObject(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options)) (*s3.HeadObjectOutput, error)
	ListObjectsV2(ctx context.Context, params *s3.ListObjectsV2Input, optFns ...func(*s3.Options)) (*s3.ListObjectsV2Output, error)
}

S3API defines S3 operations required for cache server.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server implements GitHub Actions cache protocol using S3 backend.

func NewServer

func NewServer(cfg aws.Config, bucketName string) *Server

NewServer creates a new cache server backed by S3.

func NewServerWithClients

func NewServerWithClients(s3Client S3API, presignClient PresignAPI, bucketName string) *Server

NewServerWithClients creates a cache server with custom clients for testing.

func NewServerWithClientsAndScope added in v1.0.0

func NewServerWithClientsAndScope(s3Client S3API, presignClient PresignAPI, bucketName, scope string) *Server

NewServerWithClientsAndScope creates a cache server with custom clients and scope for testing.

func NewServerWithScope added in v1.0.0

func NewServerWithScope(cfg aws.Config, bucketName, scope string) *Server

NewServerWithScope creates a new cache server with repository scoping. Scope should be in format "org/repo" for repo-level isolation or "org" for org-level sharing.

func (*Server) CommitCacheEntry

func (s *Server) CommitCacheEntry(_ context.Context, _ string) error

CommitCacheEntry finalizes the cache entry No-op for S3 backend since uploads are atomic via pre-signed URLs

func (*Server) CreateCacheEntry

func (s *Server) CreateCacheEntry(_ context.Context, key string, version string) (string, error)

CreateCacheEntry prepares for a new cache upload

func (*Server) GeneratePresignedURL

func (s *Server) GeneratePresignedURL(ctx context.Context, key string, method string) (string, error)

GeneratePresignedURL generates a pre-signed URL for uploading or downloading cache artifacts

func (*Server) GetCacheEntry

func (s *Server) GetCacheEntry(ctx context.Context, keys []string, version string) (string, bool, error)

GetCacheEntry checks if a cache entry exists (metadata lookup) GitHub Actions cache protocol uses keys array as primary + restore-keys fallbacks. The first key is treated as an exact match, subsequent keys support prefix matching.

func (*Server) SetScope added in v1.0.0

func (s *Server) SetScope(scope string)

SetScope sets the cache scope for repository isolation.

func (*Server) WithScope added in v1.0.0

func (s *Server) WithScope(scope string) *Server

WithScope returns a shallow copy of the server with the specified scope. This is useful for per-request scoping without modifying the original server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL