-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Cambio-Project/add-prometheus-authentication
Add prometheus authentication
- Loading branch information
Showing
11 changed files
with
619 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.