import logging.handlers
import getopt
import sys
+
+from auth import Authenticator, AuthException
from engine import Engine, EngineException
from osm_common.dbbase import DbException
from osm_common.fsbase import FsException
from osm_common.msgbase import MsgException
-from base64 import standard_b64decode
from http import HTTPStatus
from codecs import getreader
from os import environ, path
__version__ = "0.1.3"
version_date = "Apr 2018"
database_version = '1.0'
+auth_database_version = '1.0'
"""
North Bound Interface (O: OSM specific; 5,X: SOL005 not implemented yet; O5: SOL005 implemented)
/ns_lcm_op_occs 5 5
/<nsLcmOpOccId> 5 5 5
TO BE COMPLETED 5 5
- /vnfrs O
- /<vnfrId> O
+ /vnf_instances (also vnfrs for compatibility) O
+ /<vnfInstanceId> O
/subscriptions 5 5
/<subscriptionId> 5 X
/admin/v1
def __init__(self):
self.instance += 1
self.engine = Engine()
+ self.authenticator = Authenticator(self.engine)
self.valid_methods = { # contains allowed URL and methods
"admin": {
"v1": {
"<ID>": {"METHODS": ("GET", "PUT", "DELETE")}
},
"vnf_packages": {"METHODS": ("GET", "POST"),
- "<ID>": {"METHODS": ("GET", "DELETE"), "TODO": "PATCH", # GET: vnfPkgInfo
+ "<ID>": {"METHODS": ("GET", "DELETE", "PATCH"), # GET: vnfPkgInfo
"package_content": {"METHODS": ("GET", "PUT"), # package
"upload_from_uri": {"TODO": "POST"}
},
},
"ns_instances": {"METHODS": ("GET", "POST"),
"<ID>": {"METHODS": ("GET", "DELETE"),
- "scale": {"TODO": "POST"},
+ "scale": {"METHODS": "POST"},
"terminate": {"METHODS": "POST"},
"instantiate": {"METHODS": "POST"},
"action": {"METHODS": "POST"},
"vnfrs": {"METHODS": ("GET"),
"<ID>": {"METHODS": ("GET")}
},
+ "vnf_instances": {"METHODS": ("GET"),
+ "<ID>": {"METHODS": ("GET")}
+ },
}
},
}
- def _authorization(self):
- token = None
- user_passwd64 = None
- try:
- # 1. Get token Authorization bearer
- auth = cherrypy.request.headers.get("Authorization")
- if auth:
- auth_list = auth.split(" ")
- if auth_list[0].lower() == "bearer":
- token = auth_list[-1]
- elif auth_list[0].lower() == "basic":
- user_passwd64 = auth_list[-1]
- if not token:
- if cherrypy.session.get("Authorization"):
- # 2. Try using session before request a new token. If not, basic authentication will generate
- token = cherrypy.session.get("Authorization")
- if token == "logout":
- token = None # force Unauthorized response to insert user pasword again
- elif user_passwd64 and cherrypy.request.config.get("auth.allow_basic_authentication"):
- # 3. Get new token from user password
- user = None
- passwd = None
- try:
- user_passwd = standard_b64decode(user_passwd64).decode()
- user, _, passwd = user_passwd.partition(":")
- except Exception:
- pass
- outdata = self.engine.new_token(None, {"username": user, "password": passwd})
- token = outdata["id"]
- cherrypy.session['Authorization'] = token
- # 4. Get token from cookie
- # if not token:
- # auth_cookie = cherrypy.request.cookie.get("Authorization")
- # if auth_cookie:
- # token = auth_cookie.value
- return self.engine.authorize(token)
- except EngineException as e:
- if cherrypy.session.get('Authorization'):
- del cherrypy.session['Authorization']
- cherrypy.response.headers["WWW-Authenticate"] = 'Bearer realm="{}"'.format(e)
- raise
-
def _format_in(self, kwargs):
try:
indata = None
session = None
try:
if cherrypy.request.method == "GET":
- session = self._authorization()
+ session = self.authenticator.authorize()
outdata = "Index page"
else:
raise cherrypy.HTTPError(HTTPStatus.METHOD_NOT_ALLOWED.value,
raise NbiException("Expected application/yaml or application/json Content-Type", HTTPStatus.BAD_REQUEST)
try:
if method == "GET":
- session = self._authorization()
+ session = self.authenticator.authorize()
if token_id:
- outdata = self.engine.get_token(session, token_id)
+ outdata = self.authenticator.get_token(session, token_id)
else:
- outdata = self.engine.get_token_list(session)
+ outdata = self.authenticator.get_token_list(session)
elif method == "POST":
try:
- session = self._authorization()
+ session = self.authenticator.authorize()
except Exception:
session = None
if kwargs:
indata.update(kwargs)
- outdata = self.engine.new_token(session, indata, cherrypy.request.remote)
+ outdata = self.authenticator.new_token(session, indata, cherrypy.request.remote)
session = outdata
cherrypy.session['Authorization'] = outdata["_id"]
self._set_location_header("admin", "v1", "tokens", outdata["_id"])
if not token_id and "id" in kwargs:
token_id = kwargs["id"]
elif not token_id:
- session = self._authorization()
+ session = self.authenticator.authorize()
token_id = session["_id"]
- outdata = self.engine.del_token(token_id)
+ outdata = self.authenticator.del_token(token_id)
session = None
cherrypy.session['Authorization'] = "logout"
# cherrypy.response.cookie["Authorization"] = token_id
_format = None
method = "DONE"
engine_item = None
- rollback = None
+ rollback = []
+ session = None
try:
if not topic or not version or not item:
raise NbiException("URL must contain at least 'topic/version/item'", HTTPStatus.METHOD_NOT_ALLOWED)
return self.token(method, _id, kwargs)
# self.engine.load_dbase(cherrypy.request.app.config)
- session = self._authorization()
+ session = self.authenticator.authorize()
indata = self._format_in(kwargs)
engine_item = item
if item == "subscriptions":
engine_item = "nsrs"
if item == "ns_lcm_op_occs":
engine_item = "nslcmops"
- if item == "vnfrs":
+ if item == "vnfrs" or item == "vnf_instances":
engine_item = "vnfrs"
if engine_item == "vims": # TODO this is for backward compatibility, it will remove in the future
engine_item = "vim_accounts"
if item in ("ns_descriptors_content", "vnf_packages_content"):
_id = cherrypy.request.headers.get("Transaction-Id")
if not _id:
- _id = self.engine.new_item(session, engine_item, {}, None, cherrypy.request.headers,
+ _id = self.engine.new_item(rollback, session, engine_item, {}, None, cherrypy.request.headers,
force=force)
- rollback = {"session": session, "item": engine_item, "_id": _id, "force": True}
completed = self.engine.upload_content(session, engine_item, _id, indata, kwargs,
cherrypy.request.headers)
if completed:
cherrypy.response.headers["Transaction-Id"] = _id
outdata = {"id": _id}
elif item == "ns_instances_content":
- _id = self.engine.new_item(session, engine_item, indata, kwargs, force=force)
- rollback = {"session": session, "item": engine_item, "_id": _id, "force": True}
- self.engine.ns_operation(session, _id, "instantiate", {}, None)
+ _id = self.engine.new_item(rollback, session, engine_item, indata, kwargs, force=force)
+ self.engine.ns_operation(rollback, session, _id, "instantiate", indata, None)
self._set_location_header(topic, version, item, _id)
outdata = {"id": _id}
elif item == "ns_instances" and item2:
- _id = self.engine.ns_operation(session, _id, item2, indata, kwargs)
+ _id = self.engine.ns_operation(rollback, session, _id, item2, indata, kwargs)
self._set_location_header(topic, version, "ns_lcm_op_occs", _id)
outdata = {"id": _id}
cherrypy.response.status = HTTPStatus.ACCEPTED.value
else:
- _id = self.engine.new_item(session, engine_item, indata, kwargs, cherrypy.request.headers,
+ _id = self.engine.new_item(rollback, session, engine_item, indata, kwargs, cherrypy.request.headers,
force=force)
self._set_location_header(topic, version, item, _id)
outdata = {"id": _id}
outdata = self.engine.del_item_list(session, engine_item, kwargs)
cherrypy.response.status = HTTPStatus.OK.value
else: # len(args) > 1
- if item == "ns_instances_content":
- opp_id = self.engine.ns_operation(session, _id, "terminate", {"autoremove": True}, None)
+ if item == "ns_instances_content" and not force:
+ opp_id = self.engine.ns_operation(rollback, session, _id, "terminate", {"autoremove": True},
+ None)
outdata = {"_id": opp_id}
cherrypy.response.status = HTTPStatus.ACCEPTED.value
else:
cherrypy.response.status = e.http_code.value
if hasattr(outdata, "close"): # is an open file
outdata.close()
- if rollback:
+ for rollback_item in rollback:
try:
- self.engine.del_item(**rollback)
+ self.engine.del_item(**rollback_item, session=session, force=True)
except Exception as e2:
- cherrypy.log("Rollback Exception {}: {}".format(rollback, e2))
+ cherrypy.log("Rollback Exception {}: {}".format(rollback_item, e2))
error_text = str(e)
if isinstance(e, MsgException):
error_text = "{} has been '{}' but other modules cannot be informed because an error on bus".format(
update_dict['server.socket_host'] = v
elif k1 in ("server", "test", "auth", "log"):
update_dict[k1 + '.' + k2] = v
- elif k1 in ("message", "database", "storage"):
+ elif k1 in ("message", "database", "storage", "authenticator"):
# k2 = k2.replace('_', '.')
- if k2 == "port":
+ if k2 in ("port", "db_port"):
engine_config[k1][k2] = int(v)
else:
engine_config[k1][k2] = v
+
except ValueError as e:
cherrypy.log.error("Ignoring environ '{}': " + str(e))
except Exception as e:
logger_module.setLevel(engine_config[k1]["loglevel"])
# TODO add more entries, e.g.: storage
cherrypy.tree.apps['/osm'].root.engine.start(engine_config)
+ cherrypy.tree.apps['/osm'].root.authenticator.start(engine_config)
try:
cherrypy.tree.apps['/osm'].root.engine.init_db(target_version=database_version)
- except EngineException:
+ cherrypy.tree.apps['/osm'].root.authenticator.init_db(target_version=auth_database_version)
+ except (EngineException, AuthException):
pass
# getenv('OSMOPENMANO_TENANT', None)