External secrets (#29)

* Pass secrets into SelectValues

* Pass secrets into Create/DropTable

* Pass secrets to Insert/Update/DeleteValues. Drop and create based on existence

* Pass secrets to GetEmployeeInfo

* Pass secrets to Xero non oauth calls
This commit is contained in:
jbirddog 2023-03-01 10:04:10 -05:00 committed by GitHub
parent 4549ba8745
commit 71ee8825cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 136 additions and 43 deletions

View File

@ -7,20 +7,24 @@ import requests
class GetEmployeeInfo:
"""GetPayRate."""
def __init__(self, employee_id: str, fields: str):
def __init__(self,
api_key: str,
subdomain: str,
employee_id: str,
fields: str
):
"""__init__."""
self.api_key = api_key
self.subdomain = subdomain
self.employee_id = employee_id
self.fields = fields
def execute(self, config, task_data):
"""Execute."""
api_key = config["BAMBOOHR_API_KEY"]
subdomain = config["BAMBOOHR_SUBDOMAIN"]
url = f"https://api.bamboohr.com/api/gateway.php/{subdomain}/v1/employees/{self.employee_id}"
url = f"https://api.bamboohr.com/api/gateway.php/{self.subdomain}/v1/employees/{self.employee_id}"
headers = {"Accept": "application/json"}
params = {"fields": self.fields, "onlyCurrent": "true"}
auth = (api_key, "x")
auth = (self.api_key, "x")
try:
raw_response = requests.get(url, params, headers=headers, auth=auth)

View File

@ -1,6 +1,14 @@
import json
import psycopg2
class ConnectionConfig:
def __init__(self, database, host, port, username, password):
self.database = database
self.host = host
self.port = port
self.user = username
self.password = password
class BaseCommand:
"""BaseCommand."""
@ -69,10 +77,4 @@ class BaseCommand:
return f"WHERE {' AND '.join(columns)}", values
def _get_db_connection_str(self, config):
host = config["CONNECTOR_PROXY_POSTGRESQL_HOST"]
port = config["CONNECTOR_PROXY_POSTGRESQL_PORT"]
database = config["CONNECTOR_PROXY_POSTGRESQL_DB_NAME"]
username = config["CONNECTOR_PROXY_POSTGRESQL_USER_NAME"]
password = config["CONNECTOR_PROXY_POSTGRESQL_PASSWORD"]
return f"dbname={database} user={username} password={password} host={host} port={port}"
return f"dbname={config.database} user={config.user} password={config.password} host={config.host} port={config.port}"

View File

@ -3,13 +3,27 @@ import json
from typing import Any
from typing import Dict
from connector_postgresql.baseCommand import BaseCommand
from connector_postgresql.baseCommand import BaseCommand, ConnectionConfig
class CreateTable(BaseCommand):
"""CreateTable."""
def __init__(self, table_name: str, schema: Dict[str, Any]):
def __init__(self,
database_name: str,
database_host: str,
database_port: int,
database_user: str,
database_password: str,
table_name: str,
schema: Dict[str, Any]
):
"""__init__."""
self.connection_config = ConnectionConfig(
database_name,
database_host,
database_port,
database_user,
database_password)
self.table_name = table_name
self.schema = schema
@ -18,9 +32,9 @@ class CreateTable(BaseCommand):
columns = self._column_definitions(self.schema)
# TODO: build properly with SQL().format(Identifier(name))
# https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries
sql = f"CREATE TABLE {self.table_name} ({columns});"
sql = f"CREATE TABLE IF NOT EXISTS {self.table_name} ({columns});"
return self.execute_query(sql, config)
return self.execute_query(sql, self.connection_config)
def _column_definitions(self, schema):
def column_defintion(column):

View File

@ -3,13 +3,27 @@ import json
from typing import Any
from typing import Dict
from connector_postgresql.baseCommand import BaseCommand
from connector_postgresql.baseCommand import BaseCommand, ConnectionConfig
class DeleteValues(BaseCommand):
"""DeleteValues."""
def __init__(self, table_name: str, schema: Dict[str, Any]):
def __init__(self,
database_name: str,
database_host: str,
database_port: int,
database_user: str,
database_password: str,
table_name: str,
schema: Dict[str, Any]
):
"""__init__."""
self.connection_config = ConnectionConfig(
database_name,
database_host,
database_port,
database_user,
database_password)
self.table_name = table_name
self.schema = schema
@ -20,5 +34,5 @@ class DeleteValues(BaseCommand):
# https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries
sql = f"DELETE FROM {self.table_name} {where_clause};"
return self.execute_query(sql, config, values)
return self.execute_query(sql, self.connection_config, values)

View File

@ -1,19 +1,32 @@
import json
from connector_postgresql.baseCommand import BaseCommand
from connector_postgresql.baseCommand import BaseCommand, ConnectionConfig
class DropTable(BaseCommand):
"""DropTable."""
def __init__(self, table_name: str):
def __init__(self,
database_name: str,
database_host: str,
database_port: int,
database_user: str,
database_password: str,
table_name: str
):
"""__init__."""
self.connection_config = ConnectionConfig(
database_name,
database_host,
database_port,
database_user,
database_password)
self.table_name = table_name
def execute(self, config, task_data):
# TODO: build properly with SQL().format(Identifier(name))
# https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries
sql = f"DROP TABLE {self.table_name};"
sql = f"DROP TABLE IF EXISTS {self.table_name};"
return self.execute_query(sql, config)
return self.execute_query(sql, self.connection_config)

View File

@ -3,13 +3,27 @@ import json
from typing import Any
from typing import Dict
from connector_postgresql.baseCommand import BaseCommand
from connector_postgresql.baseCommand import BaseCommand, ConnectionConfig
class InsertValues(BaseCommand):
"""InsertValues."""
def __init__(self, table_name: str, schema: Dict[str, Any]):
def __init__(self,
database_name: str,
database_host: str,
database_port: int,
database_user: str,
database_password: str,
table_name: str,
schema: Dict[str, Any]
):
"""__init__."""
self.connection_config = ConnectionConfig(
database_name,
database_host,
database_port,
database_user,
database_password)
self.table_name = table_name
self.schema = schema
@ -22,4 +36,4 @@ class InsertValues(BaseCommand):
# https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries
sql = f"INSERT INTO {self.table_name} ({columns}) VALUES {placeholders};"
return self.execute_batch(sql, config, value_lists)
return self.execute_batch(sql, self.connection_config, value_lists)

View File

@ -3,13 +3,27 @@ import json
from typing import Any
from typing import Dict
from connector_postgresql.baseCommand import BaseCommand
from connector_postgresql.baseCommand import BaseCommand, ConnectionConfig
class SelectValues(BaseCommand):
"""SelectValues."""
def __init__(self, table_name: str, schema: Dict[str, Any]):
def __init__(self,
database_name: str,
database_host: str,
database_port: int,
database_user: str,
database_password: str,
table_name: str,
schema: Dict[str, Any]
):
"""__init__."""
self.connection_config = ConnectionConfig(
database_name,
database_host,
database_port,
database_user,
database_password)
self.table_name = table_name
self.schema = schema
@ -22,4 +36,4 @@ class SelectValues(BaseCommand):
# https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries
sql = f"SELECT {columns} FROM {self.table_name} {where_clause};"
return self.fetchall(sql, config, values)
return self.fetchall(sql, self.connection_config, values)

View File

@ -3,13 +3,27 @@ import json
from typing import Any
from typing import Dict
from connector_postgresql.baseCommand import BaseCommand
from connector_postgresql.baseCommand import BaseCommand, ConnectionConfig
class UpdateValues(BaseCommand):
"""UpdateValues."""
def __init__(self, table_name: str, schema: Dict[str, Any]):
def __init__(self,
database_name: str,
database_host: str,
database_port: int,
database_user: str,
database_password: str,
table_name: str,
schema: Dict[str, Any]
):
"""__init__."""
self.connection_config = ConnectionConfig(
database_name,
database_host,
database_port,
database_user,
database_password)
self.table_name = table_name
self.schema = schema
@ -24,7 +38,7 @@ class UpdateValues(BaseCommand):
# https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries
sql = f"UPDATE {self.table_name} {set_clause} {where_clause};"
return self.execute_query(sql, config, values)
return self.execute_query(sql, self.connection_config, values)
def _build_set_clause(self, schema):
columns_to_values = schema["set"]

View File

@ -142,6 +142,8 @@ class CreateInvoice:
def __init__(
self,
client_id: str,
client_secret: str,
access_token,
description: str,
contact_name: str,
@ -153,6 +155,8 @@ class CreateInvoice:
# account_code: str,
):
"""__init__."""
self.client_id = client_id
self.client_secret = client_secret
self.access_token = access_token
self.description = description
self.contact_name = contact_name
@ -161,9 +165,6 @@ class CreateInvoice:
def execute(self, config, task_data):
"""Creates an invoice in xero."""
client_id = config["CONNECTOR_PROXY_XERO_CLIENT_ID"]
client_secret = config["CONNECTOR_PROXY_XERO_CLIENT_SECRET"]
# this should be called token_set to match the docs
access_token = json.loads(self.access_token)
@ -175,7 +176,7 @@ class CreateInvoice:
Configuration(
debug=True,
oauth2_token=OAuth2Token(
client_id=client_id, client_secret=client_secret
client_id=self.client_id, client_secret=self.client_secret
),
),
pool_threads=1,

View File

@ -29,15 +29,18 @@ from xero_python.identity import IdentityApi # type: ignore
class GetCurrencies:
"""GetCurrencies."""
def __init__(self, access_token):
def __init__(self,
client_id: str,
client_secret: str,
access_token
):
"""__init__."""
self.client_id = client_id
self.client_secret = client_secret
self.access_token = access_token
def execute(self, config, task_data):
"""Get currencies configured in xero."""
client_id = config["CONNECTOR_PROXY_XERO_CLIENT_ID"]
client_secret = config["CONNECTOR_PROXY_XERO_CLIENT_SECRET"]
# this should be called token_set to match the docs
access_token = json.loads(self.access_token)
@ -49,7 +52,7 @@ class GetCurrencies:
Configuration(
debug=True,
oauth2_token=OAuth2Token(
client_id=client_id, client_secret=client_secret
client_id=self.client_id, client_secret=self.client_secret
),
),
pool_threads=1,
@ -86,7 +89,7 @@ class GetCurrencies:
status = 200
except Exception as e:
# TODO better error logging/reporting in debug
response = f'{{ "error": "{e.reason}" }}'
response = f'{{ "error": "{e}" }}'
status = 500
return {"response": response, "status": status, "mimetype": "application/json"}