Skip to content

Commit

Permalink
Merge pull request #73 from 6G-SANDBOX/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosAndreo authored Dec 4, 2024
2 parents 7b9bb61 + 7a50834 commit 2ad45dd
Show file tree
Hide file tree
Showing 60 changed files with 2,378 additions and 259 deletions.
4 changes: 4 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# FLASK: production or development
FLASK_ENV="production"

# GUNICORN
GUNICORN_WORKERS=3
GUNICORN_TIMEOUT=7200
Expand Down Expand Up @@ -51,6 +54,7 @@ GITHUB_6G_SANDBOX_SITES_REPOSITORY_NAME="6G-Sandbox-Sites"
SITES_TOKEN=''

# MAIL
TWO_FACTOR_AUTH=False # or True
MAIL_SERVER="smtp.gmail.com"
MAIL_PORT=465
MAIL_USE_TLS=False
Expand Down
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
# Changelog

## [v0.4.4] - 2024-12-04

### Added

- File `CONTRIBUTING.md` in `tn_template_lib` directory to explain the steps for how to add new descriptors.
- Possibility of skip two factor authentication.
- Endpoint that enable download report file of trial network in mardown format.
- `FLASK_ENV` variable in `.env` file to set the environment of the application.
- If `FLASK_ENV=development` can specify the repository url of 6G-Library and 6G-Sandbox-Sites when create a trial network.

### Changed

- Increase sleep time to verify if the component is deployed in Jenkins.
- Rename descriptors names in the `tn_template_lib` folder.
- If `FLASK_ENV=development` Swagger UI show `debug` namespace.

### Fixed

- Validate trial network descriptor in evaluate boolean expression.
- Different flows according to the possible states that can occur when purge a trial network.
- Script `deploy_vm.sh` to install TNLCM and MongoDB in a virtual machine.

## [v0.4.3] - 2024-11-15

### Changed
Expand Down Expand Up @@ -207,6 +229,7 @@

- Frontend implementation.

[v0.4.4]: https://github.com/6G-SANDBOX/TNLCM/compare/v0.4.3...v0.4.4
[v0.4.3]: https://github.com/6G-SANDBOX/TNLCM/compare/v0.4.2...v0.4.3
[v0.4.2]: https://github.com/6G-SANDBOX/TNLCM/compare/v0.4.1...v0.4.2
[v0.4.1]: https://github.com/6G-SANDBOX/TNLCM/compare/v0.4.0...v0.4.1
Expand Down
20 changes: 12 additions & 8 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from flask_jwt_extended import JWTManager
from flask_cors import CORS

from conf import TnlcmSettings, FlaskConf
from conf import TnlcmSettings, FlaskConf, MailSettings
from core.logs.log_handler import log_handler
from core.mail.mail import init_mail
from core.database.database import init_db
from core.routes import callback_namespace, debug_namespace, trial_network_namespace, user_namespace, verification_token_namespace
from core.utils.file_handler import load_toml
from core.routes import callback_namespace, debug_namespace, trial_network_namespace, user_no_two_factor_namespace, user_namespace, verification_token_namespace
from core.utils.file_handler import loads_toml

app = Flask(__name__)
CORS(app)
Expand All @@ -17,8 +17,9 @@
app.config.from_object(FlaskConf)

init_db(app)
init_mail(app)
__version__ = load_toml("pyproject.toml", "rt", "utf-8")["tool"]["poetry"]["version"]
if MailSettings.TWO_FACTOR_AUTH:
init_mail(app)
__version__ = loads_toml("pyproject.toml", "rt", "utf-8")["tool"]["poetry"]["version"]

api = Api(
app=app,
Expand All @@ -29,9 +30,12 @@
)

api.add_namespace(callback_namespace, path="/tnlcm/callback")
api.add_namespace(debug_namespace, path="/tnlcm/debug")
if FlaskConf.FLASK_ENV == "development":
api.add_namespace(debug_namespace, path="/tnlcm/debug")
api.add_namespace(trial_network_namespace, path="/tnlcm/trial-network")
api.add_namespace(user_namespace, path="/tnlcm/user")
api.add_namespace(verification_token_namespace, path="/tnlcm/verification-token")

if not MailSettings.TWO_FACTOR_AUTH:
api.add_namespace(user_no_two_factor_namespace, path="/tnlcm/user")
else:
api.add_namespace(verification_token_namespace, path="/tnlcm/verification-token")
log_handler.info(f"Start Trial Network Lifecycle Manager (TNLCM) {__version__} on http://0.0.0.0:{TnlcmSettings.TNLCM_PORT}")
1 change: 0 additions & 1 deletion conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from .mail import MailSettings
from .mongodb import MongoDBSettings
from .jenkins import JenkinsSettings
from .repository import RepositorySettings
from .sixg_library import SixGLibrarySettings
from .sixg_sandbox_sites import SixGSandboxSitesSettings
from .tnlcm import TnlcmSettings
Expand Down
52 changes: 32 additions & 20 deletions conf/flask_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from core.logs.log_handler import log_handler
from conf import MailSettings, MongoDBSettings
from core.exceptions.exceptions_handler import InvalidEnvVariableError

class FlaskConf(object):
"""
Expand All @@ -14,26 +15,37 @@ class FlaskConf(object):
ERROR_404_HELP = False

ME_CONFIG_MONGODB_URL = MongoDBSettings.ME_CONFIG_MONGODB_URL

MAIL_SERVER = MailSettings.MAIL_SERVER
MAIL_PORT = MailSettings.MAIL_PORT
MAIL_USE_TLS = MailSettings.MAIL_USE_TLS
MAIL_USE_SSL = MailSettings.MAIL_USE_SSL
MAIL_USERNAME = MailSettings.MAIL_USERNAME
MAIL_PASSWORD = MailSettings.MAIL_PASSWORD
FLASK_ENV=os.getenv("FLASK_ENV")
if FLASK_ENV != "production" and FLASK_ENV != "development":
raise InvalidEnvVariableError("FLASK_ENV", ["production", "development"])
if not MailSettings.TWO_FACTOR_AUTH:
config_dict = {
"DEBUG": DEBUG,
"TESTING": TESTING,
"SECRET_KEY": SECRET_KEY,
"ERROR_404_HELP": ERROR_404_HELP,
"ME_CONFIG_MONGODB_URL": ME_CONFIG_MONGODB_URL
}
else:
MAIL_SERVER = MailSettings.MAIL_SERVER
MAIL_PORT = MailSettings.MAIL_PORT
MAIL_USE_TLS = MailSettings.MAIL_USE_TLS
MAIL_USE_SSL = MailSettings.MAIL_USE_SSL
MAIL_USERNAME = MailSettings.MAIL_USERNAME
MAIL_PASSWORD = MailSettings.MAIL_PASSWORD

config_dict = {
"DEBUG": DEBUG,
"TESTING": TESTING,
"SECRET_KEY": SECRET_KEY,
"ERROR_404_HELP": ERROR_404_HELP,
"ME_CONFIG_MONGODB_URL": ME_CONFIG_MONGODB_URL,
"MAIL_SERVER": MAIL_SERVER,
"MAIL_PORT": MAIL_PORT,
"MAIL_USE_TLS": MAIL_USE_TLS,
"MAIL_USE_SSL": MAIL_USE_SSL,
"MAIL_USERNAME": MAIL_USERNAME,
"MAIL_PASSWORD": MAIL_PASSWORD,
}
config_dict = {
"DEBUG": DEBUG,
"TESTING": TESTING,
"SECRET_KEY": SECRET_KEY,
"ERROR_404_HELP": ERROR_404_HELP,
"ME_CONFIG_MONGODB_URL": ME_CONFIG_MONGODB_URL,
"MAIL_SERVER": MAIL_SERVER,
"MAIL_PORT": MAIL_PORT,
"MAIL_USE_TLS": MAIL_USE_TLS,
"MAIL_USE_SSL": MAIL_USE_SSL,
"MAIL_USERNAME": MAIL_USERNAME,
"MAIL_PASSWORD": MAIL_PASSWORD,
}

log_handler.info(f"Load Flask configuration: {config_dict}")
91 changes: 49 additions & 42 deletions conf/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from email_validator import validate_email, EmailNotValidError

from core.logs.log_handler import log_handler
from core.exceptions.exceptions_handler import UndefinedEnvVariableError
from core.exceptions.exceptions_handler import UndefinedEnvVariableError, InvalidEnvVariableError

def str_to_bool(s: str) -> bool:
"""
Expand All @@ -19,44 +19,51 @@ class MailSettings:
Mail Settings
"""

MAIL_SERVER = os.getenv("MAIL_SERVER")
MAIL_PORT = os.getenv("MAIL_PORT")
MAIL_USE_TLS = os.getenv("MAIL_USE_TLS")
MAIL_USE_SSL = os.getenv("MAIL_USE_SSL")
MAIL_USERNAME = os.getenv("MAIL_USERNAME")
MAIL_PASSWORD = os.getenv("MAIL_PASSWORD")
missing_variables = []
if not MAIL_SERVER:
missing_variables.append("MAIL_SERVER")
if not MAIL_PORT:
missing_variables.append("MAIL_PORT")
if not MAIL_USE_TLS:
missing_variables.append("MAIL_USE_TLS")
if not MAIL_USE_SSL:
missing_variables.append("MAIL_USE_SSL")
if not MAIL_USERNAME:
missing_variables.append("MAIL_USERNAME")
if not MAIL_PASSWORD:
missing_variables.append("MAIL_PASSWORD")
if missing_variables:
raise UndefinedEnvVariableError(missing_variables)
try:
valid = validate_email(MAIL_USERNAME, check_deliverability=False)
email = valid.email
except EmailNotValidError:
raise UndefinedEnvVariableError("Invalid 'MAIL_USERNAME' entered in .env file", 500)

MAIL_PORT = int(MAIL_PORT)
MAIL_USE_TLS = str_to_bool(MAIL_USE_TLS)
MAIL_USE_SSL = str_to_bool(MAIL_USE_SSL)

config_dict = {
"MAIL_SERVER": MAIL_SERVER,
"MAIL_PORT": MAIL_PORT,
"MAIL_USE_TLS": MAIL_USE_TLS,
"MAIL_USE_SSL": MAIL_USE_SSL,
"MAIL_USERNAME": MAIL_USERNAME,
"MAIL_PASSWORD": MAIL_PASSWORD
}

log_handler.info(f"Load Mail configuration: {config_dict}")
TWO_FACTOR_AUTH=os.getenv("TWO_FACTOR_AUTH")
if TWO_FACTOR_AUTH != "True" and TWO_FACTOR_AUTH != "False":
raise InvalidEnvVariableError("TWO_FACTOR_AUTH", ["True", "False"])
TWO_FACTOR_AUTH=str_to_bool(TWO_FACTOR_AUTH)
if not TWO_FACTOR_AUTH:
log_handler.info("Two Factor Authentication is disabled")
else:
MAIL_SERVER = os.getenv("MAIL_SERVER")
MAIL_PORT = os.getenv("MAIL_PORT")
MAIL_USE_TLS = os.getenv("MAIL_USE_TLS")
MAIL_USE_SSL = os.getenv("MAIL_USE_SSL")
MAIL_USERNAME = os.getenv("MAIL_USERNAME")
MAIL_PASSWORD = os.getenv("MAIL_PASSWORD")
missing_variables = []
if not MAIL_SERVER:
missing_variables.append("MAIL_SERVER")
if not MAIL_PORT:
missing_variables.append("MAIL_PORT")
if not MAIL_USE_TLS:
missing_variables.append("MAIL_USE_TLS")
if not MAIL_USE_SSL:
missing_variables.append("MAIL_USE_SSL")
if not MAIL_USERNAME:
missing_variables.append("MAIL_USERNAME")
if not MAIL_PASSWORD:
missing_variables.append("MAIL_PASSWORD")
if missing_variables:
raise UndefinedEnvVariableError(missing_variables)
try:
valid = validate_email(MAIL_USERNAME, check_deliverability=False)
email = valid.email
except EmailNotValidError:
raise UndefinedEnvVariableError("Invalid 'MAIL_USERNAME' entered in .env file", 500)

MAIL_PORT = int(MAIL_PORT)
MAIL_USE_TLS = str_to_bool(MAIL_USE_TLS)
MAIL_USE_SSL = str_to_bool(MAIL_USE_SSL)

config_dict = {
"MAIL_SERVER": MAIL_SERVER,
"MAIL_PORT": MAIL_PORT,
"MAIL_USE_TLS": MAIL_USE_TLS,
"MAIL_USE_SSL": MAIL_USE_SSL,
"MAIL_USERNAME": MAIL_USERNAME,
"MAIL_PASSWORD": MAIL_PASSWORD
}

log_handler.info(f"Load Mail configuration: {config_dict}")
20 changes: 0 additions & 20 deletions conf/repository.py

This file was deleted.

5 changes: 1 addition & 4 deletions conf/sixg_library.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import os

from conf import RepositorySettings
from core.logs.log_handler import log_handler
from core.exceptions.exceptions_handler import UndefinedEnvVariableError, CustomGitException
from core.exceptions.exceptions_handler import UndefinedEnvVariableError

class SixGLibrarySettings:
"""
Expand All @@ -21,8 +20,6 @@ class SixGLibrarySettings:
missing_variables.append("GITHUB_6G_LIBRARY_REPOSITORY_NAME")
if missing_variables:
raise UndefinedEnvVariableError(missing_variables)
if not RepositorySettings.is_github_repo(GITHUB_6G_LIBRARY_HTTPS_URL):
raise CustomGitException(f"Repository url specified '{GITHUB_6G_LIBRARY_HTTPS_URL}' is not correct", 500)

config_dict = {
"GITHUB_6G_LIBRARY_HTTPS_URL": GITHUB_6G_LIBRARY_HTTPS_URL,
Expand Down
5 changes: 1 addition & 4 deletions conf/sixg_sandbox_sites.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import os

from conf import RepositorySettings
from core.logs.log_handler import log_handler
from core.exceptions.exceptions_handler import UndefinedEnvVariableError, CustomGitException
from core.exceptions.exceptions_handler import UndefinedEnvVariableError

class SixGSandboxSitesSettings:
"""
Expand All @@ -24,8 +23,6 @@ class SixGSandboxSitesSettings:
missing_variables.append("SITES_TOKEN")
if missing_variables:
raise UndefinedEnvVariableError(missing_variables)
if not RepositorySettings.is_github_repo(GITHUB_6G_SANDBOX_SITES_HTTPS_URL):
raise CustomGitException(f"Repository url specified '{GITHUB_6G_SANDBOX_SITES_HTTPS_URL}' is not correct", 500)

config_dict = {
"GITHUB_6G_SANDBOX_SITES_HTTPS_URL": GITHUB_6G_SANDBOX_SITES_HTTPS_URL,
Expand Down
6 changes: 6 additions & 0 deletions core/exceptions/exceptions_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def __init__(self, missing_variables):
message = f"Set the value of the variables {', '.join(missing_variables)} in the .env file"
super().__init__(message, 500)

class InvalidEnvVariableError(CustomException):
"""Error thrown when the variables are invalid in the .env file"""
def __init__(self, variable, possible_values):
message = f"Possible values for the variable {variable} are {', '.join(possible_values)}"
super().__init__(message, 500)

###################################
######## MongoDB exception ########
###################################
Expand Down
Loading

0 comments on commit 2ad45dd

Please sign in to comment.