Skip to content

Commit

Permalink
feat: 500 lock if crunch file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
dskart committed May 24, 2024
1 parent efa7cc5 commit 501331c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 43 deletions.
25 changes: 16 additions & 9 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net/http"
"time"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/project-n-oss/sidekick/app/aws"
"go.uber.org/zap"
"golang.org/x/oauth2"
Expand All @@ -17,29 +19,40 @@ type App struct {

standardHttpClient *http.Client
gcpHttpClient *http.Client
s3Client *s3.Client
}

func New(ctx context.Context, logger *zap.Logger, cfg Config) (*App, error) {
if err := cfg.Validate(); err != nil {
return nil, err
}

var gcpHttpClient http.Client
standardHttpClient := http.Client{
Timeout: time.Duration(90) * time.Second,
}

ret := &App{
cfg: cfg,
logger: logger,
standardHttpClient: &standardHttpClient,
}

switch cfg.CloudPlatform {
case AwsCloudPlatform.String():
aws.RefreshCredentialsPeriodically(ctx, logger)
awsConfig, err := config.LoadDefaultConfig(ctx)
if err != nil {
return nil, err
}
ret.s3Client = s3.NewFromConfig(awsConfig)

case GcpCloudPlatform.String():
creds, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/devstorage.read_write")
if err != nil {
return nil, err
}
ts := oauth2.TokenSource(creds.TokenSource)
gcpHttpClient = http.Client{
ret.gcpHttpClient = &http.Client{
Timeout: time.Duration(90) * time.Second,
Transport: &oauth2.Transport{
Base: http.DefaultTransport,
Expand All @@ -49,13 +62,7 @@ func New(ctx context.Context, logger *zap.Logger, cfg Config) (*App, error) {
}

logger.Sugar().Infof("Cloud Platform: %s, CrunchErr: %v", cfg.CloudPlatform, !cfg.NoCrunchErr)
return &App{
cfg: cfg,
logger: logger,

standardHttpClient: &standardHttpClient,
gcpHttpClient: &gcpHttpClient,
}, nil
return ret, nil
}

func (a *App) Close(ctx context.Context) error {
Expand Down
53 changes: 19 additions & 34 deletions app/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import (
"net/http"
"strings"

"github.com/project-n-oss/sidekick/app/aws"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
sidekickAws "github.com/project-n-oss/sidekick/app/aws"
)

func statusCodeIs2xx(statusCode int) bool {
return statusCode >= 200 && statusCode < 300
}

// DoRequest makes a request to the cloud platform
// Does a request to the source bucket and if it returns 404, tries the crunched bucket
// Returns the response and a boolean indicating if the response is from the crunched bucket
Expand All @@ -27,15 +25,16 @@ func (sess *Session) DoRequest(req *http.Request) (*http.Response, bool, error)
const crunchFileFoundErrStatus = "500 Src file not found, but crunched file found"

// DoAwsRequest makes a request to AWS
// Does a request to the source bucket and if it returns 404, tries the crunched bucket
// Returns the response and a boolean indicating if the response is from the crunched bucket
// If a crunched version of the source file exists, returns a 500 response
// Returns the response and a boolean indicating if a crunched file was found
// You can disable this behavior by setting NoCrunchErr to true in the config
func (sess *Session) DoAwsRequest(req *http.Request) (*http.Response, bool, error) {
sourceBucket, err := aws.ExtractSourceBucket(req)
sourceBucket, err := sidekickAws.ExtractSourceBucket(req)
if err != nil {
return nil, false, fmt.Errorf("failed to extract source bucket from request: %w", err)
}

cloudRequest, err := aws.NewRequest(sess.Context(), sess.Logger(), req, sourceBucket)
cloudRequest, err := sidekickAws.NewRequest(sess.Context(), sess.Logger(), req, sourceBucket)
if err != nil {
return nil, false, fmt.Errorf("failed to make aws request: %w", err)
}
Expand All @@ -45,34 +44,20 @@ func (sess *Session) DoAwsRequest(req *http.Request) (*http.Response, bool, erro
return nil, false, fmt.Errorf("failed to do aws request: %w", err)
}

statusCode := -1
if resp != nil {
statusCode = resp.StatusCode
}

if statusCode == 404 && !isCrunchedFile(req.URL.Path) && !sess.app.cfg.NoCrunchErr {
crunchedFilePath := makeCrunchFilePath(req.URL.Path)
crunchedRequest, err := aws.NewRequest(sess.Context(), sess.Logger(), req, sourceBucket, aws.WithPath(crunchedFilePath))
if err != nil {
return nil, false, fmt.Errorf("failed to make aws request: %w", err)
}

resp, err := http.DefaultClient.Do(crunchedRequest)
if err != nil {
return nil, false, fmt.Errorf("failed to do crunched aws request: %w", err)
}
crunchedStatusCode := -1
if resp != nil {
crunchedStatusCode = resp.StatusCode
}

// return 500 to client if there is a crunch version of the file
if statusCodeIs2xx(crunchedStatusCode) {
// if the source file is not already a crunched file, check if the crunched file exists
if !sess.app.cfg.NoCrunchErr && !isCrunchedFile(cloudRequest.URL.Path) {
objectKey := makeCrunchFilePath(cloudRequest.URL.Path)
// ignore errors, we only want to check if the object exists
headResp, _ := sess.app.s3Client.HeadObject(sess.Context(), &s3.HeadObjectInput{
Bucket: aws.String(sourceBucket.Bucket),
Key: aws.String(objectKey),
})
// found crunched file, return 500 to client
if headResp != nil && headResp.ETag != nil {
resp.StatusCode = 500
resp.Status = crunchFileFoundErrStatus
}

return resp, true, err
return resp, true, nil
}

return resp, false, err
Expand Down

0 comments on commit 501331c

Please sign in to comment.