def runTest(self): sslUtil = SSLUtil() sslUtil.timeout = 10 # Reset the test certificate = sslUtil.getServerCertificate('https://gecos.solutia-it.es:8443/') sslUtil.removeCertificateFromTrustedCAs(certificate) certificate = sslUtil.getServerCertificate('https://ws003.juntadeandalucia.es/') sslUtil.removeCertificateFromTrustedCAs(certificate) # Start the test SSLUtil.disableSSLCertificatesVerification() self.assertFalse(SSLUtil.isSSLCertificatesVerificationEnabled()) self.assertFalse(SSLUtil.isSSLCertificatesVerificationEnabled())
def save(self, data): ''' Saving ''' self.logger.debug('save - BEGIN') if data is None: raise ValueError('data is None') if not isinstance(data, WorkstationData): raise ValueError('data is not a WorkstationData instance') if data is None: raise ValueError('data is None') # Save name to pclabel file try: fd = open(self.pclabel_file, 'w') if fd != None: fd.write(data.get_name()) fd.close() except Exception: self.logger.error('Error writing file: %s', self.pclabel_file) self.logger.error(str(traceback.format_exc())) # Do not save OU in GECOS CC at this point!! # save node_name to gcc.control file jsonUtil = JSONUtil() json_data = jsonUtil.loadJSONFromFile(self.gcc_control_file) uri_gcc = '' gcc_username = '' if json_data is not None: uri_gcc = json_data['uri_gcc'] gcc_username = json_data['gcc_username'] template = Template() template.source = get_data_file('templates/gcc.control') template.destination = self.gcc_control_file template.owner = 'root' template.group = 'root' template.mode = 00755 template.variables = { 'uri_gcc': uri_gcc, 'gcc_username': gcc_username, 'gcc_nodename': data.get_node_name(), 'ssl_verify': SSLUtil.isSSLCertificatesVerificationEnabled() } template.save() self.logger.debug('save - END')
def search_ou_by_text(self, data, searchFilter): ''' Search ou by text ''' self.logger.debug('Search ou by text...') if not self._check_credentials(data): return False if searchFilter is None: searchFilter = '' # Get the list of OUs try: url = str(data.get_url()) if url.endswith('/'): url = url[0:-1] url = "{}/ou/gca/?q={}".format(url, searchFilter) self.logger.debug('Try to connect to: %s', url) headers = { 'Content-type': 'application/json', 'Accept': 'text/plain' } user = data.get_login() password = data.get_password() r = requests.get( url, auth=(user, password), headers=headers, verify=SSLUtil.isSSLCertificatesVerificationEnabled(), timeout=self.timeout) if r.ok: self.logger.debug('Response: %s', url) arr_ou = False if hasattr(r, 'text'): self.logger.debug('Response: %s', r.text) arr_ou = json.loads(r.text)['ous'] else: self.logger.debug('Response: %s', r.content) arr_ou = json.loads(r.content)['ous'] return arr_ou self.logger.debug('Response: NOT OK') except Exception: self.logger.warn('Error connecting to Gecos server: %s', data.get_url()) self.logger.warn(str(traceback.format_exc())) return False
def delete(self, data): ''' Deleting ''' self.logger.debug('delete - BEGIN') if data is None: raise ValueError('data is None') if not isinstance(data, WorkstationData): raise ValueError('data is not a WorkstationData instance') if data.get_name() is None: raise ValueError('data.name is None') # Remove pclabel_file try: if os.path.isfile(self.pclabel_file): os.remove(self.pclabel_file) except Exception: self.logger.error('Error removing file: %s', self.pclabel_file) self.logger.error(str(traceback.format_exc())) # Eliminate node_name from gcc.control file jsonUtil = JSONUtil() json_data = jsonUtil.loadJSONFromFile(self.gcc_control_file) uri_gcc = '' gcc_username = '' if json_data is not None: uri_gcc = json_data['uri_gcc'] gcc_username = json_data['gcc_username'] template = Template() template.source = get_data_file('templates/gcc.control') template.destination = self.gcc_control_file template.owner = 'root' template.group = 'root' template.mode = 00755 template.variables = { 'uri_gcc': uri_gcc, 'gcc_username': gcc_username, 'gcc_nodename': '', 'ssl_verify': SSLUtil.isSSLCertificatesVerificationEnabled() } template.save() self.logger.debug('delete - END')
def get_computer_names(self, data): ''' Get all computer names by text ''' self.logger.debug('Get all computer names by text...') if not self._check_credentials(data): return False # Get the list of workstation names try: url = str(data.get_url()) if url.endswith('/'): url = url[0:-1] url = "{}/computers/list/".format(url) self.logger.debug('Try to connect to: %s', url) headers = { 'Content-type': 'application/json', 'Accept': 'text/plain' } user = data.get_login() password = data.get_password() r = requests.get( url, auth=(user, password), headers=headers, verify=SSLUtil.isSSLCertificatesVerificationEnabled(), timeout=self.timeout) if r.ok: self.logger.debug('Response: %s', url) computer_names = False if hasattr(r, 'text'): self.logger.debug('Response: %s', r.text) computer_names = json.loads(r.text)['computers'] else: self.logger.debug('Response: %s', r.content) computer_names = json.loads(r.content)['computers'] return computer_names self.logger.debug('Response: NOT OK') except Exception: self.logger.warn('Error connecting to Gecos server: %s', data.get_url()) self.logger.warn(str(traceback.format_exc())) return False
def save(self, data): ''' Saving data ''' self.logger.debug('save - BEGIN') if data is None: raise ValueError('data is None') if not isinstance(data, GecosAccessData): raise ValueError('data is not a GecosAccessData instance') # Insert the data in cache memory self.previous_saved_data = data # Get gcc_nodename from data file try: jsonUtil = JSONUtil() json_data = jsonUtil.loadJSONFromFile(self.data_file) gcc_nodename = '' if json_data is not None: gcc_nodename = json_data['gcc_nodename'] if gcc_nodename is None or gcc_nodename.strip() == '': gcc_nodename = self.calculate_workstation_node_name() except Exception: # Can't get gcc_nodename from file, calculate it gcc_nodename = self.calculate_workstation_node_name() # Save data to data file template = Template() template.source = get_data_file('templates/gcc.control') template.destination = self.data_file template.owner = 'root' template.group = 'root' template.mode = 00755 url = data.get_url() if url.endswith('/'): url = url[0:-1] template.variables = { 'uri_gcc': url, 'gcc_username': data.get_login(), 'gcc_nodename': gcc_nodename, 'ssl_verify': SSLUtil.isSSLCertificatesVerificationEnabled() } return template.save()
def validate_credentials(self, data): ''' Validating credentials ''' self.logger.debug('Validating credentials...') if not self._check_credentials(data): return False # Check credentials try: url = str(data.get_url()) if urlparse(url).path in ['', '/']: url = "{}/auth/config/".format( url[0:-1] if url.endswith('/') else url) self.logger.debug('Try to connect to: %s', url) headers = { 'Content-type': 'application/json', 'Accept': 'text/plain' } user = data.get_login() password = data.get_password() r = requests.get( url, auth=(user, password), headers=headers, verify=SSLUtil.isSSLCertificatesVerificationEnabled(), timeout=self.timeout) if r.ok: if hasattr(r, 'text'): self.last_request_content = r.text else: self.last_request_content = r.content return True except Exception: self.logger.warn('Error connecting to Gecos server: %s', data.get_url()) self.logger.warn(str(traceback.format_exc())) return False
def is_registered_chef_node(self, data, nodename): ''' Is registered Chef node ? ''' self.logger.debug('IsRegistered? Chef node (%s)...', nodename) if not self._check_credentials(data): return False if nodename is None or nodename.strip() == '': self.logger.warn('nodename is empty!') return False # Unregister the Chef Node try: url = str(data.get_url()) if url.endswith('/'): url = url[0:-1] url = "{}/register/node/?node_id={}".format(url, nodename) self.logger.debug('Try to connect to: %s', url) headers = { 'Content-type': 'application/json', 'Accept': 'text/plain' } user = data.get_login() password = data.get_password() r = requests.get( url, auth=(user, password), headers=headers, verify=SSLUtil.isSSLCertificatesVerificationEnabled(), timeout=self.timeout) if r.ok: self.logger.debug('Response: %s', url) response_json = False if hasattr(r, 'text'): self.logger.debug('Response: %s', r.text) response_json = json.loads(r.text) else: self.logger.debug('Response: %s', r.content) response_json = json.loads(r.content) if response_json is None: self.logger.error( 'Error unregistering computer: NO RESPONSE') return False if not response_json["ok"]: self.logger.error('Error unregistering computer: %s', response_json['message']) return False return True self.logger.debug('Response: NOT OK') except Exception: self.logger.warn('Error connecting to Gecos server: %s', data.get_url()) self.logger.warn(str(traceback.format_exc())) return False
def reregister_chef_node(self, data, nodename): ''' Re-registering computer ''' self.logger.debug('Re-Register chef node (%s)...', nodename) if not self._check_credentials(data): return False if nodename is None or nodename.strip() == '': self.logger.warn('nodename is empty!') return False # Register in the Chef Node try: url = str(data.get_url()) if url.endswith('/'): url = url[0:-1] url = "{}/register/node/".format(url) self.logger.debug('Try to connect to: %s', url) user = data.get_login() password = data.get_password() payload = {'node_id': nodename} self.logger.debug('payload: %s', json.dumps(payload)) r = requests.put( url, auth=(user, password), verify=SSLUtil.isSSLCertificatesVerificationEnabled(), timeout=self.timeout, data=payload) if r.ok: self.logger.debug('Response: %s', url) response_json = False if hasattr(r, 'text'): self.logger.debug('Response: %s', r.text) response_json = json.loads(r.text) else: self.logger.debug('Response: %s', r.content) response_json = json.loads(r.content) if response_json is None: self.logger.error( 'Error registering computer: NO RESPONSE') return False if not response_json["ok"]: self.logger.error('Error registering computer: %s', response_json['message']) return False return response_json["client_private_key"] self.logger.debug('Response: NOT OK') except Exception: self.logger.warn('Error connecting to Gecos server: %s', data.get_url()) self.logger.warn(str(traceback.format_exc())) return False