Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add v2 / previous version support #249

Merged
merged 3 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ import (

// convenient global values
const (
DefaultKeyID = "current"
applicationName, apiBase = "tr1d1um", "api/v3"
DefaultKeyID = "current"
apiVersion = "v3"
prevAPIVersion = "v2"
applicationName = "tr1d1um"
apiBase = "api/" + apiVersion
apiBaseDualVersion = "api/{version:" + apiVersion + "|" + prevAPIVersion + "}"
)

const (
Expand Down Expand Up @@ -141,7 +145,13 @@ func tr1d1um(arguments []string) (exitCode int) {
}
rootRouter.Use(otelmux.Middleware("mainSpan", otelMuxOptions...), candlelight.EchoFirstTraceNodeInfo(tracing.Propagator()))

APIRouter := rootRouter.PathPrefix(fmt.Sprintf("/%s/", apiBase)).Subrouter()
// if we want to support the previous API version, then include it in the
// api base.
urlPrefix := fmt.Sprintf("/%s/", apiBase)
if v.GetBool("previousVersionSupport") {
urlPrefix = fmt.Sprintf("/%s/", apiBaseDualVersion)
}
APIRouter := rootRouter.PathPrefix(urlPrefix).Subrouter()

//
// Webhooks (if not configured, handlers are not set up)
Expand Down
23 changes: 20 additions & 3 deletions primaryHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/go-kit/kit/log"
"github.com/goph/emperror"
"github.com/gorilla/mux"
"github.com/justinas/alice"
"github.com/spf13/viper"
"github.com/xmidt-org/bascule"
Expand Down Expand Up @@ -168,6 +169,9 @@ func authenticationHandler(v *viper.Viper, logger log.Logger, registry xmetrics.
}

authConstructor := basculehttp.NewConstructor(options...)
authConstructorLegacy := basculehttp.NewConstructor(append([]basculehttp.COption{
basculehttp.WithCErrorHTTPResponseFunc(basculehttp.LegacyOnErrorHTTPResponse),
}, options...)...)

bearerRules := bascule.Validators{
bchecks.NonEmptyPrincipal(),
Expand Down Expand Up @@ -208,8 +212,21 @@ func authenticationHandler(v *viper.Viper, logger log.Logger, registry xmetrics.
basculehttp.WithRules("Bearer", bearerRules),
basculehttp.WithEErrorResponseFunc(listener.OnErrorResponse),
)
constructors := []alice.Constructor{setLogger(logger), authConstructor, authEnforcer, basculehttp.NewListenerDecorator(listener)}

chain := alice.New(constructors...)
return &chain, nil
authChain := alice.New(setLogger(logger), authConstructor, authEnforcer, basculehttp.NewListenerDecorator(listener))
authChainLegacy := alice.New(setLogger(logger), authConstructorLegacy, authEnforcer, basculehttp.NewListenerDecorator(listener))

versionCompatibleAuth := alice.New(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(r http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
if vars != nil {
if vars["version"] == prevAPIVersion {
authChainLegacy.Then(next).ServeHTTP(r, req)
return
}
}
authChain.Then(next).ServeHTTP(r, req)
})
})
return &versionCompatibleAuth, nil
}
8 changes: 7 additions & 1 deletion tr1d1um.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,10 @@ tracing:
# skipTraceExport: true

# endpoint is where trace information should be routed. Applies to zipkin and jaegar.
# endpoint: "http://localhost:9411/api/v2/spans"
# endpoint: "http://localhost:9411/api/v2/spans"

# previousVersionSupport allows us to support two different major versions of
# the API at the same time from the same application. When this is true,
# tr1d1um will support both "/v2" and "/v3" endpoints. When false, only "/v3"
# endpoints will be supported.
previousVersionSupport: true