Skip to content

Commit

Permalink
Load usernames from config. (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
KFilippopolitis authored Sep 25, 2023
1 parent 3d237ab commit 5f5fa7b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
31 changes: 9 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
19 changes: 18 additions & 1 deletion mipdb/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
from typing import Union

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

from mipdb.exceptions import DataBaseError
from mipdb.exceptions import UserInputError

METADATA_SCHEMA = "mipdb_metadata"
METADATA_TABLE = "variables_metadata"

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

class Status:
ENABLED = "ENABLED"
Expand Down Expand Up @@ -591,6 +592,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 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'] if credentials['MONETDB_LOCAL_USERNAME'] else "executor"
admin = credentials['MONETDB_ADMIN_USERNAME'] if credentials['MONETDB_ADMIN_USERNAME'] else "admin"
guest = credentials['MONETDB_PUBLIC_USERNAME'] if credentials['MONETDB_PUBLIC_USERNAME'] else "guest"


@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 5f5fa7b

Please sign in to comment.