Skip to content

Commit 2e1a425

Browse files
committed
feat: implement app authentication | ops-2905
1 parent b7bb40a commit 2e1a425

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

.talismanrc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
fileignoreconfig:
2+
- filename: README.md
3+
checksum: 2779a70f5ec6f2712c8f54a7dc7bee58311474a55390c4a87c60bc861c70ef34
24
- filename: app.py
3-
checksum: 31c4fd4cf73838c756706736591971ffd4ea63de9c40b5717b57e640076df13b
4-
version: "2.0"
5+
checksum: 91c3d92e2dd0753d547083695b095a049a2709314bd906c968b7222074179693
6+
version: "3.0"

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ docker build -t github-cctray:latest .
3434
* Launch the Docker container
3535

3636
```bash
37-
docker run -p 8000:8000 -e GITHUB_TOKEN="<your_token>" github-cctray:latest
37+
docker run -p 8000:8000 \
38+
-e GITHUB_TOKEN="<your_token>" \
39+
-e BASIC_AUTH_USERNAME="<your_username>" \
40+
-e BASIC_AUTH_PASSWORD="<your_password>" \
41+
github-cctray:latest
3842
```
3943

4044
# Usage
@@ -132,6 +136,8 @@ pip install -r requirements.txt
132136

133137
```bash
134138
export GITHUB_TOKEN=<token>
139+
export BASIC_AUTH_USERNAME=<user>
140+
export BASIC_AUTH_PASSWORD=<pass>
135141
python app.py
136142
```
137143

app.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
import xml.etree.ElementTree as ET
77
import requests
88
from flask import Flask, request, make_response
9+
from flask_basicauth import BasicAuth
910

1011
logging.basicConfig(level=logging.INFO)
1112
logger = logging.getLogger(__name__)
1213

1314
app = Flask('github-cctray')
15+
app.config['BASIC_AUTH_USERNAME'] = os.environ.get("BASIC_AUTH_USERNAME")
16+
app.config['BASIC_AUTH_PASSWORD'] = os.environ.get("BASIC_AUTH_PASSWORD")
17+
18+
basic_auth = BasicAuth(app)
1419

1520
API_BASE_URL = "https://api.github.com"
1621
MAX_WORKERS = 10
@@ -88,12 +93,14 @@ def get_all_workflow_runs(owner, repo, token):
8893

8994

9095
@app.route('/')
96+
@basic_auth.required
9197
def index():
9298
"""Endpoint for generating the CCTray XML.
9399
94100
Returns:
95101
flask.Response: The XML response containing the project information.
96102
"""
103+
97104
owner = request.args.get("owner") or request.form.get('owner')
98105
repo = request.args.get("repo") or request.form.get('repo')
99106
token = os.environ.get("GITHUB_TOKEN")

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
requests
22
flask
3-
waitress
3+
Flask-BasicAuth
4+
waitress

tests/test_app.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ class TestApp(unittest.TestCase):
1010
def setUp(self):
1111
"""Set up the test environment."""
1212
app.testing = True
13+
app.config['BASIC_AUTH_USERNAME'] = 'user'
14+
app.config['BASIC_AUTH_PASSWORD'] = 'pass'
1315
self.client = app.test_client()
16+
self.headers = {
17+
'Authorization': 'Basic dXNlcjpwYXNz'
18+
}
1419

1520
def test_index_missing_parameters(self):
1621
"""Test handling missing parameters in the index route."""
17-
response = self.client.get('/')
22+
response = self.client.get('/', headers=self.headers)
1823
self.assertEqual(response.status_code, 400)
1924
self.assertIn(b"Missing parameter(s)", response.data)
2025

@@ -30,7 +35,7 @@ def test_index_successful_response(self, mock_get_all_workflow_runs):
3035
"html_url": "https://github.com/owner/repo/actions/runs/1234"
3136
}
3237
]
33-
response = self.client.get('/?owner=owner&repo=repo')
38+
response = self.client.get('/?owner=owner&repo=repo', headers=self.headers)
3439
self.assertEqual(response.status_code, 200)
3540
self.assertIn(b'<Project', response.data)
3641
self.assertIn(b'name="repo/CI"', response.data)
@@ -50,7 +55,7 @@ def test_index_unknown_build_status(self, mock_get_all_workflow_runs):
5055
"html_url": "https://github.com/owner/repo/actions/runs/1234"
5156
}
5257
]
53-
response = self.client.get('/?owner=owner&repo=repo')
58+
response = self.client.get('/?owner=owner&repo=repo', headers=self.headers)
5459
self.assertEqual(response.status_code, 200)
5560
self.assertIn(b'<Project', response.data)
5661
self.assertIn(b'lastBuildStatus="Unknown"', response.data)
@@ -59,7 +64,7 @@ def test_index_unknown_build_status(self, mock_get_all_workflow_runs):
5964
def test_index_failed_response(self, mock_get_all_workflow_runs):
6065
"""Test failed response in the index route."""
6166
mock_get_all_workflow_runs.return_value = []
62-
response = self.client.get('/?owner=owner&repo=repo')
67+
response = self.client.get('/?owner=owner&repo=repo', headers=self.headers)
6368
self.assertEqual(response.status_code, 200)
6469
self.assertIn(b'<Projects />', response.data)
6570

0 commit comments

Comments
 (0)