Skip to content

Commit

Permalink
add auth.json migration
Browse files Browse the repository at this point in the history
  • Loading branch information
an-li-the-dev committed Mar 22, 2023
1 parent 16a6173 commit 1bfd63b
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 16 deletions.
2 changes: 1 addition & 1 deletion tests/test_web/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from click.testing import CliRunner

from tidy3d.web.cli import tidy3d_cli
from tidy3d.web.cli.app import CONFIG_FILE
from tidy3d.web.cli.constants import CONFIG_FILE


def test_tidy3d_cli():
Expand Down
3 changes: 3 additions & 0 deletions tidy3d/web/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
""" imports interfaces for interacting with server """
import sys

from .cli.migrate import migrate
from .webapi import run, upload, get_info, start, monitor, delete, download, load, estimate_cost
from .webapi import get_tasks, delete_old, download_json, download_log, load_simulation, real_cost
from .container import Job, Batch, BatchData
from .auth import get_credentials
from .cli import tidy3d_cli
from .asynchronous import run_async

migrate()
58 changes: 44 additions & 14 deletions tidy3d/web/cli/app.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
"""
Commandline interface for tidy3d.
"""
import json
import os.path
from os.path import expanduser

import click
import requests
import toml

from tidy3d.web.cli.constants import TIDY3D_DIR, CONFIG_FILE, CREDENTIAL_FILE
from tidy3d.web.cli.migrate import migrate
from tidy3d.web.config import DEFAULT_CONFIG

TIDY3D_DIR = f"{expanduser('~')}/.tidy3d"
CONFIG_FILE = TIDY3D_DIR + "/config"
if not os.path.exists(TIDY3D_DIR):
os.mkdir(TIDY3D_DIR)

if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
content = f.read()
config = toml.loads(content)
config_description = f"API Key:\nCurrent:[{config.get('apikey', '')}]\nNew"

def get_description():
"""Get the description for the config command.
Returns
-------
str
The description for the config command.
"""

if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
content = f.read()
config = toml.loads(content)
return config.get("apikey", "")
return ""


@click.group()
Expand All @@ -28,9 +40,7 @@ def tidy3d_cli():


@click.command()
@click.option(
"--apikey", prompt=config_description if "config_description" in globals() else "API Key"
)
@click.option("--apikey", prompt=False)
def configure(apikey):
"""Click command to configure the api key.
Parameters
Expand All @@ -53,11 +63,24 @@ def auth(req):
req.headers["simcloud-api-key"] = apikey
return req

resp = requests.get(f"{DEFAULT_CONFIG.web_api_endpoint}/apikey", auth=auth)
if os.path.exists(CREDENTIAL_FILE):
with open(CREDENTIAL_FILE, "r", encoding="utf-8") as fp:
auth_json = json.load(fp)
email = auth_json["email"]
password = auth_json["password"]
if email and password:
if migrate():
click.echo("Migrate successfully. auth.json is renamed to auth.json.bak.")
return

if not apikey:
current_apikey = get_description()
message = f"Current API key: [{current_apikey}]\n" if current_apikey else ""
apikey = click.prompt(f"{message}Please enter your api key", type=str)

resp = requests.get(f"{DEFAULT_CONFIG.web_api_endpoint}/apikey", auth=auth, verify=False)
if resp.status_code == 200:
click.echo("Configured successfully.")
if not os.path.exists(TIDY3D_DIR):
os.mkdir(TIDY3D_DIR)
with open(CONFIG_FILE, "w+", encoding="utf-8") as config_file:
toml_config = toml.loads(config_file.read())
toml_config.update({"apikey": apikey})
Expand All @@ -66,4 +89,11 @@ def auth(req):
click.echo("API key is invalid.")


@click.command()
def migration():
"""Click command to migrate the credential to api key."""
migrate()


tidy3d_cli.add_command(configure)
tidy3d_cli.add_command(migration)
6 changes: 6 additions & 0 deletions tidy3d/web/cli/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Constants for the CLI."""
from os.path import expanduser

TIDY3D_DIR = f"{expanduser('~')}/.tidy3d"
CONFIG_FILE = TIDY3D_DIR + "/config"
CREDENTIAL_FILE = TIDY3D_DIR + "/auth.json"
71 changes: 71 additions & 0 deletions tidy3d/web/cli/migrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import json
import os

import click
import requests
import toml

from tidy3d.web.cli.constants import CONFIG_FILE, CREDENTIAL_FILE, TIDY3D_DIR
from tidy3d.web.config import DEFAULT_CONFIG


# disable pylint for this file
# pylint: disable-all

def migrate() -> bool:
"""Click command to migrate the credential to api key."""
if os.path.exists(CREDENTIAL_FILE):
with open(CREDENTIAL_FILE, "r", encoding="utf-8") as fp:
auth_json = json.load(fp)
email = auth_json["email"]
password = auth_json["password"]
if email and password:
is_migrate = click.prompt(
"auth.json found which doesn't support anymore, do you want to migrate to api key?",
type=bool,
default=True,
)
if is_migrate:
headers = {"Application": "TIDY3D"}
resp = requests.get(
f"{DEFAULT_CONFIG.auth_api_endpoint}/auth",
headers=headers,
auth=(email, password),
)
if resp.status_code != 200:
click.echo(f"Migrate to api key failed: {resp.text}")
return False
else:
# click.echo(json.dumps(resp.json(), indent=4))
access_token = resp.json()["data"]["auth"]["accessToken"]
headers["Authorization"] = f"Bearer {access_token}"
resp = requests.get(
f"{DEFAULT_CONFIG.web_api_endpoint}/apikey", headers=headers
)
if resp.status_code != 200:
click.echo(f"Migrate to api key failed: {resp.text}")
return False
else:
click.echo(json.dumps(resp.json(), indent=4))
apikey = resp.json()["data"]
if not apikey:
resp = requests.post(
f"{DEFAULT_CONFIG.web_api_endpoint}/apikey", headers=headers
)
if resp.status_code != 200:
click.echo(f"Migrate to api key failed: {resp.text}")
return False
else:
apikey = resp.json()["data"]
if not os.path.exists(TIDY3D_DIR):
os.mkdir(TIDY3D_DIR)
with open(CONFIG_FILE, "w+", encoding="utf-8") as config_file:
toml_config = toml.loads(config_file.read())
toml_config.update({"apikey": apikey})
config_file.write(toml.dumps(toml_config))

# rename auth.json to auth.json.bak
os.rename(CREDENTIAL_FILE, CREDENTIAL_FILE + ".bak")
return True
else:
click.echo("You can migrate to api key by running 'tidy3d migrate' command.")
2 changes: 1 addition & 1 deletion tidy3d/web/httputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import requests

from .auth import get_credentials
from .cli.app import CONFIG_FILE
from .cli.constants import CONFIG_FILE
from .config import DEFAULT_CONFIG as Config
from ..log import WebError
from ..version import __version__
Expand Down

0 comments on commit 1bfd63b

Please sign in to comment.