-
Notifications
You must be signed in to change notification settings - Fork 6
/
vmcatcher_endorser
executable file
·243 lines (224 loc) · 9.6 KB
/
vmcatcher_endorser
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
#!/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 logging
import optparse
from smimeX509validation import TrustStore, LoadDirChainOfTrust,smimeX509validation, smimeX509ValidationError
import sys
from vmcatcher.__version__ import version
import vmcatcher
from hepixvmitrust.vmitrustlib import VMimageListDecoder as VMimageListDecoder
from hepixvmitrust.vmitrustlib import time_format_definition as time_format_definition
try:
import json
except:
import simplejson
from vmcatcher.listutils import pairsNnot
# User interface
from vmcatcher.vmcatcher_endorser.controler import db_controler
def main():
"""Runs program and handles command line options"""
p = optparse.OptionParser(version = "%prog " + version)
p.add_option('-l', '--list', action ='store_true',help='List endorsers.')
p.add_option('-d', '--database', action ='store', help='Database Initiation string.')
p.add_option('-s', '--subscription_uuid', action ='append',help='Select subscription by uuid.', metavar='UUID')
p.add_option('-e', '--endorser_uuid', action ='append',help='Select endorser by uuid.', metavar='UUID')
p.add_option('-C', '--create', action ='store_true',help='Create an endorser.')
p.add_option('--dn',action ='append',help='Depricated version of --subject.', metavar='SUBJECT')
p.add_option('-I', '--issuer',action ='append',help="Endorsers certificate issuer's distinguished name.", metavar='ISSUER')
p.add_option('-D', '--delete', action ='store_true',help='Delete an endorser.')
p.add_option('-K', '--links', action ='store_true',help='List endorser subscription links.')
p.add_option('-k', '--link', action ='store_true',help='Add endorser to subscription.')
p.add_option('-u', '--unlink', action ='store_true',help='Remove endorser from Subscription.')
p.add_option('-i', '--info', action ='store_true',help='Information on endoser.')
p.add_option('-f', '--format', action ='store',help='Sets the output format')
p.add_option('-S', '--subject',action ='append',help='Endorsers certificate subject name.', metavar='SUBJECT')
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()
anchor_needed = False
outputformats = set(['SMIME','message','lines','json'])
output_format_selected = "lines"
actions = set([])
endorsers_selected = []
subscriptions_selected = []
subjects_selected = []
issuers_selected = []
actionsrequiring_endorser = set(['create','delete','link','unlink','info'])
actionsrequiring_subscription = set(['link','unlink'])
actionsrequiring_subject = set(['create'])
actionsrequiring_issuer = set(['create'])
databaseConnectionString = None
logFile = None
debugSqlEcho = False
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_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.list:
actions.add('list')
if options.links:
actions.add('links')
if options.endorser_uuid:
endorsers_selected = options.endorser_uuid
if options.subscription_uuid:
subscriptions_selected = options.subscription_uuid
if options.create:
actions.add('create')
if options.delete:
actions.add('delete')
if options.link:
actions.add('link')
if options.unlink:
actions.add('unlink')
if options.info:
actions.add('info')
if options.format:
output_format_selected = options.format
if options.database:
databaseConnectionString = options.database
if options.subject:
log.info("Option '--subject' and '-S' are depricated in favour of '--dn'.")
subjects_selected = options.subject
if options.dn:
subjects_selected = options.dn
if options.issuer:
issuers_selected = options.issuer
# 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) == 0:
log.error("No actions selected")
sys.exit(1)
if len(actions) > 1:
log.error("More than one action selected.")
sys.exit(1)
# 1.1 Initate DB
database = db_controler(databaseConnectionString,debugSqlEcho)
# Handle actions selections beign required.
# Check endorser selected
actions_req_endorser = actionsrequiring_endorser.intersection(actions)
actions_req_endorser_len = len(actions_req_endorser)
if actions_req_endorser_len == 1:
if len(endorsers_selected) == 0:
log.error('No endorsers selected.')
sys.exit(1)
# Check subscription selected
actions_req_subscription = actionsrequiring_subscription.intersection(actions)
actions_req_subscription_len = len(actions_req_subscription)
if actions_req_subscription_len == 1:
if len(subscriptions_selected) == 0:
log.error('No subscriptions selected.')
sys.exit(1)
# Check subject selected
actions_req_subject = actionsrequiring_subject.intersection(actions)
actions_req_subject_len = len(actions_req_subject)
if actions_req_subject_len == 1:
if len(subjects_selected) == 0:
log.error('No subjects selected.')
sys.exit(1)
# Check issuer selected
actions_req_issuer = actionsrequiring_issuer.intersection(actions)
actions_req_issuer_len = len(actions_req_issuer)
if actions_req_issuer_len == 1:
if len(issuers_selected) == 0:
log.error('No issuers selected.')
sys.exit(1)
# Handle conflicting identifiers
if not output_format_selected in outputformats:
log.error("Invalid format '%s' allowed formats are '%s'" % (options.format,outputformats))
sys.exit(1)
database.setup_view_format(output_format_selected)
#database.setup_selector_factory(queryby_uuid)
selector_str = 'uuid'
selectorMapper = {'uuid' : 'endorser_uuid',
}
if not selector_str in selectorMapper.keys():
log.error('Invalid selector.')
sys.exit(1)
rc = database.set_selector(selectorMapper[selector_str])
if 'create' in actions:
if len(endorsers_selected) > 1:
log.error("More than one endorser cannot be created at a time.")
sys.exit(1)
if not database.endorser_create(endorsers_selected[0],subjects_selected,issuers_selected):
sys.exit(12)
if 'list' in actions:
database.endosers_list()
if 'links' in actions:
database.links_list()
if 'link' in actions:
database.link(endorsers_selected,subscriptions_selected)
if 'unlink' in actions:
database.unlink(endorsers_selected,subscriptions_selected)
if 'delete' in actions:
database.endorser_delete(endorsers_selected)
if 'info' in actions:
database.endorsers_info(endorsers_selected)
if __name__ == "__main__":
main()