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

Use process user name as proxyUser value for NO_AUTH #462

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 30 additions & 1 deletion sparkmagic/sparkmagic/tests/test_configuration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from mock import MagicMock
from nose.tools import assert_equals, assert_not_equals, raises, with_setup
from nose.tools import assert_equals, assert_not_equals, assert_true, raises, with_setup
import json

import sparkmagic.utils.configuration as conf
Expand Down Expand Up @@ -78,3 +78,32 @@ def test_share_config_between_pyspark_and_pyspark3():
kpc = { 'username': 'U', 'password': 'P', 'base64_password': 'cGFzc3dvcmQ=', 'url': 'L', 'auth': AUTH_BASIC }
overrides = { conf.kernel_python_credentials.__name__: kpc }
assert_equals(conf.base64_kernel_python3_credentials(), conf.base64_kernel_python_credentials())

@with_setup(_setup)
def test_configuration_override_livy_impersonation_false():
conf.override(conf.livy_user_impersonation.__name__, False)
assert_equals(conf.default_sesion_configs(), {})

@with_setup(_setup)
def test_configuration_override_livy_impersonation_true_with_auth():
conf.override(conf.livy_user_impersonation.__name__, True)
kpc = { 'username': 'U', 'password': 'P', 'base64_password': 'cGFzc3dvcmQ=', 'url': 'L', 'auth': AUTH_BASIC}
overrides = { conf.kernel_python_credentials.__name__: kpc,
conf.kernel_python3_credentials.__name__: kpc,
conf.kernel_r_credentials.__name__: kpc,
conf.kernel_scala_credentials.__name__: kpc }
conf.override_all(overrides)
assert_true('proxyUser' not in conf.default_sesion_configs())

@with_setup(_setup)
def test_configuration_override_livy_impersonation_true_no_auth():
conf.override(conf.livy_user_impersonation.__name__, True)
kpc = { 'username': '', 'password': '', 'base64_password': '=', 'url': 'L', 'auth': NO_AUTH}
overrides = { conf.kernel_python_credentials.__name__: kpc,
conf.kernel_python3_credentials.__name__: kpc,
conf.kernel_r_credentials.__name__: kpc,
conf.kernel_scala_credentials.__name__: kpc }
conf.override_all(overrides)
assert_true('proxyUser' in conf.default_sesion_configs())


40 changes: 30 additions & 10 deletions sparkmagic/sparkmagic/utils/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import copy
import sys
import base64
import pwd
import os
from hdijupyterutils.constants import EVENTS_HANDLER_CLASS_NAME, LOGGING_CONFIG_CLASS_NAME
from hdijupyterutils.utils import join_paths
from hdijupyterutils.configuration import override as _override
Expand All @@ -13,14 +15,12 @@
SESSION_KIND_SPARKR, SESSION_KIND_SPARK, SESSION_KIND_PYSPARK, CONFIGURABLE_RETRY
from sparkmagic.livyclientlib.exceptions import BadUserConfigurationException
import sparkmagic.utils.constants as constants

from requests_kerberos import REQUIRED


d = {}
path = join_paths(HOME_PATH, CONFIG_FILE)


def override(config, value):
_override(d, path, config, value)

Expand Down Expand Up @@ -48,18 +48,38 @@ def get_livy_kind(language):
def get_auth_value(username, password):
if username == '' and password == '':
return constants.NO_AUTH

return constants.AUTH_BASIC


# Configs


def get_session_properties(language):
default_properties = default_sesion_configs()
properties = copy.deepcopy(session_configs())
properties.update(default_properties)
properties[LIVY_KIND_PARAM] = get_livy_kind(language)
return properties

def default_sesion_configs():
if livy_user_impersonation() is True:
python_creds = base64_kernel_python_credentials()
python3_creds = base64_kernel_python3_credentials()
r_creds = base64_kernel_r_credentials()
scala_creds = base64_kernel_scala_credentials()

# for No Auth use process user for impersonation
if python_creds["auth"] in (None, constants.NO_AUTH) and \
python3_creds["auth"] in (None, constants.NO_AUTH) and \
r_creds["auth"] in (None, constants.NO_AUTH) and \
scala_creds["auth"] in (None, constants.NO_AUTH):
return {u'proxyUser': pwd.getpwuid(os.getuid()).pw_name}
else:
return {}
return {}

@_with_override
def livy_user_impersonation():
return True

@_with_override
def session_configs():
Expand All @@ -69,8 +89,8 @@ def session_configs():
@_with_override
def kernel_python_credentials():
return {u'username': u'', u'base64_password': u'', u'url': u'http://localhost:8998', u'auth': constants.NO_AUTH}


def base64_kernel_python_credentials():
return _credentials_override(kernel_python_credentials)

Expand All @@ -90,7 +110,7 @@ def kernel_scala_credentials():
return {u'username': u'', u'base64_password': u'', u'url': u'http://localhost:8998', u'auth': constants.NO_AUTH}


def base64_kernel_scala_credentials():
def base64_kernel_scala_credentials():
return _credentials_override(kernel_scala_credentials)

@_with_override
Expand Down Expand Up @@ -198,7 +218,7 @@ def pyspark_dataframe_encoding():
@_with_override
def heartbeat_refresh_seconds():
return 30


@_with_override
def heartbeat_retry_seconds():
Expand Down