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

feat: create new helper which enables passing in of a key manager #109

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Changes from all commits
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
30 changes: 19 additions & 11 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,12 @@ func combine(f, g func(http.HandlerFunc) http.HandlerFunc) func(http.HandlerFunc
}
}

// GetRouter creates the mux router that serves knox routes.
// All routes are declared in this file. Each handler itself takes in the db and
// auth provider interfaces and returns a handler that the is processed through
// the API Middleware.
func GetRouter(
// GetRouterFromKeyManager creates the mux router that serves knox routes from a key manager
func GetRouterFromKeyManager(
cryptor keydb.Cryptor,
db keydb.DB,
keyManager KeyManager,
decorators [](func(http.HandlerFunc) http.HandlerFunc),
additionalRoutes []Route) (*mux.Router, error) {

existingRouteIds := map[string]Route{}
existingRouteMethodAndPaths := map[string]map[string]Route{}
allRoutes := append(routes[:], additionalRoutes[:]...)
Expand Down Expand Up @@ -103,16 +99,28 @@ func GetRouter(
decorator = combine(decorators[j], decorator)
}

m := NewKeyManager(cryptor, db)

r.NotFoundHandler = setupRoute("404", m)(decorator(WriteErr(errF(knox.NotFoundCode, ""))))
r.NotFoundHandler = setupRoute("404", keyManager)(decorator(WriteErr(errF(knox.NotFoundCode, ""))))

for _, route := range allRoutes {
addRoute(r, route, decorator, m)
addRoute(r, route, decorator, keyManager)
}
return r, nil
}

// GetRouter creates the mux router that serves knox routes.
// All routes are declared in this file. Each handler itself takes in the db and
// auth provider interfaces and returns a handler that the is processed through
// the API Middleware.
func GetRouter(
cryptor keydb.Cryptor,
db keydb.DB,
decorators [](func(http.HandlerFunc) http.HandlerFunc),
additionalRoutes []Route) (*mux.Router, error) {
m := NewKeyManager(cryptor, db)

return GetRouterFromKeyManager(cryptor, m, decorators, additionalRoutes)
}

func addRoute(
router *mux.Router,
route Route,
Expand Down
Loading