[175b444] | 1 | #!/usr/local/bin/python |
---|
| 2 | |
---|
| 3 | import os,sys |
---|
| 4 | import re |
---|
| 5 | import string |
---|
| 6 | import copy |
---|
| 7 | import pickle |
---|
| 8 | import logging |
---|
| 9 | import time |
---|
| 10 | |
---|
| 11 | from threading import Thread, Lock |
---|
| 12 | from subprocess import Popen, call, PIPE, STDOUT |
---|
| 13 | |
---|
| 14 | from util import * |
---|
| 15 | from allocate_project import allocate_project_local, allocate_project_remote |
---|
| 16 | from fedid import fedid, generate_fedid |
---|
[dee164e] | 17 | from authorizer import authorizer, abac_authorizer |
---|
[175b444] | 18 | from service_error import service_error |
---|
| 19 | from remote_service import xmlrpc_handler, soap_handler, service_caller |
---|
| 20 | |
---|
| 21 | import httplib |
---|
| 22 | import tempfile |
---|
| 23 | from urlparse import urlparse |
---|
| 24 | |
---|
| 25 | import topdl |
---|
| 26 | import list_log |
---|
| 27 | |
---|
[fa4a4a8] | 28 | from access import access_base |
---|
[dee164e] | 29 | from legacy_access import legacy_access |
---|
[175b444] | 30 | |
---|
| 31 | # Make log messages disappear if noone configures a fedd logger |
---|
| 32 | class nullHandler(logging.Handler): |
---|
| 33 | def emit(self, record): pass |
---|
| 34 | |
---|
| 35 | fl = logging.getLogger("fedd.access") |
---|
| 36 | fl.addHandler(nullHandler()) |
---|
| 37 | |
---|
[dee164e] | 38 | class access(access_base, legacy_access): |
---|
[175b444] | 39 | @staticmethod |
---|
| 40 | def parse_vlans(v, log=None): |
---|
| 41 | """ |
---|
| 42 | Parse a vlan parameter into a set of vlan ids. Comma separated |
---|
| 43 | sequences of vlan ranges are acceptable. |
---|
| 44 | """ |
---|
| 45 | # the vlans can be a single integer, a comma separated list or a |
---|
| 46 | # comma separated lists of dashed ranges. E.g 100 or 100,300 or |
---|
| 47 | # 100,300-305,400 |
---|
| 48 | vset = set() |
---|
| 49 | if v: |
---|
| 50 | for v in [ x.strip() for x in v.split(",") ]: |
---|
| 51 | try: |
---|
| 52 | if v.count("-") == 1: |
---|
| 53 | f, t = v.split("-", 1) |
---|
| 54 | for i in range(int(f), int(t)+1): |
---|
| 55 | vset.add(i) |
---|
| 56 | else: |
---|
| 57 | vset.add(int(v)) |
---|
| 58 | except ValueError: |
---|
| 59 | if log: |
---|
| 60 | log.warn("Invalid expression in vlan list: %s" % v) |
---|
| 61 | return vset |
---|
| 62 | |
---|
| 63 | def __init__(self, config=None, auth=None): |
---|
| 64 | """ |
---|
| 65 | Initializer. Pulls parameters out of the ConfigParser's access section. |
---|
| 66 | """ |
---|
| 67 | |
---|
[fa4a4a8] | 68 | access_base.__init__(self, config, auth) |
---|
[175b444] | 69 | self.domain = config.get("access", "domain") |
---|
| 70 | vlan_str = config.get("access", "vlans") |
---|
| 71 | self.vlans = self.parse_vlans(vlan_str) |
---|
| 72 | |
---|
| 73 | self.attrs = { } |
---|
| 74 | self.access = { } |
---|
| 75 | # State is a dict of dicts indexed by segment fedid that includes the |
---|
| 76 | # owners of the segment as fedids (who can manipulate it, key: owners), |
---|
| 77 | # the repo dir/user for the allocation (key: user), Current allocation |
---|
| 78 | # log (key: log), and GRI of the reservation once made (key: gri) |
---|
| 79 | self.log = logging.getLogger("fedd.access") |
---|
| 80 | set_log_level(config, "access", self.log) |
---|
| 81 | |
---|
| 82 | |
---|
[dee164e] | 83 | # authorization information |
---|
| 84 | self.auth_type = config.get('access', 'auth_type') \ |
---|
| 85 | or 'legacy' |
---|
| 86 | self.auth_dir = config.get('access', 'auth_dir') |
---|
| 87 | accessdb = config.get("access", "accessdb") |
---|
| 88 | # initialize the authorization system |
---|
| 89 | if self.auth_type == 'legacy': |
---|
| 90 | self.access = { } |
---|
| 91 | if accessdb: |
---|
| 92 | self.legacy_read_access(accessdb) |
---|
| 93 | elif self.auth_type == 'abac': |
---|
| 94 | self.auth = abac_authorizer(load=self.auth_dir) |
---|
| 95 | self.access = [ ] |
---|
| 96 | if accessdb: |
---|
| 97 | self.read_access(accessdb, default=[('access', None)]) |
---|
| 98 | else: |
---|
| 99 | raise service_error(service_error.internal, |
---|
| 100 | "Unknown auth_type: %s" % self.auth_type) |
---|
| 101 | |
---|
| 102 | if self.auth_type == 'legacy': |
---|
| 103 | # Add the ownership attributes to the authorizer. Note that the |
---|
| 104 | # indices of the allocation dict are strings, but the attributes are |
---|
| 105 | # fedids, so there is a conversion. |
---|
| 106 | self.state_lock.acquire() |
---|
| 107 | for k in self.state.keys(): |
---|
| 108 | for o in self.state[k].get('owners', []): |
---|
| 109 | self.auth.set_attribute(o, fedid(hexstr=k)) |
---|
| 110 | self.auth.set_attribute(fedid(hexstr=k),fedid(hexstr=k)) |
---|
| 111 | # If the allocation has a vlan assigned, remove it from the |
---|
| 112 | # available vlans |
---|
| 113 | v = self.state[k].get('vlan', None) |
---|
| 114 | if v: |
---|
| 115 | self.vlans.discard(v) |
---|
| 116 | self.state_lock.release() |
---|
| 117 | |
---|
| 118 | self.lookup_access = self.legacy_lookup_access_base |
---|
| 119 | # under ABAC we use access.lookup_access |
---|
[fa4a4a8] | 120 | |
---|
[175b444] | 121 | |
---|
| 122 | self.call_GetValue= service_caller('GetValue') |
---|
| 123 | self.call_SetValue= service_caller('SetValue') |
---|
| 124 | |
---|
| 125 | self.soap_services = {\ |
---|
| 126 | 'RequestAccess': soap_handler("RequestAccess", self.RequestAccess), |
---|
| 127 | 'ReleaseAccess': soap_handler("ReleaseAccess", self.ReleaseAccess), |
---|
| 128 | 'StartSegment': soap_handler("StartSegment", self.StartSegment), |
---|
| 129 | 'TerminateSegment': soap_handler("TerminateSegment", |
---|
| 130 | self.TerminateSegment), |
---|
| 131 | } |
---|
| 132 | self.xmlrpc_services = {\ |
---|
| 133 | 'RequestAccess': xmlrpc_handler('RequestAccess', |
---|
| 134 | self.RequestAccess), |
---|
| 135 | 'ReleaseAccess': xmlrpc_handler('ReleaseAccess', |
---|
| 136 | self.ReleaseAccess), |
---|
| 137 | 'StartSegment': xmlrpc_handler("StartSegment", self.StartSegment), |
---|
| 138 | 'TerminateSegment': xmlrpc_handler('TerminateSegment', |
---|
| 139 | self.TerminateSegment), |
---|
| 140 | } |
---|
| 141 | |
---|
[9973d57] | 142 | # RequestAccess and ReleaseAccess come from the base |
---|
[175b444] | 143 | |
---|
| 144 | def extract_parameters(self, top): |
---|
| 145 | """ |
---|
[fa4a4a8] | 146 | DETER's internal networking currently supports a fixed capacity link |
---|
| 147 | between two endpoints. Those endpoints may specify a VPN (or list or |
---|
| 148 | range) to use. This extracts the and vpn preferences from the segments |
---|
| 149 | (as attributes) and the capacity of the connection as given by the |
---|
[175b444] | 150 | substrate. The two endpoints VLAN choices are intersected to get set |
---|
| 151 | of VLANs that are acceptable (no VLAN requiremnets means any is |
---|
| 152 | acceptable). |
---|
| 153 | """ |
---|
| 154 | segments = filter(lambda x: isinstance(x, topdl.Segment), top.elements) |
---|
| 155 | |
---|
| 156 | if len(segments) != 2 or len(top.substrates) != 1: |
---|
| 157 | raise service_error(service_error.req, |
---|
| 158 | "Requests to DRAGON must have exactlty two segments " +\ |
---|
| 159 | "and one substrate") |
---|
| 160 | |
---|
| 161 | vlans = set() |
---|
| 162 | for s in segments: |
---|
| 163 | v = s.get_attribute('vlans') |
---|
| 164 | vlans &= self.parse_vlans(v) |
---|
| 165 | |
---|
| 166 | if len(vlans) == 0: |
---|
| 167 | vlans = None |
---|
| 168 | |
---|
| 169 | sub = top.substrates[0] |
---|
| 170 | if sub.capacity: |
---|
| 171 | cap = int(sub.capacity.rate / 1000.0) |
---|
| 172 | if cap < 1: cap = 1 |
---|
| 173 | else: |
---|
| 174 | cap = 100 |
---|
| 175 | |
---|
| 176 | return cap, vlans |
---|
| 177 | |
---|
| 178 | def export_store_info(self, cf, vlan, connInfo): |
---|
| 179 | """ |
---|
| 180 | For the export requests in the connection info, install the peer names |
---|
| 181 | at the experiment controller via SetValue calls. |
---|
| 182 | """ |
---|
| 183 | |
---|
| 184 | for c in connInfo: |
---|
| 185 | for p in [ p for p in c.get('parameter', []) \ |
---|
| 186 | if p.get('type', '') == 'output']: |
---|
| 187 | |
---|
| 188 | if p.get('name', '') != 'vlan_id': |
---|
| 189 | self.log.error("Unknown export parameter: %s" % \ |
---|
| 190 | p.get('name')) |
---|
| 191 | continue |
---|
| 192 | |
---|
| 193 | k = p.get('key', None) |
---|
| 194 | surl = p.get('store', None) |
---|
| 195 | if surl and k: |
---|
| 196 | value = "%s" % vlan |
---|
| 197 | req = { 'name': k, 'value': value } |
---|
| 198 | self.call_SetValue(surl, req, cf) |
---|
| 199 | else: |
---|
| 200 | self.log.error("Bad export request: %s" % p) |
---|
| 201 | |
---|
| 202 | def StartSegment(self, req, fid): |
---|
| 203 | err = None # Any service_error generated after tmpdir is created |
---|
| 204 | rv = None # Return value from segment creation |
---|
| 205 | |
---|
| 206 | try: |
---|
| 207 | req = req['StartSegmentRequestBody'] |
---|
[fa4a4a8] | 208 | topref = req['segmentdescription']['topdldescription'] |
---|
[175b444] | 209 | except KeyError: |
---|
| 210 | raise service_error(server_error.req, "Badly formed request") |
---|
| 211 | |
---|
| 212 | auth_attr = req['allocID']['fedid'] |
---|
| 213 | aid = "%s" % auth_attr |
---|
| 214 | attrs = req.get('fedAttr', []) |
---|
[e83f2f2] | 215 | access_ok, proof = self.auth.check_attribute(fid, auth_attr, |
---|
| 216 | with_proof=True) |
---|
| 217 | if not access_ok: |
---|
[175b444] | 218 | raise service_error(service_error.access, "Access denied") |
---|
| 219 | else: |
---|
| 220 | # See if this is a replay of an earlier succeeded StartSegment - |
---|
| 221 | # sometimes SSL kills 'em. If so, replay the response rather than |
---|
| 222 | # redoing the allocation. |
---|
| 223 | self.state_lock.acquire() |
---|
| 224 | retval = self.state[aid].get('started', None) |
---|
| 225 | self.state_lock.release() |
---|
| 226 | if retval: |
---|
| 227 | self.log.warning("Duplicate StartSegment for %s: " % aid + \ |
---|
| 228 | "replaying response") |
---|
| 229 | return retval |
---|
| 230 | |
---|
| 231 | certfile = "%s/%s.pem" % (self.certdir, aid) |
---|
| 232 | |
---|
[fa4a4a8] | 233 | if topref: topo = topdl.Topology(**topref) |
---|
[175b444] | 234 | else: |
---|
| 235 | raise service_error(service_error.req, |
---|
| 236 | "Request missing segmentdescription'") |
---|
| 237 | |
---|
| 238 | connInfo = req.get('connection', []) |
---|
| 239 | |
---|
| 240 | cap, vlans = self.extract_parameters(topo) |
---|
| 241 | |
---|
| 242 | # No vlans passes in, consider any vlan acceptable |
---|
| 243 | if not vlans: |
---|
| 244 | vlans = self.vlans |
---|
| 245 | |
---|
| 246 | avail = self.vlans | vlans |
---|
| 247 | |
---|
| 248 | if len(avail) != 0: |
---|
| 249 | vlan_no = avail.pop() |
---|
| 250 | self.vlans.discard(vlan_no) |
---|
| 251 | else: |
---|
| 252 | raise service_error(service_error.federant, "No vlan available") |
---|
| 253 | |
---|
| 254 | self.export_store_info(certfile, vlan_no, connInfo) |
---|
| 255 | |
---|
| 256 | |
---|
[fa4a4a8] | 257 | # This annotation isn't strictly necessary, but may help in debugging |
---|
[175b444] | 258 | rtopo = topo.clone() |
---|
| 259 | for s in rtopo.substrates: |
---|
| 260 | s.set_attribute('vlan', vlan_no) |
---|
| 261 | |
---|
| 262 | # Grab the log (this is some anal locking, but better safe than |
---|
| 263 | # sorry) |
---|
| 264 | self.state_lock.acquire() |
---|
| 265 | self.state[aid]['vlan'] = vlan_no |
---|
| 266 | logv = "Allocated vlan: %d" % vlan_no |
---|
| 267 | # It's possible that the StartSegment call gets retried (!). |
---|
| 268 | # if the 'started' key is in the allocation, we'll return it rather |
---|
| 269 | # than redo the setup. |
---|
| 270 | self.state[aid]['started'] = { |
---|
| 271 | 'allocID': req['allocID'], |
---|
| 272 | 'allocationLog': logv, |
---|
[e83f2f2] | 273 | 'segmentdescription': { 'topdldescription': rtopo.to_dict() }, |
---|
| 274 | 'proof': proof.to_dict(), |
---|
[175b444] | 275 | } |
---|
[8dbbdc5] | 276 | retval = copy.deepcopy(self.state[aid]['started']) |
---|
[175b444] | 277 | self.write_state() |
---|
| 278 | self.state_lock.release() |
---|
| 279 | |
---|
| 280 | return retval |
---|
| 281 | |
---|
| 282 | def TerminateSegment(self, req, fid): |
---|
| 283 | try: |
---|
| 284 | req = req['TerminateSegmentRequestBody'] |
---|
| 285 | except KeyError: |
---|
| 286 | raise service_error(server_error.req, "Badly formed request") |
---|
| 287 | |
---|
| 288 | auth_attr = req['allocID']['fedid'] |
---|
| 289 | aid = "%s" % auth_attr |
---|
| 290 | |
---|
| 291 | self.log.debug("Terminate request for %s" %aid) |
---|
[e83f2f2] | 292 | access_ok, proof = self.auth.check_attribute(fid, auth_attr, |
---|
| 293 | with_proof=True) |
---|
| 294 | if not access_ok: |
---|
[175b444] | 295 | raise service_error(service_error.access, "Access denied") |
---|
| 296 | |
---|
| 297 | self.state_lock.acquire() |
---|
| 298 | if self.state.has_key(aid): |
---|
| 299 | vlan_no = self.state[aid].get('vlan', None) |
---|
| 300 | else: |
---|
| 301 | vlan_no = None |
---|
| 302 | self.state_lock.release() |
---|
[7dbfaed] | 303 | self.log.debug("Stop segment for vlan: %s" % vlan_no) |
---|
[175b444] | 304 | |
---|
| 305 | if not vlan_no: |
---|
| 306 | raise service_error(service_error.internal, |
---|
| 307 | "Can't find assigfned vlan for for %s" % aid) |
---|
| 308 | |
---|
| 309 | self.vlans.add(vlan_no) |
---|
| 310 | self.state_lock.acquire() |
---|
| 311 | self.state[aid]['vlan'] = None |
---|
| 312 | self.state_lock.release() |
---|
[e83f2f2] | 313 | return { 'allocID': req['allocID'], 'proof': proof.to_dict() } |
---|