[6cc5c81] | 1 | #!/usr/local/bin/python |
---|
| 2 | |
---|
| 3 | import sys, os |
---|
| 4 | import re |
---|
[87807f42] | 5 | import subprocess |
---|
[d894c21] | 6 | import os.path |
---|
[6cc5c81] | 7 | |
---|
[87807f42] | 8 | from string import join |
---|
[d894c21] | 9 | from optparse import OptionParser, OptionValueError |
---|
| 10 | from tempfile import mkdtemp |
---|
| 11 | |
---|
[6cc5c81] | 12 | from federation.fedid import fedid |
---|
[d894c21] | 13 | from federation.authorizer import abac_authorizer |
---|
[62f3dd9] | 14 | from federation.util import abac_split_cert, abac_pem_type, file_expanding_opts |
---|
[6cc5c81] | 15 | |
---|
[87807f42] | 16 | |
---|
[6cc5c81] | 17 | class attribute: |
---|
[af25848] | 18 | ''' |
---|
[87807f42] | 19 | Encapculate a principal/attribute/link tuple. |
---|
[af25848] | 20 | ''' |
---|
[87807f42] | 21 | bad_attr = re.compile('[^a-zA-Z0-9_]+') |
---|
| 22 | def __init__(self, p, a, l=None): |
---|
[6cc5c81] | 23 | self.principal = p |
---|
[87807f42] | 24 | self.attr = attribute.bad_attr.sub('_', a) |
---|
| 25 | if l: self.link = attribute.bad_attr.sub('_', l) |
---|
| 26 | else: self.link = None |
---|
[6cc5c81] | 27 | |
---|
| 28 | def __str__(self): |
---|
[87807f42] | 29 | if self.link: |
---|
| 30 | return "%s.%s.%s" % (self.principal, self.attr, self.link) |
---|
| 31 | elif self.attr: |
---|
[af25848] | 32 | return "%s.%s" % (self.principal, self.attr) |
---|
| 33 | else: |
---|
| 34 | return "%s" % self.principal |
---|
[6cc5c81] | 35 | |
---|
| 36 | class credential: |
---|
[af25848] | 37 | ''' |
---|
| 38 | A Credential, that is the requisites (as attributes) and the assigned |
---|
| 39 | attribute (as principal, attr). If req is iterable, the requirements are |
---|
| 40 | an intersection/conjunction. |
---|
| 41 | ''' |
---|
[87807f42] | 42 | bad_attr = re.compile('[^a-zA-Z0-9_]+') |
---|
[6cc5c81] | 43 | def __init__(self, p, a, req): |
---|
| 44 | self.principal = p |
---|
| 45 | if isinstance(a, (tuple, list, set)) and len(a) == 1: |
---|
[87807f42] | 46 | self.attr = credential.bad_attr.sub('_', a[0]) |
---|
[6cc5c81] | 47 | else: |
---|
[87807f42] | 48 | self.attr = credential.bad_attr.sub('_', a) |
---|
[6cc5c81] | 49 | self.req = req |
---|
| 50 | |
---|
| 51 | def __str__(self): |
---|
| 52 | if isinstance(self.req, (tuple, list, set)): |
---|
| 53 | return "%s.%s <- %s" % (self.principal, self.attr, |
---|
[87807f42] | 54 | join(["%s" % r for r in self.req], ' & ')) |
---|
[6cc5c81] | 55 | else: |
---|
| 56 | return "%s.%s <- %s" % (self.principal, self.attr, self.req) |
---|
| 57 | |
---|
[353db8c] | 58 | # Mappinng generation function and the access parser throw these when there is |
---|
[af25848] | 59 | # a parsing problem. |
---|
[6cc5c81] | 60 | class parse_error(RuntimeError): pass |
---|
| 61 | |
---|
[87807f42] | 62 | # Error creating a credential |
---|
| 63 | class credential_error(RuntimeError): pass |
---|
| 64 | |
---|
[af25848] | 65 | # Functions to parse the individual access maps as well as an overall function |
---|
| 66 | # to parse generic ones. The specific ones create a credential to local |
---|
| 67 | # attributes mapping and the global one creates the access policy credentials. |
---|
| 68 | |
---|
| 69 | # All the local parsing functions get the unparsed remainder of the line |
---|
| 70 | # (after the three-name and the attribute it maps to), the credential list to |
---|
| 71 | # add the new ABAC credential(s) that will be mapped into the loacl |
---|
| 72 | # credentials, the fedid of this entity, a dict mapping the local credentials |
---|
| 73 | # to ABAC credentials that are required to exercise those local rights and the |
---|
| 74 | # three-name (p, gp, gu) that is being mapped. |
---|
[87807f42] | 75 | def parse_emulab(l, creds, me, to_id, p, gp, gu, lr): |
---|
[af25848] | 76 | ''' |
---|
| 77 | Parse the emulab (project, allocation_user, access_user) format. Access |
---|
| 78 | users are deprecates and allocation users used for both. This fuction |
---|
| 79 | collapses them. |
---|
| 80 | ''' |
---|
[6cc5c81] | 81 | right_side_str = '\s*,\s*\(\s*%s\s*,\s*%s\s*,\s*%s\s*\)' % \ |
---|
| 82 | (id_same_str, id_same_str,id_same_str) |
---|
| 83 | |
---|
| 84 | m = re.match(right_side_str, l) |
---|
| 85 | if m: |
---|
| 86 | project, user = m.group(1,2) |
---|
[af25848] | 87 | # Resolve "<same>"s in project and user |
---|
[6cc5c81] | 88 | if project == '<same>': |
---|
| 89 | if gp is not None: |
---|
| 90 | project = gp |
---|
| 91 | else: |
---|
| 92 | raise parse_error("Project cannot be decisively mapped: %s" % l) |
---|
| 93 | if user == '<same>': |
---|
| 94 | if gu is not None: |
---|
[c324ad3] | 95 | user = gu |
---|
[6cc5c81] | 96 | else: |
---|
| 97 | raise parse_error("User cannot be decisively mapped: %s" % l) |
---|
[af25848] | 98 | |
---|
| 99 | # Create a semi-mnemonic name for the destination credential (the one |
---|
| 100 | # that will be mapped to the local attributes |
---|
[c324ad3] | 101 | if gp and gu: |
---|
| 102 | a = 'project_%s_user_%s' % (gp, gu) |
---|
| 103 | elif gp: |
---|
| 104 | a = 'project_%s' % gp |
---|
| 105 | elif gu: |
---|
| 106 | a = 'user_%s' % gu |
---|
[6cc5c81] | 107 | else: |
---|
| 108 | raise parse_error("No mapping for %s/%s!?" % (gp, gu)) |
---|
| 109 | |
---|
[af25848] | 110 | # Store the creds and map entries |
---|
[6cc5c81] | 111 | c = credential(me, a, |
---|
[87807f42] | 112 | [attribute(p, x, lr) for x in (gp, gu) if x is not None]) |
---|
[6cc5c81] | 113 | creds.add(c) |
---|
[5a721ed] | 114 | if (project, user) in to_id: to_id[(project,user)].append(c) |
---|
| 115 | else: to_id[(project,user)] = [ c ] |
---|
[6cc5c81] | 116 | else: |
---|
| 117 | raise parse_error("Badly formatted local mapping: %s" % l) |
---|
| 118 | |
---|
| 119 | |
---|
[87807f42] | 120 | def parse_protogeni(l, creds, me, to_id, p, gp, gu, lr): |
---|
[af25848] | 121 | ''' |
---|
| 122 | Parse the protoGENI (cert, user, user_key, cert_pw) format. |
---|
| 123 | ''' |
---|
[5a721ed] | 124 | right_side_str = '\s*,\s*\(\s*(%s)\s*,\s*(%s)\s*,\s*(%s)\s*,\s*(%s)\s*\)' \ |
---|
| 125 | % (path_str, id_str, path_str, id_str) |
---|
| 126 | |
---|
| 127 | m = re.match(right_side_str, l) |
---|
| 128 | if m: |
---|
| 129 | cert, user, key, pw = m.group(1,2,3,4) |
---|
[af25848] | 130 | # The credential is formed from just the path (with / mapped to _) and |
---|
| 131 | # the username. |
---|
[5a721ed] | 132 | acert = re.sub('/', '_', cert) |
---|
| 133 | |
---|
| 134 | a = "cert_%s_user_%s" % (acert, user) |
---|
[af25848] | 135 | |
---|
| 136 | # Store em |
---|
[5a721ed] | 137 | c = credential(me, a, |
---|
[87807f42] | 138 | [attribute(p, x, lr) for x in (gp, gu) if x is not None]) |
---|
[5a721ed] | 139 | creds.add(c) |
---|
| 140 | if (cert, user, key, pw) in to_id: |
---|
| 141 | to_id[(cert, user, key, pw)].append(c) |
---|
| 142 | else: |
---|
| 143 | to_id[(cert, user, key, pw)] = [ c ] |
---|
| 144 | else: |
---|
| 145 | raise parse_error("Badly formatted local mapping: %s" % l) |
---|
| 146 | |
---|
[87807f42] | 147 | def parse_dragon(l, creds, me, to_id, p, gp, gu, lr): |
---|
[af25848] | 148 | ''' |
---|
| 149 | Parse the dragon (repository_name) version. |
---|
| 150 | ''' |
---|
[6cc5c81] | 151 | right_side_str = '\s*,\s*\(\s*(%s)\s*\)' % \ |
---|
| 152 | (id_str) |
---|
| 153 | |
---|
| 154 | m = re.match(right_side_str, l) |
---|
| 155 | if m: |
---|
| 156 | repo= m.group(1) |
---|
| 157 | c = credential(me, 'repo_%s' % repo, |
---|
[87807f42] | 158 | [attribute(p, x, lr) for x in (gp, gu) if x is not None]) |
---|
[6cc5c81] | 159 | creds.add(c) |
---|
| 160 | if repo in to_id: to_id[repo].append(c) |
---|
| 161 | else: to_id[repo] = [ c ] |
---|
| 162 | else: |
---|
| 163 | raise parse_error("Badly formatted local mapping: %s" % l) |
---|
[9cce15a] | 164 | |
---|
[87807f42] | 165 | def parse_skel(l, creds, me, to_id, p, gp, gu, lr): |
---|
[af25848] | 166 | ''' |
---|
| 167 | Parse the skeleton (local_attr) version. |
---|
| 168 | ''' |
---|
| 169 | right_side_str = '\s*,\s*\(\s*(%s)\s*\)' % \ |
---|
| 170 | (id_str) |
---|
| 171 | |
---|
| 172 | m = re.match(right_side_str, l) |
---|
| 173 | if m: |
---|
| 174 | lattr = m.group(1) |
---|
| 175 | c = credential(me, 'lattr_%s' % lattr, |
---|
[87807f42] | 176 | [attribute(p, x, lr) for x in (gp, gu) if x is not None]) |
---|
[af25848] | 177 | creds.add(c) |
---|
| 178 | if lattr in to_id: to_id[lattr].append(c) |
---|
| 179 | else: to_id[lattr] = [ c ] |
---|
| 180 | else: |
---|
| 181 | raise parse_error("Badly formatted local mapping: %s" % l) |
---|
[9cce15a] | 182 | |
---|
[af25848] | 183 | # internal plug-ins have no local attributes. |
---|
[87807f42] | 184 | def parse_internal(l, creds, me, to_id, p, gp, gu, lr): pass |
---|
[6cc5c81] | 185 | |
---|
| 186 | |
---|
[d894c21] | 187 | def parse_access(fn, mapper): |
---|
| 188 | """ |
---|
| 189 | Parse the access file, calling out to the mapper to parse specific |
---|
| 190 | credential types. Mappers are above this code. |
---|
| 191 | """ |
---|
| 192 | creds = set() |
---|
| 193 | to_id = { } |
---|
| 194 | f = open(fn, "r") |
---|
| 195 | for i, l in enumerate(f): |
---|
| 196 | try: |
---|
| 197 | if comment_re.match(l): |
---|
| 198 | continue |
---|
| 199 | else: |
---|
| 200 | m = line_re.match(l) |
---|
| 201 | if m: |
---|
| 202 | p, da = m.group(1, 4) |
---|
| 203 | gp, gu = m.group(2, 3) |
---|
| 204 | if gp == '<any>': gp = None |
---|
| 205 | if gu == '<any>': gu = None |
---|
| 206 | |
---|
| 207 | creds.add(credential(me, da, |
---|
| 208 | [attribute(p, x, delegation_link) \ |
---|
| 209 | for x in (gp, gu) \ |
---|
| 210 | if x is not None])) |
---|
| 211 | if m.group(5) and mapper: |
---|
| 212 | mapper(m.group(5), creds, me, to_id, p, gp, gu, |
---|
| 213 | delegation_link) |
---|
| 214 | else: |
---|
| 215 | raise parse_error('Syntax error') |
---|
| 216 | except parse_error, e: |
---|
| 217 | f.close() |
---|
| 218 | raise parse_error('Error on line %d of %s: %s' % \ |
---|
| 219 | (i, fn, e.message)) |
---|
| 220 | f.close() |
---|
| 221 | |
---|
| 222 | return creds, to_id |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | |
---|
[62f3dd9] | 226 | class access_opts(file_expanding_opts): |
---|
[af25848] | 227 | ''' |
---|
| 228 | Parse the options for this program. Most are straightforward, but the |
---|
| 229 | mapper uses a callback to convert from a string to a local mapper function. |
---|
| 230 | ''' |
---|
| 231 | # Valid mappers |
---|
[9cce15a] | 232 | mappers = { |
---|
| 233 | 'emulab': parse_emulab, |
---|
| 234 | 'dragon': parse_dragon, |
---|
| 235 | 'internal': parse_internal, |
---|
| 236 | 'skel': parse_skel, |
---|
[5a721ed] | 237 | 'protogeni': parse_protogeni, |
---|
[9cce15a] | 238 | } |
---|
[6cc5c81] | 239 | |
---|
| 240 | @staticmethod |
---|
| 241 | def parse_mapper(opt, s, val, parser, dest): |
---|
| 242 | if val in access_opts.mappers: |
---|
| 243 | setattr(parser.values, dest, access_opts.mappers[val]) |
---|
| 244 | else: |
---|
| 245 | raise OptionValueError('%s must be one of %s' % \ |
---|
[87807f42] | 246 | (s, join(access_opts.mappers.keys(), ', '))) |
---|
[6cc5c81] | 247 | |
---|
| 248 | def __init__(self): |
---|
[62f3dd9] | 249 | file_expanding_opts.__init__(self, usage='%prog [opts] file [...]') |
---|
[6cc5c81] | 250 | self.add_option('--cert', dest='cert', default=None, |
---|
[62f3dd9] | 251 | type='str', action='callback', callback=self.expand_file, |
---|
[6cc5c81] | 252 | help='my fedid as an X.509 certificate') |
---|
| 253 | self.add_option('--key', dest='key', default=None, |
---|
[62f3dd9] | 254 | type='str', action='callback', callback=self.expand_file, |
---|
[6cc5c81] | 255 | help='key for the certificate') |
---|
[5a721ed] | 256 | self.add_option('--dir', dest='dir', default=None, |
---|
[62f3dd9] | 257 | type='str', action='callback', callback=self.expand_file, |
---|
[5a721ed] | 258 | help='Output directory for credentials') |
---|
[6cc5c81] | 259 | self.add_option('--type', action='callback', nargs=1, type='str', |
---|
| 260 | callback=access_opts.parse_mapper, |
---|
| 261 | callback_kwargs = { 'dest': 'mapper'}, |
---|
| 262 | help='Type of access file to parse. One of %s. ' %\ |
---|
[87807f42] | 263 | join(access_opts.mappers.keys(), ', ') + \ |
---|
[6cc5c81] | 264 | 'Omit for generic parsing.') |
---|
[5a721ed] | 265 | self.add_option('--quiet', dest='quiet', action='store_true', |
---|
| 266 | default=False, |
---|
| 267 | help='Do not print credential to local attribute map') |
---|
[62f3dd9] | 268 | self.add_option('--create_creds', action='store_true', |
---|
[5a721ed] | 269 | dest='create_creds', default=False, |
---|
| 270 | help='create credentials for rules. Requires ' + \ |
---|
| 271 | '--cert, --key, and --dir to be given.') |
---|
[87807f42] | 272 | self.add_option('--file', dest='file', default=None, |
---|
[62f3dd9] | 273 | type='str', action='callback', callback=self.expand_file, |
---|
[87807f42] | 274 | help='Access DB to parse. If this is present, ' + \ |
---|
| 275 | 'omit the positional filename') |
---|
| 276 | self.add_option('--mapfile', dest='map', default=None, |
---|
[62f3dd9] | 277 | type='str', action='callback', callback=self.expand_file, |
---|
[87807f42] | 278 | help='File for the attribute to local authorization data') |
---|
| 279 | self.add_option('--no-delegate', action='store_false', dest='delegate', |
---|
| 280 | default=True, |
---|
| 281 | help='do not accept delegated attributes with the ' +\ |
---|
| 282 | 'acting_for linking role') |
---|
[d894c21] | 283 | self.add_option('--auth', action='store_true', dest='create_auth', |
---|
| 284 | default=False, help='create a full ABAC authorizer') |
---|
[87807f42] | 285 | self.add_option('--debug', action='store_true', dest='debug', |
---|
| 286 | default=False, help='Just print actions') |
---|
[6cc5c81] | 287 | self.set_defaults(mapper=None) |
---|
| 288 | |
---|
[87807f42] | 289 | def create_creds(creds, cert, key, dir, debug=False, |
---|
| 290 | creddy='/usr/local/bin/creddy'): |
---|
[af25848] | 291 | ''' |
---|
| 292 | Make the creddy calls to create the attributes from the list of credential |
---|
| 293 | objects in the creds parameter. |
---|
| 294 | ''' |
---|
[5a721ed] | 295 | def attrs(r): |
---|
[af25848] | 296 | ''' |
---|
| 297 | Convert an attribute into creddy --subject-id and --subject-role |
---|
| 298 | parameters |
---|
| 299 | ''' |
---|
[87807f42] | 300 | if r.principal and r.link and r.attr: |
---|
| 301 | return ['--subject-id=%s' % r.principal, |
---|
| 302 | '--subject-role=%s.%s' % (r.attr, r.link), |
---|
| 303 | ] |
---|
| 304 | elif r.principal and r.attr: |
---|
[5a721ed] | 305 | return ['--subject-id=%s' % r.principal, |
---|
| 306 | '--subject-role=%s' %r.attr] |
---|
| 307 | elif r.principal: |
---|
| 308 | return ['--subject-id=%s' % r.prinicpal] |
---|
| 309 | else: |
---|
| 310 | raise parse_error('Attribute without a principal?') |
---|
[6cc5c81] | 311 | |
---|
[af25848] | 312 | # main line of create_creds |
---|
[5a721ed] | 313 | for i, c in enumerate(creds): |
---|
| 314 | cmd = [creddy, '--attribute', '--issuer=%s' % cert, '--key=%s' % key, |
---|
[547aa3b] | 315 | '--role=%s' % c.attr, '--out=%s/cred%d_attr.der' % (dir, i)] |
---|
[5a721ed] | 316 | for r in c.req: |
---|
| 317 | cmd.extend(attrs(r)) |
---|
[87807f42] | 318 | if debug: |
---|
| 319 | print join(cmd) |
---|
| 320 | else: |
---|
| 321 | rv = subprocess.call(cmd) |
---|
| 322 | if rv != 0: |
---|
| 323 | raise credential_error("%s: %d" % (join(cmd), rv)) |
---|
[6cc5c81] | 324 | |
---|
[d894c21] | 325 | def clear_dir(dir): |
---|
| 326 | for path, dirs, files in os.walk(dir, topdown=False): |
---|
| 327 | for f in files: os.unlink(os.path.join(path, f)) |
---|
| 328 | for d in dirs: os.rmdir(os.path.join(path, d)) |
---|
| 329 | |
---|
[af25848] | 330 | # Regular expressions and parts thereof for parsing |
---|
[6cc5c81] | 331 | comment_re = re.compile('^\s*#|^$') |
---|
| 332 | fedid_str = 'fedid:([0-9a-fA-F]{40})' |
---|
[9cce15a] | 333 | id_str = '[a-zA-Z][\w_-]*' |
---|
[5a721ed] | 334 | path_str = '[a-zA-Z_/\.-]+' |
---|
[6cc5c81] | 335 | id_any_str = '(%s|<any>)' % id_str |
---|
| 336 | id_same_str = '(%s|<same>)' % id_str |
---|
| 337 | left_side_str = '\(\s*%s\s*,\s*%s\s*,\s*%s\s*\)' % \ |
---|
| 338 | (fedid_str, id_any_str, id_any_str) |
---|
| 339 | right_side_str = '(%s)(\s*,\s*\(.*\))?' % (id_str) |
---|
| 340 | line_re = re.compile('%s\s*->\s*%s' % (left_side_str, right_side_str)) |
---|
| 341 | |
---|
| 342 | p = access_opts() |
---|
| 343 | opts, args = p.parse_args() |
---|
| 344 | |
---|
[de7cb08] | 345 | cert, key = None, None |
---|
| 346 | delete_certs = False |
---|
[d894c21] | 347 | delete_creds = False |
---|
[de7cb08] | 348 | |
---|
[87807f42] | 349 | if opts.file: |
---|
| 350 | args.append(opts.file) |
---|
| 351 | |
---|
[af25848] | 352 | # Validate arguments |
---|
[6cc5c81] | 353 | if len(args) < 1: |
---|
| 354 | sys.exit('No filenames given to parse') |
---|
| 355 | |
---|
[de7cb08] | 356 | if opts.key: |
---|
| 357 | if not os.access(opts.key, os.R_OK): |
---|
| 358 | key = opts.key |
---|
| 359 | else: |
---|
| 360 | sys.exit('Cannot read key (%s)' % opts.key) |
---|
[5a721ed] | 361 | |
---|
| 362 | if opts.dir: |
---|
[c324ad3] | 363 | if not os.access(opts.dir, os.F_OK): |
---|
| 364 | try: |
---|
| 365 | os.mkdir(opts.dir, 0700) |
---|
| 366 | except EnvironmentError, e: |
---|
| 367 | sys.exit("Cannot create %s: %s" % (e.filename, e.strerror)) |
---|
[5a721ed] | 368 | if not os.path.isdir(opts.dir): |
---|
| 369 | sys.exit('%s is not a directory' % opts.dir) |
---|
| 370 | elif not os.access(opts.dir, os.W_OK): |
---|
| 371 | sys.exit('%s is not writable' % opts.dir) |
---|
| 372 | |
---|
[d894c21] | 373 | if opts.create_auth: |
---|
| 374 | creds_dir = mkdtemp() |
---|
| 375 | delete_creds = True |
---|
| 376 | auth_dir = opts.dir |
---|
| 377 | else: |
---|
| 378 | creds_dir = opts.dir |
---|
| 379 | auth_dir = None |
---|
| 380 | |
---|
[87807f42] | 381 | if opts.delegate: delegation_link = 'acting_for' |
---|
| 382 | else: delegation_link = None |
---|
| 383 | |
---|
[a0e20ac] | 384 | if not opts.mapper and (opts.map or opts.debug): |
---|
| 385 | print >>sys.stderr, "No --type specified, mapping file will be empty." |
---|
[6cc5c81] | 386 | |
---|
[de7cb08] | 387 | if opts.cert: |
---|
[6cc5c81] | 388 | try: |
---|
[de7cb08] | 389 | me = fedid(file=opts.cert) |
---|
[6cc5c81] | 390 | except EnvironmentError, e: |
---|
[de7cb08] | 391 | sys.exit('Bad --cert: %s (%s)' % (e.strerror, e.filename or '?!')) |
---|
[6cc5c81] | 392 | |
---|
[de7cb08] | 393 | if not opts.key: |
---|
| 394 | if abac_pem_type(opts.cert) == 'both': |
---|
| 395 | key, cert = abac_split_cert(opts.cert) |
---|
| 396 | delete_certs = True |
---|
| 397 | else: |
---|
| 398 | cert = opts.cert |
---|
| 399 | else: |
---|
| 400 | print >>sys.stderr, 'No --cert, using dummy fedid' |
---|
| 401 | me = fedid(hexstr='0123456789012345678901234567890123456789') |
---|
| 402 | cert = None |
---|
| 403 | |
---|
| 404 | # The try block makes sure that credentials split into tmp files are deleted |
---|
| 405 | try: |
---|
| 406 | # Do the parsing |
---|
| 407 | for fn in args: |
---|
[87807f42] | 408 | try: |
---|
[a0e20ac] | 409 | creds, to_id = parse_access(fn, opts.mapper) |
---|
[de7cb08] | 410 | except parse_error, e: |
---|
| 411 | print >> sys.stderr, "%s" % e |
---|
| 412 | continue |
---|
| 413 | |
---|
[87807f42] | 414 | except EnvironmentError, e: |
---|
[de7cb08] | 415 | print >>sys.stderr, "File error %s: %s" % \ |
---|
| 416 | (e.filename or '!?', e.strerror) |
---|
| 417 | continue |
---|
| 418 | |
---|
| 419 | # Credential output |
---|
| 420 | if opts.create_creds: |
---|
| 421 | if all([cert, key, opts.dir]): |
---|
| 422 | try: |
---|
| 423 | create_creds([c for c in creds if c.principal == me], |
---|
[d894c21] | 424 | cert, key, creds_dir, opts.debug) |
---|
[de7cb08] | 425 | except credential_error, e: |
---|
| 426 | sys.exit('Credential creation failed: %s' % e) |
---|
| 427 | else: |
---|
[d894c21] | 428 | print >>sys.stderr, 'Cannot create credentials. ' + \ |
---|
| 429 | 'Missing parameter' |
---|
[de7cb08] | 430 | |
---|
| 431 | # Local map output |
---|
| 432 | if opts.map or opts.debug: |
---|
| 433 | try: |
---|
| 434 | if opts.map and opts.map != '-' and not opts.debug: |
---|
| 435 | f = open(opts.map, 'w') |
---|
| 436 | else: |
---|
| 437 | f = sys.stdout |
---|
| 438 | for k, c in to_id.items(): |
---|
[5d7f1e8] | 439 | # Keys are either a single string or a tuple of them; join |
---|
| 440 | # the tuples into a comma-separated string. |
---|
| 441 | if isinstance(k, basestring): rhs = k |
---|
| 442 | else: rhs = join(k, ', ') |
---|
| 443 | |
---|
[de7cb08] | 444 | for a in set(["%s.%s" % (x.principal, x.attr) for x in c]): |
---|
[5d7f1e8] | 445 | print >>f, "%s -> (%s)" % (a, rhs) |
---|
[de7cb08] | 446 | except EnvironmentError, e: |
---|
[d894c21] | 447 | sys.exit("Cannot open %s: %s" % (e.filename or '!?', |
---|
| 448 | e.strerror)) |
---|
| 449 | |
---|
| 450 | # Create an authorizer if requested. |
---|
| 451 | if opts.create_auth: |
---|
| 452 | clear_dir(auth_dir) |
---|
| 453 | try: |
---|
| 454 | # Pass in the options rather than the potentially split key |
---|
| 455 | # because abac_authorizer will split it and store it |
---|
| 456 | # internally. The opts.cert may get split twice, but we won't |
---|
| 457 | # lose one. |
---|
| 458 | a = abac_authorizer(key=opts.key, me=opts.cert, |
---|
| 459 | certs=creds_dir, save=auth_dir) |
---|
| 460 | a.save(auth_dir) |
---|
| 461 | except EnvironmentError, e: |
---|
[5d7f1e8] | 462 | sys.exit("Can't create or write %s: %s" % \ |
---|
| 463 | (e.filename, e.strerror)) |
---|
[d894c21] | 464 | except abac_authorizer.bad_cert_error, e: |
---|
| 465 | sys.exit("Error creating authorizer: %s" % e) |
---|
| 466 | |
---|
[de7cb08] | 467 | finally: |
---|
[d894c21] | 468 | try: |
---|
| 469 | if delete_certs: |
---|
| 470 | if cert: os.unlink(cert) |
---|
| 471 | if key: os.unlink(key) |
---|
| 472 | if delete_creds and creds_dir: |
---|
| 473 | clear_dir(creds_dir) |
---|
| 474 | os.rmdir(creds_dir) |
---|
| 475 | except EnvironmentError, e: |
---|
| 476 | sys.exit("Can't remove %s: %s" % ( e.filename, e.strerror)) |
---|