Skip to content

Commit

Permalink
Merge pull request #11 from Cambio-Project/add-prometheus-authentication
Browse files Browse the repository at this point in the history
Add prometheus authentication
  • Loading branch information
julianbrott authored Jan 23, 2024
2 parents ac48783 + 1cb7678 commit 5b9bb7e
Show file tree
Hide file tree
Showing 11 changed files with 619 additions and 298 deletions.
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,19 @@ services:
ports:
- 8080:80

proxy:
build:
context: ./proxy
dockerfile: Dockerfile
network: host
ports:
- 3000:3000
networks:
- dashboardNetwork
depends_on:
- dashboard
extra_hosts:
- host.docker.internal:host-gateway

networks:
dashboardNetwork:
20 changes: 20 additions & 0 deletions proxy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use an official Node runtime as a parent image
FROM node:latest

# Set the working directory in the container
WORKDIR /dist/src/app

# Copy the current directory contents into the container at /usr/src/app
COPY . .

RUN npm cache clean --force

# Copy files from local machine to virtual directory in docker image
COPY . .
RUN npm install

# Make port 3000 available to the world outside this container
EXPOSE 3000

# Run prometheus.proxy.js when the container launches
CMD ["node", "prometheus.proxy.js", "--docker"]
12 changes: 12 additions & 0 deletions proxy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "proxy",
"version": "0.0.0",
"scripts": {},
"private": true,
"dependencies": {
"axios": "^1.6.5",
"cors": "^2.8.5",
"express": "^4.18.2"
},
"devDependencies": { }
}
66 changes: 66 additions & 0 deletions proxy/prometheus.proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const app = express();
const port = 3000;

// handle arguments
const showHelp = () => {
console.log('Usage: node proxy.js [--docker]');
console.log(' --docker: proxy to host.docker.internal instead of localhost');
process.exit(1);
}
if (process.argv.indexOf('-h') > -1 || process.argv.indexOf('--help') > -1) {
showHelp();
}
const isDocker = process.argv.indexOf('--docker') > -1;

// Enable CORS for all routes
app.use(cors());
app.use(express.json());

// Proxy endpoint
app.all('/proxy', async (req, res) => {
// Extracting target URL and basic auth credentials from headers
let targetUrl;
if (isDocker) {
targetUrl = req.headers['x-target-url'] = req.headers['x-target-url'].replace('localhost', 'host.docker.internal');
}
else {
targetUrl = req.headers['x-target-url'];
}

const authHeader = req.headers['authorization']; // Basic Auth Header

if (!targetUrl) {
return res.status(400).send({error: 'Target URL is required'});
}

console.log(`Proxying request to ${targetUrl}`);

try {
// Forwarding the request to the target URL
const response = await axios({
method: req.method,
url: targetUrl,
data: req.body,
headers: { ...req.headers, 'Authorization': authHeader },
responseType: 'stream'
});

// Sending back the response from the target URL
response.data.pipe(res);
} catch (error) {
// Handling errors
if (error.response) {
// Forwarding the error response from the target server
return res.status(error.response.status).sendStatus(error.response.status);
} else {
return res.status(500).send({ error: error.message });
}
}
});

app.listen(port, () => {
console.log(`Proxy server running at http://localhost:${port} (docker mode: ${isDocker})`);
});
16 changes: 16 additions & 0 deletions src/app/core/services/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { AuthService } from './auth.service';

describe('AuthService', () => {
let service: AuthService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(AuthService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
21 changes: 21 additions & 0 deletions src/app/core/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable } from '@angular/core';

@Injectable({
providedIn: 'root'
})
export class AuthService {
private sessionKey = 'auth-credentials';

cacheCredentials(username: string, password: string): void {
const encodedCredentials = btoa(`${username}:${password}`);
sessionStorage.setItem(this.sessionKey, encodedCredentials);
}

getCredentials(): string | null {
return sessionStorage.getItem(this.sessionKey);
}

clearCredentials(): void {
sessionStorage.removeItem(this.sessionKey);
}
}
Loading

0 comments on commit 5b9bb7e

Please sign in to comment.