Skip to content

Commit

Permalink
added v2 endpoint configurable support (#167)
Browse files Browse the repository at this point in the history
* added v2 endpoint configurable support

* small fixes
  • Loading branch information
kristinapathak committed Mar 23, 2022
1 parent 0e55518 commit 490040a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 28 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [v0.6.2]
- Updated spec file and rpkg version macro to be able to choose when the 'v' is included in the version. [#163](https://github.com/xmidt-org/scytale/pull/163)
- Reconfigured the Bascule Logger settings so that the logger isn't overwritten [#166](https://github.com/xmidt-org/scytale/pull/166)
- Added configurable v2 endpoint support. []()

## [v0.6.1]
- Fixed url parsing bug where we were leaving a '/'. [#161](https://github.com/xmidt-org/scytale/pull/161)
Expand Down Expand Up @@ -100,7 +103,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Initial creation.


[Unreleased]: https://github.com/xmidt-org/scytale/compare/v0.6.1...HEAD
[Unreleased]: https://github.com/xmidt-org/scytale/compare/v0.6.2...HEAD
[v0.6.2]: https://github.com/xmidt-org/scytale/compare/v0.6.1...v0.6.2
[v0.6.1]: https://github.com/xmidt-org/scytale/compare/v0.6.0...v0.6.1
[v0.6.0]: https://github.com/xmidt-org/scytale/compare/v0.5.0...v0.6.0
[v0.5.0]: https://github.com/xmidt-org/scytale/compare/v0.4.11...v0.5.0
Expand Down
6 changes: 6 additions & 0 deletions deploy/packaging/scytale_spruce.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,9 @@ tracing:

#authtoken used to make spruce work better for authAcquirer
authToken: (( grab $AUTH_TOKEN || "dXNlcjpwYXNz" ))

# previousVersionSupport allows us to support two different major versions of
# the API at the same time from the same application. When this is true,
# scytale will support both "/v2" and "/v3" endpoints. When false, only "/v3"
# endpoints will be supported.
previousVersionSupport: (( grab $PREV_VERSION_SUPPORT || true ))
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ const (
DefaultKeyID = "current"

applicationName = "scytale"
release = "Developer"
tracingConfigKey = "tracing"
)

Expand Down
60 changes: 35 additions & 25 deletions primaryHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ import (
)

const (
apiBase = "/api/v3"
apiVersion = "v3"
prevAPIVersion = "v2"
apiBase = "api/" + apiVersion
apiBaseDualVersion = "api/{version:" + apiVersion + "|" + prevAPIVersion + "}"

basicAuthConfigKey = "authHeader"
jwtAuthConfigKey = "jwtValidator"
Expand Down Expand Up @@ -92,7 +95,6 @@ func authChain(v *viper.Viper, logger log.Logger, registry xmetrics.Registry) (a
options := []basculehttp.COption{
basculehttp.WithCLogger(getLogger),
basculehttp.WithCErrorResponseFunc(listener.OnErrorResponse),
basculehttp.WithParseURLFunc(basculehttp.CreateRemovePrefixURLFunc(apiBase+"/", basculehttp.DefaultParseURLFunc)),
}
if len(basicAllowed) > 0 {
options = append(options, basculehttp.WithTokenFactory("Basic", basculehttp.BasicTokenFactory(basicAllowed)))
Expand All @@ -113,8 +115,13 @@ func authChain(v *viper.Viper, logger log.Logger, registry xmetrics.Registry) (a
Leeway: jwtVal.Leeway,
}))
}

authConstructor := basculehttp.NewConstructor(options...)
authConstructor := basculehttp.NewConstructor(append([]basculehttp.COption{
basculehttp.WithParseURLFunc(basculehttp.CreateRemovePrefixURLFunc("/"+apiBase+"/", basculehttp.DefaultParseURLFunc)),
}, options...)...)
authConstructorLegacy := basculehttp.NewConstructor(append([]basculehttp.COption{
basculehttp.WithParseURLFunc(basculehttp.CreateRemovePrefixURLFunc("/api/"+prevAPIVersion+"/", basculehttp.DefaultParseURLFunc)),
basculehttp.WithCErrorHTTPResponseFunc(basculehttp.LegacyOnErrorHTTPResponse),
}, options...)...)

bearerRules := bascule.Validators{
bchecks.NonEmptyPrincipal(),
Expand Down Expand Up @@ -157,23 +164,22 @@ func authChain(v *viper.Viper, logger log.Logger, registry xmetrics.Registry) (a
basculehttp.WithEErrorResponseFunc(listener.OnErrorResponse),
)

constructors := []alice.Constructor{setLogger(logger, extractIntendedDestination), authConstructor, authEnforcer, basculehttp.NewListenerDecorator(listener)}

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

// custom logger func that extracts the intended destination of requests
func extractIntendedDestination(kv []interface{}, request *http.Request) []interface{} {
if deviceName := request.Header.Get("X-Webpa-Device-Name"); len(deviceName) > 0 {
return append(kv, "X-Webpa-Device-Name", deviceName)
}

if variables := mux.Vars(request); len(variables) > 0 {
if deviceID := variables["deviceID"]; len(deviceID) > 0 {
return append(kv, "deviceID", deviceID)
}
}
return kv
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
}

// createEndpoints examines the configuration and produces an appropriate fanout.Endpoints, either using the configured
Expand Down Expand Up @@ -296,10 +302,14 @@ func NewPrimaryHandler(logger log.Logger, v *viper.Viper, registry xmetrics.Regi
)
}

var (
router = mux.NewRouter()
sendSubrouter = router.Path(fmt.Sprintf("%s/device", apiBase)).Methods("POST", "PUT").Subrouter()
)
router := mux.NewRouter()
// 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)
}
sendSubrouter := router.Path(fmt.Sprintf("%s/device", urlPrefix)).Methods("POST", "PUT").Subrouter()

otelMuxOptions := []otelmux.Option{
otelmux.WithPropagators(tracing.Propagator()),
Expand Down Expand Up @@ -380,7 +390,7 @@ func NewPrimaryHandler(logger log.Logger, v *viper.Viper, registry xmetrics.Regi
Handler(authChain.Then(sendWRPHandler))

router.Handle(
fmt.Sprintf("%s/device/{deviceID}/stat", apiBase),
fmt.Sprintf("%s/device/{deviceID}/stat", urlPrefix),
authChain.Extend(fanoutChain).Then(
fanout.New(
endpoints,
Expand Down
8 changes: 7 additions & 1 deletion scytale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,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,
# scytale will support both "/v2" and "/v3" endpoints. When false, only "/v3"
# endpoints will be supported.
previousVersionSupport: true

0 comments on commit 490040a

Please sign in to comment.