Skip to content

Commit

Permalink
sc-12639 sc-12638 deprecate api key prompting (#17)
Browse files Browse the repository at this point in the history
* sc-12639 sc-12638 deprecate api key prompting

---------

Co-authored-by: Matthew Warren <[email protected]>
  • Loading branch information
mattwwarren and Matthew Warren committed Apr 23, 2024
1 parent 8ad7523 commit a7d3cef
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,12 @@ docker run --rm -v ${PWD}/files:/app/files cloudtruth/dynamic-importer regenerat
```

## Uploading data to CloudTruth
Once you are comfortable with your template and associated data, you're ready to upload to CloudTruth!
Once you are comfortable with your template and associated data, you're ready to upload to CloudTruth! Be sure to provide your CloudTruth API Key as an environment variable

```
docker run --rm -v ${PWD}/files:/app/files cloudtruth/dynamic-importer create-data --data-file /app/files/.env.ctconfig -m /app/files/.env.cttemplate -p "Meaningful Project Name"
docker run --rm -e CLOUDTRUTH_API_KEY="myverysecureS3CR3T!!" -v ${PWD}/files:/app/files cloudtruth/dynamic-importer create-data --data-file /app/files/.env.ctconfig -m /app/files/.env.cttemplate -p "Meaningful Project Name"
```

By default, the utility will prompt for your CloudTruth API Key. You may also provide it via `-e CLOUDTRUTH_API_KEY="myverysecureS3CR3T!!"`

# Development
1. Set up a virtualenv
1. From your checkout of the code, `pip install -e .[dev]`
Expand Down
19 changes: 8 additions & 11 deletions src/dynamic_importer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,10 @@ def regenerate_template(default_values, env_values, file_type, data_file):
@click.option("-c", help="Create missing projects and enviroments", is_flag=True)
@click.option("-u", help="Upsert values", is_flag=True)
def create_data(data_file, template_file, project, k, c, u):
api_key = os.environ.get("CLOUDTRUTH_API_KEY") or click.prompt(
"Enter your CloudTruth API Key", hide_input=True
)
with open(data_file, "r") as dfp, open(template_file, "r") as tfp:
config_data = json.load(dfp)
template_data = tfp.read()
_create_data(
config_data, str(template_file), template_data, project, api_key, k, c, u
)
_create_data(config_data, str(template_file), template_data, project, k, c, u)

click.echo("Data upload to CloudTruth complete!")

Expand All @@ -207,11 +202,16 @@ def _create_data(
template_name: str,
template_data: str,
project: str,
api_key: str,
k: bool,
c: bool,
u: bool,
):
api_key = os.environ.get("CLOUDTRUTH_API_KEY")
if not api_key:
raise click.UsageError(
"CLOUDTRUTH_API_KEY environment variable is required. "
"Please visit https://app.cloudtruth.io/organization/api to generate one."
)
if k:
urllib3.disable_warnings()
client = CTClient(api_key, skip_ssl_validation=k)
Expand Down Expand Up @@ -326,15 +326,12 @@ def walk_directories(config_dir, file_types, exclude_dirs, create_hierarchy, k,
"config_data": config_data,
}

api_key = os.environ.get("CLOUDTRUTH_API_KEY")
for project, ct_data in processed_data.items():
click.echo(f"Uploading data for {project}")
for template_name, template_data in ct_data.items():
template_body = template_data["template_body"]
config_data = template_data["config_data"]
_create_data(
config_data, template_name, template_body, project, api_key, k, c, u
)
_create_data(config_data, template_name, template_body, project, k, c, u)
click.echo("Data upload to CloudTruth complete!")


Expand Down
34 changes: 34 additions & 0 deletions src/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import shutil
import uuid
from tempfile import gettempdir
from tempfile import NamedTemporaryFile
from unittest import mock
from unittest import TestCase

Expand Down Expand Up @@ -57,6 +58,39 @@ def test_regenerate_template_no_args(self):
)


@pytest.mark.usefixtures("tmp_path")
def test_create_data_no_api_key(tmp_path):
runner = CliRunner()
with runner.isolated_filesystem(temp_dir=tmp_path) as td:
with (
NamedTemporaryFile(dir=td, delete=False) as tmp_data,
NamedTemporaryFile(dir=td, delete=False) as tmp_template,
):
tmp_data.write(b"{}")
tmp_data.close()
tmp_template.write(b"{}")
tmp_template.close()

result = runner.invoke(
import_config,
[
"create-data",
"-d",
f"{tmp_data.name}",
"-m",
f"{tmp_template.name}",
"-p",
"testproj",
],
catch_exceptions=False,
)
assert result.exit_code == 2
assert (
"Error: CLOUDTRUTH_API_KEY environment variable is required."
in result.output
)


@pytest.mark.usefixtures("tmp_path")
def test_cli_process_configs_dotenv(tmp_path):
runner = CliRunner()
Expand Down

0 comments on commit a7d3cef

Please sign in to comment.