Skip to content

Commit

Permalink
Merge pull request #487 from mesosphere/auth-bug
Browse files Browse the repository at this point in the history
error based on user command auth not auth to get CLUSTER_ID
  • Loading branch information
tamarrow committed Feb 23, 2016
2 parents ff45e21 + be43c3c commit 511e2d5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
9 changes: 5 additions & 4 deletions cli/dcoscli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pkg_resources
from dcos import (auth, constants, emitting, errors, http, mesos, subcommand,
util)
from dcos.errors import DCOSException
from dcos.errors import DCOSAuthenticationException, DCOSException
from dcoscli import analytics

logger = util.get_logger(__name__)
Expand Down Expand Up @@ -57,11 +57,12 @@ def _main():
executable = subcommand.command_executables(command)

cluster_id = None
if dcoscli.version != 'SNAPSHOT' and command and command != "config":
if dcoscli.version != 'SNAPSHOT' and command and \
command not in ["config", "help"]:
try:
cluster_id = mesos.DCOSClient().metadata().get('CLUSTER_ID')
except DCOSException:
raise
except DCOSAuthenticationException:
raise
except:
msg = 'Unable to get the cluster_id of the cluster.'
logger.exception(msg)
Expand Down
6 changes: 2 additions & 4 deletions dcos/cosmospackage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dcos import emitting, http, util
from dcos.errors import DCOSException, DCOSHTTPException
from dcos.errors import DCOSAuthenticationException

from six.moves import urllib

Expand All @@ -26,9 +26,7 @@ def enabled(self):
headers=_get_cosmos_header("capabilities"))
# return `Authentication failed` error messages, but all other errors
# are treated as endpoint not available
except DCOSHTTPException:
return False
except DCOSException:
except DCOSAuthenticationException:
raise
except Exception as e:
logger.exception(e)
Expand Down
26 changes: 26 additions & 0 deletions dcos/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ def __str__(self):
self.response.reason)


class DCOSAuthenticationException(DCOSHTTPException):
"""A wrapper around Response objects for HTTP Authentication errors (401).
:param response: requests Response object
:type response: Response
"""
def __init__(self, response):
self.response = response

def __str__(self):
return "Authentication failed"


class DCOSAuthorizationException(DCOSHTTPException):
"""A wrapper around Response objects for HTTP Authorization errors (403).
:param response: requests Response object
:type response: Response
"""
def __init__(self, response):
self.response = response

def __str__(self):
return "You are not authorized to perform this operation"


class Error(object):
"""Abstract class for describing errors."""

Expand Down
8 changes: 5 additions & 3 deletions dcos/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import requests
from dcos import config, constants, util
from dcos.errors import DCOSException, DCOSHTTPException
from dcos.errors import (DCOSAuthenticationException,
DCOSAuthorizationException, DCOSException,
DCOSHTTPException)
from requests.auth import AuthBase, HTTPBasicAuth

from six.moves import urllib
Expand Down Expand Up @@ -147,7 +149,7 @@ def _request_with_auth(response,
i += 1

if response.status_code == 401:
raise DCOSException("Authentication failed")
raise DCOSAuthenticationException(response)

return response

Expand Down Expand Up @@ -201,7 +203,7 @@ def request(method,
if is_success(response.status_code):
return response
elif response.status_code == 403:
raise DCOSException("You are not authorized to perform this operation")
raise DCOSAuthorizationException(response)
else:
raise DCOSHTTPException(response)

Expand Down

0 comments on commit 511e2d5

Please sign in to comment.