-
Notifications
You must be signed in to change notification settings - Fork 0
/
amc.py
163 lines (127 loc) · 5.39 KB
/
amc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
"""
This tool compares the Git author and e-mail address of the latest commit, against a custom white list.
If it found a match, it prints a success message to stdout.
Else, it prints an error message to stderr and terminates with an exit code 1.
This tool isn't meant for OpSec, only for an additional quality gate.
Copyright (C) 2023 worstprgr <adam@seishin.io> GPG Key: key.seishin.io
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import pathlib
import subprocess
import sys
import argparse
import os
class GitLog:
def __init__(self):
self.author_email: str = str
# Checking only the latest commit
self.git_command: list = ['git', 'log', '-1', '--pretty=format:%an %ae']
def get_author_email(self) -> None:
try:
git_response: str = subprocess.check_output(self.git_command, stderr=subprocess.STDOUT, text=True)
except subprocess.CalledProcessError:
print('[ERROR]: No Git installed or no Git repository available')
sys.exit(1)
self.author_email: str = git_response.strip().lower()
class FetchSecretEnvVariable:
def __init__(self):
self.whitelist_env: str = 'WHITELIST_AMC'
def get_env(self) -> list[str]:
env: str = os.environ.get(self.whitelist_env)
if env is None or env == '':
print('Secret environment variable not found')
sys.exit(1)
else:
env: list = env.strip().split('\n')
return env
class FetchWhitelist(FetchSecretEnvVariable):
def __init__(self, wl_path: pathlib.Path, secret_env: bool):
super().__init__()
self.whitelist_file: pathlib.Path = wl_path
self.author_email: list = []
self.ignore_symbol: str = '#'
self.secret_env = secret_env
def fetch(self):
if self.secret_env:
self.parse_whitelist_env()
else:
self.parse_whitelist_file()
def parse_whitelist_env(self) -> None:
self.author_email += self.get_env()
def parse_whitelist_file(self) -> None:
try:
with open(self.whitelist_file, 'r', encoding='utf8') as f:
whitelist: list = f.readlines()
except FileNotFoundError:
print(f'[ERROR]: Can not find "{self.whitelist_file}"', file=sys.stderr)
sys.exit(1)
item: str
for item in whitelist:
if item.startswith('\n') or item.startswith(' ') or item.startswith(self.ignore_symbol):
continue
user_pair = item.strip().lower()
self.author_email.append(user_pair)
class FilePaths:
def __init__(self, path: str = None):
self.whitelist_file: pathlib.Path = self.conv_path('whitelist.amc')
if path:
self.whitelist_file = self.conv_path(path)
@staticmethod
def conv_path(path: str) -> pathlib.Path:
if type(path) is str:
return pathlib.Path(path)
return path
class ArgHandler:
def __init__(self):
# Parser
self.path_help = 'A custom path to the whitelist file. Usage: -p <Your Path>'
self.secret_help = 'Enables the checking via a secret variable, setup in your CI pipeline ' \
'-> README.md for more information'
self.parser = argparse.ArgumentParser()
self.parser.add_argument('-p', '--path', action='store', help=self.path_help)
self.parser.add_argument('-s', '--secret', action='store_true', help=self.secret_help)
# Args
self.args = self.parser.parse_args()
self.custom_path = self.args.path
self.secret = self.args.secret
class Main:
def __init__(self, whitelist_path, secret_env: bool):
# Init
self.fw = FetchWhitelist(whitelist_path, secret_env)
self.gl = GitLog()
self.error: bool = True
self.secret_mode: bool = secret_env
def run_check(self):
# Run actions
self.fw.fetch()
self.gl.get_author_email()
# Data to compare
git_log_author_email: str = self.gl.author_email
whitelist_author_email: list[str] = self.fw.author_email
# Compare author and email address
self.cmp(git_log_author_email, whitelist_author_email)
if not self.error:
print('[OK]: Authors & E-Mail addresses are matching with whitelist')
def cmp(self, from_git: str, from_whitelist: list) -> None or Exception:
if not (from_git in from_whitelist):
if self.secret_mode:
print('[ERROR]: Author(s) and E-Mail addresses not found in whitelist', file=sys.stderr)
else:
print(f'[ERROR]: "{from_git}" not found in whitelist', file=sys.stderr)
sys.exit(1)
self.error = False
if __name__ == '__main__':
ap = ArgHandler()
p = FilePaths(ap.custom_path)
m = Main(p.whitelist_file, ap.secret)
m.run_check()