From bb04e744c32599be395d7dd198921d06f428ada5 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Fri, 13 Jan 2023 11:46:54 +0100 Subject: [PATCH 1/3] use a dotenv file --- .env.dist | 2 ++ .gitignore | 3 ++- app.py | 4 ++++ requirements.txt | 2 ++ truthseeker/__init__.py | 35 +++-------------------------------- 5 files changed, 13 insertions(+), 33 deletions(-) create mode 100644 .env.dist diff --git a/.env.dist b/.env.dist new file mode 100644 index 0000000..aa4af79 --- /dev/null +++ b/.env.dist @@ -0,0 +1,2 @@ +FLASK_SECRET="" +DISCORD_BOT_TOKEN="" diff --git a/.gitignore b/.gitignore index 9b9b034..cdfd457 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ **/__pycache__ instance/ data_persistance/secret.py -**/.vscode \ No newline at end of file +**/.vscode +.env diff --git a/app.py b/app.py index 0a00714..da5364f 100644 --- a/app.py +++ b/app.py @@ -1,3 +1,7 @@ +# Load .env file +from dotenv import load_dotenv +load_dotenv() + from truthseeker import APP as app # the variable 'app' is detected by `flask run` if __name__ == "__main__": diff --git a/requirements.txt b/requirements.txt index 1575c7f..ecc89ed 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,5 @@ Flask-SocketIO==5.3.2 SQLAlchemy==1.4.20 pymysql==1.0.2 discord.py==2.1.0 +discord.py==2.1.0 +python-dotenv==0.21.0 diff --git a/truthseeker/__init__.py b/truthseeker/__init__.py index 3794976..909bb11 100644 --- a/truthseeker/__init__.py +++ b/truthseeker/__init__.py @@ -18,13 +18,13 @@ class TruthSeekerApp(flask.Flask): super().__init__("truthseeker") self.games_list = {} - - self.set_app_secret() + + self.config["SECRET_KEY"] = os.getenv("FLASK_SECRET") self.socketio_app = SocketIO(self) self.discord_bot = discord_bot.DiscordBot() - token = self.get_discord_bot_token() + token = os.getenv("DISCORD_BOT_TOKEN") if token: pass self.discord_bot.start(token) @@ -34,35 +34,6 @@ class TruthSeekerApp(flask.Flask): def run_app(self): self.socketio_app.run(self) - def set_app_secret(self) -> None: - """ - Set the secret used by flask - """ - if os.path.isfile("instance/secret.txt"): - f = open("instance/secret.txt", "r") - self.config["SECRET_KEY"] = f.read() - f.close() - print("Read secret from secret.txt !") - else: - import secrets - self.config["SECRET_KEY"] = secrets.token_hex() - os.makedirs("instance", exist_ok=True) - f = open("instance/secret.txt", "w") - f.write(self.config["SECRET_KEY"]) - f.close() - print("Generated secret and wrote to secret.txt !") - - def get_discord_bot_token(self) -> str: - """ - Get the token used by the discord bot - """ - if os.path.isfile("instance/discord_bot_token.txt"): - f = open("instance/discord_bot_token.txt", "r") - token = f.read() - f.close() - return token - return None - APP = TruthSeekerApp() from truthseeker.routes import routes_api, routes_ui, routes_socketio From b9768aa37deae0bcf07cc74e36894e3e189d95e8 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Fri, 13 Jan 2023 11:56:38 +0100 Subject: [PATCH 2/3] use dotenv to connect to the db --- .env.dist | 8 ++++++++ .gitignore | 1 - truthseeker/logic/data_persistance/data_access.py | 13 +++++++------ truthseeker/logic/data_persistance/remote.py | 14 +++++++------- truthseeker/logic/data_persistance/secret.py | 3 --- 5 files changed, 22 insertions(+), 17 deletions(-) delete mode 100644 truthseeker/logic/data_persistance/secret.py diff --git a/.env.dist b/.env.dist index aa4af79..0910c22 100644 --- a/.env.dist +++ b/.env.dist @@ -1,2 +1,10 @@ +# Common FLASK_SECRET="" DISCORD_BOT_TOKEN="" + +# Database +DB_HOST="" +DB_PORT=3306 +DB_USER="" +DB_PASSWORD="" +DB_DBNAME="" diff --git a/.gitignore b/.gitignore index cdfd457..1dadb1a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ **/__pycache__ instance/ -data_persistance/secret.py **/.vscode .env diff --git a/truthseeker/logic/data_persistance/data_access.py b/truthseeker/logic/data_persistance/data_access.py index 0141532..346ee2a 100644 --- a/truthseeker/logic/data_persistance/data_access.py +++ b/truthseeker/logic/data_persistance/data_access.py @@ -1,17 +1,18 @@ +import os + from sqlalchemy import create_engine from sqlalchemy.orm import Session from sqlalchemy import engine as eg import random import truthseeker.logic.data_persistance.tables as tables -from truthseeker.logic.data_persistance.secret import HOST, USER, PASS url_object = eg.URL.create( "mariadb+pymysql", - username=USER, - password=PASS, - host=HOST, - port=6776, - database="truthInquiry", + username=os.getenv("DB_USER"), + password=os.getenv("DB_PASSWORD"), + host=os.getenv("DB_HOST"), + port=os.getenv("DB_PORT"), + database=os.getenv("DB_DBNAME"), ) engine = create_engine(url_object) session = Session(engine) diff --git a/truthseeker/logic/data_persistance/remote.py b/truthseeker/logic/data_persistance/remote.py index acf5fd4..b2a9f3f 100644 --- a/truthseeker/logic/data_persistance/remote.py +++ b/truthseeker/logic/data_persistance/remote.py @@ -1,3 +1,5 @@ +import os + from sqlalchemy import create_engine from sqlalchemy.orm import Session from sqlalchemy import engine as eg @@ -12,19 +14,17 @@ from data.questions import QUESTIONS from data.reactions import REACTIONS from data.traits import TRAITS -from secret import HOST, USER, PASS url_object = eg.URL.create( "mariadb+pymysql", - username=USER, - password=PASS, - host=HOST, - port=6776, - database="truthInquiry", + username=os.getenv("DB_USER"), + password=os.getenv("DB_PASSWORD"), + host=os.getenv("DB_HOST"), + port=os.getenv("DB_PORT"), + database=os.getenv("DB_DBNAME"), ) engine = create_engine(url_object) - # Reset data tables with Session(engine) as session: Base.metadata.drop_all(engine) diff --git a/truthseeker/logic/data_persistance/secret.py b/truthseeker/logic/data_persistance/secret.py deleted file mode 100644 index c31b0e7..0000000 --- a/truthseeker/logic/data_persistance/secret.py +++ /dev/null @@ -1,3 +0,0 @@ -HOST = "mariadb.simailadjalim.fr" -USER = "truthInquiry" -PASS = "truthInquiry" \ No newline at end of file From 74622c78a98d1d80a7b2ed9ba47909e80493b2fd Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Fri, 13 Jan 2023 12:01:54 +0100 Subject: [PATCH 3/3] Use github actions secret for to pass variables for tests --- .github/workflows/tests.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index cde5719..f32c6b5 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -16,5 +16,12 @@ jobs: pip install -r dev-requirements.txt - name: Run tests + env: + FLASK_SECRET: ${{SECRETS.FLASK_SECRET}} + DB_HOST: ${{SECRETS.DB_HOST}} + DB_PORT: ${{SECRETS.DB_PORT}} + DB_USER: ${{SECRETS.DB_USER}} + DB_PASSWORD: ${{SECRETS.DB_PASSWORD}} + DB_DBNAME: ${{SECRETS.DB_DBNAME}} run: | python -m pytest --verbose