Esempio n. 1
0
def login(dcos_url, password_str, password_env, password_file, provider,
          username, key_path):
    """
    :param dcos_url: URL of DC/OS cluster
    :type dcos_url: str
    :param password_str: password
    :type password_str: str
    :param password_env: name of environment variable with password
    :type password_env: str
    :param password_file: path to file with password
    :type password_file: bool
    :param provider: name of provider to authentication with
    :type provider: str
    :param username: username
    :type username: str
    :param key_path: path to file with private key
    :type param: str
    :rtype: int
    """

    password = _get_password(password_str, password_env, password_file)
    if provider is None:
        if username and password:
            auth.dcos_uid_password_auth(dcos_url, username, password)
        elif username and key_path:
            auth.servicecred_auth(dcos_url, username, key_path)
        else:
            try:
                providers = auth.get_providers()
                # Let users know if they have non-default providers configured
                # This is a weak check, we should check default versions per
                # DC/OS version since defaults will change. jj
                if len(providers) > 2:
                    msg = ("\nYour cluster has several authentication "
                           "providers enabled. Run `dcos auth "
                           "list-providers` to see all providers and `dcos "
                           "auth login --provider <provider-id>` to "
                           "authenticate with a specific provider\n")
                    emitter.publish(DefaultError(msg))
            except DCOSException:
                pass
            finally:
                auth.header_challenge_auth(dcos_url)
    else:
        providers = auth.get_providers()
        if providers.get(provider):
            _trigger_client_method(provider, providers[provider], username,
                                   password, key_path)
        else:
            msg = "Provider [{}] not configured on your cluster"
            raise DCOSException(msg.format(provider))

    return 0
Esempio n. 2
0
def test_get_providers_errors(config, req):
    config.return_value = "http://localhost"

    resp = create_autospec(requests.Response)
    resp.status_code = 404
    req.return_value = resp

    with pytest.raises(DCOSException) as e:
        auth.get_providers()

    err_msg = "This command is not supported for your cluster"
    assert str(e.value) == err_msg
Esempio n. 3
0
def test_get_providers_errors(config, req):
    config.return_value = "http://localhost"

    resp = create_autospec(requests.Response)
    resp.status_code = 404
    req.return_value = resp

    with pytest.raises(DCOSException) as e:
        auth.get_providers()

    err_msg = "This command is not supported for your cluster"
    assert str(e.value) == err_msg
Esempio n. 4
0
def test_get_providers(config, req_mock):
    resp = create_autospec(requests.Response)
    resp.return_value = {}
    req_mock.return_value = resp
    config.return_value = "http://localhost"

    auth.get_providers()
    req_mock.assert_called_with("http://localhost/acs/api/v1/auth/providers")

    # test url construction valid with trailing slash
    config.return_value = "http://localhost/"

    auth.get_providers()
    req_mock.assert_called_with("http://localhost/acs/api/v1/auth/providers")
Esempio n. 5
0
def test_get_providers(config, req_mock):
    resp = create_autospec(requests.Response)
    resp.return_value = {}
    req_mock.return_value = resp
    config.return_value = "http://localhost"

    auth.get_providers()
    req_mock.assert_called_with(
        "http://localhost/acs/api/v1/auth/providers")

    # test url construction valid with trailing slash
    config.return_value = "http://localhost/"

    auth.get_providers()
    req_mock.assert_called_with(
        "http://localhost/acs/api/v1/auth/providers")
Esempio n. 6
0
def _list_providers(json_):
    """
    :returns: providers available for configured cluster
    :rtype: dict
    """

    providers = auth.get_providers()
    if providers or json_:
        emitting.publish_table(emitter, providers, tables.auth_provider_table,
                               json_)
    else:
        raise DCOSException("No providers configured for your cluster")
Esempio n. 7
0
def _link(dcos_url, provider_id):
    """
    Link a DC/OS cluster to the current one.

    :param dcos_url: master ip of the cluster to link to
    :type dcos_url: str
    :param provider_id: login provider ID for the linked cluster
    :type provider_id: str
    :returns: process status
    :rtype: int
    """

    current_cluster = cluster.get_attached_cluster()
    if not current_cluster:
        raise DCOSException('No cluster is attached, cannot link.')
    current_cluster_url = current_cluster.get_url()

    # Accept the same formats as the `setup` command
    # eg. "my-cluster.example.com" -> "https://my-cluster.example.com"
    dcos_url = jsonitem._parse_url(dcos_url)

    try:
        linked_cluster_ip = socket.gethostbyname(urlparse(dcos_url).netloc)
    except OSError as error:
        raise DCOSException("Unable to retrieve IP for '{dcos_url}': {error}"
                            .format(dcos_url=dcos_url, error=error))

    # Check if the linked cluster is already configured (based on its IP)
    for configured_cluster in cluster.get_clusters():
        configured_cluster_host = \
            urlparse(configured_cluster.get_url()).netloc
        configured_cluster_ip = socket.gethostbyname(configured_cluster_host)

        if linked_cluster_ip == configured_cluster_ip:
            linked_cluster_id = configured_cluster.get_cluster_id()
            linked_cluster_name = configured_cluster.get_name()
            break
    else:
        msg = ("The cluster you are linking to must be set up locally before\n"
               "running the `cluster link` command. To set it up now, run:\n"
               "    $ dcos cluster setup {}".format(dcos_url))
        raise DCOSException(msg)

    providers = auth.get_providers(dcos_url)

    if provider_id:
        if provider_id not in providers:
            raise DCOSException(
                "Incorrect provider ID '{}'.".format(provider_id))
        provider_type = providers[provider_id]['authentication-type']
    else:
        (provider_id, provider_type) = _prompt_for_login_provider(providers)

    message = {
        'id': linked_cluster_id,
        'name': linked_cluster_name,
        'url': dcos_url,
        'login_provider': {
            'id': provider_id,
            'type': provider_type}}

    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'}

    http.post(
        urllib.parse.urljoin(current_cluster_url, '/cluster/v1/links'),
        data=json.dumps(message),
        headers=headers)

    return 0