def test_get_ambari_server_api_base(self): # Test case of using http protocol properties = FakeProperties({ SSL_API: "false", CLIENT_API_PORT_PROPERTY: None }) result = get_ambari_server_api_base(properties) self.assertEquals(result, 'http://127.0.0.1:8080/api/v1/') # Test case of using http protocol and custom port properties = FakeProperties({ SSL_API: "false", CLIENT_API_PORT_PROPERTY: "8033" }) result = get_ambari_server_api_base(properties) self.assertEquals(result, 'http://127.0.0.1:8033/api/v1/') # Test case of using https protocol (and ssl port) fqdn = socket.getfqdn() self.assertTrue(len(fqdn) > 0) properties = FakeProperties({ SSL_API: "true", SSL_API_PORT: "8443", CLIENT_API_PORT_PROPERTY: None }) result = get_ambari_server_api_base(properties) self.assertEquals(result, 'https://{0}:8443/api/v1/'.format(fqdn))
def test_get_ambari_server_api_base(self): # Test case of using http protocol properties = FakeProperties({ SSL_API: "false", CLIENT_API_PORT_PROPERTY: None }) result = get_ambari_server_api_base(properties) self.assertEquals(result, 'http://127.0.0.1:8080/api/v1/') # Test case of using http protocol and custom port properties = FakeProperties({ SSL_API: "false", CLIENT_API_PORT_PROPERTY: "8033" }) result = get_ambari_server_api_base(properties) self.assertEquals(result, 'http://127.0.0.1:8033/api/v1/') # Test case of using https protocol (and ssl port) properties = FakeProperties({ SSL_API: "true", SSL_API_PORT : "8443", CLIENT_API_PORT_PROPERTY: None }) result = get_ambari_server_api_base(properties) self.assertEquals(result, 'https://127.0.0.1:8443/api/v1/')
def test_get_ambari_server_api_base(self): # Test case of using http protocol properties = FakeProperties({ SSL_API: "false", }) result = get_ambari_server_api_base(properties) self.assertEquals(result, 'http://127.0.0.1:8080/api/v1/') # Test case of using https protocol (and ssl port) properties = FakeProperties({SSL_API: "true", SSL_API_PORT: "8443"}) result = get_ambari_server_api_base(properties) self.assertEquals(result, 'https://127.0.0.1:8443/api/v1/')
def update_ldap_configuration(properties, ldap_property_value_map): admin_login = get_validated_string_input("Enter Ambari Admin login: "******"Enter Ambari Admin password: "******"Configuration": { "category": "ldap-configuration", "properties": { } } } data['Configuration']['properties'] = ldap_property_value_map request.add_data(json.dumps(data)) request.get_method = lambda: 'PUT' try: response = urllib2.urlopen(request) except Exception as e: err = 'Updating LDAP configuration failed. Error details: %s' % e raise FatalException(1, err) response_status_code = response.getcode() if response_status_code != 200: err = 'Error during setup-ldap. Http status code - ' + str(response_status_code) raise FatalException(1, err)
def get_eligible_services(properties, admin_login, admin_password, cluster_name): safe_cluster_name = urllib2.quote(cluster_name) url = get_ambari_server_api_base( properties) + URL_TO_FETCH_SERVICES_ELIGIBLE_FOR_SSO.replace( ":CLUSTER_NAME", safe_cluster_name) admin_auth = base64.encodestring( '%s:%s' % (admin_login, admin_password)).replace('\n', '') request = urllib2.Request(url) request.add_header('Authorization', 'Basic %s' % admin_auth) request.add_header('X-Requested-By', 'ambari') request.get_method = lambda: 'GET' services = [SERVICE_NAME_AMBARI] sys.stdout.write('\nFetching SSO enabled services') numOfTries = 0 request_in_progress = True while request_in_progress: numOfTries += 1 if (numOfTries == 60): raise FatalException( 1, "Could not fetch eligible services within a minute; giving up!" ) sys.stdout.write('.') sys.stdout.flush() try: with closing(urllib2.urlopen(request)) as response: response_status_code = response.getcode() if response_status_code != 200: request_in_progress = False err = 'Error while fetching eligible services. Http status code - ' + str( response_status_code) raise FatalException(1, err) else: response_body = json.loads(response.read()) items = response_body['items'] if len(items) > 0: for item in items: services.append( item['ServiceInfo']['service_name']) if not services: time.sleep(1) else: request_in_progress = False else: request_in_progress = False except Exception as e: request_in_progress = False err = 'Error while fetching eligible services. Error details: %s' % e raise FatalException(1, err) if (len(services) == 0): sys.stdout.write('\nThere is no SSO enabled services found\n') else: sys.stdout.write('\nFound SSO enabled services: {0}\n'.format( ', '.join(str(s) for s in services))) return services
def get_sso_property_from_db(properties, admin_login, admin_password, property_name): sso_property = None url = get_ambari_server_api_base(properties) + SETUP_SSO_CONFIG_URL admin_auth = base64.encodestring( '%s:%s' % (admin_login, admin_password)).replace('\n', '') request = urllib2.Request(url) request.add_header('Authorization', 'Basic %s' % admin_auth) request.add_header('X-Requested-By', 'ambari') request.get_method = lambda: 'GET' request_in_progress = True sys.stdout.write('\nFetching SSO configuration from DB') numOfTries = 0 while request_in_progress: numOfTries += 1 if (numOfTries == 60): raise FatalException( 1, "Could not fetch SSO configuration within a minute; giving up!" ) sys.stdout.write('.') sys.stdout.flush() try: with closing(urllib2.urlopen(request)) as response: response_status_code = response.getcode() if response_status_code != 200: request_in_progress = False err = 'Error while fetching SSO configuration. Http status code - ' + str( response_status_code) raise FatalException(1, err) else: response_body = json.loads(response.read()) sso_properties = response_body['Configuration'][ 'properties'] sso_property = sso_properties[property_name] if not sso_property: time.sleep(1) else: request_in_progress = False except urllib2.HTTPError as http_error: if http_error.code == 404: # This means that there is no SSO configuration in the database yet -> we can not fetch the property (but this is NOT an error) request_in_progress = False sso_property = None else: raise http_error except Exception as e: request_in_progress = False err = 'Error while fetching SSO configuration. Error details: %s' % e raise FatalException(1, err) return sso_property
def set_current(options): logger.info("Set current cluster version.") server_status, pid = is_server_runing() if not server_status: err = 'Ambari Server is not running.' raise FatalException(1, err) finalize_options = SetCurrentVersionOptions(options) if finalize_options.no_finalize_options_set(): err = 'Must specify --cluster-name and --version-display-name. Please invoke ambari-server.py --help to print the options.' raise FatalException(1, err) admin_login = get_validated_string_input(prompt="Enter Ambari Admin login: "******"Enter Ambari Admin password: "******"Failed to read properties file.") base_url = get_ambari_server_api_base(properties) url = base_url + "clusters/{0}/stack_versions".format(finalize_options.cluster_name) admin_auth = base64.encodestring('%s:%s' % (admin_login, admin_password)).replace('\n', '') request = urllib2.Request(url) request.add_header('Authorization', 'Basic %s' % admin_auth) request.add_header('X-Requested-By', 'ambari') data = { "ClusterStackVersions": { "repository_version": finalize_options.desired_repo_version, "state": "CURRENT", "force": finalize_options.force_repo_version } } if get_verbose(): sys.stdout.write('\nCalling API ' + url + ' : ' + str(data) + '\n') request.add_data(json.dumps(data)) request.get_method = lambda: 'PUT' try: response = urllib2.urlopen(request) except urllib2.HTTPError, e: code = e.getcode() content = e.read() err = 'Error during setting current version. Http status code - {0}. \n {1}'.format( code, content) raise FatalException(1, err)
def set_current(options): server_status, pid = is_server_runing() if not server_status: err = 'Ambari Server is not running.' raise FatalException(1, err) finalize_options = SetCurrentVersionOptions(options) if finalize_options.no_finalize_options_set(): err = 'Must specify --cluster-name and --version-display-name. Please invoke ambari-server.py --help to print the options.' raise FatalException(1, err) admin_login = get_validated_string_input(prompt="Enter Ambari Admin login: "******"Enter Ambari Admin password: "******"Failed to read properties file.") base_url = get_ambari_server_api_base(properties) url = base_url + "clusters/{0}/stack_versions".format(finalize_options.cluster_name) admin_auth = base64.encodestring('%s:%s' % (admin_login, admin_password)).replace('\n', '') request = urllib2.Request(url) request.add_header('Authorization', 'Basic %s' % admin_auth) request.add_header('X-Requested-By', 'ambari') data = { "ClusterStackVersions": { "repository_version": finalize_options.desired_repo_version, "state": "CURRENT", "force": "true" if finalize_options.force_repo_version else "false" } } if get_verbose(): sys.stdout.write('\nCalling API ' + url + ' : ' + str(data) + '\n') request.add_data(json.dumps(data)) request.get_method = lambda: 'PUT' try: response = urllib2.urlopen(request) except urllib2.HTTPError, e: code = e.getcode() content = e.read() err = 'Error during setting current version. Http status code - {0}. \n {1}'.format( code, content) raise FatalException(1, err)
def getLdapPropertyFromDB(properties, admin_login, admin_password, property_name): ldapProperty = None url = get_ambari_server_api_base(properties) + SETUP_LDAP_CONFIG_URL admin_auth = base64.encodestring('%s:%s' % (admin_login, admin_password)).replace('\n', '') request = urllib2.Request(url) request.add_header('Authorization', 'Basic %s' % admin_auth) request.add_header('X-Requested-By', 'ambari') request.get_method = lambda: 'GET' request_in_progress = True sys.stdout.write('\nFetching LDAP configuration from DB') numOfTries = 0 while request_in_progress: numOfTries += 1 if (numOfTries == 60): raise FatalException(1, "Could not fetch LDAP configuration within a minute; giving up!") sys.stdout.write('.') sys.stdout.flush() try: with closing(urllib2.urlopen(request, context=get_ssl_context(properties))) as response: response_status_code = response.getcode() if response_status_code != 200: request_in_progress = False err = 'Error while fetching LDAP configuration. Http status code - ' + str(response_status_code) raise FatalException(1, err) else: response_body = json.loads(response.read()) ldapProperties = response_body['Configuration']['properties'] ldapProperty = ldapProperties[property_name] if not ldapProperty: time.sleep(1) else: request_in_progress = False except Exception as e: request_in_progress = False err = 'Error while fetching LDAP configuration. Error details: %s' % e raise FatalException(1, err) return ldapProperty
def sync_ldap(options): if not is_root(): err = 'Ambari-server sync-ldap should be run with ' \ 'root-level privileges' raise FatalException(4, err) server_status, pid = is_server_runing() if not server_status: err = 'Ambari Server is not running.' raise FatalException(1, err) properties = get_ambari_properties() if properties == -1: raise FatalException(1, "Failed to read properties file.") ldap_configured = properties.get_property(IS_LDAP_CONFIGURED) if ldap_configured != 'true': err = "LDAP is not configured. Run 'ambari-server setup-ldap' first." raise FatalException(1, err) # set ldap sync options ldap_sync_options = LdapSyncOptions(options) if ldap_sync_options.no_ldap_sync_options_set(): err = 'Must specify a sync option (all, existing, users or groups). Please invoke ambari-server.py --help to print the options.' raise FatalException(1, err) admin_login = get_validated_string_input(prompt="Enter Ambari Admin login: "******"Enter Ambari Admin password: "******"Event":{"specs":[{"principal_type":"users","sync_type":"all"},{"principal_type":"groups","sync_type":"all"}]}}] elif ldap_sync_options.ldap_sync_existing: sys.stdout.write('Syncing existing.') bodies = [{"Event":{"specs":[{"principal_type":"users","sync_type":"existing"},{"principal_type":"groups","sync_type":"existing"}]}}] else: sys.stdout.write('Syncing specified users and groups.') bodies = [{"Event":{"specs":[]}}] body = bodies[0] events = body['Event'] specs = events['specs'] if ldap_sync_options.ldap_sync_users is not None: new_specs = [{"principal_type":"users","sync_type":"specific","names":""}] get_ldap_event_spec_names(ldap_sync_options.ldap_sync_users, specs, new_specs) if ldap_sync_options.ldap_sync_groups is not None: new_specs = [{"principal_type":"groups","sync_type":"specific","names":""}] get_ldap_event_spec_names(ldap_sync_options.ldap_sync_groups, specs, new_specs) if get_verbose(): sys.stdout.write('\nCalling API ' + url + ' : ' + str(bodies) + '\n') request.add_data(json.dumps(bodies)) request.get_method = lambda: 'POST' try: response = urllib2.urlopen(request) except Exception as e: err = 'Sync event creation failed. Error details: %s' % e raise FatalException(1, err) response_status_code = response.getcode() if response_status_code != 201: err = 'Error during syncing. Http status code - ' + str(response_status_code) raise FatalException(1, err) response_body = json.loads(response.read()) url = response_body['resources'][0]['href'] request = urllib2.Request(url) request.add_header('Authorization', 'Basic %s' % admin_auth) request.add_header('X-Requested-By', 'ambari') body = [{"LDAP":{"synced_groups":"*","synced_users":"*"}}] request.add_data(json.dumps(body)) request.get_method = lambda: 'GET' request_in_progress = True while request_in_progress: sys.stdout.write('.') sys.stdout.flush() try: response = urllib2.urlopen(request) except Exception as e: request_in_progress = False err = 'Sync event check failed. Error details: %s' % e raise FatalException(1, err) response_status_code = response.getcode() if response_status_code != 200: err = 'Error during syncing. Http status code - ' + str(response_status_code) raise FatalException(1, err) response_body = json.loads(response.read()) sync_info = response_body['Event'] if sync_info['status'] == 'ERROR': raise FatalException(1, str(sync_info['status_detail'])) elif sync_info['status'] == 'COMPLETE': print '\n\nCompleted LDAP Sync.' print 'Summary:' for principal_type, summary in sync_info['summary'].iteritems(): print ' {0}:'.format(principal_type) for action, amount in summary.iteritems(): print ' {0} = {1!s}'.format(action, amount) request_in_progress = False else: time.sleep(1) sys.stdout.write('\n') sys.stdout.flush()