-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvmcatcher_subscribe
executable file
·397 lines (367 loc) · 16.1 KB
/
vmcatcher_subscribe
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env python
import sys
if sys.version_info < (2, 4):
print "Your python interpreter is too old. Please consider upgrading."
sys.exit(1)
if sys.version_info < (2, 5):
import site
import os.path
from distutils.sysconfig import get_python_lib
found = False
module_dir = get_python_lib()
for name in os.listdir(module_dir):
lowername = name.lower()
if lowername[0:10] == 'sqlalchemy' and 'egg' in lowername:
sqlalchemy_dir = os.path.join(module_dir, name)
if os.path.isdir(sqlalchemy_dir):
site.addsitedir(sqlalchemy_dir)
found = True
break
if not found:
print "Could not find SQLAlchemy installed."
sys.exit(1)
import vmcatcher.databaseDefinition as model
import os
import re
import logging
import optparse
from smimeX509validation import TrustStore, LoadDirChainOfTrust,smimeX509validation, smimeX509ValidationError
from vmcatcher.__version__ import version
import vmcatcher
import urllib2
import urllib
import hashlib
import datetime
import uuid
from hepixvmitrust.vmitrustlib import VMimageListDecoder as VMimageListDecoder
from hepixvmitrust.vmitrustlib import time_format_definition as time_format_definition
from vmcatcher.vmcatcher_subscribe.stringsort import split_numeric_sort
from vmcatcher.launch import EventObj
from vmcatcher.vmcatcher_subscribe.msgcheck import fileView
from vmcatcher.vmcatcher_subscribe.fatcontroler import db_controler
# User interface
from vmcatcher.listutils import pairsNnot
def main():
log = logging.getLogger("main")
"""Runs program and handles command line options"""
p = optparse.OptionParser(version = "%prog " + version)
p.add_option('-l', '--list', action ='store_true',help='list subscriptions')
p.add_option('-d', '--database', action ='store', help='Database Initiation string')
p.add_option('-s', '--subscribe', action ='append',help='Subscribe to imagelist available at URI', metavar='INPUTURL')
p.add_option('-e', '--auto-endorse', action ='store_true',help='Add endorser in your subscribe to imagelist action.')
p.add_option('-c', '--cert-dir', action ='store',help='Certificate directory.', metavar='INPUTDIR')
p.add_option('-U', '--update', action ='store_true',help='update image metadata')
p.add_option('--image-list', action ='store_true',help='list accepted image UUID for a subscription.', metavar='image UUID')
p.add_option('--image-accept', action ='append',help='accept image UUID in subscription.', metavar='image UUID')
p.add_option('--image-refuse', action ='append',help='refuse image UUID in subscription.', metavar='image UUID')
p.add_option('--imagelist-newimage-accept', action ='store_true',help='Accept new image UUID in subscription.')
p.add_option('--imagelist-newimage-subscribe', action ='store_true',help='Accept and subscribe to new image UUID in subscription.')
p.add_option('--imagelist-newimage-refuse', action ='store_true',help='Refuse new image UUID in subscription. (default)')
p.add_option('--uri-trustanchor-set', action ='store',help='Set trust anchor to authenticate imagelist hoster.', metavar='TRUST ANCHOR')
p.add_option('--uri-username-set', action ='store',help='User name to access imagelist hoster.', metavar='USERNAME')
p.add_option('--uri-username-clear', action ='store_true',help='User name to access imagelist hoster.', metavar='USERNAME')
p.add_option('--uri-password-set', action ='store',help='Password to access imagelist hoster.', metavar='PASSWORD')
p.add_option('--uri-password-clear', action ='store_true',help='Password to access imagelist hoster.', metavar='PASSWORD')
p.add_option('-u', '--uuid', action ='append',help='Select subscription', metavar='UUID')
p.add_option('-r', '--uri', action ='append',help='Select subscription', metavar='URL')
p.add_option('-f', '--format', action ='store',help='Sets the output format')
p.add_option('-D', '--delete', action ='store_true',help='Delete subscription')
p.add_option('-i', '--info', action ='store_true',help='Information on subscription')
p.add_option('-o', '--output', action ='append',help='Export File.', metavar='OUTPUTFILE')
p.add_option('-x', '--execute', action ='store',help='Event application to launch.', metavar='EVENT')
p.add_option('-v', '--verbose', action ='count',help='Change global log level, increasing log output.')
p.add_option('-q', '--quiet', action ='count',help='Change global log level, decreasing log output.')
p.add_option('--log-config', action ='store',help='Logfile configuration file, (overrides command line).', metavar='LOG_CONFIG')
p.add_option('--log-sql-info', action ='store_true',help='Echo all SQL commands.', metavar='LOGFILE')
options, arguments = p.parse_args()
autoEndorse = False
anchor_needed = False
format_needed = False
actions = set([])
decorators = set([])
subscriptions_selected = []
subscription_url_list = []
actionsrequiring_selections = set(['message',
'json',
'delete',
'info',
'image_accept',
'image_list',
'image_refuse',
'uri_trustanchor',
'uri_username',
'uri_password',
'imagelist_newimage'])
outputformats = set(['SMIME','message','lines','json'])
output_format_selected = 'lines'
inputformats = set(['uuid','url'])
input_format_selected = set([])
outputfiles = []
images_aproved = []
images_refused = []
databaseConnectionString = None
eventCmd = None
logFile = None
certDir = None
eventExecutionString = None
debugSqlEcho = False
uri_trustanchor = 0
uri_trustanchor_str = None
uri_trustanchor_str_valid = { 'none' : 0, 'browser' : 1, 'igtf' : 2 }
uri_username = None
uri_password = None
imagelist_newimage = 0
if 'VMCATCHER_RDBMS' in os.environ:
databaseConnectionString = os.environ['VMCATCHER_RDBMS']
if 'VMCATCHER_LOG_CONF' in os.environ:
logFile = os.environ['VMCATCHER_LOG_CONF']
if 'VMCATCHER_DIR_CERT' in os.environ:
certDir = os.environ['VMCATCHER_DIR_CERT']
if 'VMCATCHER_SUB_EVENT' in os.environ:
eventExecutionString = os.environ['VMCATCHER_SUB_EVENT']
if 'VMCATCHER_OUTPUT_FORMAT' in os.environ:
output_format_selected = os.environ['VMCATCHER_OUTPUT_FORMAT']
# Set up log file
LoggingLevel = logging.WARNING
LoggingLevelCounter = 2
if options.verbose:
LoggingLevelCounter = LoggingLevelCounter - options.verbose
if options.verbose == 1:
LoggingLevel = logging.INFO
if options.verbose == 2:
LoggingLevel = logging.DEBUG
if options.quiet:
LoggingLevelCounter = LoggingLevelCounter + options.quiet
if LoggingLevelCounter <= 0:
LoggingLevel = logging.DEBUG
if LoggingLevelCounter == 1:
LoggingLevel = logging.INFO
if LoggingLevelCounter == 2:
LoggingLevel = logging.WARNING
if LoggingLevelCounter == 3:
LoggingLevel = logging.ERROR
if LoggingLevelCounter == 4:
LoggingLevel = logging.FATAL
if LoggingLevelCounter >= 5:
LoggingLevel = logging.CRITICAL
if options.log_config:
logFile = str(options.log_config)
if logFile != None:
if os.path.isfile(logFile):
logging.config.fileConfig(logFile)
else:
logging.basicConfig(level=LoggingLevel)
log = logging.getLogger("main")
log.error("Logfile configuration file '%s' was not found." % (options.log_config))
sys.exit(1)
else:
logging.basicConfig(level=LoggingLevel)
log = logging.getLogger("main")
# Now logging is set up process other options
if options.log_sql_info:
debugSqlEcho = True
if options.cert_dir:
certDir = options.cert_dir
if options.list:
actions.add('list')
#output_format_selected.add('lines')
if options.update:
actions.add('update')
anchor_needed = True
if options.image_list:
actions.add('image_list')
if options.image_accept:
actions.add('image_accept')
images_aproved = options.image_accept
if options.image_refuse:
actions.add('image_refuse')
images_refused = options.image_refuse
if options.subscribe:
anchor_needed = True
actions.add('subscribe')
subscription_url_list = options.subscribe
if options.uuid:
subscriptions_selected = options.uuid
input_format_selected.add('uuid')
if options.uri:
subscriptions_selected = options.uri
input_format_selected.add('url')
if options.format:
if options.format in outputformats:
output_format_selected = options.format
if output_format_selected == "message":
anchor_needed = True
else:
log.error("Invalid format '%s' allowed formats are '%s'" % (options.format,outputformats))
sys.exit(1)
if options.delete:
actions.add('delete')
if options.info:
format_needed = True
actions.add('info')
if options.output:
format_needed = True
outputfiles = options.output
if options.database:
databaseConnectionString = options.database
if options.auto_endorse:
autoEndorse = True
if options.execute:
eventExecutionString = options.execute
if options.uri_trustanchor_set:
# Set Trust anchor
decorators.add('uri_trustanchor')
uri_trustanchor_str = options.uri_trustanchor_set.lower()
if options.uri_username_clear:
# User name to access imagelist hoster.
decorators.add('uri_username')
uri_username = ""
if options.uri_username_set:
# User name to access imagelist hoster.
decorators.add('uri_username')
uri_username = options.uri_username_set
if options.uri_password_clear:
# Password to access imagelist hoster
decorators.add('uri_password')
uri_password = ""
if options.uri_password_set:
# Password to access imagelist hoster
decorators.add('uri_password')
uri_password = options.uri_password_set
if options.imagelist_newimage_accept:
decorators.add('imagelist_newimage')
imagelist_newimage = 1
anchor_needed = True
if options.imagelist_newimage_subscribe:
decorators.add('imagelist_newimage')
imagelist_newimage = 3
anchor_needed = True
if options.imagelist_newimage_refuse:
decorators.add('imagelist_newimage')
imagelist_newimage = 0
anchor_needed = True
# 1 So we have some command line validation
if databaseConnectionString == None:
databaseConnectionString = 'sqlite:///vmcatcher.db'
log.info("Defaulting DB connection to '%s'" % (databaseConnectionString))
if len(actions) + len(decorators) == 0:
log.error("No actions selected")
sys.exit(1)
if len(actions) > 1:
log.error("More than one action selected.")
sys.exit(1)
if format_needed and output_format_selected == 0:
log.error("No output format selected")
sys.exit(1)
# 1.1 Initate DB
database = db_controler(databaseConnectionString,debugSqlEcho)
# 1.2 Set up callbacks
if eventExecutionString != None:
EventInstance = EventObj(eventExecutionString)
database.callbackEventImageNew = EventInstance.eventImageNew
# 2 Initate CA's to manage files
if anchor_needed:
if certDir == None:
certDir = "/etc/grid-security/certificates/"
log.info("Defaulting Certificate directory to '%s'" % (certDir))
if not os.path.isdir(certDir):
log.error('Invalid x509 trust store directory: %s.' % (certDir))
sys.exit(1)
database.setup_trust_anchor(certDir)
# Handle conflicting actions
actions_req_sel = actionsrequiring_selections.intersection(actions)
actions_req_sel_len = len(actions_req_sel)
if actions_req_sel_len == 1:
if len(subscriptions_selected) == 0:
log.error('No subscription selections made.')
sys.exit(1)
if actions_req_sel_len > 1:
log.error('Conflicting functions.')
sys.exit(1)
# Handle decorators
if not 'subscribe' in actions:
decorators_req_sel = actionsrequiring_selections.intersection(decorators)
if len(decorators_req_sel) > 0:
if len(subscriptions_selected) == 0:
log.error('No subscription selections made.')
sys.exit(1)
# Handle conflicting identifiers
selectors_types = inputformats.intersection(input_format_selected)
selectors_types_len = len(selectors_types)
if selectors_types_len > 1:
log.error('Conflicting selectors.')
sys.exit(1)
selector_str = 'uuid'
if selectors_types_len == 1:
selector_str = input_format_selected.pop()
selectorMapper = {'uuid' : 'sub_uuid',
'url' : 'sub_uri',
}
if not selector_str in selectorMapper.keys():
log.error('Invalid selector.')
sys.exit(1)
rc = database.set_selector(selectorMapper[selector_str])
if rc != True:
log.error("Invalid format set : %s" % (selector_str))
sys.exit(1)
if uri_trustanchor_str != None:
if uri_trustanchor_str in uri_trustanchor_str_valid.keys():
uri_trustanchor = uri_trustanchor_str_valid[uri_trustanchor_str]
else:
log.error("Invalid trust anchor value")
log.info("Allowed trust anchors '%s'" % ( "', '".join(uri_trustanchor_str_valid.keys())))
sys.exit(1)
# Handle the output_view
if not output_format_selected in outputformats:
log.error("Invalid format '%s' allowed formats are '%s'" % (output_format_selected,outputformats))
sys.exit(1)
database.setup_view_format(output_format_selected)
# set default return code
rc = 0
if 'subscribe' in actions:
metadata = { "subscription_url_list" : subscription_url_list,
"autoEndorse" : autoEndorse,
}
if 'uri_username' in decorators:
metadata["userName"] = uri_username
if 'uri_password' in decorators:
metadata["password"] = uri_password
if 'imagelist_newimage' in decorators:
metadata["imagelist_newimage"] = imagelist_newimage
if not database.subscriptions_subscribe(metadata):
log.error("Failed to subscribe to '%s'" % (subscription_url_list))
sys.exit(1)
if 'list' in actions:
database.sessions_list()
if 'update' in actions:
rc = database.subscriptions_update()
if 'delete' in actions:
if not database.subscriptions_delete(subscriptions_selected):
log.error("Failed to remove a subscription from '%s'" % (subscriptions_selected))
rc = 1
if 'dump' in actions:
if not 'select' in actions:
log.error('No subscriptions selected.')
database.message_files(subscriptions_selected,outputfiles)
if 'json' in actions:
database.dumpfiles(subscriptions_selected,outputfiles)
if 'info' in actions:
database.subscriptions_info(subscriptions_selected,outputfiles)
if 'image_list' in actions:
database.subscriptions_image_list(subscriptions_selected,outputfiles)
if 'image_accept' in actions:
database.subscriptions_image_accept(subscriptions_selected,images_aproved)
if 'image_refuse' in actions:
database.subscriptions_image_refuse(subscriptions_selected,images_refused)
if 'uri_trustanchor' in decorators:
database.subscriptions_trustanchor_set(subscriptions_selected, uri_trustanchor)
if 'uri_username' in decorators:
database.subscriptions_username_set(subscriptions_selected, uri_username)
if 'uri_password' in decorators:
database.subscriptions_password_set(subscriptions_selected, uri_password)
if 'imagelist_newimage' in decorators:
database.subscriptions_imagelist_newimage_set(subscriptions_selected, imagelist_newimage)
database.subscriptions_update_selected(subscriptions_selected)
sys.exit(rc)
if __name__ == "__main__":
main()