Skip to content

Commit

Permalink
Huge scripts refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
x-mass committed Jan 5, 2024
1 parent c39ddba commit 2ace625
Show file tree
Hide file tree
Showing 13 changed files with 660 additions and 370 deletions.
12 changes: 11 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
requests==2.28.2
annotated-types==0.6.0
certifi==2023.11.17
charset-normalizer==3.3.2
idna==3.6
pydantic==2.5.3
pydantic_core==2.14.6
requests==2.28.2
retrying==1.3.4
six==1.16.0
typing_extensions==4.9.0
urllib3==1.26.18
143 changes: 103 additions & 40 deletions scripts/auth_tools.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,130 @@
import os
import argparse
import json
from constants import URL
import logging
import os
import requests
import stat
from pathlib import Path
from urllib.parse import urljoin

import constants

def create_credentials_file(file_name: str, value: str):
with open(os.path.dirname(os.path.abspath(__file__)) + f"/.{file_name}", "w") as f:
f.write(value)

logger = logging.getLogger(__name__)

def read_credentials_file(file_name: str) -> str:
credentials_file_path = (
os.path.dirname(os.path.abspath(__file__)) + f"/.{file_name}"
)

if not os.path.exists(credentials_file_path):
return None
def _warn_if_wide_permissions(path):
file_stat = os.stat(path)
mode = file_stat.st_mode
if mode & stat.S_IRWXG or mode & stat.S_IRWXO:
logger.warning(f"File {path} has too wide permissions: {oct(mode & 0o777)}")

return open(credentials_file_path, "r").read().strip("\n")

class Authenticator:
USER_ENV = "NIL_USER"
SECRET_ENV = "NIL_SECRET"
STORAGE_DIR = Path.home().joinpath(".config", "proof-market")
CREDENTIALS_BASENAME = "credentials.json"
AUTH_BASENAME = "auth.json"

secret = read_credentials_file("secret")
user = read_credentials_file("user")
class Helpers:
@staticmethod
def get_base_dir(directory):
return Authenticator.STORAGE_DIR if directory is None else Path(directory)

@staticmethod
def get_auth_file_path(directory):
return Authenticator.Helpers.get_base_dir(directory) / Authenticator.AUTH_BASENAME

def update_auth(auth):
@staticmethod
def get_credentials_file_path(directory):
return Authenticator.Helpers.get_base_dir(directory) / Authenticator.CREDENTIALS_BASENAME

url = URL + "/user/signin"
body = {"username": user, "password": secret}
def __init__(self, url: str, directory: str | None = None):
self.credentials_path = Authenticator.Helpers.get_credentials_file_path(directory)
self.auth_path = Authenticator.Helpers.get_auth_file_path(directory)
self.username, self.secret = self.get_credentials()
self.url = url

response = requests.post(url, json=body)
if response.status_code != 200:
print(f"Update auth error: {response.status_code} {response.text}")
else:
with open(auth, "w") as f:
headers = {"Authorization": f'Bearer {response.json()["jwt"]}'}
json.dump(headers, f)
return response
def get_credentials(self) -> tuple[str, str]:
username = os.environ.get(self.USER_ENV)
secret = os.environ.get(self.SECRET_ENV)

if username is None or secret is None:
logger.info(f"Environment variables not set. Reading credentials from {self.credentials_path}.")
_warn_if_wide_permissions(self.credentials_path)
with open(self.credentials_path, 'r') as file:
data = json.load(file)
username = data.get('username')
secret = data.get('secret')
if not username or not secret:
raise ValueError("Credentials not found in JSON file.")
return username, secret

def get_headers(auth):
headers = {}
if auth is None:
auth = "auth.json"
response = update_auth(auth)
def update_auth_file(self):
url = urljoin(self.url, "/user/signin")
body = {"username": self.username, "password": self.secret}

response = requests.post(url, json=body)
if response.status_code != 200:
return
with open(auth, "r") as f:
raise RuntimeError(f"Failed to fetch auth: {response.status_code} {response.text}")
with open(self.auth_path, "w", opener=lambda path, flags: os.open(path, flags, 0o600)) as f:
headers = {"Authorization": f"Bearer {response.json()['jwt']}"}
json.dump(headers, f)
logger.info("Auth file updated.")

@staticmethod
def create_credentials_file(username: str, secret: str, directory: str | None = None):
credentials_data = {
"username": username,
"secret": secret,
}
credentials_json = json.dumps(credentials_data)
credentials_path = Authenticator.Helpers.get_credentials_file_path(directory)
os.makedirs(os.path.dirname(credentials_path), exist_ok=True)
with open(credentials_path, "w", opener=lambda path, flags: os.open(path, flags, 0o600)) as f:
f.write(credentials_json)
logger.info(f"Credentials saved to {credentials_path}")


def get_headers(directory=None, url=None):
auth_path = Authenticator.Helpers.get_auth_file_path(directory)
if not os.path.exists(auth_path):
logger.info(f"Auth file {auth_path} does not exist.")
if url is not None:
logger.info(f"Trying to create it with request to {url}")
authenticator = Authenticator(url)
authenticator.update_auth_file()
else:
raise RuntimeError("No way to fetch auth.json")

_warn_if_wide_permissions(auth_path)
with open(auth_path, "r") as f:
auth_data = json.load(f)
headers.update(auth_data)
return headers
return auth_data


if __name__ == "__main__":
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"-a",
"--auth",
"-u",
"--url",
action="store",
default="auth.json",
help="file to store jwt token",
default=constants.URL,
help="URL of proof market API",
)
parser.add_argument(
"-d",
"--dir",
action="store",
default=constants.URL,
help="Directory to store credentials and auth file",
)
args = parser.parse_args()

update_auth(args.auth)
Authenticator(args.url, args.dir).update_auth_path()
logger.info(f"Auth file at {args.dir} updated")


if __name__ == "__main__":
main()
3 changes: 1 addition & 2 deletions scripts/constants.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import os

VERSION = open(os.path.dirname(os.path.abspath(__file__)) + "/../VERSION", "r").read()
URL = "https://api.proof.market.nil.foundation/"
URL = "http://49.12.15.410:83000"
DB_NAME = "market"
MOUNT = "/v" + VERSION.replace(".", "_")
AUTH_FILE = "./.auth.json"
REQUEST_TIMEOUT = 100
7 changes: 1 addition & 6 deletions scripts/proof_producer/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
VERSION = open(
os.path.dirname(os.path.abspath(__file__)) + "/../../VERSION", "r"
).read()
URL = "https://api.proof.market.nil.foundation/"
URL = "http://49.12.15.40:3000"
DB_NAME = "market"
MOUNT = "/v" + VERSION.replace(".", "_")
AUTH_FILE = "./.auth.json"
PROOFS_DIR = "./proofs/statements/"
WAIT_BEFORE_SEND_PROOF = 30
ASK_UPDATE_INTERVAL = 20
MY_STATEMENTS = {
"32326": {"cost": 5, "asks_limit": 10},
"32292": {"cost": 5, "asks_limit": 10},
"79169223": {"cost": 5, "asks_limit": 10},
}
USER = "skm"
REQUEST_TIMEOUT = 100
2 changes: 1 addition & 1 deletion scripts/proof_producer/proof-producer.service
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Restart=always
ExecStart=/usr/bin/python3 /proof-market-toolchain/scripts/proof_producer/proof_producer.py start -p /proof-market-toolchain/build/bin/proof-generator/proof-generator

[Install]
WantedBy=multi-user.target
WantedBy=multi-user.target
Loading

0 comments on commit 2ace625

Please sign in to comment.