Handle config not set cases more gracefully

This commit is contained in:
PCoder 2019-10-13 09:08:40 +05:30
parent ab1714e733
commit 1897cc087c
2 changed files with 28 additions and 12 deletions

View file

@ -1,7 +1,7 @@
import logging import logging
from etcd3_wrapper import Etcd3Wrapper from etcd3_wrapper import Etcd3Wrapper
from decouple import config from decouple import config, UndefinedValueError
logging.basicConfig( logging.basicConfig(
level=logging.DEBUG, level=logging.DEBUG,
@ -11,7 +11,11 @@ logging.basicConfig(
datefmt='%Y-%m-%d:%H:%M:%S', datefmt='%Y-%m-%d:%H:%M:%S',
) )
# load configs
etcd_client = Etcd3Wrapper(host=config("ETCD_HOST"), port=config("ETCD_PORT"))
APP_PORT = config("APP_PORT", 5000) APP_PORT = config("APP_PORT", 5000)
try:
etcd_client = Etcd3Wrapper(host=config("ETCD_HOST"), port=config("ETCD_PORT"))
STRIPE_API_PRIVATE_KEY = config("STRIPE_API_PRIVATE_KEY") STRIPE_API_PRIVATE_KEY = config("STRIPE_API_PRIVATE_KEY")
except UndefinedValueError as uve:
print(str(uve))
exit(1)

View file

@ -2,7 +2,7 @@ import binascii
import json import json
import requests import requests
from decouple import config, Csv from decouple import config, Csv, UndefinedValueError
from datetime import datetime from datetime import datetime
from flask import Flask, request from flask import Flask, request
from flask_restful import Resource, Api from flask_restful import Resource, Api
@ -16,13 +16,26 @@ import time
app = Flask(__name__) app = Flask(__name__)
api = Api(app) api = Api(app)
# load configs
AUTH_NAME = config("AUTH_NAME", "")
AUTH_TOKEN = TOTP(config("AUTH_SEED", "")).now()
AUTH_REALM = config("AUTH_REALM", "")
OTP_SERVER = config("OTP_SERVER", "")
OTP_VERIFY_ENDPOINT = config("OTP_VERIFY_ENDPOINT", "verify/")
try:
INIT_ORDER_ID = config("INIT_ORDER_ID")
REALM_ALLOWED = config("REALM_ALLOWED", cast=Csv(str))
except UndefinedValueError as uve:
print(str(uve))
exit(1)
def check_otp(name, realm, token): def check_otp(name, realm, token):
try: try:
data = { data = {
"auth_name": config("AUTH_NAME", ""), "auth_name": AUTH_NAME,
"auth_token": TOTP(config("AUTH_SEED", "")).now(), "auth_token": AUTH_TOKEN,
"auth_realm": config("AUTH_REALM", ""), "auth_realm": AUTH_REALM,
"name": name, "name": name,
"realm": realm, "realm": realm,
"token": token, "token": token,
@ -32,8 +45,8 @@ def check_otp(name, realm, token):
response = requests.post( response = requests.post(
"{OTP_SERVER}{OTP_VERIFY_ENDPOINT}".format( "{OTP_SERVER}{OTP_VERIFY_ENDPOINT}".format(
OTP_SERVER=config("OTP_SERVER", ""), OTP_SERVER=OTP_SERVER,
OTP_VERIFY_ENDPOINT=config("OTP_VERIFY_ENDPOINT", "verify/"), OTP_VERIFY_ENDPOINT=OTP_VERIFY_ENDPOINT,
), ),
data=data, data=data,
) )
@ -50,7 +63,7 @@ def get_order_id():
if order_id_kv is not None: if order_id_kv is not None:
order_id = int(order_id_kv.value) + 1 order_id = int(order_id_kv.value) + 1
else: else:
order_id = config("INIT_ORDER_ID") order_id = INIT_ORDER_ID
client.put("/v1/last_order_id", str(order_id)) client.put("/v1/last_order_id", str(order_id))
return "OR-{}".format(order_id) return "OR-{}".format(order_id)
@ -97,7 +110,6 @@ class AddProduct(Resource):
def post(): def post():
data = request.json data = request.json
logging.debug("Got data: {}".format(str(data))) logging.debug("Got data: {}".format(str(data)))
REALM_ALLOWED = config("REALM_ALLOWED", cast=Csv(str))
logging.debug("REALM_ALLOWED = {}".format(REALM_ALLOWED)) logging.debug("REALM_ALLOWED = {}".format(REALM_ALLOWED))
if data["realm"] not in REALM_ALLOWED: if data["realm"] not in REALM_ALLOWED:
logging.error( logging.error(