def wrapper(*args, **kwargs): """ Decorator wrapper """ self = args[0] try: # Try auth = self.request.headers.get('Authorization') scheme, _, token = auth.partition(' ') if scheme.lower() == 'basic': # Decode user and password user, _, pwd = base64.decodestring(token).partition(':') if user == CONFIG.get('api_user') and pwd == CONFIG.get('api_pass'): return fun(*args, **kwargs) else: # Raise UNAUTHORIZED HTTP Error (401) logging.error("Unauthorized access from: %s", self.request.headers) raise tornado.web.HTTPError(UNAUTHORIZED) else: # We only support basic authentication logging.error("Authentication scheme not recognized") return "Authentication scheme not recognized" except AttributeError: # Raise UNAUTHORIZED HTTP Error (401) if the request is not # using autentication (auth will be None and partition() will fail logging.error("Unauthorized access from: %s", self.request.headers) raise tornado.web.HTTPError(UNAUTHORIZED)
def wrapper(*args, **kwargs): """ Decorator wrapper """ self = args[0] try: # Try auth = self.request.headers.get('Authorization') scheme, _, token = auth.partition(' ') if scheme.lower() == 'basic': # Decode user and password user, _, pwd = base64.decodestring(token).partition(':') if user == CONFIG.get('api_user') and pwd == CONFIG.get( 'api_pass'): return fun(*args, **kwargs) else: # Raise UNAUTHORIZED HTTP Error (401) logging.error("Unauthorized access from: %s", self.request.headers) raise tornado.web.HTTPError(UNAUTHORIZED) else: # We only support basic authentication logging.error("Authentication scheme not recognized") return "Authentication scheme not recognized" except AttributeError: # Raise UNAUTHORIZED HTTP Error (401) if the request is not # using autentication (auth will be None and partition() will fail logging.error("Unauthorized access from: %s", self.request.headers) raise tornado.web.HTTPError(UNAUTHORIZED)
# Copyright (C) 2015, CERN # This software is distributed under the terms of the GNU General Public # Licence version 3 (GPL Version 3), copied verbatim in the file "LICENSE". # In applying this license, CERN does not waive the privileges and immunities # granted to it by virtue of its status as Intergovernmental Organization # or submit itself to any jurisdiction. """ REST API Server for the DB On Demand System """ import cx_Oracle as cx import logging from dbod.config import CONFIG _dsnfim = cx.makedsn(CONFIG.get("fim_host"), CONFIG.get("fim_port"), service_name=CONFIG.get("fim_servname")) _FIMPOOL = cx.SessionPool(CONFIG.get("fim_user"), CONFIG.get("fim_pass"), dsn=_dsnfim, min=1, max=5, increment=1) def get_connection(): """Gets and returns a connection from the pool""" try: return _FIMPOOL.acquire() except cx.DatabaseError as dberr: logging.error("FIM Database Error: %s", dberr) def end_connection(con): """Release a connection back to the pool""" _FIMPOOL.release(con)
# Copyright (C) 2015, CERN # This software is distributed under the terms of the GNU General Public # Licence version 3 (GPL Version 3), copied verbatim in the file "LICENSE". # In applying this license, CERN does not waive the privileges and immunities # granted to it by virtue of its status as Intergovernmental Organization # or submit itself to any jurisdiction. """ REST API Server for the DB On Demand System """ import cx_Oracle as cx import logging from dbod.config import CONFIG _dsnoracle = cx.makedsn(CONFIG.get('inst_host'), CONFIG.get('inst_port'), service_name = CONFIG.get('inst_servname')) _ORAPOOL = cx.SessionPool(CONFIG.get('inst_user'), CONFIG.get('inst_pass'), dsn=_dsnoracle, min=1, max=5, increment=1) def get_connection(): """Gets and returns a connection from the pool""" try: return _ORAPOOL.acquire() except cx.DatabaseError as dberr: logging.error("Instance Database Error: %s", dberr) def end_connection(con): """Releases a connection back to the pool"""
# Copyright (C) 2015, CERN # This software is distributed under the terms of the GNU General Public # Licence version 3 (GPL Version 3), copied verbatim in the file "LICENSE". # In applying this license, CERN does not waive the privileges and immunities # granted to it by virtue of its status as Intergovernmental Organization # or submit itself to any jurisdiction. """ REST API Server for the DB On Demand System """ import cx_Oracle as cx import logging from dbod.config import CONFIG _dsnfim = cx.makedsn(CONFIG.get('fim_host'), CONFIG.get('fim_port'), service_name = CONFIG.get('fim_servname')) _FIMPOOL = cx.SessionPool(CONFIG.get('fim_user'), CONFIG.get('fim_pass'), dsn=_dsnfim, min=1, max=5, increment=1) def get_connection(): """Gets and returns a connection from the pool""" try: return _FIMPOOL.acquire() except cx.DatabaseError as dberr: logging.error("FIM Database Error: %s", dberr) def end_connection(con): """Release a connection back to the pool"""
# granted to it by virtue of its status as Intergovernmental Organization # or submit itself to any jurisdiction. """ This file contains all database related code """ from psycopg2 import connect, DatabaseError, pool, errorcodes import sys, traceback, logging from dbod.config import CONFIG try: POOL = pool.ThreadedConnectionPool( 5, # Min. # of connections 20, # Max. # of connections database=CONFIG.get('db_name'), user=CONFIG.get('db_user'), host=CONFIG.get('db_host'), port=CONFIG.get('db_port'), password=CONFIG.get('db_pass')) except DatabaseError as dberr: logging.error("PG Error: %s", errorcodes.lookup(dberr.pgcode[:2])) logging.error("PG Error: %s", errorcodes.lookup(dberr.pgcode)) def get_metadata(entity): """Returns a JSON object containing all the metadata for a certain entity""" try: entity = str(entity) with POOL.getconn() as conn: with conn.cursor() as curs:
""" from psycopg2 import connect, DatabaseError, pool, errorcodes import sys, traceback, logging import json, datetime from dbod.config import CONFIG # Define the default handler for date objects json.JSONEncoder.default = lambda self,obj: (obj.isoformat() if isinstance(obj, datetime.date) else None) try: POOL = pool.ThreadedConnectionPool( 5, # Min. # of connections 20, # Max. # of connections database = CONFIG.get('db_name'), user = CONFIG.get('db_user'), host = CONFIG.get('db_host'), port = CONFIG.get('db_port'), password = CONFIG.get('db_pass')) except DatabaseError as dberr: logging.error("PG Error: %s", errorcodes.lookup(dberr.pgcode[:2])) logging.error("PG Error: %s", errorcodes.lookup(dberr.pgcode)) def create_resource(username, db_name, e_group, category, expiry_date, db_type, db_size, no_connections, project, description): """Creates an entry in the resources table""" try: with POOL.getconn() as conn: with conn.cursor() as curs: logging.debug("Creating resource entry for %s", db_name) curs.execute("""insert into dod_instance_request(username, db_name, e_group, category, creation_date, expiry_date, db_type, db_size, no_connections, project, description)