Skip to content

Commit

Permalink
Load usernames from config.
Browse files Browse the repository at this point in the history
  • Loading branch information
KFilippopolitis committed Sep 25, 2023
1 parent 3d237ab commit 1732c2c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
38 changes: 16 additions & 22 deletions mipdb/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import os
import glob

import toml

from mipdb.database import MonetDB
from mipdb.database import MonetDB, credentials_from_config
from mipdb.reader import JsonFileReader
from mipdb.usecases import (
AddDataModel,
Expand Down Expand Up @@ -36,28 +35,16 @@
from mipdb.usecases import ValidateDataset


def load_credentials_options_from_config():
try:
config = toml.load("/opt/credentials/config.toml")
return {
"--ip": config["DB_IP"],
"--port": config["DB_PORT"],
"--username": config["MONETDB_ADMIN_USERNAME"],
"--password": config["MONETDB_LOCAL_PASSWORD"],
"--db_name": config["DB_NAME"],
}
except FileNotFoundError:
return {
"--ip": "",
"--port": "",
"--username": "",
"--password": "",
"--db_name": "",
}

class NotRequiredIf(cl.Option):
def __init__(self, *args, **kwargs):
option_to_env_var = load_credentials_options_from_config()
credentials = credentials_from_config()
option_to_env_var = {
"--ip": credentials["DB_IP"],
"--port": credentials["DB_PORT"],
"--username": credentials["MONETDB_ADMIN_USERNAME"],
"--password": credentials["MONETDB_LOCAL_PASSWORD"],
"--db_name": credentials["DB_NAME"],
}
option = args[0][0]
if option_to_env_var[option]:
kwargs["required"] = False
Expand Down Expand Up @@ -110,6 +97,13 @@ def get_db_config(ip, port, username, password, db_name):
except ValueError:
raise UserInputError("Invalid ip provided")

option_to_env_var = {
"--ip": credentials["DB_IP"],
"--port": credentials["DB_PORT"],
"--username": credentials["MONETDB_ADMIN_USERNAME"],
"--password": credentials["MONETDB_LOCAL_PASSWORD"],
"--db_name": credentials["DB_NAME"],
}
config = {
"ip": ip,
"port": port,
Expand Down
19 changes: 19 additions & 0 deletions mipdb/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Union

import sqlalchemy as sql
import toml
from pymonetdb.sql import monetize

from mipdb.exceptions import DataBaseError
Expand All @@ -12,6 +13,7 @@
METADATA_SCHEMA = "mipdb_metadata"
METADATA_TABLE = "variables_metadata"

CONFIG = "/opt/credentials/config.toml"

class Status:
ENABLED = "ENABLED"
Expand Down Expand Up @@ -591,6 +593,22 @@ def execute(self, query, *args, **kwargs) -> list:
return self._executor.execute(query, *args, **kwargs) or []


def credentials_from_config():
try:
return toml.load(CONFIG)
except FileNotFoundError:
return {
"DB_IP":"",
"DB_PORT": "",
"MONETDB_ADMIN_USERNAME":"",
"MONETDB_LOCAL_USERNAME":"",
"MONETDB_LOCAL_PASSWORD":"",
"MONETDB_PUBLIC_USERNAME":"",
"MONETDB_PUBLIC_PASSWORD":"",
"DB_NAME": "",
}


class MonetDB(DBExecutorMixin, DataBase):
"""Concrete DataBase object connecting to a MonetDB instance. Gets all its
query executing methods from DBExecutorMixin."""
Expand All @@ -609,6 +627,7 @@ def from_config(self, dbconfig) -> "MonetDB":
url = f"monetdb://{username}:{password}@{ip}:{port}/{dbfarm}"
return MonetDB(url)


@handle_errors
def execute(self, query, *args, **kwargs) -> list:
"""Wrapper around SQLAlchemy's execute. Required because pymonetdb
Expand Down
11 changes: 6 additions & 5 deletions mipdb/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from typing import Union, List

import sqlalchemy as sql
from sqlalchemy import ForeignKey, Integer, MetaData
from sqlalchemy import ForeignKey, MetaData
from sqlalchemy.ext.compiler import compiles

from mipdb.database import DataBase, Connection
from mipdb.database import DataBase, Connection, credentials_from_config
from mipdb.database import METADATA_SCHEMA
from mipdb.database import METADATA_TABLE
from mipdb.dataelements import CommonDataElement
Expand All @@ -19,9 +19,10 @@


class User(Enum):
executor = os.getenv('MONETDB_LOCAL_USERNAME', 'executor')
admin = os.getenv('MONETDB_ADMIN_USERNAME','admin')
guest = os.getenv('MONETDB_PUBLIC_USERNAME','guest')
credentials = credentials_from_config()
executor = credentials['MONETDB_LOCAL_USERNAME']
admin = credentials['MONETDB_ADMIN_USERNAME']
guest = credentials['MONETDB_PUBLIC_USERNAME']


@compiles(sql.types.JSON, "monetdb")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mipdb"
version = "2.4.0"
version = "2.4.1"
description = ""
authors = ["Your Name <[email protected]>"]

Expand Down

0 comments on commit 1732c2c

Please sign in to comment.