예제 #1
0
파일: main.py 프로젝트: unterstein/dcos-cli
def _login():
    """
    :returns: process status
    :rtype: int
    """

    # every call to login will generate a new token if applicable
    _logout()
    dcos_url = config.get_config_val("core.dcos_url")
    if dcos_url is None:
        msg = ("Please provide the url to your DC/OS cluster: "
               "`dcos config set core.dcos_url`")
        raise DCOSException(msg)

    # hit protected endpoint which will prompt for auth if cluster has auth
    try:
        endpoint = '/pkgpanda/active.buildinfo.full.json'
        url = urllib.parse.urljoin(dcos_url, endpoint)
        http.request_with_auth('HEAD', url)
    # if the user is authenticated, they have effectively "logged in" even if
    # they are not authorized for this endpoint
    except DCOSAuthorizationException:
        pass

    emitter.publish("Login successful!")
    return 0
예제 #2
0
def test_request_with_bad_auth_basic(mock, req_mock, auth_mock):
    mock.url = 'http://domain.com'
    mock.headers = {'www-authenticate': 'Basic realm="Restricted"'}
    mock.status_code = 401

    auth_mock.return_value = HTTPBasicAuth("username", "password")

    req_mock.return_value = mock

    with pytest.raises(DCOSException) as e:
        http.request_with_auth("method", mock.url)
    msg = "Authentication failed. Please run `dcos auth login`"
    assert e.exconly().split(':')[1].strip() == msg
예제 #3
0
def test_request_with_bad_oauth(mock, req_mock, auth_mock):
    mock.url = 'http://domain.com'
    mock.headers = {'www-authenticate': 'oauthjwt'}
    mock.status_code = 401

    auth_mock.return_value = http.DCOSAcsAuth("token")

    req_mock.return_value = mock

    with pytest.raises(DCOSException) as e:
        http.request_with_auth("method", mock.url)
    msg = "Your core.dcos_acs_token is invalid. Please run: `dcos auth login`"
    assert e.exconly().split(':', 1)[1].strip() == msg
예제 #4
0
def test_request_with_auth_oauth(mock, req_mock, auth_mock):
    mock.url = 'http://domain.com'
    mock.headers = {'www-authenticate': 'oauthjwt'}
    mock.status_code = 401

    auth = http.DCOSAcsAuth("token")
    auth_mock.return_value = auth

    mock2 = copy.deepcopy(mock)
    mock2.status_code = 200
    req_mock.return_value = mock2

    response = http.request_with_auth("method", mock.url)
    assert response.status_code == 200
예제 #5
0
def test_request_with_auth_basic(mock, req_mock, auth_mock):
    mock.url = 'http://domain.com'
    mock.headers = {'www-authenticate': 'Basic realm="Restricted"'}
    mock.status_code = 401

    auth = HTTPBasicAuth("username", "password")
    auth_mock.return_value = auth

    mock2 = copy.deepcopy(mock)
    mock2.status_code = 200
    req_mock.return_value = mock2

    response = http.request_with_auth("method", mock.url)
    assert response.status_code == 200