Skip to content

Commit

Permalink
CR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyaug committed Dec 28, 2020
1 parent 4537a33 commit d5172aa
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
11 changes: 10 additions & 1 deletion cmd/lakefs/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/signal"
"syscall"
Expand Down Expand Up @@ -126,6 +127,14 @@ var runCmd = &cobra.Command{
)

// init gateway server
s3Fallback := cfg.GetS3GatewayFallbackURL()
var s3FallbackURL *url.URL
if s3Fallback != "" {
s3FallbackURL, err = url.Parse(s3Fallback)
if err != nil {
logger.WithError(err).Fatal("Failed to parse s3 fallback URL")
}
}
s3gatewayHandler := gateway.NewHandler(
cfg.GetS3GatewayRegion(),
cataloger,
Expand All @@ -135,7 +144,7 @@ var runCmd = &cobra.Command{
cfg.GetS3GatewayDomainName(),
bufferedCollector,
dedupCleaner,
cfg.GetS3GatewayProxyURL(),
s3FallbackURL,
)
ctx, cancelFn := context.WithCancel(context.Background())
go bufferedCollector.Run(ctx)
Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ func (c *Config) GetS3GatewayDomainName() string {
return viper.GetString("gateways.s3.domain_name")
}

func (c *Config) GetS3GatewayProxyURL() string {
return viper.GetString("gateways.s3.proxy_url")
func (c *Config) GetS3GatewayFallbackURL() string {
return viper.GetString("gateways.s3.fallback_url")
}

func (c *Config) GetListenAddress() string {
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ This reference uses `.` to denote the nesting of values.
(`*.s3.local.lakefs.io` always resolves to 127.0.0.1, useful for
local development
* `gateways.s3.region` `(string : "us-east-1")` - AWS region we're pretending to be. Should match the region configuration used in AWS SDK clients
* `gateways.s3.proxy_url` `(string)` - If specified, requests with a non-existing repository will be forwarded to this url.
* `gateways.s3.fallback_url` `(string)` - If specified, requests with a non-existing repository will be forwarded to this url.
* `stats.enabled` `(boolean : true)` - Whether or not to periodically collect anonymous usage statistics
{: .ref-list }

Expand Down
40 changes: 17 additions & 23 deletions gateway/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type ServerContext struct {
authService simulator.GatewayAuthService
stats stats.Collector
dedupCleaner *dedup.Cleaner
proxyURL string
fallbackProxy *gohttputil.ReverseProxy
}

const operationIDNotFound = "not_found_operation"
Expand All @@ -65,7 +65,7 @@ func (c *ServerContext) WithContext(ctx context.Context) *ServerContext {
authService: c.authService,
stats: c.stats,
dedupCleaner: c.dedupCleaner,
proxyURL: c.proxyURL,
fallbackProxy: c.fallbackProxy,
}
}

Expand All @@ -78,8 +78,12 @@ func NewHandler(
bareDomain string,
stats stats.Collector,
dedupCleaner *dedup.Cleaner,
proxyURL string,
fallbackURL *url.URL,
) http.Handler {
var fallbackProxy *gohttputil.ReverseProxy
if fallbackURL != nil {
fallbackProxy = gohttputil.NewSingleHostReverseProxy(fallbackURL)
}
sc := &ServerContext{
ctx: context.Background(),
cataloger: cataloger,
Expand All @@ -90,7 +94,7 @@ func NewHandler(
authService: authService,
stats: stats,
dedupCleaner: dedupCleaner,
proxyURL: proxyURL,
fallbackProxy: fallbackProxy,
}

// setup routes
Expand Down Expand Up @@ -274,17 +278,12 @@ func RepoOperationHandler(sc *ServerContext, repoID string, handler operations.R
// validate repo exists
repo, err := authOp.Cataloger.GetRepository(sc.ctx, repoID)
if errors.Is(err, db.ErrNotFound) {
authOp.Log().WithField("repository", repoID).Warn("the specified repo does not exist")
if sc.proxyURL == "" {
if sc.fallbackProxy == nil {
authOp.Log().WithField("repository", repoID).Warn("the specified repo does not exist")
authOp.EncodeError(gatewayerrors.ErrNoSuchBucket.ToAPIErr())
return
}
proxyURL, err := url.Parse(sc.proxyURL)
if err != nil {
authOp.EncodeError(gatewayerrors.ErrInternalError.ToAPIErr())
return
} else {
sc.fallbackProxy.ServeHTTP(writer, request)
}
gohttputil.NewSingleHostReverseProxy(proxyURL).ServeHTTP(writer, request)
return
}
if err != nil {
Expand Down Expand Up @@ -320,21 +319,16 @@ func PathOperationHandler(sc *ServerContext, repoID, refID, path string, handler
// validate repo exists
repo, err := authOp.Cataloger.GetRepository(sc.ctx, repoID)
if errors.Is(err, db.ErrNotFound) {
authOp.Log().WithField("repository", repoID).Warn("the specified repo does not exist")
if sc.proxyURL == "" {
if sc.fallbackProxy == nil {
authOp.Log().WithField("repository", repoID).Warn("the specified repo does not exist")
authOp.EncodeError(gatewayerrors.ErrNoSuchBucket.ToAPIErr())
return
} else {
sc.fallbackProxy.ServeHTTP(writer, request)
}
proxyURL, err := url.Parse(sc.proxyURL)
if err != nil {
authOp.EncodeError(gatewayerrors.ErrInternalError.ToAPIErr())
return
}
gohttputil.NewSingleHostReverseProxy(proxyURL).ServeHTTP(writer, request)
return
}
if err != nil {
authOp.EncodeError(gatewayerrors.Codes.ToAPIErr(gatewayerrors.ErrInternalError))
authOp.EncodeError(gatewayerrors.ErrInternalError.ToAPIErr())
return
}
// run callback
Expand Down

0 comments on commit d5172aa

Please sign in to comment.