-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_package.py
executable file
·129 lines (102 loc) · 3.82 KB
/
check_package.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
#!/usr/bin/env python3
##
# Copyright 2019 Mentor Graphics
# SPDX-License-Identifier: Apache-2.0
import json, time, sys, base64
from pprint import pprint as pp
from time import sleep
import getpass
import getopt
import tempfile
import ssl
import urllib.request
import subprocess
import configparser
config = configparser.ConfigParser()
config.readfp(open('content.cfg'))
def usage():
print("""
Usage:
check_package.py [options]
Description:
Check's a package against defined standards in content.cfg.
Options:
-h, --help display this help and exit
-p, --package [required] name of the tanium package to get
-d, --debug turn on debugging
Example:
./check_sensor.py --package 'Puppet Apply Linux'
""")
def main(argv):
#print(argv)
global loglevel
creds = {}
try:
opts, args = getopt.getopt(argv,"d:h:p",["debug:","help","package="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit(2)
if opt in ('-s', '--package'):
packagename = arg
if opt in ('d', '--debug'):
loglevel = arg
try:
packagename
except NameError:
print("--package parameter required")
usage()
sys.exit(2)
##
# load the JSON object.
with open('package/'+packagename+'.json') as json_data:
package = json.load(json_data)
json_data.close()
failmessage="Package '" + package["name"] + "' failed testing!"
failurecount=0
warnmessage="Package '" + package["name"] + "' has warnings:"
warncount=0
if "files" in package:
for file in package["files"]:
# if 'source' not in file:
# failmessage+="\n\nPackage files must be remote files, not local files."
# failurecount+=1
if 'source' in file and len(file['source']) > 0:
if file["download_seconds"] > int(config.get('package','max_download_seconds')):
failmessage+="\n\nFile (" + file["name"] + ") exceeds max download seconds."
failmessage+="\n Set 'Check for update' to " + config.get('package', 'max_download_seconds') + " seconds or less."
failurecount+=1
try:
context = ssl._create_unverified_context()
response = urllib.request.urlopen(file["source"], context=context)
data = response.read()
except:
failmessage+="\n\nFile source (" + file["source"] + ") is not downloadable."
failurecount+=1
badurl=True
for url in config.get('package','remote_file_urls').split(" "):
if url in file["source"]:
badurl=False
if badurl:
failmessage+="\n\nRemote file url (" + file["source"] + ") is not allowed.\n Remote files must be hosted at one of these locations:"
failmessage+="\n - " + "\n - ".join(config.get('package','remote_file_urls').split(" "))
prefixtest=False
for prefix in config.get('prefix','name').split(","):
if package["name"].startswith(prefix):
prefixtest=True
if not prefixtest:
failmessage+="\n\nTo avoid confusion betwen Siemens DISW developed content and Tanium provided content, please prefix the name with '"
failmessage+=config.get('prefix','name') + "'"
failurecount+=1
if failurecount != 0:
print(failmessage)
sys.exit(failurecount)
if warncount != 0:
f = open('check_package_warnings.log', 'a')
f.write(warnmessage + "\n\n")
f.close()
if __name__ == "__main__":
main(sys.argv[1:])