Changeset af25848
- Timestamp:
- Sep 15, 2010 2:25:01 AM (14 years ago)
- Branches:
- axis_example, compt_changes, info-ops, master
- Children:
- 87c0fc1
- Parents:
- 5a721ed
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fedd/access_to_abac.py
r5a721ed raf25848 8 8 9 9 class attribute: 10 ''' 11 Encapculate a principal/attribute pair. 12 ''' 10 13 def __init__(self, p, a): 11 14 self.principal = p … … 13 16 14 17 def __str__(self): 15 return "%s.%s" % (self.principal, self.attr) 18 if self.attr: 19 return "%s.%s" % (self.principal, self.attr) 20 else: 21 return "%s" % self.principal 16 22 17 23 class credential: 24 ''' 25 A Credential, that is the requisites (as attributes) and the assigned 26 attribute (as principal, attr). If req is iterable, the requirements are 27 an intersection/conjunction. 28 ''' 18 29 def __init__(self, p, a, req): 19 30 self.principal = p … … 31 42 return "%s.%s <- %s" % (self.principal, self.attr, self.req) 32 43 44 # Mappinng generation functiona and the access parser throw these when there is 45 # a parsing problem. 33 46 class parse_error(RuntimeError): pass 34 47 48 # Functions to parse the individual access maps as well as an overall function 49 # to parse generic ones. The specific ones create a credential to local 50 # attributes mapping and the global one creates the access policy credentials. 51 52 # All the local parsing functions get the unparsed remainder of the line 53 # (after the three-name and the attribute it maps to), the credential list to 54 # add the new ABAC credential(s) that will be mapped into the loacl 55 # credentials, the fedid of this entity, a dict mapping the local credentials 56 # to ABAC credentials that are required to exercise those local rights and the 57 # three-name (p, gp, gu) that is being mapped. 35 58 def parse_emulab(l, creds, me, to_id, p, gp, gu): 59 ''' 60 Parse the emulab (project, allocation_user, access_user) format. Access 61 users are deprecates and allocation users used for both. This fuction 62 collapses them. 63 ''' 36 64 right_side_str = '\s*,\s*\(\s*%s\s*,\s*%s\s*,\s*%s\s*\)' % \ 37 65 (id_same_str, id_same_str,id_same_str) … … 40 68 if m: 41 69 project, user = m.group(1,2) 70 # Resolve "<same>"s in project and user 42 71 if project == '<same>': 43 72 if gp is not None: … … 50 79 else: 51 80 raise parse_error("User cannot be decisively mapped: %s" % l) 81 82 # Create a semi-mnemonic name for the destination credential (the one 83 # that will be mapped to the local attributes 52 84 if project and user: 53 85 a = 'project_%s_user_%s' % (project, user) … … 59 91 raise parse_error("No mapping for %s/%s!?" % (gp, gu)) 60 92 93 # Store the creds and map entries 61 94 c = credential(me, a, 62 95 [attribute(p, x) for x in (gp, gu) if x is not None]) … … 69 102 70 103 def parse_protogeni(l, creds, me, to_id, p, gp, gu): 104 ''' 105 Parse the protoGENI (cert, user, user_key, cert_pw) format. 106 ''' 71 107 right_side_str = '\s*,\s*\(\s*(%s)\s*,\s*(%s)\s*,\s*(%s)\s*,\s*(%s)\s*\)' \ 72 108 % (path_str, id_str, path_str, id_str) … … 75 111 if m: 76 112 cert, user, key, pw = m.group(1,2,3,4) 113 # The credential is formed from just the path (with / mapped to _) and 114 # the username. 77 115 acert = re.sub('/', '_', cert) 78 116 79 117 a = "cert_%s_user_%s" % (acert, user) 118 119 # Store em 80 120 c = credential(me, a, 81 121 [attribute(p, x) for x in (gp, gu) if x is not None]) … … 89 129 90 130 def parse_dragon(l, creds, me, to_id, p, gp, gu): 131 ''' 132 Parse the dragon (repository_name) version. 133 ''' 91 134 right_side_str = '\s*,\s*\(\s*(%s)\s*\)' % \ 92 135 (id_str) … … 103 146 raise parse_error("Badly formatted local mapping: %s" % l) 104 147 105 parse_skel = parse_dragon 106 148 def parse_skel(l, creds, me, to_id, p, gp, gu): 149 ''' 150 Parse the skeleton (local_attr) version. 151 ''' 152 right_side_str = '\s*,\s*\(\s*(%s)\s*\)' % \ 153 (id_str) 154 155 m = re.match(right_side_str, l) 156 if m: 157 lattr = m.group(1) 158 c = credential(me, 'lattr_%s' % lattr, 159 [attribute(p, x) for x in (gp, gu) if x is not None]) 160 creds.add(c) 161 if lattr in to_id: to_id[lattr].append(c) 162 else: to_id[lattr] = [ c ] 163 else: 164 raise parse_error("Badly formatted local mapping: %s" % l) 165 166 # internal plug-ins have no local attributes. 107 167 def parse_internal(l, creds, me, to_id, p, gp, gu): pass 108 168 109 169 110 170 class access_opts(OptionParser): 171 ''' 172 Parse the options for this program. Most are straightforward, but the 173 mapper uses a callback to convert from a string to a local mapper function. 174 ''' 175 # Valid mappers 111 176 mappers = { 112 177 'emulab': parse_emulab, … … 149 214 150 215 def create_creds(creds, cert, key, dir, creddy='/usr/local/bin/creddy'): 216 ''' 217 Make the creddy calls to create the attributes from the list of credential 218 objects in the creds parameter. 219 ''' 151 220 def attrs(r): 221 ''' 222 Convert an attribute into creddy --subject-id and --subject-role 223 parameters 224 ''' 152 225 if r.principal and r.attr: 153 226 return ['--subject-id=%s' % r.principal, … … 158 231 raise parse_error('Attribute without a principal?') 159 232 233 # main line of create_creds 160 234 for i, c in enumerate(creds): 161 235 cmd = [creddy, '--attribute', '--issuer=%s' % cert, '--key=%s' % key, … … 165 239 print " ".join(cmd) 166 240 241 # Regular expressions and parts thereof for parsing 167 242 comment_re = re.compile('^\s*#|^$') 168 243 fedid_str = 'fedid:([0-9a-fA-F]{40})' … … 179 254 opts, args = p.parse_args() 180 255 256 # Validate arguments 181 257 if len(args) < 1: 182 258 sys.exit('No filenames given to parse') … … 202 278 mapper = opts.mapper 203 279 280 # Do the parsing 204 281 for fn in args: 205 282 creds = set() … … 227 304 raise parse_error('Syntax error') 228 305 except parse_error, e: 306 f.close() 229 307 raise parse_error('Error on line %d of %s: %s' % \ 230 308 (i, fn, e.message)) … … 240 318 continue 241 319 320 # Credential output 242 321 if opts.create_creds: 243 322 if all([opts.cert, opts.key, opts.dir]): … … 247 326 print >>sys.stderr, 'Cannot create credentials. Missing parameter' 248 327 328 # Local map output 249 329 if not opts.quiet: 250 330 for k, c in to_id.items():
Note: See TracChangeset
for help on using the changeset viewer.