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

Adding auth to upstream servers #90

Merged
merged 4 commits into from
Jun 19, 2024
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
5 changes: 3 additions & 2 deletions src/api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import morgan from "morgan";
import { HttpError, BadRequestError, asyncHandler } from "./utils/asyncHandler";
import { entriesDb } from "./db";
import { reconfigureNGINX } from "./utils/nginx";
import { sanitizeExternal, sanitizeFrom, sanitizeTo } from "./utils/sanitize";
import { sanitizeAuth, sanitizeExternal, sanitizeFrom, sanitizeTo } from "./utils/sanitize";
import { config } from "../config";

function getHttpsApi(dappnodeDomain: string): Express {
Expand All @@ -22,6 +22,7 @@ function getHttpsApi(dappnodeDomain: string): Express {
const from = await sanitizeFrom(req.query.from as string);
const to = sanitizeTo(req.query.to as string);
const external = sanitizeExternal(req.query.external as string); //true if not set, we should swap this, but it left like this for backwards compatibility
const auth = sanitizeAuth(req.query.auth as string);

const entries = entriesDb.read();
if (entries.some((entry) => entry.from === from)) {
Expand All @@ -38,7 +39,7 @@ function getHttpsApi(dappnodeDomain: string): Express {
);
}

entries.push({ from, to, external });
entries.push({ from, to, external, auth });
entriesDb.write(entries);

const reconfigured = await reconfigureNGINX(dappnodeDomain);
Expand Down
7 changes: 7 additions & 0 deletions src/api/utils/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export function sanitizeExternal(external: string): boolean {
return false;
}

export function sanitizeAuth(auth: string): string {
if (!auth) {
return "";
}
return auth;
}

function assertIsSubdomain(subdomain: string): void {
if (subdomain.includes(".")) {
throw Error("Must not be FQDN nor contain any subdomains");
Expand Down
7 changes: 7 additions & 0 deletions src/nginx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ export async function updateServerConfigs(
await ensureValidCert(true);
checkedCert = true;
}
if(mapping.auth.length > 0) {
3alpha marked this conversation as resolved.
Show resolved Hide resolved
console.log(` *-> Adding auth <-*`);
await fs.writeFileSync(
path.join(config.serverConfigDir, `${mapping.from}.${dappnodeDomain}.auth`),
mapping.auth
)
}
await fs.writeFileSync(
path.join(config.serverConfigDir, filename),
await generateServerConfig(mapping, dappnodeDomain)
Expand Down
1 change: 1 addition & 0 deletions src/nginx/templater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export async function generateServerConfig(
domain: `${mapping.from}.${dappnodeDomain}`,
target: mapping.to,
external: mapping.external,
auth: mapping.auth.length > 0
3alpha marked this conversation as resolved.
Show resolved Hide resolved
};
return await ejs.renderFile("./templates/default.ssl.conf.ejs", {
data: data,
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ export interface DomainMapping {
* Indicates whether mapping should made public
*/
external?: boolean;
/**
* Optional auth string
*/
auth?: string;
}
5 changes: 5 additions & 0 deletions templates/default.ssl.conf.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ server {

location / {

<% if(data.auth) { %>
auth_basic "Creds Required";
auth_basic_user_file <%- data.domain %>.auth;
<% } %>

set $backend http://<%-data.target %>;
proxy_pass $backend;
proxy_set_header Host $host;
Expand Down
Loading