Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add insecure option to configure #118

Merged
merged 3 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions databricks_cli/configure/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
PROMPT_TOKEN = 'Token' # NOQA


def _configure_cli_token(profile):
def _configure_cli_token(profile, insecure):
config = get_config_for_profile(profile)
host = click.prompt(PROMPT_HOST, default=config.host, type=_DbfsHost())
token = click.prompt(PROMPT_TOKEN, default=config.token)
new_config = DatabricksConfig.from_token(host, token)
new_config = DatabricksConfig.from_token(host, token, insecure)
update_and_persist_config(profile, new_config)


def _configure_cli_password(profile):
def _configure_cli_password(profile, insecure):
config = get_config_for_profile(profile)
if config.password:
default_password = '*' * len(config.password)
Expand All @@ -56,23 +56,25 @@ def _configure_cli_password(profile):
confirmation_prompt=True)
if password == default_password:
password = config.password
new_config = DatabricksConfig.from_password(host, username, password)
new_config = DatabricksConfig.from_password(host, username, password, insecure)
update_and_persist_config(profile, new_config)


@click.command(context_settings=CONTEXT_SETTINGS,
short_help='Configures host and authentication info for the CLI.')
@click.option('--token', show_default=True, is_flag=True, default=False)
@click.option('--insecure', show_default=True, is_flag=True, default=None)
@profile_option
def configure_cli(token):
def configure_cli(token, insecure):
"""
Configures host and authentication info for the CLI.
"""
profile = get_profile_from_context()
insecure_str = str(insecure) if insecure is not None else None
if token:
_configure_cli_token(profile)
_configure_cli_token(profile, insecure_str)
else:
_configure_cli_password(profile)
_configure_cli_password(profile, insecure_str)


class _DbfsHost(ParamType):
Expand Down
5 changes: 3 additions & 2 deletions databricks_cli/configure/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def callback(ctx, param, value): # NOQA


def _get_api_client(config):
verify = config.insecure is None
if config.is_valid_with_token:
return ApiClient(host=config.host, token=config.token)
return ApiClient(host=config.host, token=config.token, verify=verify)
return ApiClient(user=config.username, password=config.password,
host=config.host)
host=config.host, verify=verify)
16 changes: 10 additions & 6 deletions databricks_cli/configure/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
USERNAME = 'username'
PASSWORD = 'password' # NOQA
TOKEN = 'token'
INSECURE = 'insecure'
DEFAULT_SECTION = 'DEFAULT'


Expand Down Expand Up @@ -86,6 +87,7 @@ def update_and_persist_config(profile, databricks_config):
_set_option(raw_config, profile, USERNAME, databricks_config.username)
_set_option(raw_config, profile, PASSWORD, databricks_config.password)
_set_option(raw_config, profile, TOKEN, databricks_config.token)
_set_option(raw_config, profile, INSECURE, databricks_config.insecure)
_overwrite_config(raw_config)


Expand All @@ -100,23 +102,25 @@ def get_config_for_profile(profile):
username = _get_option_if_exists(raw_config, profile, USERNAME)
password = _get_option_if_exists(raw_config, profile, PASSWORD)
token = _get_option_if_exists(raw_config, profile, TOKEN)
return DatabricksConfig(host, username, password, token)
insecure = _get_option_if_exists(raw_config, profile, INSECURE)
return DatabricksConfig(host, username, password, token, insecure)


class DatabricksConfig(object):
def __init__(self, host, username, password, token): # noqa
def __init__(self, host, username, password, token, insecure): # noqa
self.host = host
self.username = username
self.password = password
self.token = token
self.insecure = insecure

@classmethod
def from_token(cls, host, token):
return DatabricksConfig(host, None, None, token)
def from_token(cls, host, token, insecure=None):
return DatabricksConfig(host, None, None, token, insecure)

@classmethod
def from_password(cls, host, username, password):
return DatabricksConfig(host, username, password, None)
def from_password(cls, host, username, password, insecure=None):
return DatabricksConfig(host, username, password, None, insecure)

@property
def is_valid_with_token(self):
Expand Down
10 changes: 10 additions & 0 deletions tests/configure/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def test_configure_cli_token():
input=(TEST_HOST + '\n' + TEST_TOKEN + '\n'))
assert get_config_for_profile(DEFAULT_SECTION).host == TEST_HOST
assert get_config_for_profile(DEFAULT_SECTION).token == TEST_TOKEN
assert get_config_for_profile(DEFAULT_SECTION).insecure is None


def test_configure_two_sections():
Expand All @@ -67,3 +68,12 @@ def test_configure_two_sections():
assert get_config_for_profile(DEFAULT_SECTION).token == TEST_TOKEN
assert get_config_for_profile(TEST_PROFILE).host == TEST_HOST_2
assert get_config_for_profile(TEST_PROFILE).token == TEST_TOKEN


def test_configure_cli_insecure():
runner = CliRunner()
runner.invoke(cli.configure_cli, ['--token', '--insecure'],
input=(TEST_HOST + '\n' + TEST_TOKEN + '\n'))
assert get_config_for_profile(DEFAULT_SECTION).host == TEST_HOST
assert get_config_for_profile(DEFAULT_SECTION).token == TEST_TOKEN
assert get_config_for_profile(DEFAULT_SECTION).insecure == 'True'
1 change: 1 addition & 0 deletions tox-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Test reqs
prospector[with_pyroma]==0.12.7
pylint==1.8.2
pep8-naming==0.5.0
pytest==3.2.1
mock==2.0.0
decorator==4.2.1
Expand Down