-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
58 lines (52 loc) · 2.13 KB
/
app.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
import os
import cherrypy
from subprocess import Popen
class admission_webhook:
@cherrypy.expose
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
def validate(self, **keywords):
request_info = cherrypy.request.json
uid = request_info["request"]["uid"]
is_secure = True
insecure_containers = []
for each_image in request_info["request"]["object"]["spec"]["containers"]:
command = [
"trivy",
"image",
"--format",
"json",
"--severity",
os.environ.get("TRIVY_WEBHOOK_SEVERITY", "CRITICAL"),
"--exit-code",
"1",
each_image["image"],
]
if os.environ.get("TRIVY_WEBHOOK_ALLOW_INSECURE_REGISTRIES", "False").lower() == "true":
command.insert(-1, "--insecure")
print("Running command: %s" % " ".join(command))
r = Popen(command)
r.communicate()
if r.returncode == 1:
insecure_containers.append(each_image["image"])
is_secure = False
if is_secure:
return admission_response(True, "All containers are secure", uid)
return admission_response(False, 'Check Failed! These are insecure container images: ' + ', '.join(insecure_containers), uid)
def admission_response(allowed, message, uid):
msg = {
"apiVersion": "admission.k8s.io/v1",
"kind": "AdmissionReview",
"response": {"uid": uid, "allowed": allowed, "status": {"message": message}},
}
return msg
if __name__ == "__main__":
server_config={
"server.socket_host": os.environ.get("TRIVY_WEBHOOK_SSL_IP", "0.0.0.0"),
"server.socket_port": int(os.environ.get("TRIVY_WEBHOOK_SSL_PORT", "443")),
"server.ssl_module": "pyopenssl",
"server.ssl_certificate": os.environ.get("TRIVY_WEBHOOK_SSL_CERT", "/certs/tls.crt"),
"server.ssl_private_key": os.environ.get("TRIVY_WEBHOOK_SSL_KEY", "/certs/tls.key"),
}
cherrypy.config.update(server_config)
cherrypy.quickstart(admission_webhook())