def get(self, path, **kwargs): """Just a wrapper over request.get, just in case. Returns a requests.Response :rtype : request.Response :param str path: remaining path from root url; empty if desired path equal to rooturl. :param kwargs: arguments to requests.get Example: c = Client("http://localhost:8080/service") c.get("/resource", headers = { "accept": "application/json" }) """ url = _buildpath(self.rooturl, path) if "testbed" in kwargs: url = url + "?testbed=" + kwargs["testbed"] if "headers" not in kwargs: kwargs["headers"] = {"accept": "application/xml"} kwargs["auth"] = HTTPBasicAuth(settings.SLA_COLLECTOR_USER, settings.SLA_COLLECTOR_PASSWORD) # for key, values in kwargs.iteritems(): # print key, values result = requests.get(url, verify=False, **kwargs) logger.debug('SLA GET {} - result: {}'.format(result.url, result.status_code)) # print "GET {} {} {}".format( # result.url, result.status_code, result.text[0:70]) # print result.encoding return result
def get_or_post(self, wsgi_request, method): """ """ errors = [] authorities = getAuthorities(wsgi_request, admin = True) #authority_hrn = None ## REGISTRY ONLY TO BE REMOVED WITH MANIFOLD-V2 #authorities_query = Query.get('authority').select('name', 'authority_hrn') #authorities = execute_admin_query(wsgi_request, authorities_query) #if authorities is not None: # # Remove the root authority from the list # matching = [s for s in authorities if "." in s['authority_hrn']] # authorities = sorted(matching, key=lambda k: k['authority_hrn']) # authorities = sorted(matching, key=lambda k: k['name']) logger.debug("############ BREAKPOINT 1 #################") # Page rendering page = Page(wsgi_request) page.add_css_files ( [ "https://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" ] ) page.add_js_files ( [ "js/jquery.validate.js", "js/my_account.register.js", "js/jquery.qtip.min.js","js/jquery-ui.js","js/jquery-ui-combobox.js" ] ) page.add_css_files ( [ "css/onelab.css", "css/registration.css", "css/jquery.qtip.min.css", "css/jquery.ui.combobox.css" ] ) page.expose_js_metadata() logger.debug("############ BREAKPOINT 2 #################") if method == 'POST': reg_form = {}
def get_metadata (self): # look in session's cache - we don't want to retrieve this for every request session=self.request.session if 'manifold' not in session: session['manifold'] = {} manifold = session['manifold'] # if cached, use it if 'metadata' in manifold and isinstance(manifold['metadata'],MetaData): # cached_metadata = SessionCache().get_metadata(self.request) # if cached_metadata and isinstance(cached_metadata, MetaData): logger.debug("Page.get_metadata: return cached value") return manifold['metadata'] # return cached_metadata metadata_auth = {'AuthMethod':'anonymous'} from myslice.settings import config url = config.manifold_url() metadata = MetaData(url, metadata_auth) metadata.fetch(self.request) # store it for next time manifold['metadata']=metadata.to_json() # SessionCache().store_metadata(self.request, metadata) logger.debug("Page.get_metadata: return new value") return metadata
def _execute_query(url, request, query, manifold_api_session_auth): manifold_api = ManifoldAPI(url, auth=manifold_api_session_auth) logger.debug("MANIFOLD -> QUERY : {}".format(" ".join(str(query).split()))) result = manifold_api.forward(query.to_dict()) if result['code'] == 2: # this is gross; at the very least we need to logout() # but most importantly there is a need to refine that test, since # code==2 does not necessarily mean an expired session # XXX only if we know it is the issue #SessionCache().end_session(request) # Flush django session del request.session['manifold'] request.session.flush() #raise Exception, 'Error running query: {}'.format(result) if result['code'] == 1: logger.warning("MANIFOLD : {}".format(result['description'])) # XXX Handle errors #Error running query: {'origin': [0, 'XMLRPCAPI'], 'code': 2, 'description': 'No such session: No row was found for one()', 'traceback': 'Traceback (most recent call last):\n File "/usr/local/lib/python2.7/dist-packages/manifold/core/xmlrpc_api.py", line 68, in xmlrpc_forward\n user = Auth(auth).check()\n File "/usr/local/lib/python2.7/dist-packages/manifold/auth/__init__.py", line 245, in check\n return self.auth_method.check()\n File "/usr/local/lib/python2.7/dist-packages/manifold/auth/__init__.py", line 95, in check\n raise AuthenticationFailure, "No such session: %s" % e\nAuthenticationFailure: No such session: No row was found for one()\n', 'type': 2, 'ts': None, 'value': None} return result['value']
def post (self, request, slicename): logger.debug("---------------- POST CloudView ------------------") logger.debug(request.POST) username = self.request.user platforms = self.get_platforms(request) cloud_platforms = ["onelab-cloud","fuseco"] len_platforms = len(platforms) #if 'action' in request.POST: # if request.POST['action'] == 'add': # elif request.POST['action'] == 'delete': # for key,val in request.POST: # if key.endswith('_vm'): # request.POST['platform'] # # elif request.POST['action'] == 'reserve': # # else: # log.error("action %s not supported" % request.POST['action']) env = { 'theme' : self.theme, 'slicename':slicename, 'platforms':platforms, 'cloud_platforms':cloud_platforms, 'len_platforms': len_platforms, 'post_values': request.POST, 'request':self.request, } return render_to_response(self.template, env, context_instance=RequestContext(request))
def setKey(self): # What about key formed of multiple fields??? query = Query.get('local:object').filter_by('table', '==', self.type).select('key') results = execute_query(self.request, query) logger.debug("key of object = {}".format(results)) if results : for r in results[0]['key'] : self.id = r else : raise Exception, 'Manifold db error'
def topmenu_items_static(current, request): has_user = request.user.is_authenticated() result = [] logger.debug("request user = {}".format(request.user)) if has_user: result.append({'label': 'Dashboard', 'href': '/portal/dashboard/'}) result.append({ 'label': 'Request a slice', 'href': '/portal/slice_request/' }) # always create a disabled button for validation, and let the # topmenuvalidation plugin handle that asynchroneously, based on this domid result.append({ 'label': 'Validation', 'href': '/portal/validate/', 'domid': 'topmenu-validation', 'disabled': True }) dropdown = [] dropdown.append({'label': 'Platforms', 'href': '/portal/platforms/'}) dropdown.append({'label': 'My Account', 'href': '/portal/account/'}) dropdown.append({ 'label': 'Contact Support', 'href': '/portal/contact/' }) result.append({ 'label': 'More', 'href': "#", 'dropdown': True, 'contents': dropdown }) else: result.append({'label': 'Home', 'href': '/login'}) # looks like this is accessible to non-logged users result.append({'label': 'Platforms', 'href': '/portal/platforms/'}) result.append({'label': 'Register', 'href': '/portal/register/'}) result.append({'label': 'Join us', 'href': '/portal/join/'}) result.append({'label': 'Contact Support', 'href': '/portal/contact/'}) # mark active if the provided 'current', even if shorter, matches the beginning of d['label'] if current is not None: current = current.lower() curlen = len(current) def mark_active(d, up=None): if d['label'][:curlen].lower() == current: d['is_active'] = True if up is not None: up['is_active'] = True for d in result: mark_active(d) if 'dropdown' in d: for dd in d['contents']: mark_active(dd, d) return result
def __init__ (self, query=None, query_all=None, checkboxes=False, columns=None, init_key=None, datatables_options={}, **settings): Plugin.__init__ (self, **settings) self.query = query # Until we have a proper way to access queries in Python self.query_all = query_all self.query_all_uuid = query_all.query_uuid if query_all else None self.checkboxes = checkboxes # XXX We need to have some hidden columns until we properly handle dynamic queries if columns is not None: _columns = columns _hidden_columns = [] elif self.query: logger.debug("self.query.fields = {}".format(self.query_all.fields)) # Columns displayed by default if self.default_fields is not None: _columns = [field for field in self.default_fields if not field == 'urn'] else: _columns = [field for field in self.query.fields if not field == 'urn'] if query_all: # We need a list because sets are not JSON-serializable if self.default_fields is not None: logger.debug(self.query_all.fields) _hidden_columns = list(self.query_all.fields - set(self.default_fields)) else: _hidden_columns = list(self.query_all.fields - self.query.fields) _hidden_columns.append('urn') else: _hidden_columns = [] else: _columns = [] _hidden_columns = [] logger.debug("_columns={}".format(_columns)) self.columns = { self.mapping.get(c, c) : c for c in _columns } self.hidden_columns = { self.mapping.get(c, c) : c for c in _hidden_columns } logger.debug("self.columns {}".format(self.columns)) logger.debug("self.hidden_columns {}".format(self.hidden_columns)) self.init_key=init_key self.datatables_options=datatables_options # if checkboxes were required, we tell datatables about this column's type # so that sorting can take place on a selected-first basis (or -last of course) # this relies on the template exposing the checkboxes 'th' with class 'checkbox' if self.checkboxes: # we use aoColumnDefs rather than aoColumns -- ignore user-provided aoColumns if 'aoColumns' in self.datatables_options: logger.warning('WARNING: querytable uses aoColumnDefs, your aoColumns spec. is discarded') del self.datatables_options['aoColumns'] # set aoColumnDefs in datatables_options - might already have stuff in there aoColumnDefs = self.datatables_options.setdefault ('aoColumnDefs',[])
def create(self): query = Query.create(self.type) # No filters for create if self.params : for p in self.params : for k,v in p.iteritems() : logger.debug("param: {} : {}".format(k, v)) query.set({k : v}) logger.debug("query = {}".format(query)) else: raise Exception, "Params are required for create" return execute_query(self.request, query)
def _store(self, request, key, value): "internal - set key, attach and create extension if needed" session = request.session if cache_key not in session: session[cache_key] = uuid.uuid1().int cached_uuid = session[cache_key] if cached_uuid not in self: self[cached_uuid] = _SessionExtension() extension = self[cached_uuid] setattr(extension, key, value) logger.debug("sessioncache._store_{} session={}".format( key, SessionCache._debug_session(session)))
def end_session(self, request): """ Clear all data related to this request's session has we are logging out This is for garbage collection """ session = request.session logger.debug("SessionCache.end_session() {}".format( self._debug_session(session))) if cache_key not in session: return cached_uuid = session[cache_key] if cached_uuid in self: del self[cached_uuid]
def _get(self, request, key): "internal - retrieve key - do not create anything" session = request.session logger.debug("sessioncache._get_{} session={}".format( key, SessionCache._debug_session(session))) # self._debug(request) if cache_key not in session: return None cached_uuid = session[cache_key] if cached_uuid not in self: return None extension = self[cached_uuid] return getattr(extension, key)
def sfa_client(request, method, hrn=None, urn=None, object_type=None, rspec=None, recursive=False, options=None, platforms=None, output_format=None, admin=False): Config = ConfigParser.ConfigParser() monitor_file = os.path.abspath( os.path.dirname(__file__) + '/../myslice/monitor.ini') Config.read(monitor_file) if admin: user_email, admin_password = config.manifold_admin_user_password() else: #logger.debug(request.session['user']['email']) user_email = request.session['user']['email'] results = dict() if hrn is None: hrn = '' if urn is None: urn = '' if object_type is None: object_type = '' if rspec is None: rspec = '' else: logger.debug("RSPEC = %s" % rspec) if recursive is None: recursive = False if options is None: options = dict() if platforms is None: platforms = list() if method not in ['GetVersion', 'ListResources']: try: if not hrn: hrn = urn_to_hrn(urn) else: urn = hrn_to_urn(hrn, object_type) except Exception, e: logger.error(e) raise Exception, "Provide urn OR hrn + type as parameters of method %s" % method
def prelude_env(self): env = {} env['js_urls'] = [ Prelude.full_url(js_file) for js_file in self.js_files ] env['css_urls'] = [ Prelude.full_url(css_file) for css_file in self.css_files ] env['all_js_chunks'] = self.js_init_chunks + self.js_chunks env['css_chunks'] = self.css_chunks if debug: logger.debug("prelude has {} js_files, {} css files, ({}+{}) js chunks and {} css_chunks"\ .format (len(self.js_files), len(self.css_files), len(self.js_init_chunks), len(self.js_chunks), len(self.css_chunks),)) # render this with prelude.html and put the result in header_prelude header_prelude = render_to_string('prelude.html', env) return {'header_prelude': header_prelude}
def update(self): query = Query.update(self.type) query = self.applyFilters(query, True) if self.params : for p in self.params : for k,v in p.iteritems() : logger.debug("param: {} : {}".format(k, v)) query.set({k : v}) logger.debug("query = {}".format(query)) else: raise Exception, "Params are required for update" if self.id is not None: query.select(self.id) return execute_query(self.request, query)
def dispatch(request, object_type, object_name): o = ObjectRequest(request, object_type, object_name) if request.method == 'POST': req_items = request.POST elif request.method == 'GET': #return error('only post request is supported') req_items = request.GET logger.debug(req_items) for el in req_items.items(): logger.debug("#===============> {}".format(el)) if el[0].startswith('filters'): o.filters[el[0][8:-1]] = el[1] elif el[0].startswith('params'): logger.debug("#======> 0 {}".format(el[0])) logger.debug("#======> 1 {}".format(req_items.getlist(el[0]))) if (el[0][-2:] == '[]'): # when receiving params[key][] = 'value1' ... #o.params.append({el[0][7:-3]:",".join(req_items.getlist(el[0]))}) o.params.append({el[0][7:-3]: req_items.getlist(el[0])}) else: # when receiving params[key] = 'value' o.params.append({el[0][7:-1]: el[1]}) logger.debug("o.params = {}".format(o.params)) elif el[0].startswith('fields'): o.fields = req_items.getlist('fields[]') elif el[0].startswith('options'): o.options = req_items.getlist('options[]') try: response = o.update() if response: return success('record updated') else: return error('an error has occurred') except Exception, e: return error("exception:" + str(e))
def func(*args, **kwds): import time start = time.time() # the message to display auth_message = "<AuthMethod not set in {}>".format(self.auth) if 'AuthMethod' not in self.auth \ else "[session]" if self.auth['AuthMethod'] == 'session' \ else "user:{}".format(self.auth['Username']) if self.auth['AuthMethod'] == 'password' \ else "anonymous" if self.auth['AuthMethod'] == 'anonymous' \ else "[???]" + "{}".format(self.auth) end_message = "MANIFOLD <- {}( {}( {} ) ) with auth={} to {}"\ .format(methodName, args[0]['action'] or '', args[0]['object'] or '', auth_message, self.url) try: args += ({'authentication': self.auth}, ) result = getattr(self.server, methodName)(*args, **kwds) logger.debug("{} executed in {} seconds -> {}"\ .format(end_message, time.time() - start, truncate_result(result))) return ResultValue(**result) except Exception as error: logger.error( "===== xmlrpc catch-all exception: {}".format(error)) import traceback logger.error(traceback.format_exc(limit=3)) if "Connection refused" in error: raise ManifoldException( ManifoldResult(code=ManifoldCode.SERVER_UNREACHABLE, output="{} answered {}".format( self.url, error))) # otherwise logger.error("{} FAILED - executed in {} seconds"\ .format(end_message, time.time() - start)) logger.error("MANIFOLD {}".format(error)) raise ManifoldException( ManifoldResult(code=ManifoldCode.SERVER_UNREACHABLE, output="{}".format(error)))
def _debug(self, request): session = request.session logger.debug("SessionCache: ---------- with session {}".format( self._debug_session(session))) for k, v in self.iteritems(): logger.debug("SessionCache {} -> {}".format(k, v)) if cache_key not in session: return cached_uuid = session[cache_key] if cached_uuid not in self: return extension = self[cached_uuid] logger.debug("SessionCache: found extension {}".format(extension)) logger.debug("SessionCache: ----------")
def post(self, request, *args, **kwargs): c = restclient.Client(SLA_COLLECTOR_URL) data = request.POST.copy() testbed_urn_regex = r"\+(.*?)\+" pattern = re.compile(testbed_urn_regex) testbed_urn = pattern.search( data["SLIVER_INFO_AGGREGATE_URN"]).group(1) # Fix for django QueryDict list parameters slivers = data.getlist("SLIVER_INFO_URN[]") data["SLIVER_INFO_URN"] = slivers del data["SLIVER_INFO_URN[]"] # Timestamp to ISO date + timezone tstmp = data["SLIVER_INFO_EXPIRATION"] dt = datetime.fromtimestamp(float(tstmp)) # gmt_2 = pytz.timezone("Etc/GMT-2") # dlocal = gmt_2.localize(dt).isoformat() dlocal = dt.isoformat() + "CET" data["SLIVER_INFO_EXPIRATION"] = dlocal # logger.debug("SLA Agreement parameters: {}".format(data.dict())) # import pdb; pdb.set_trace() try: response = c.post("agreementslist/", data=json.dumps(data), headers={ "accept": "application/json", "content-type": "application/json" }) except Exception as e: # import traceback, sys # # traceback.print_exc(file=sys.stdout) logger.debug("SLA Error: CreateAgreement {}".format(e.message)) return HttpResponse(response.text, status=response.status_code)
def get (self, request, slicename, state=None): username = self.request.user platforms = self.get_platforms(request) cloud_platforms = ["onelab-cloud","fuseco"] len_platforms = len(platforms) result = sfa_client(request,'ListResources',platforms=cloud_platforms) # Handle errors in ListResources, example AM is down for key, value in result.iteritems(): logger.debug("key in result = %s" % key) if 'error' in value: cloud_platforms.remove(key) env = { 'theme' : self.theme, 'slicename':slicename, 'platforms':platforms, 'result':result, 'cloud_platforms':cloud_platforms, 'len_platforms': len_platforms, 'request':self.request, } return render_to_response(self.template, env, context_instance=RequestContext(request))
def dispatch(request, method): hrn = None urn = None object_type = None rspec = None output_format = None recursive = False # Have to be hashable for lru_cache options = frozenset() # dict() platforms = frozenset() # list() results = dict() display = None if request.method == 'POST': req_items = request.POST elif request.method == 'GET': req_items = request.GET logger.debug("dispatch got = %s" % req_items.dict()) #t = dict(req_items.iterlists()) #rspec = req_items.getlist('rspec') #logger.debug("dispatch got = %s" % t) platforms = req_items.getlist('platform[]') for k in req_items.dict(): logger.debug("key = %s - value = %s" % (k, req_items.get(k))) if k == 'rspec': rspec = req_items.get(k) if k == 'options': options = req_items.get(k) if k == 'output_format': output_format = req_items.get(k) if k == 'hrn': hrn = req_items.get(k) if k == 'urn': urn = req_items.get(k) if k == 'type': object_type = req_items.get(k) if k == 'recursive': if v == '1': recursive = True else: recursive = False if k == 'display': display = req_items.get(k) if rspec is not None: try: rspec = json.loads(rspec) except Exception, e: logger.debug("rspec type = %s" % type(rspec)) if type(rspec) is dict: rspec = xmltodict.unparse(rspec)
def get_context_data(self, **kwargs): page = Page(self.request) page.add_js_files(["js/common.functions.js"]) for key, value in kwargs.iteritems(): logger.debug("{} = {}".format(key, value)) if key == "platformname": platformname = value network_query = Query().get('local:platform')\ .filter_by('platform', '==', platformname)\ .select('platform','platform_longname','gateway_type') page.enqueue_query(network_query) # ListResources of the platform metadata = page.get_metadata() resource_md = metadata.details_by_object('resource') resource_fields = [column['name'] for column in resource_md['column']] resources = platformname + ':resource' query_resource_all = Query.get(resources).select(resource_fields) page.enqueue_query(query_resource_all) query_resource_default_fields = Query.get(resources).select( 'hrn', 'hostname', 'type', 'country') page.enqueue_query(query_resource_default_fields) page.expose_js_metadata() networklist = QueryTable( page=page, title='List', domid='checkboxes', # this is the query at the core of the slice list query=network_query, query_all=network_query, checkboxes=False, datatables_options={ 'iDisplayLength': 25, 'bLengthChange': True, }, ) # networklist = SimpleList( # title = None, # page = page, # key = 'platform', # query = network_query, # ) # # -------------------------------------------------------------------------- # RESOURCES # for internal use in the querytable plugin; # needs to be a unique column present for each returned record main_query_init_key = 'hrn' # the resources part is made of a Tabs (Geographic, List), resources_as_gmap = GoogleMap( page=page, title='Geographic view', domid='resources-map', # tab's sons preferably turn this off togglable=False, query=query_resource_default_fields, query_all=query_resource_all, # this key is the one issued by google googlemap_api_key=ConfigEngine().googlemap_api_key(), # the key to use at init-time init_key=main_query_init_key, checkboxes=False, # center on Paris latitude=49., longitude=9, zoom=4, ) resources_as_3dmap = SensLabMap( page=page, title='3D Map', domid='senslabmap', query=query_resource_default_fields, query_all=query_resource_all, ) resources_as_list = QueryTable( page=page, domid='resources-list', title='List view', # this is the query at the core of the slice list query=query_resource_default_fields, query_all=query_resource_all, init_key=main_query_init_key, checkboxes=False, datatables_options={ 'iDisplayLength': 25, 'bLengthChange': True, 'bAutoWidth': True, }, ) resources_sons = [ resources_as_gmap, resources_as_3dmap, resources_as_list, ] resources_area = Tabs( page=page, domid="resources", togglable=True, title="Resources", outline_complete=True, sons=resources_sons, active_domid='resources-map', persistent_active=True, ) context = super(PlatformView, self).get_context_data(**kwargs) context['person'] = self.request.user context['networks'] = networklist.render(self.request) context['resources'] = resources_area.render(self.request) # XXX This is repeated in all pages # more general variables expected in the template context['title'] = 'Platforms connected to MySlice' # the menu items on the top context['topmenu_items'] = topmenu_items_live('Platforms', page) # so we can sho who is logged context['username'] = the_user(self.request) context['theme'] = self.theme context.update(page.prelude_env()) return context
def get_context_data(self, **kwargs): pi = "" # We might have slices on different registries with different user accounts # We note that this portal could be specific to a given registry, to which we register users, but i'm not sure that simplifies things # Different registries mean different identities, unless we identify via SFA HRN or have associated the user email to a single hrn #messages.info(self.request, 'You have logged in') page = Page(self.request) ctx_my_authorities = {} ctx_delegation_authorities = {} ctx_sub_authorities = {} dest = {} # The user need to be logged in if the_user(self.request): # Who can a PI validate: # His own authorities + those he has credentials for. # In MySlice we need to look at credentials also. # XXX This will have to be asynchroneous. Need to implement barriers, # for now it will be sufficient to have it working statically # get user_id to later on query accounts # XXX Having real query plan on local tables would simplify all this # XXX $user_email is still not available for local tables #user_query = Query().get('local:user').filter_by('email', '==', '$user_email').select('user_id') user_query = Query().get('local:user').filter_by( 'email', '==', the_user(self.request)).select('user_id') user, = execute_query(self.request, user_query) user_id = user['user_id'] # Query manifold to learn about available SFA platforms for more information # In general we will at least have the portal # For now we are considering all registries all_authorities = [] platform_ids = [] sfa_platforms_query = Query().get('local:platform').filter_by( 'gateway_type', '==', 'sfa').select('platform_id', 'platform', 'auth_type') sfa_platforms = execute_query(self.request, sfa_platforms_query) for sfa_platform in sfa_platforms: logger.info("SFA PLATFORM > {}".format( sfa_platform['platform'])) if not 'auth_type' in sfa_platform: continue auth = sfa_platform['auth_type'] if not auth in all_authorities: all_authorities.append(auth) platform_ids.append(sfa_platform['platform_id']) logger.warning("W: Hardcoding platform myslice") # There has been a tweak on how new platforms are referencing a # so-called 'myslice' platform for storing authentication tokens. # XXX This has to be removed in final versions. myslice_platforms_query = Query().get('local:platform').filter_by( 'platform', '==', 'myslice').select('platform_id') myslice_platforms = execute_query(self.request, myslice_platforms_query) if myslice_platforms: myslice_platform, = myslice_platforms platform_ids.append(myslice_platform['platform_id']) # We can check on which the user has authoritity credentials = PI rights credential_authorities = set() credential_authorities_expired = set() # User account on these registries user_accounts_query = Query.get('local:account').filter_by( 'user_id', '==', user_id).filter_by('platform_id', 'included', platform_ids).select('auth_type', 'config') user_accounts = execute_query(self.request, user_accounts_query) #print "=" * 80 #print user_accounts #print "=" * 80 for user_account in user_accounts: logger.debug("USER ACCOUNT {}".format(user_account)) if user_account['auth_type'] == 'reference': continue # we hardcoded the myslice platform... config = json.loads(user_account['config']) creds = [] logger.debug("CONFIG KEYS {}".format(config.keys())) if 'authority_credentials' in config: logger.debug("*** AC {}".format( config['authority_credentials'].keys())) for authority_hrn, credential in config[ 'authority_credentials'].items(): #if credential is not expired: credential_authorities.add(authority_hrn) #else # credential_authorities_expired.add(authority_hrn) if 'delegated_authority_credentials' in config: logger.debug("*** DAC {}".format( config['delegated_authority_credentials'].keys())) for authority_hrn, credential in config[ 'delegated_authority_credentials'].items(): #if credential is not expired: credential_authorities.add(authority_hrn) #else # credential_authorities_expired.add(authority_hrn) logger.debug( 'credential_authorities = {}'.format(credential_authorities)) logger.debug('credential_authorities_expired = {}'.format( credential_authorities_expired)) # # Using cache manifold-tables to get the list of authorities faster # all_authorities_query = Query.get('authority').select('name', 'authority_hrn') # all_authorities = execute_query(self.request, all_authorities_query) # ** Where am I a PI ** # For this we need to ask SFA (of all authorities) = PI function pi_authorities_query = Query.get('myslice:user').filter_by( 'user_hrn', '==', '$user_hrn').select('pi_authorities') pi_authorities_tmp = execute_query(self.request, pi_authorities_query) pi_authorities = set() try: for pa in pi_authorities_tmp: pi_authorities |= set(pa['pi_authorities']) except Exception as e: logger.error('No pi_authorities') # TODO: exception if no parent_authority # try: # for pa in pi_authorities_tmp: # pi_authorities |= set(pa['pi_authorities']) # except: # # include all sub-authorities of the PI # # if PI on ple, include all sub-auths ple.upmc, ple.inria and so on... # pi_subauthorities = set() # for authority in all_authorities: # authority_hrn = authority['authority_hrn'] # for my_authority in pi_authorities: # if authority_hrn.startswith(my_authority) and authority_hrn not in pi_subauthorities: # pi_subauthorities.add(authority_hrn) #print "pi_authorities =", pi_authorities #print "pi_subauthorities =", pi_subauthorities # My authorities + I have a credential pi_credential_authorities = pi_authorities & credential_authorities pi_no_credential_authorities = pi_authorities - credential_authorities - credential_authorities_expired pi_expired_credential_authorities = pi_authorities & credential_authorities_expired # Authorities I've been delegated PI rights pi_delegation_credential_authorities = credential_authorities - pi_authorities pi_delegation_expired_authorities = credential_authorities_expired - pi_authorities #print "pi_credential_authorities =", pi_credential_authorities #print "pi_no_credential_authorities =", pi_no_credential_authorities #print "pi_expired_credential_authorities =", pi_expired_credential_authorities #print "pi_delegation_credential_authorities = ", pi_delegation_credential_authorities #print "pi_delegation_expired_authorities = ", pi_delegation_expired_authorities # Summary intermediary pi_my_authorities = pi_credential_authorities | pi_no_credential_authorities | pi_expired_credential_authorities pi_delegation_authorities = pi_delegation_credential_authorities | pi_delegation_expired_authorities #print "--" #print "pi_my_authorities = ", pi_my_authorities #print "pi_delegation_authorities = ", pi_delegation_authorities #print "pi_subauthorities = ", pi_subauthorities # Summary all queried_pending_authorities = pi_my_authorities | pi_delegation_authorities #| pi_subauthorities #print "----" #print "queried_pending_authorities = ", queried_pending_authorities # iterate on the requests and check if the authority matches a prefix startswith an authority on which the user is PI requests = get_requests() # requests = get_requests(queried_pending_authorities) for request in requests: auth_hrn = request['authority_hrn'] for my_auth in pi_my_authorities: if auth_hrn.startswith(my_auth): dest = ctx_my_authorities request['allowed'] = 'allowed' for my_auth in pi_delegation_authorities: if auth_hrn.startswith(my_auth): dest = ctx_delegation_authorities request['allowed'] = 'allowed' if auth_hrn in pi_expired_credential_authorities: request['allowed'] = 'expired' if 'allowed' not in request: request['allowed'] = 'denied' #print "authority for this request", auth_hrn # if auth_hrn in pi_my_authorities: # dest = ctx_my_authorities # # # define the css class # if auth_hrn in pi_credential_authorities: # request['allowed'] = 'allowed' # elif auth_hrn in pi_expired_credential_authorities: # request['allowed'] = 'expired' # else: # pi_no_credential_authorities # request['allowed'] = 'denied' # # elif auth_hrn in pi_delegation_authorities: # dest = ctx_delegation_authorities # # if auth_hrn in pi_delegation_credential_authorities: # request['allowed'] = 'allowed' # else: # pi_delegation_expired_authorities # request['allowed'] = 'expired' # # elif auth_hrn in pi_subauthorities: # dest = ctx_sub_authorities # # if auth_hrn in pi_subauthorities: # request['allowed'] = 'allowed' # else: # pi_delegation_expired_authorities # request['allowed'] = 'denied' # # else: # continue if not auth_hrn in dest: dest[auth_hrn] = [] dest[auth_hrn].append(request) context = super(ValidatePendingView, self).get_context_data(**kwargs) logger.debug("testing") logger.debug(ctx_my_authorities) context['my_authorities'] = ctx_my_authorities context['sub_authorities'] = ctx_sub_authorities context['delegation_authorities'] = ctx_delegation_authorities # XXX This is repeated in all pages # more general variables expected in the template context['title'] = 'Test view that combines various plugins' # the menu items on the top context['topmenu_items'] = topmenu_items_live('Validation', page) # so we can sho who is logged context['username'] = the_user(self.request) context['pi'] = "is_pi" context['theme'] = self.theme context['section'] = "Requests" # XXX We need to prepare the page for queries #context.update(page.prelude_env()) return context
def _proxy(url, request, format): """the view associated with /manifold/proxy/ with the query passed using POST""" # expecting a POST if request.method != 'POST': logger.error("MANIFOLDPROXY unexpected method {} -- exiting".format(request.method)) return HttpResponse ({"ret":0}, content_type="application/json") # we only support json for now # if needed in the future we should probably cater for # format_in : how is the query encoded in POST # format_out: how to serve the results if format != 'json': logger.error("MANIFOLDPROXY unexpected format {} -- exiting".format(format)) return HttpResponse ({"ret":0}, content_type="application/json") try: # translate incoming POST request into a query object #logger.debug("MANIFOLDPROXY request.POST {}".format(request.POST)) manifold_query = Query() #manifold_query = ManifoldQuery() manifold_query.fill_from_POST(request.POST) # retrieve session for request # We allow some requests to use the ADMIN user account if (manifold_query.get_from() == 'local:user' and manifold_query.get_action() == 'create') \ or (manifold_query.get_from() == 'local:platform' and manifold_query.get_action() == 'get'): admin_user, admin_password = config.manifold_admin_user_password() manifold_api_session_auth = {'AuthMethod': 'password', 'Username': admin_user, 'AuthString': admin_password} else: if 'manifold' in request.session: manifold_api_session_auth = request.session['manifold']['auth'] else: #manifold_api_session_auth = SessionCache().get_auth(request) #if not manifold_api_session_auth: return HttpResponse (json.dumps({'code':0,'value':[]}), content_type="application/json") if debug_empty and manifold_query.action.lower()=='get': return HttpResponse (json.dumps({'code':0,'value':[]}), content_type="application/json") # actually forward manifold_api= ManifoldAPI(url, auth=manifold_api_session_auth) # for the benefit of the python code, manifoldAPI raises an exception if something is wrong # however in this case we want to propagate the complete manifold result to the js world result = manifold_api.forward(manifold_query.to_dict()) # XXX TEMP HACK if 'description' in result and result['description'] \ and isinstance(result['description'], (tuple, list, set, frozenset)): result [ 'description' ] = [ ResultValue.to_html (x) for x in result['description'] ] # # register activity # # resource reservation if (manifold_query.action.lower() == 'update') : logger.debug(result['value'][0]) if 'resource' in result['value'][0] : for resource in result['value'][0]['resource'] : activity.slice.resource(request, { 'slice' : result['value'][0]['slice_hrn'], 'resource' : resource['hostname'], 'resource_type' : resource['type'], 'facility' : resource['facility_name'], 'testbed' : resource['testbed_name'] } ) json_answer=json.dumps(result) return HttpResponse (json_answer, content_type="application/json") except Exception as e: logger.error("MANIFOLDPROXY {}".format(e)) import traceback logger.error(traceback.format_exc()) return HttpResponse ({"ret":0}, content_type="application/json")
user_local_config = json.loads(user_local_config) user_local_authority = user_local_config.get( 'authority') if 'user_details' not in env or 'parent_authority' not in env[ 'user_details'] or env['user_details'][ 'parent_authority'] is None: env['user_details'] = { 'parent_authority': user_local_authority } except Exception, e: env['error'] = "Please check your Credentials" else: env['project'] = True env['user_details'] = {'parent_authority': authority_hrn} logger.debug("BEFORE ####------#### is_pi") logger.debug("is_pi = {}".format(is_pi)) pi = is_pi(self.request, '$user_hrn', env['user_details']['parent_authority']) else: env['person'] = None pi = False env['theme'] = self.theme env['section'] = "Institution" env['pi'] = pi env['username'] = the_user(request) env['topmenu_items'] = topmenu_items(None, request) if state: env['state'] = state elif not env['username']: env['state'] = None # use one or two columns for the layout - not logged in users will see the login prompt env['layout_1_or_2'] = "layout-unfold2.html" if not env[
if type(rspec) is dict: rspec = xmltodict.unparse(rspec) start_time = time.time() results = sfa_client(request, method, hrn=hrn, urn=urn, object_type=object_type, rspec=rspec, recursive=recursive, options=options, platforms=platforms, output_format=output_format, admin=False) logger.debug("EXEC TIME - sfa_client() - %s sec." % (time.time() - start_time)) if display == 'table': return render_to_response( 'table-default.html', { 'data': data, 'fields': columns, 'id': '@component_id', 'options': None }) else: return HttpResponse(json.dumps(results, cls=MyEncoder), content_type="application/json") def get_user_account(request, user_email, platform_name): """
} recipients = authority_get_pi_emails(request,reg_auth) # backup email: if authority_get_pi_emails fails recipients.append('*****@*****.**') msg = render_to_string('user_request_email.txt', ctx) send_mail("Onelab New User request for %s submitted"%reg_email, msg, '*****@*****.**', recipients) return render(request, 'user_register_complete.html') ======= if wsgi_request.is_secure(): current_site = 'https://' else: current_site = 'http://' current_site += wsgi_request.META['HTTP_HOST'] logger.debug("############ BREAKPOINT 3 #################") post_email = wsgi_request.POST.get('email','').lower() salt = randint(1,100000) email_hash = md5(str(salt)+post_email).hexdigest() #email_hash = md5(post_email).digest().encode('base64')[:-1] user_request = { 'first_name' : wsgi_request.POST.get('firstname', ''), 'last_name' : wsgi_request.POST.get('lastname', ''), 'authority_hrn' : wsgi_request.POST.get('org_name', ''), 'email' : post_email, 'password' : wsgi_request.POST.get('password', ''), 'current_site' : current_site, 'email_hash' : email_hash, 'pi' : '', 'validation_link': current_site + '/portal/email_activation/'+ email_hash }
def get_context_data(self, **kwargs): # We might have slices on different registries with different user accounts # We note that this portal could be specific to a given registry, to which we register users, but i'm not sure that simplifies things # Different registries mean different identities, unless we identify via SFA HRN or have associated the user email to a single hrn #messages.info(self.request, 'You have logged in') page = Page(self.request) logger.info("Dashboard page") # Slow... #slice_query = Query().get('slice').filter_by('user.user_hrn', 'contains', user_hrn).select('slice_hrn') testbed_query = Query().get('network').select('network_hrn', 'platform', 'version') # DEMO GEC18 Query only PLE # user_query = Query().get('local:user').select('config','email') # user_details = execute_query(self.request, user_query) # not always found in user_details... # config={} # for user_detail in user_details: # #email = user_detail['email'] # if user_detail['config']: # config = json.loads(user_detail['config']) # user_detail['authority'] = config.get('authority',"Unknown Authority") # # print user_detail # if user_detail['authority'] is not None: # sub_authority = user_detail['authority'].split('.') # root_authority = sub_authority[0] # slice_query = Query().get(root_authority+':user').filter_by('user_hrn', '==', '$user_hrn').select('user_hrn', 'slice.slice_hrn') # else: logger.debug("SLICE QUERY") logger.debug("-" * 80) slice_query = Query().get('myslice:user').filter_by( 'user_hrn', '==', '$user_hrn').select('slices.slice_hrn') page.enqueue_query(slice_query) page.enqueue_query(testbed_query) slicelist = SliceList( page=page, title="slices", warning_msg="<a href='../slice_request'>Request Slice</a>", query=slice_query, ) testbedlist = TestbedList( page=page, title="testbeds", query=testbed_query, ) context = super(DashboardView, self).get_context_data(**kwargs) context['person'] = self.request.user context['testbeds'] = testbedlist.render(self.request) context['slices'] = slicelist.render(self.request) # XXX This is repeated in all pages # more general variables expected in the template context['title'] = 'Dashboard' # the menu items on the top context['topmenu_items'] = topmenu_items_live('Dashboard', page) # so we can sho who is logged context['username'] = the_user(self.request) context['theme'] = self.theme page.expose_js_metadata() # the page header and other stuff context.update(page.prelude_env()) return context
def get(self, request, slicename): page = Page(request) agreement_id = None enforcements = {} violations = {} keys = [ 'provider', 'agreement', 'date', 'status', 'result', 'ok', 'slivers' ] ag_info = [] agreements = _get_agreements_by_slice(slicename) for agreement in agreements: row = [] provider = agreement.context.provider row.append(provider) # Provider row.append(agreement) # Agreement row.append(agreement.context.time_formatted()) # Date enf = _get_enforcement(agreement.agreement_id, provider) if enf.enabled == 'true': row.append('Evaluating') # Status row.append('') # Result row.append('') # Ok else: if agreement.guaranteestatus == "NON_DETERMINED": row.append('Provisioned') # Status row.append('') # Result row.append('') # Ok else: row.append('Finished') # Status violations_list = _get_agreement_violations( agreement.agreement_id, provider, "GT_Performance") if len(violations_list) > 0: value = '%.2f' % float(violations_list[0].actual_value) row.append('%d' % (float(value) * 100)) # Result else: row.append('100') # Result if agreement.guaranteestatus == "VIOLATED": row.append('false') # Ok if agreement.guaranteestatus == "FULFILLED": row.append('true') # Ok for _, terms in agreement.guaranteeterms.items(): try: s = ast.literal_eval(terms.scopes[0].scope.lstrip()) logger.debug('SLA scope: {}'.format(s)) row.append(s) break except Exception as e: logger.debug("SLA EXCEPTION: {}".format(e.message)) ag_info.append(dict(zip(keys, row))) template_env = {} # write something of our own instead # more general variables expected in the template template_env['title'] = 'SLA Agreements' template_env['agreements'] = agreements template_env['username'] = request.user template_env['slicename'] = slicename template_env['enforcements'] = enforcements template_env['last_violation_list'] = violations template_env['ag_info'] = ag_info # the prelude object in page contains a summary of the requirements() for all plugins # define {js,css}_{files,chunks} prelude_env = page.prelude_env() template_env.update(prelude_env) return render_to_response(self.template_name, template_env, context_instance=RequestContext(request))
def get (self, request, state=None): env = self.default_env() acc_auth_cred={} try: if request.user.is_authenticated(): jfed_identity = get_jfed_identity(request) if jfed_identity is not None: import base64 encoded_jfed_identity = base64.b64encode(jfed_identity) env['jfed_identity'] = encoded_jfed_identity else: env['jfed_identity'] = None ## check user is pi or not platform_details = {} account_details = {} acc_auth_cred = {} acc_user_cred = {} platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled') account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config') # XXX Something like an invalid session seems to make the execute fail sometimes, and thus gives an error on the main page account_detail = get_myslice_account(self.request) if 'config' in account_detail and account_detail['config'] is not '': account_config = json.loads(account_detail['config']) acc_auth_cred = account_config.get('delegated_authority_credentials','N/A') acc_user_cred = account_config.get('delegated_user_credential','N/A') # assigning values #if acc_auth_cred=={} or acc_auth_cred=='N/A': # pi = "is_not_pi" #else: # pi = "is_pi" user_email = str(self.request.user) #pi = authority_check_pis(self.request, user_email) # check if the user has creds or not if acc_user_cred == {} or acc_user_cred == 'N/A': user_cred = 'no_creds' else: exp_date = get_expiration(acc_user_cred, 'timestamp') if exp_date < time.time(): user_cred = 'creds_expired' else: user_cred = 'has_creds' # list the pending slices of this user pending_slices = [] for slices in PendingSlice.objects.filter(type_of_nodes__iexact=self.request.user).all(): pending_slices.append(slices.slice_name) env['pending_slices'] = pending_slices #env['pi'] = pi env['user_cred'] = user_cred env['person'] = self.request.user else: env['person'] = None except Exception as e: print e env['person'] = None env['state'] = "Your session has expired" env['theme'] = self.theme env['section'] = "Dashboard" logger.debug("homeview called") env['next'] = request.GET.get('next',None) env['username']=the_user(request) env['topmenu_items'] = topmenu_items(None, request) env['request'] = request if state: env['state'] = state elif not env['username']: env['state'] = None # use one or two columns for the layout - not logged in users will see the login prompt return render_to_response(self.template, env, context_instance=RequestContext(request))