forked from projectatomic/atomic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatomic_dbus.py
executable file
·296 lines (271 loc) · 10.8 KB
/
atomic_dbus.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/python -Es
import dbus
from time import sleep
import threading
import dbus.service
import dbus.mainloop.glib
import json
from gi.repository import GLib # pylint: disable=no-name-in-module
import slip.dbus.service
from Atomic import Atomic
from Atomic.verify import Verify
from Atomic.storage import Storage
from Atomic.diff import Diff
from Atomic.scan import Scan
from Atomic.ps import Ps
class atomic_dbus(slip.dbus.service.Object):
default_polkit_auth_required = "org.atomic.readwrite"
class Args():
def __init__(self):
self.image = None
self.recurse = False
self.debug = False
self.devices = None
self.driver = None
self.graph = None
self.force = None
self.import_location = None
self.export_location = None
self.compares = []
self.json = False
self.no_files = False
self.names_only = False
self.rpms = False
self.verbose = False
self.scan_targets = []
self.scanner = None
self.scan_type = None
self.list = False
self.rootfs = []
self.all = False
self.images = False
self.containers = False
self.container = False
self.prune = False
self.heading = False
self.truncate = False
def __init__(self, *p, **k):
super(atomic_dbus, self).__init__(*p, **k)
self.atomic = Atomic()
self.tasks = []
self.tasks_lock = threading.Lock()
self.last_token = 0
self.scheduler_thread = threading.Thread(target = self.Scheduler)
self.scheduler_thread.daemon = True
self.scheduler_thread.start()
self.results = dict()
self.results_lock = threading.Lock()
def Scheduler(self):
while True:
current_task = None
with self.tasks_lock:
if(len(self.tasks) > 0):
current_task = self.tasks.pop(0)
if current_task is not None:
result = current_task[1].scan()
with self.results_lock:
self.results[current_task[0]] = result
sleep(1)
def AllocateToken(self):
with self.tasks_lock:
self.last_token += 1
return self.last_token
# The Version method takes in an image name and returns its version
# information
@slip.dbus.polkit.require_auth("org.atomic.read")
@dbus.service.method("org.atomic", in_signature='asb',
out_signature='aa{sv}')
def Version(self, images, recurse=False):
versions = []
for image in images:
args = self.Args()
args.image = image
args.recurse = recurse
self.atomic.set_args(args)
versions.append({"Image": image,
"Version": self.atomic.version()})
return versions
# The Verify method takes in an image name and returns whether or not the
# image should be updated
@slip.dbus.polkit.require_auth("org.atomic.read")
@dbus.service.method("org.atomic", in_signature='as', out_signature='s')
def Verify(self, images):
verifications = []
verify = Verify()
verify.useTTY = False
for image in images:
args = self.Args()
args.image = image
verify.set_args(args)
verifications.append({"Image": image,
"Verification": verify.verify()}) #pylint: disable=no-member
return json.dumps(verifications)
# The StorageReset method deletes all containers and images from a system.
# Resets storage to its initial configuration.
@slip.dbus.polkit.require_auth("org.atomic.readwrite")
@dbus.service.method("org.atomic", in_signature='', out_signature='')
def StorageReset(self):
storage = Storage()
# No arguments are passed for storage_reset function
args = self.Args()
storage.set_args(args)
storage.reset()
# The StorageImport method imports all containers and their associated
# contents from a filesystem directory.
@slip.dbus.polkit.require_auth("org.atomic.readwrite")
@dbus.service.method("org.atomic", in_signature='ss', out_signature='')
def StorageImport(self, graph, import_location):
storage = Storage()
args = self.Args()
args.graph = graph
args.import_location = import_location
storage.set_args(args)
storage.Import()
# The StorageExport method exports all containers and their associated
# contents into a filesystem directory.
@slip.dbus.polkit.require_auth("org.atomic.readwrite")
@dbus.service.method("org.atomic", in_signature='ssb', out_signature='')
def StorageExport(self, graph="/var/lib/docker", export_location="/var/lib/atomic/migrate", force = False):
storage = Storage()
args = self.Args()
args.graph = graph
args.export_location = export_location
args.force = force
storage.set_args(args)
storage.Export()
# The StorageModify method modifies the default storage setup.
@slip.dbus.polkit.require_auth("org.atomic.readwrite")
@dbus.service.method("org.atomic", in_signature='asv', out_signature='')
def StorageModify(self, devices=None, driver = None):
storage = Storage()
args = self.Args()
if devices:
args.devices = devices
else:
args.devices = []
args.driver = driver
storage.set_args(args)
storage.modify()
# The Diff method shows differences between two container images, file
# diff or RPMS.
@slip.dbus.polkit.require_auth("org.atomic.read")
@dbus.service.method("org.atomic", in_signature='ss',
out_signature='s')
def Diff(self, first, second):
diff = Diff()
args = self.Args()
args.compares = [first, second]
args.json = True
args.no_files = False
args.names_only = False
args.rpms = True
args.verbose = True
diff.set_args(args)
return diff.diff()
# The ScanList method will return a list of all scanners.
@slip.dbus.polkit.require_auth("org.atomic.read")
@dbus.service.method("org.atomic", in_signature='',
out_signature= 's')
def ScanList(self):
scan_list = Scan()
args = self.Args()
scan_list.set_args(args)
return scan_list.get_scanners_list()
# The LastScanned method will return the time of the last scan which was done.
@slip.dbus.polkit.require_auth("org.atomic.read")
@dbus.service.method("org.atomic", in_signature='', out_signature='s')
def LastScanned(self):
scan = Scan()
args = self.Args()
scan.set_args(args)
last_scanned = scan.get_last_time_all_scanned()
if last_scanned:
return last_scanned.strftime("%Y-%m-%d %H:%M:%S")
return "Last scan time was not found"
# The ScanSetup method will create the scan object.
def _ScanSetup(self, scan_targets, scanner, scan_type, rootfs, _all, images, containers):
scan = Scan()
args = self.Args()
scan.useTTY = False
if scan_targets:
args.scan_targets = scan_targets
if scanner:
args.scanner = scanner
if scan_type:
args.scan_type = scan_type
if len(scan_targets):
args.scan_targets = scan_targets
args.rootfs = rootfs
args.all = _all
args.images = images
args.containers = containers
scan.set_args(args)
return scan
# The Scan method will return a string.
@slip.dbus.polkit.require_auth("org.atomic.read")
@dbus.service.method("org.atomic", in_signature='asssasbbb',
out_signature= 's')
def Scan(self, scan_targets, scanner, scan_type, rootfs, _all, images, containers):
return self._ScanSetup(scan_targets, scanner, scan_type, rootfs, _all, images, containers).scan()
# The ScheduleScan method will return a token.
@slip.dbus.polkit.require_auth("org.atomic.read")
@dbus.service.method("org.atomic", in_signature='asssasbbb',
out_signature= 'x')
def ScheduleScan(self, scan_targets, scanner, scan_type, rootfs, _all, images, containers):
scan = self._ScanSetup(scan_targets, scanner, scan_type, rootfs, _all, images, containers)
token = self.AllocateToken()
with self.tasks_lock:
self.tasks.append((token, scan))
return token
# The GetScanResults method will determine whether or not the results for
# the token are ready.
@slip.dbus.polkit.require_auth("org.atomic.read")
@dbus.service.method("org.atomic", in_signature='x',
out_signature= 's')
def GetScanResults(self, token):
with self.results_lock:
if token in self.results:
ret = self.results[token]
del self.results[token]
return ret
else:
return ""
# The Update method downloads the latest container image.
@slip.dbus.polkit.require_auth("org.atomic.readwrite")
@dbus.service.method("org.atomic", in_signature='s', out_signature='')
def Update(self, image):
args = self.Args()
args.image = image
self.atomic.set_args(args)
self.atomic.update()
# The Images method will list all installed container images on the system.
@slip.dbus.polkit.require_auth("org.atomic.read")
@dbus.service.method("org.atomic", in_signature='', out_signature='s')
def Images(self):
args = self.Args()
self.atomic.set_args(args)
return json.dumps(self.atomic.images())
# The Vulnerable method will send back information that says
# whether or not an installed container image is vulnerable
@slip.dbus.polkit.require_auth("org.atomic.read")
@dbus.service.method("org.atomic", in_signature='', out_signature='s')
def VulnerableInfo(self):
args = self.Args()
self.atomic.set_args(args)
return self.atomic.get_all_vulnerable_info()
# The Ps method will list all containers on the system.
@slip.dbus.polkit.require_auth("org.atomic.read")
@dbus.service.method("org.atomic", in_signature='', out_signature='s')
def Ps(self):
ps = Ps()
args = self.Args()
ps.set_args(args)
return json.dumps(ps.ps())
if __name__ == "__main__":
mainloop = GLib.MainLoop()
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
system_bus = dbus.SystemBus()
name = dbus.service.BusName("org.atomic", system_bus)
atomic_dbus(system_bus, "/org/atomic/object")
slip.dbus.service.set_mainloop(mainloop)
mainloop.run()