Skip to content

Commit

Permalink
Changed getRoute function to work with route IDs to be more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
rorylshanks committed Nov 26, 2023
1 parent 03d1c46 commit 727628a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ jobs:
id: buildx
uses: docker/setup-buildx-action@v1
- name: Login to Docker Hub
if: github.event_name != 'pull_request'
uses: docker/login-action@v1
with:
username: rorylshanks
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ The Dynamic Backend Configuration feature is especially useful for scenarios tha

- [ ] Add device-aware context
- [ ] Add UI for debugging users
- [ ] Fix getRouteForHostname to also match based on path
- [x] Fix getRouteFromRequest to also match based on path
- [ ] Add metrics for monitoring
- [ ] Better logging

Expand Down
4 changes: 2 additions & 2 deletions lib/sso.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Issuer } from 'openid-client';
import { decodeJWT, createJWT } from '../util/jwt.js';
import { getConfig, getRouteForHostname } from '../util/config.js';
import { getConfig, getRouteFromRequest } from '../util/config.js';
import { URL, URLSearchParams } from 'url';
import log from '../util/logging.js'
import authz from './authz.js'
Expand Down Expand Up @@ -30,7 +30,7 @@ async function verifyAuth(req, res) {
var requestUrl = new URL(`${req.get("X-Forwarded-Proto")}://${req.get("X-Forwarded-Host")}${req.get("X-Forwarded-Path") || ""}`)
var currentConfig = getConfig()
// Then get the route-specific configuration for this hostname. Note that currently veriflow only supports per-host routes
var route = getRouteForHostname(requestUrl.hostname)
var route = getRouteFromRequest(req)
if (!route) {
log.warn({ message: "No route found for request", host: requestUrl })
var html = await errorpages.renderErrorPage(404)
Expand Down
11 changes: 7 additions & 4 deletions util/caddyModels.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import axios from 'axios';
import utils from './utils.js';
import errorpage from './errorpage.js'

function saturateRoute(proxyFrom, proxyTo, route, isSecure) {
function saturateRoute(proxyFrom, proxyTo, route, isSecure, routeId) {
var config = getConfig()
var copyHeaders = {
"X-Veriflow-User-Id": [
Expand Down Expand Up @@ -148,6 +148,9 @@ function saturateRoute(proxyFrom, proxyTo, route, isSecure) {
],
"X-Forwarded-Uri": [
"{http.request.uri}"
],
"X-Veriflow-Route-Id": [
routeId
]
}
}
Expand Down Expand Up @@ -197,9 +200,9 @@ function saturateRoute(proxyFrom, proxyTo, route, isSecure) {
function saturateAllRoutesFromConfig(config) {
var renderedRoutes = []
var routes = config.policy
for (var route of routes) {
for (var routeId in routes) {
try {

var route = routes[id]
var fromURL = new URL(route.from)
var toHostname = utils.urlToCaddyUpstream(route.to)
var toURL = new URL(route.to)
Expand All @@ -211,7 +214,7 @@ function saturateAllRoutesFromConfig(config) {
isSecure = true
}
var fromHostname = fromURL.hostname
var saturatedRoute = saturateRoute(fromHostname, toHostname, route, isSecure)
var saturatedRoute = saturateRoute(fromHostname, toHostname, route, isSecure, routeId)
renderedRoutes.push(saturatedRoute)
// log.debug({ "message": "Added route", route })
} catch (error) {
Expand Down
11 changes: 8 additions & 3 deletions util/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ function getConfig() {
return currentConfig
}

function getRouteForHostname(hostname) {
function getRouteFromRequest(req) {
var config = getConfig()
var route = config.policy.find(element => element.from.includes(hostname))
var routeId = req.get("X-Veriflow-Route-Id")
if (!routeId) {
log.error({ message: "No route ID included in request", context: {route, hostname: hostname, numRoutes: config.policy.length}})
return null
}
var route = config.policy[routeId]
if (route) {
return route
} else {
Expand Down Expand Up @@ -85,6 +90,6 @@ export {
reloadConfig,
getConfig,
getIdpConfig,
getRouteForHostname,
getRouteFromRequest,
getUserById
};

0 comments on commit 727628a

Please sign in to comment.