source: fedd/federation/ns2topdl.py @ 454f398

axis_examplecompt_changesinfo-ops
Last change on this file since 454f398 was 454f398, checked in by Ted Faber <faber@…>, 13 years ago

Add abac to ns2topdl

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[4700b3b]1#!/usr/local/bin/python
2
3import os,sys
4import subprocess
5import tempfile
[c971895]6import logging
[3159f5d]7import topdl
[4700b3b]8
[ec4fb42]9from util import *
[51cc9df]10from fedid import fedid
[9460b1e]11from remote_service import xmlrpc_handler, soap_handler
[4700b3b]12from service_error import *
[454f398]13from authorizer import authorizer, abac_authorizer
[4700b3b]14
15
16class nullHandler(logging.Handler):
17    def emit(self, record): pass
18
[2c6128f]19fl = logging.getLogger("fedd.splitter")
[4700b3b]20fl.addHandler(nullHandler())
21
[5f6929a]22class ns2topdl_local:
[3f6bc5f]23    def __init__(self, config=None, auth=None):
[4700b3b]24        """
25        Intialize the various attributes, most from the config object
26        """
[5f6929a]27        self.debug = config.getboolean("ns2topdl", "debug")
28        self.muxmax = config.getint("ns2topdl", "muxmax", 3)
29        self.tclsh = config.get("ns2topdl", "tclsh", 
[05191a6]30                "/usr/local/bin/otclsh")
[5f6929a]31        self.tcl_splitter = config.get("ns2topdl", "tcl_splitter",
[05191a6]32                "/usr/testbed/lib/ns2ir/parse.tcl")
[454f398]33        self.auth_type = config.get('ns2topdl', 'auth_type') or 'legacy'
[5f6929a]34        access_db = config.get("ns2topdl", "accessdb", None)
[454f398]35        self.allow_any = config.getboolean("ns2topdl", "allow_any", False)
36        auth_dir = config.get('ns2topdl', 'auth_dir')
[f4f4117]37
[5f6929a]38        self.log = logging.getLogger("fedd.ns2topdl")
39        set_log_level(config, "ns2topdl", self.log)
[4700b3b]40        self.trace_file = sys.stderr
41
[5f6929a]42        set_log_level(config, "ns2topdl", self.log)
[05191a6]43
44        if auth:
45            self.auth= auth
46        else:
47            self.auth = authorizer()
[5f6929a]48            self.log.warning("[ns2topdl] No authorizer passed in, " +\
[05191a6]49                    "using local one")
50
[454f398]51
52        if self.auth_type == 'legacy':
53            if access_db and self.allow_any: 
54                raise service_error(service_error.internal, 
55                        "Cannot specify both an access database and " + 
56                        "allow_any for ns2topdl")
57           
58            if access_db:
59                try:
60                    read_simple_accessdb(access_db, self.auth, 'ns2topdl')
61                except EnvironmentError, e:
62                    raise service_error(service_error.internal,
63                            "Error reading accessDB %s: %s" % (access_db, e))
64                except ValueError:
65                    raise service_error(service_error.internal, "%s" % e)
66            elif self.allow_any:
67                auth.set_global_attribute("ns2topdl")
68        elif self.auth_type == 'abac':
69            self.auth = abac_authorizer(load=auth_dir)
70        else:
[05191a6]71            raise service_error(service_error.internal, 
[454f398]72                    "Unknown auth_type: %s" % self.auth_type)
[05191a6]73
74
[4700b3b]75        # Dispatch tables
76        self.soap_services = {\
[5f6929a]77                'Ns2Topdl': soap_handler("Ns2Topdl", self.run_ns2topdl),
[4700b3b]78        }
79
80        self.xmlrpc_services = {\
[5f6929a]81                'Ns2Topdl': xmlrpc_handler('Ns2Topdl', self.run_ns2topdl),
[4700b3b]82        }
83
[5f6929a]84    def run_ns2topdl(self, req, fid):
[4700b3b]85        """
[5f6929a]86        The external interface to tcl to topdl translation.
[4700b3b]87
[5f6929a]88        Creates a working directory, translates the incoming description using
89        the tcl script.
[4700b3b]90        """
[05191a6]91
[454f398]92        if self.allow_any:
93            self.auth.set_attribute(fid, 'ns2topdl')
94
95        access_ok, proof = self.auth.check_attribute(fid, 'ns2topdl',
96            with_proof=True)
97
98        if not access_ok:
99            raise service_error(service_error.access, "Access Denied", 
100                proof=proof)
[05191a6]101
[4700b3b]102        try:
[5f6929a]103            tmpdir = tempfile.mkdtemp(prefix="ns2topdl-")
[d3c8759]104        except EnvironmentError:
[4700b3b]105            raise service_error(service_error.internal, "Cannot create tmp dir")
106
107        tclfile = tmpdir + "/experiment.tcl"
108        pid = "dummy"
109        gid = "dummy"
[f4f4117]110        eid = "dummy"
[5f6929a]111        master = "dummy"
[4700b3b]112
[5f6929a]113        req = req.get('Ns2TopdlRequestBody', None)
[4700b3b]114        if not req:
115            raise service_error(service_error.req,
[5f6929a]116                    "Bad request format (no Ns2TopdlRequestBody)")
[4700b3b]117        # The tcl parser needs to read a file so put the content into that file
[f4f4117]118        descr=req.get('description', None)
[4700b3b]119        if descr:
120            file_content=descr.get('ns2description', None)
121            if file_content:
122                try:
123                    f = open(tclfile, 'w')
124                    f.write(file_content)
125                    f.close()
[d3c8759]126                except EnvironmentError:
[4700b3b]127                    raise service_error(service_error.internal,
128                            "Cannot write temp experiment description")
129            else:
130                raise service_error(service_error.req, 
131                        "Only ns2descriptions supported")
132        else:
[f4f4117]133            raise service_error(service_error.req, "No description")
[4700b3b]134
135       
[3159f5d]136        tclcmd = [self.tclsh, self.tcl_splitter, '-t', '-x', 
[2c6128f]137            str(self.muxmax), '-m', master]
138        tclcmd.extend([pid, gid, eid, tclfile])
139        self.log.debug("Calling splitter %s" % " ".join(tclcmd))
[c971895]140        tclparser = subprocess.Popen(tclcmd, stdout=subprocess.PIPE)
[4700b3b]141
[3159f5d]142        top = topdl.topology_from_xml(file=tclparser.stdout, top="experiment")
[4700b3b]143
144        # Walk up tmpdir, deleting as we go
145        for path, dirs, files in os.walk(tmpdir, topdown=False):
146            for f in files:
147                os.remove(os.path.join(path, f))
148            for d in dirs:
149                os.rmdir(os.path.join(path, d))
150        os.rmdir(tmpdir)
151
[3159f5d]152        resp = { 
153                'experimentdescription':  { 
154                    'topdldescription': top.to_dict(),
[454f398]155                    },
156                'proof': proof.to_dict(),
[3159f5d]157                }
[4700b3b]158
159        return resp
160
161
Note: See TracBrowser for help on using the repository browser.