Skip to content

Commit f62d32c

Browse files
authored
Resolve hosts dynamically (#28)
1 parent b3e2b2b commit f62d32c

File tree

4 files changed

+53
-35
lines changed

4 files changed

+53
-35
lines changed

monitor/dist/index.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123879,6 +123879,13 @@ async function run() {
123879123879
core.exportVariable('RUNNER_DEBUG', 1);
123880123880
}
123881123881

123882+
const hosts = new Set();
123883+
hosts.add(process.env.GITHUB_SERVER_URL.split('/')[2].toLowerCase());
123884+
hosts.add(process.env.GITHUB_API_URL.split('/')[2].toLowerCase());
123885+
if (process.env.ACTIONS_ID_TOKEN_REQUEST_URL) {
123886+
hosts.add(process.env.ACTIONS_ID_TOKEN_REQUEST_URL.split('/')[2].toLowerCase());
123887+
}
123888+
123882123889
if (!!core.getState('isPost')) {
123883123890

123884123891
let rootDir = '';
@@ -123906,14 +123913,6 @@ async function run() {
123906123913

123907123914
const results = JSON.parse(`[${data.trim().replace(/\r?\n|\r/g, ',')}]`);
123908123915

123909-
const hosts = new Set();
123910-
hosts.add('api.github.com');
123911-
hosts.add('github.com');
123912-
if (process.env.ACTIONS_ID_TOKEN_REQUEST_URL) {
123913-
const host = process.env.ACTIONS_ID_TOKEN_REQUEST_URL.split('/')[2];
123914-
hosts.add(host.toLowerCase());
123915-
}
123916-
123917123916
let permissions = new Map();
123918123917
for (const result of results) {
123919123918
if (!hosts.has(result.host.toLowerCase()))
@@ -123967,7 +123966,7 @@ async function run() {
123967123966
core.saveState('isPost', true)
123968123967
const { spawn } = __nccwpck_require__(32081);
123969123968

123970-
bashArgs = ['-e', 'setup.sh'];
123969+
bashArgs = ['-e', 'setup.sh', Array.from(hosts).join(",")];
123971123970
if (debug)
123972123971
bashArgs.unshift('-v');
123973123972

monitor/index.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ async function run() {
2929
core.exportVariable('RUNNER_DEBUG', 1);
3030
}
3131

32+
const hosts = new Set();
33+
hosts.add(process.env.GITHUB_SERVER_URL.split('/')[2].toLowerCase());
34+
hosts.add(process.env.GITHUB_API_URL.split('/')[2].toLowerCase());
35+
if (process.env.ACTIONS_ID_TOKEN_REQUEST_URL) {
36+
hosts.add(process.env.ACTIONS_ID_TOKEN_REQUEST_URL.split('/')[2].toLowerCase());
37+
}
38+
3239
if (!!core.getState('isPost')) {
3340

3441
let rootDir = '';
@@ -56,14 +63,6 @@ async function run() {
5663

5764
const results = JSON.parse(`[${data.trim().replace(/\r?\n|\r/g, ',')}]`);
5865

59-
const hosts = new Set();
60-
hosts.add('api.github.com');
61-
hosts.add('github.com');
62-
if (process.env.ACTIONS_ID_TOKEN_REQUEST_URL) {
63-
const host = process.env.ACTIONS_ID_TOKEN_REQUEST_URL.split('/')[2];
64-
hosts.add(host.toLowerCase());
65-
}
66-
6766
let permissions = new Map();
6867
for (const result of results) {
6968
if (!hosts.has(result.host.toLowerCase()))
@@ -117,7 +116,7 @@ async function run() {
117116
core.saveState('isPost', true)
118117
const { spawn } = require('child_process');
119118

120-
bashArgs = ['-e', 'setup.sh'];
119+
bashArgs = ['-e', 'setup.sh', Array.from(hosts).join(",")];
121120
if (debug)
122121
bashArgs.unshift('-v');
123122

monitor/mitm_plugin.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def is_public_repo(self, repo):
3535
return self.repo_map[repo]
3636

3737
repo_path = 'repos' if '/' in repo else 'repositories'
38-
url = f'https://api.github.com/{repo_path}/{repo}'
38+
url = f'{ctx.options.GITHUB_API_URL}/{repo_path}/{repo}'
3939
response = requests.get(url, headers={'Authorization': 'Bearer %s' % ctx.options.token})
4040
if response.status_code == 200:
4141
self.repo_map[repo] = response.json()['private'] == False
@@ -248,11 +248,9 @@ def get_permission(self, path, method, query):
248248
if id == 'issue_number':
249249
url = ''
250250
if path_segments[1] == 'repos':
251-
url = 'https://api.github.com/repos/%s/%s/pulls/%s' % (
252-
path_segments[2], path_segments[3], path_segments[5])
251+
url = f'{ctx.options.GITHUB_API_URL}/repos/{path_segments[2]}/{path_segments[3]}/pulls/{path_segments[5]}'
253252
elif path_segments[1] == 'repositories':
254-
url = 'https://api.github.com/repositories/%s/pulls/%s' % (
255-
path_segments[2], path_segments[4])
253+
url = f'{ctx.options.GITHUB_API_URL}/repositories/{path_segments[2]}/pulls/path_segments[4]'
256254
response = requests.get(
257255
url, headers={'Authorization': 'Bearer %s' % ctx.options.token})
258256
self.log_debug(
@@ -264,11 +262,9 @@ def get_permission(self, path, method, query):
264262
elif id == 'comment_id':
265263
url = ''
266264
if path_segments[1] == 'repos':
267-
url = 'https://api.github.com/repos/%s/%s/issues/comments/%s' % (
268-
path_segments[2], path_segments[3], path_segments[6])
265+
url = f'{ctx.options.GITHUB_API_URL}/repos/{path_segments[2]}/{path_segments[3]}/issues/comments/{path_segments[6]}'
269266
elif path_segments[1] == 'repositories':
270-
url = 'https://api.github.com/repositories/%s/issues/comments/%s' % (
271-
path_segments[2], path_segments[5])
267+
url = f'{ctx.options.GITHUB_API_URL}/repositories/{path_segments[2]}/issues/comments/{path_segments[5]}'
272268
response = requests.get(
273269
url, headers={'Authorization': 'Bearer %s' % ctx.options.token})
274270
self.log_debug(
@@ -285,11 +281,9 @@ def get_permission(self, path, method, query):
285281
elif id == 'event_id':
286282
url = ''
287283
if path_segments[1] == 'repos':
288-
url = 'https://api.github.com/repos/%s/%s/issues/events/%s' % (
289-
path_segments[2], path_segments[3], path_segments[6])
284+
url = f'{ctx.options.GITHUB_API_URL}/repos/{path_segments[2]}/{path_segments[3]}/issues/events/{path_segments[6]}'
290285
elif path_segments[1] == 'repositories':
291-
url = 'https://api.github.com/repositories/%s/issues/events/%s' % (
292-
path_segments[2], path_segments[5])
286+
url = f'{ctx.options.GITHUB_API_URL}/repositories/{path_segments[2]}/issues/events/{path_segments[5]}'
293287
response = requests.get(
294288
url, headers={'Authorization': 'Bearer %s' % ctx.options.token})
295289
self.log_debug(
@@ -451,6 +445,12 @@ def load(self, loader):
451445
default='',
452446
help='Comma delimited list of hosts to monitor',
453447
)
448+
loader.add_option(
449+
name='GITHUB_API_URL',
450+
typespec=str,
451+
default='',
452+
help='GITHUB_API_URL environment variable',
453+
)
454454

455455
def log_debug(self, msg):
456456
if ctx.options.debug:
@@ -487,6 +487,10 @@ def configure(self, updates):
487487
print('error: GITHUB_REPOSITORY is empty')
488488
sys.exit(1)
489489

490+
if not bool(ctx.options.GITHUB_API_URL):
491+
print('error: GITHUB_API_URL is empty')
492+
sys.exit(1)
493+
490494
self.id_token_request_url = None
491495
if bool(ctx.options.ACTIONS_ID_TOKEN_REQUEST_URL):
492496
self.id_token_request_url = urlsplit(ctx.options.ACTIONS_ID_TOKEN_REQUEST_URL)

monitor/setup.sh

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
set -e
44

5+
# build the filter regex for mitmproxy --allow-hosts
6+
filter='\b('
7+
first=true
8+
IFS=',' read -ra args <<< "$@"
9+
for arg in "${args[@]}"; do
10+
if [ "$first" = true ] ; then
11+
first=false
12+
else
13+
filter+='|'
14+
fi
15+
filter+=${arg//./\\.}
16+
done
17+
filter+=')(:\d+)?|$'
18+
519
if [ "$RUNNER_OS" = "macOS" ]; then
620

721
echo "runner ALL=(ALL) NOPASSWD: ALL" | sudo tee -a /etc/sudoers
@@ -65,19 +79,20 @@ if [ "$RUNNER_OS" = "macOS" ]; then
6579
sudo -u mitmproxyuser -H bash -e -c "cd /Users/mitmproxyuser && /Users/mitmproxyuser/mitmproxy/venv/bin/mitmdump \
6680
--mode transparent \
6781
--showhost \
68-
--allow-hosts '\bgithub\.com(:\d+)$' \
82+
--allow-hosts '$filter' \
6983
-q \
7084
`#--set termlog_verbosity=debug` \
7185
`#--set proxy_debug=true` \
7286
-s /Users/mitmproxyuser/mitm_plugin.py \
7387
--set output='/Users/mitmproxyuser/out.txt' \
7488
--set token='$INPUT_TOKEN' \
75-
--set hosts='api.github.com,github.com' \
89+
--set hosts=$@ \
7690
--set debug='$RUNNER_DEBUG' \
7791
--set ACTIONS_ID_TOKEN_REQUEST_URL='$ACTIONS_ID_TOKEN_REQUEST_URL' \
7892
--set ACTIONS_ID_TOKEN_REQUEST_TOKEN='$ACTIONS_ID_TOKEN_REQUEST_TOKEN' \
7993
--set GITHUB_REPOSITORY_ID='$GITHUB_REPOSITORY_ID' \
8094
--set GITHUB_REPOSITORY='$GITHUB_REPOSITORY' \
95+
--set GITHUB_API_URL='$GITHUB_API_URL' \
8196
&"
8297
# >>/Users/mitmproxyuser/out.txt 2>&1
8398

@@ -118,19 +133,20 @@ elif [ "$RUNNER_OS" = "Linux" ]; then
118133
/home/mitmproxyuser/mitmproxy/venv/bin/mitmdump \
119134
--mode transparent \
120135
--showhost \
121-
--allow-hosts '\bgithub\.com(:\d+)$' \
136+
--allow-hosts '$filter' \
122137
-q \
123138
`#--set termlog_verbosity=debug` \
124139
`#--set proxy_debug=true` \
125140
-s /home/mitmproxyuser/mitm_plugin.py \
126141
--set output='/home/mitmproxyuser/out.txt' \
127142
--set token='$INPUT_TOKEN' \
128-
--set hosts='api.github.com,github.com' \
143+
--set hosts=$@ \
129144
--set debug='$RUNNER_DEBUG' \
130145
--set ACTIONS_ID_TOKEN_REQUEST_URL='$ACTIONS_ID_TOKEN_REQUEST_URL' \
131146
--set ACTIONS_ID_TOKEN_REQUEST_TOKEN='$ACTIONS_ID_TOKEN_REQUEST_TOKEN' \
132147
--set GITHUB_REPOSITORY_ID='$GITHUB_REPOSITORY_ID' \
133148
--set GITHUB_REPOSITORY='$GITHUB_REPOSITORY' \
149+
--set GITHUB_API_URL='$GITHUB_API_URL' \
134150
&"
135151
# >>/home/mitmproxyuser/out.txt 2>&1
136152

0 commit comments

Comments
 (0)