Commit ab8cb3d4 authored by Manuel's avatar Manuel

added environment variable to set global resource directory for local mode

parent 3c9e0825
...@@ -15,3 +15,5 @@ src/modules/certificate/articonf1-chain.crt ...@@ -15,3 +15,5 @@ src/modules/certificate/articonf1-chain.crt
src/modules/security/regular_user_credentials.json src/modules/security/regular_user_credentials.json
src/modules/security/default_users.json src/modules/security/default_users.json
resources/
...@@ -2,6 +2,15 @@ ...@@ -2,6 +2,15 @@
import os import os
def get_resources_path():
if is_running_locally():
try:
return os.environ['ARTICONF_RESOURCES_PATH']
except:
return '/srv/articonf'
else:
return '/srv/articonf'
def is_running_locally(): def is_running_locally():
'''Set env var ARTICONF_LOCAL=1 to run locally.''' '''Set env var ARTICONF_LOCAL=1 to run locally.'''
server = True server = True
......
...@@ -33,7 +33,6 @@ class TokenStash: ...@@ -33,7 +33,6 @@ class TokenStash:
return None return None
def decodeToken(token: str, roles:List[str]=[]) -> Dict: def decodeToken(token: str, roles:List[str]=[]) -> Dict:
''' '''
verifies the passed token on the user-microservice and returns a dictionary with the verifies the passed token on the user-microservice and returns a dictionary with the
......
# global import, red is normal don't worry # global import, red is normal don't worry
import network_constants import network_constants
from env_info import is_running_locally from env_info import is_running_locally, get_resources_path
import os import os
import json import json
...@@ -22,15 +22,12 @@ class TokenManager: ...@@ -22,15 +22,12 @@ class TokenManager:
def getToken(self) -> str: def getToken(self) -> str:
if self._token == None: if self._token == None:
if is_running_locally(): credentials_path = get_resources_path()
credentials_path = '../../../modules/security/'
else:
credentials_path = '/srv/articonf/'
print("Looking for credentials at ... "+str(credentials_path)) print("Looking for credentials at ... "+str(credentials_path))
with open(f'{credentials_path}regular_user_credentials.json') as file: with open(f'{credentials_path}/regular_user_credentials.json') as file_handler:
credentials = json.loads(file.read()) credentials = json.loads(file_handler.read())
url = f'https://{network_constants.REST_GATEWAY_HOSTNAME}:{network_constants.REST_GATEWAY_REST_PORT}/api/tokens' url = f'https://{network_constants.REST_GATEWAY_HOSTNAME}:{network_constants.REST_GATEWAY_REST_PORT}/api/tokens'
......
...@@ -117,7 +117,6 @@ def add_by_layers(): ...@@ -117,7 +117,6 @@ def add_by_layers():
else: else:
print(f"Response: {response.status_code}: {response.text}") print(f"Response: {response.status_code}: {response.text}")
def add_by_schema(): def add_by_schema():
''' '''
take the columns and add the mappings at the server take the columns and add the mappings at the server
......
...@@ -8,11 +8,10 @@ modules_path = '../../../modules/' ...@@ -8,11 +8,10 @@ modules_path = '../../../modules/'
if os.path.exists(modules_path): if os.path.exists(modules_path):
sys.path.insert(1, modules_path) sys.path.insert(1, modules_path)
# load swagger config # load swagger config
import connexion import connexion
from security import swagger_util from security import swagger_util
from env_info import is_running_locally from env_info import is_running_locally, get_resources_path
app = connexion.App(__name__, specification_dir='configs/') app = connexion.App(__name__, specification_dir='configs/')
...@@ -23,23 +22,17 @@ def api_root(): ...@@ -23,23 +22,17 @@ def api_root():
return 'Endpoint of business-logic-microservice!' return 'Endpoint of business-logic-microservice!'
# SSL configuration # SSL configuration
try: certificate_path = get_resources_path()
certificate_path = os.environ['ARTICONF_CERTIFICATE_PATH']
except KeyError:
certificate_path = '/srv/articonf/'
context = (os.path.normpath(f'{certificate_path}/articonf1.crt'), os.path.normpath(f'{certificate_path}/articonf1.key')) # certificate and key files context = (os.path.normpath(f'{certificate_path}/articonf1.crt'), os.path.normpath(f'{certificate_path}/articonf1.key')) # certificate and key files
if is_running_locally(): if is_running_locally():
# Local Mode print("Running locally...")
print("Running with local settings...")
app.add_api(swagger_util.get_bundled_specs(Path("configs/swagger_local.yml")), app.add_api(swagger_util.get_bundled_specs(Path("configs/swagger_local.yml")),
resolver = connexion.RestyResolver("cms_rest_api")) resolver = connexion.RestyResolver("cms_rest_api"))
context = 'adhoc'
else: else:
app.add_api(swagger_util.get_bundled_specs(Path("configs/swagger.yml")), app.add_api(swagger_util.get_bundled_specs(Path("configs/swagger.yml")),
resolver = connexion.RestyResolver("cms_rest_api")) resolver = connexion.RestyResolver("cms_rest_api"))
# start app # start app
if __name__ == '__main__': if __name__ == '__main__':
print(context)
app.run(host='0.0.0.0', port=5000, debug=False, ssl_context=context) app.run(host='0.0.0.0', port=5000, debug=False, ssl_context=context)
...@@ -11,15 +11,12 @@ if os.path.exists(modules_path): ...@@ -11,15 +11,12 @@ if os.path.exists(modules_path):
sys.path.insert(1, modules_path) sys.path.insert(1, modules_path)
from services.user_service import UserService from services.user_service import UserService
from env_info import is_running_locally from env_info import is_running_locally, get_resources_path
if __name__ == "__main__": if __name__ == "__main__":
if is_running_locally(): resources_path = get_resources_path()
path = f'{modules_path}/security/default_users.json'
else:
path = '/srv/articonf/default_users.json'
with open(path, "r") as file_handler: with open(f'{resources_path}/default_users.json', "r") as file_handler:
content = file_handler.read() content = file_handler.read()
users = json.loads(content) users = json.loads(content)
......
...@@ -19,7 +19,7 @@ LOGGER = logging.getLogger(__name__) ...@@ -19,7 +19,7 @@ LOGGER = logging.getLogger(__name__)
import connexion import connexion
from security import swagger_util from security import swagger_util
from env_info import is_running_locally from env_info import is_running_locally, get_resources_path
app = connexion.App(__name__, specification_dir='configs/') app = connexion.App(__name__, specification_dir='configs/')
...@@ -28,18 +28,13 @@ def api_root(): ...@@ -28,18 +28,13 @@ def api_root():
return 'Endpoint of SMART RESTful API Gateway!' return 'Endpoint of SMART RESTful API Gateway!'
# SSL configuration # SSL configuration
try: certificate_path = get_resources_path()
certificate_path = os.environ['ARTICONF_CERTIFICATE_PATH']
except KeyError:
certificate_path = '/srv/articonf/'
context = (os.path.normpath(f'{certificate_path}/articonf1.crt'), os.path.normpath(f'{certificate_path}/articonf1.key')) # certificate and key files context = (os.path.normpath(f'{certificate_path}/articonf1.crt'), os.path.normpath(f'{certificate_path}/articonf1.key')) # certificate and key files
if is_running_locally(): if is_running_locally():
# Local Mode print("Running locally...")
print("Running with local settings...")
app.add_api(swagger_util.get_bundled_specs(Path("configs/swagger_local.yml")), app.add_api(swagger_util.get_bundled_specs(Path("configs/swagger_local.yml")),
resolver = connexion.RestyResolver("cms_rest_api")) resolver = connexion.RestyResolver("cms_rest_api"))
context = 'adhoc'
else: else:
app.add_api(swagger_util.get_bundled_specs(Path("configs/swagger.yml")), app.add_api(swagger_util.get_bundled_specs(Path("configs/swagger.yml")),
resolver = connexion.RestyResolver("cms_rest_api")) resolver = connexion.RestyResolver("cms_rest_api"))
......
# global imports (dont't worry, red is normal) # global imports (dont't worry, red is normal)
from db.entities.user import User from db.entities.user import User
from services.user_service import UserService from services.user_service import UserService
from env_info import get_resources_path
import jwt import jwt
from datetime import datetime, timedelta from datetime import datetime, timedelta
...@@ -43,7 +44,7 @@ class TokenService: ...@@ -43,7 +44,7 @@ class TokenService:
@staticmethod @staticmethod
def read_secret() -> str: def read_secret() -> str:
with open('/srv/articonf/jwt_secret.txt', 'r') as file: with open(f'{get_resources_path()}/jwt_secret.txt', 'r') as file:
secret = file.read().replace('\n', '') secret = file.read().replace('\n', '')
return secret return secret
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment