Skip to content

Commit 07a5f61

Browse files
committed
updates to readme
1 parent eb99499 commit 07a5f61

File tree

4 files changed

+59
-24
lines changed

4 files changed

+59
-24
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,49 @@ docker run -p 8080:80 --env-file ./.env copilot-metrics-viewer
142142
```
143143
The application will be accessible at http://localhost:8080
144144

145+
## Running with API Proxy
146+
147+
Project can run with an API proxy which hides GitHub tokens and is secure enough to be deployed.
148+
Api Proxy project is in `\api` directory. Vue app makes the calls to `/api/github` which then are proxied to `https://api.github.com` with appropriate bearer token.
149+
150+
Proxy can authenticate user using GitHub App. In order to do that, following environment variables are required:
151+
152+
* `GITHUB_CLIENT_ID` - client Id of the GitHub App registered and installed in the enterprise/org with permissions listed above.
153+
* `GITHUB_CLIENT_SECRET` - client secret of the GitHub App
154+
* `SESSION_SECRET` - random string for securing session state
155+
156+
For local development register `http://localhost:3000/callback` as GH App callback Uri.
157+
For deployed version use the Uri of your app.
158+
159+
To build and run the app with API proxy:
160+
161+
```
162+
docker build -t copilot-metrics-viewer-with-api -f api.Dockerfile .
163+
```
164+
165+
To run:
166+
167+
```
168+
docker run -p 8080:3000 --env-file ./.env copilot-metrics-viewer-api
169+
```
170+
171+
## Azure Deployment
172+
173+
Application can be deployed using [Azure Developer CLI](https://aka.ms/azd) (azd).
174+
175+
Before running `azd up` configure GitHub variables:
176+
177+
```bash
178+
azd env set VUE_APP_SCOPE <organization/enterprise>
179+
# when using organization
180+
azd env set VUE_APP_GITHUB_ORG <org name>
181+
# when using enterprise
182+
azd env set VUE_APP_GITHUB_ENT <ent name>
183+
azd env set VUE_APP_GITHUB_API_URL /api/github
184+
azd env set GITHUB_CLIENT_ID <client id>
185+
azd env set GITHUB_CLIENT_SECRET <client secret>
186+
```
187+
145188
## License
146189

147190
This project is licensed under the terms of the MIT open source license. Please refer to [MIT](./LICENSE.txt) for the full terms.

api/.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Client Id from GitHub App installed on the organization
2-
CLIENT_ID=
2+
GITHUB_CLIENT_ID=
33

44
# Client Secret from GitHub App installed on the organization
5-
CLIENT_SECRET=
5+
GITHUB_CLIENT_SECRET=
66

77
# Secret for the session
88
SESSION_SECRET=

api/server.mjs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from 'path';
44
import axios from 'axios';
55
import { fileURLToPath } from 'url';
66
import session from 'express-session';
7-
import {createProxyMiddleware} from 'http-proxy-middleware';
7+
import { createProxyMiddleware } from 'http-proxy-middleware';
88

99
// Construct __dirname equivalent in ES module scope
1010
const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -22,10 +22,17 @@ app.use(session({
2222

2323
// Middleware to add Authorization header
2424
const authMiddleware = (req, res, next) => {
25-
if (!req.session.token) {
25+
// not ideal but if someone wanted to use hardcoded token on the backend
26+
if (!req.session.token && !process.env.VUE_APP_GITHUB_TOKEN) {
2627
res.status(401).send('Unauthorized');
2728
return;
2829
}
30+
31+
if (process.env.VUE_APP_GITHUB_TOKEN) {
32+
// Use the hardcoded token if it's available
33+
req.session.token = process.env.VUE_APP_GITHUB_TOKEN;
34+
}
35+
2936
req.headers['Authorization'] = `Bearer ${req.session.token}`;
3037
console.log('Added Authorization to:', req.url);
3138
next();
@@ -48,8 +55,8 @@ app.use('/api/github', authMiddleware, githubProxy);
4855

4956
const exchangeCode = async (code) => {
5057
const params = new URLSearchParams({
51-
client_id: process.env.CLIENT_ID,
52-
client_secret: process.env.CLIENT_SECRET,
58+
client_id: process.env.GITHUB_CLIENT_ID,
59+
client_secret: process.env.GITHUB_CLIENT_SECRET,
5360
code: code,
5461
});
5562

@@ -80,7 +87,7 @@ app.get('/login', (req, res) => {
8087
// store the state in the session
8188
req.session.state = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
8289

83-
res.redirect(`https://github.com/login/oauth/authorize?client_id=${process.env.CLIENT_ID}&redirect_uri=${redirectUrl}&state=${req.session.state}`);
90+
res.redirect(`https://github.com/login/oauth/authorize?client_id=${process.env.GITHUB_CLIENT_ID}&redirect_uri=${redirectUrl}&state=${req.session.state}`);
8491
});
8592

8693
app.get('/callback', async (req, res) => {

infra/main.parameters.json

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,17 @@
1111
"copilotMetricsViewerExists": {
1212
"value": "${SERVICE_COPILOT_METRICS_VIEWER_RESOURCE_EXISTS=false}"
1313
},
14-
"gitHubClientId" : {
15-
"value": "${GITHUB_CLIENT_ID}"
16-
},
17-
"gitHubClientSecret" : {
18-
"value": "${GITHUB_CLIENT_SECRET}"
19-
},
20-
"appScope" : {
21-
"value": "${APP_SCOPE}"
22-
},
23-
"gitHubOrg" : {
24-
"value": "${GITHUB_ORG}"
25-
},
26-
"gitHubEnt" : {
27-
"value": "${GITHUB_ENT}"
28-
},
2914
"copilotMetricsViewerDefinition": {
3015
"value": {
3116
"settings": [
3217
{
33-
"name": "CLIENT_ID",
18+
"name": "GITHUB_CLIENT_ID",
3419
"value": "${GITHUB_CLIENT_ID}",
3520
"secret": true,
3621
"_comment_name": "GitHub App Client id - The name of the environment variable when running in Azure."
3722
},
3823
{
39-
"name": "CLIENT_SECRET",
24+
"name": "GITHUB_CLIENT_SECRET",
4025
"value": "${GITHUB_CLIENT_SECRET}",
4126
"secret": true,
4227
"_comment_name": "GitHub App Client Secret - The name of the environment variable when running in Azure."

0 commit comments

Comments
 (0)