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

Validate user credendials before trying to bootstrap. #209

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
validate_login() to silently validate user credentials
Johan Bergström committed Aug 29, 2017
commit 8d691bc2a4f30cab333c9b27caa54d65778772df
53 changes: 40 additions & 13 deletions bootstrap.py
Original file line number Diff line number Diff line change
@@ -469,11 +469,17 @@ def http_error_206(self, request, response, code, msg, hdrs):
return response


def call_api(url, data=None, method='GET'):
def call_api(url, data=None, method='GET', no_verify_ssl=False, silent=False):
"""
Helper function to place an API call returning JSON results and doing
some error handling. Any error results in an exit.
"""
if sys.version_info >= (2,7) and no_verify_ssl:
# import the ssl module only when on Python 2.7
import ssl
ssl_context = ssl._create_unverified_context()
else:
ssl_context = None
try:
request = urllib2.Request(url)
if options.verbose:
@@ -487,23 +493,27 @@ def call_api(url, data=None, method='GET'):
if data:
request.add_data(json.dumps(data))
request.get_method = lambda: method
result = urllib2.urlopen(request)
if ssl_context:
result = urllib2.urlopen(request, context=ssl_context)
else:
result = urllib2.urlopen(request)
jsonresult = json.load(result)
if options.verbose:
print 'result: %s' % json.dumps(jsonresult, sort_keys=False, indent=2)
return jsonresult
except urllib2.URLError, e:
print 'An error occured: %s' % e
print 'url: %s' % url
if isinstance(e, urllib2.HTTPError):
print 'code: %s' % e.code
if data:
print 'data: %s' % json.dumps(data, sort_keys=False, indent=2)
try:
jsonerr = json.load(e)
print 'error: %s' % json.dumps(jsonerr, sort_keys=False, indent=2)
except:
print 'error: %s' % e
if not silent:
print 'An error occured: %s' % e
print 'url: %s' % url
if isinstance(e, urllib2.HTTPError):
print 'code: %s' % e.code
if data:
print 'data: %s' % json.dumps(data, sort_keys=False, indent=2)
try:
jsonerr = json.load(e)
print 'error: %s' % json.dumps(jsonerr, sort_keys=False, indent=2)
except:
print 'error: %s' % e
sys.exit(1)
except Exception, e:
print "FATAL Error - %s" % (e)
@@ -768,6 +778,21 @@ class MEOptions:
# cleanup
disable_rhn_plugin()

def validate_login():
"""
Function to authenticate a user to validate login credentials before
running any other API calls. Strip down output so it doesn't print a
big exception and scare users. This function is going to be used
before we have the certificates from katello-consumer rpm installed,
so we need to disable SSL verification since it's now enabled
per default in RHEL7.4+.
"""
myurl = "https://" + options.sat6_ip + ":" + API_PORT + "/api/v2/organizations/"
try:
jsonresult=call_api(myurl,no_verify_ssl=True,silent=True)
except:
print "ERROR, could not authenticate username. Please try again."
sys.exit(2)

def enable_service(service, failonerror=True):
"""
@@ -1013,6 +1038,8 @@ def exec_service(service, command, failonerror=True):
# > Clean the environment from LD_... variables
clean_environment()

validate_login()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment like # > Validate credentials, this will ease the generation of the flow as described in https://github.com/Katello/katello-client-bootstrap/blob/master/CONTRIBUTING.md#developer-and-contributor-notes


# > IF RHEL 5 and not removing, prepare the migration.
if not options.remove and int(RELEASE[0]) == 5:
prepare_rhel5_migration()