def create_user(private_id, public_id, domain, password, ifc, plaintext=False): callback = Callback() homestead.get_digest(private_id, callback) response = callback.wait()[0] if isinstance(response, HTTPError) and response.code != 404: _log.error("Failed to check private ID %s - HTTP status code %d", private_id, response.code) return False if response.code == 200: _log.error("Private ID %s already exists - not creating", private_id) return True homestead.create_private_id(private_id, domain, password, callback, plaintext=plaintext) response = callback.wait()[0] if isinstance(response, HTTPError): _log.error("Failed to create private ID %s - HTTP status code %d", private_id, response.code) return False homestead.create_public_id(private_id, public_id, ifc, callback) response = callback.wait()[0] if isinstance(response, HTTPError): _log.error("Failed to create public ID %s - HTTP status code %d", public_id, response.code) return False return True
def test_get_password(self, settings, AsyncHTTPClient): self.standard_setup(settings, AsyncHTTPClient) callback = Mock() homestead.get_digest(PRIVATE_URI, callback) self.mock_httpclient.fetch.assert_called_once_with( 'http://homestead/privatecredentials/pri%40foo.bar/digest', callback, method='GET')
def display_user(public_id, short=False, quiet=False): success = True callback = Callback() if not short: conditional_print(not quiet, "Public User ID %s:" % (public_id)) homestead.get_associated_privates(public_id, callback) response = callback.wait()[0] public_id_missing = (response.code == 404) if response.code == 200: private_ids = json.loads(response.body) for private_id in private_ids['private_ids']: homestead.get_digest(private_id, callback) response = callback.wait()[0] if response.code == 200: av = json.loads(response.body) if 'digest_ha1' in av: password = av['digest_ha1'] if 'plaintext_password' in av: password += " (%s)" % (av['plaintext_password'],) if short: conditional_print(not quiet, "%s/%s: %s" % (public_id, private_id, password)) else: conditional_print(not quiet, " Private User ID %s:" % (private_id,)) conditional_print(not quiet, " HA1 digest: %s" % (password,)) else: _log.error("Failed to retrieve digest for private ID %s - HTTP status code %d", private_id, response.code) success = False else: _log.error("Failed to retrieve private IDs for public ID %s - HTTP status code %d", public_id, response.code) success = False # Default to True so that if we don't check, we may still report that no # information was found. filter_criteria_missing = True if not short: homestead.get_filter_criteria(public_id, callback) response = callback.wait()[0] filter_criteria_missing = (response.code == 404) if response.code == 200: ifc = xml.dom.minidom.parseString(response.body) ifc_str = ifc.toprettyxml(indent=" ") ifc_str = "\n".join(filter(lambda l: l.strip() != "", ifc_str.split("\n"))) ifc_str = " " + ifc_str.replace("\n", "\n ") conditional_print(not quiet, " iFC:") conditional_print(not quiet, ifc_str) else: _log.error("Failed to retrieve iFC for public ID %s - HTTP status code %d", public_id, response.code) success = False if filter_criteria_missing and public_id_missing: _log.error("Failed to find any information for public ID %s.", public_id) return success
def display_user(public_id, short=False, quiet=False): success = True callback = Callback() if not short: conditional_print(not quiet, "Public User ID %s:" % (public_id)) homestead.get_associated_privates(public_id, callback) response = callback.wait()[0] public_id_missing = (response.code == 404) if response.code == 200: private_ids = json.loads(response.body) for private_id in private_ids['private_ids']: homestead.get_digest(private_id, callback) response = callback.wait()[0] if response.code == 200: av = json.loads(response.body) if 'digest_ha1' in av: password = av['digest_ha1'] if 'plaintext_password' in av: password += " (%s)" % (av['plaintext_password'],) if short: conditional_print(not quiet, "%s/%s: %s" % (public_id, private_id, password)) else: conditional_print(not quiet, " Private User ID %s:" % (private_id,)) conditional_print(not quiet, " HA1 digest: %s" % (password,)) else: _log.error("Failed to retrieve digest for private ID %s - HTTP status code %d", private_id, response.code) success = False else: _log.error("Failed to retrieve private IDs for public ID %s - HTTP status code %d", public_id, response.code) success = False # Default to True so that if we don't check, we may still report that no # information was found. filter_criteria_missing = True if not short: homestead.get_filter_criteria(public_id, callback) response = callback.wait()[0] filter_criteria_missing = (response.code == 404) if response.code == 200: ifc = defusedxml.minidom.parseString(response.body) ifc_str = ifc.toprettyxml(indent=" ") ifc_str = "\n".join(filter(lambda l: l.strip() != "", ifc_str.split("\n"))) ifc_str = " " + ifc_str.replace("\n", "\n ") conditional_print(not quiet, " iFC:") conditional_print(not quiet, ifc_str) else: _log.error("Failed to retrieve iFC for public ID %s - HTTP status code %d", public_id, response.code) success = False if filter_criteria_missing and public_id_missing: _log.error("Failed to find any information for public ID %s.", public_id) return success
def ensure_digest_exists(sip_uri, **kwargs): private_id = kwargs["private_ids"][0] print "Verifying digest exists for %s" % private_id global pending_requests pending_requests+=1 delete_line = number_deleter(private_id, sip_uri) homestead.get_digest(private_id, create_get_handler(sip_uri, on_found=ensure_valid_ifc, on_not_found=delete_line))
def test_get_password(self, settings, AsyncHTTPClient): self.standard_setup(settings, AsyncHTTPClient) callback = Mock() homestead.get_digest(PRIVATE_URI, callback) self.mock_httpclient.fetch.assert_called_once_with( 'http://homestead/private/pri%40foo.bar', callback, method='GET', follow_redirects=False, allow_ipv6=True)
def test_get_password(self, settings, AsyncHTTPClient): self.standard_setup(settings, AsyncHTTPClient) callback = Mock() homestead.get_digest(PRIVATE_URI, callback) self.mock_httpclient.fetch.assert_called_once_with( 'http://homestead/private/pri%40foo.bar', ANY, method='GET', follow_redirects=False, allow_ipv6=True)
def post_digest_callback(response, sip_uri=sip_uri): """ Handle post response, report errors, issue a final GET to check we succeeded. """ global num_responses, num_requests, errors print "%s Post digest response %s" % (sip_uri, response.code) if response.code // 100 != 2: print >> sys.stderr, "%s Bad response %s" % (sip_uri, response) errors += 1 homestead.get_digest(sip_uri, check_callback)
def ensure_digest_exists(sip_uri, **kwargs): private_id = kwargs["private_ids"][0] print "Verifying digest exists for %s" % private_id global pending_requests pending_requests += 1 delete_line = number_deleter(private_id, sip_uri) homestead.get_digest( private_id, create_get_handler(sip_uri, on_found=ensure_valid_ifc, on_not_found=delete_line))
def standalone(): global num_responses, num_requests db_sess = connection.Session() c = db_sess.execute("SELECT number FROM numbers where OWNER_ID is not NULL;") def inc_resp_count(): global num_responses, num_requests num_responses += 1 print "%s responses outstanding" % (num_requests - num_responses) if num_responses == num_requests: print "Last response" print "There were %d errors" % errors io_loop.stop() for (sip_uri,) in c: print "%s Sending request" % (sip_uri,) def get_ifc_callback(response, sip_uri=sip_uri): """ Handle response to initial GET, only PUT new data if there wasn't anything there. """ global num_responses, num_requests print "%s Get ifc response %s" % (sip_uri, response.code) if response.code == 404: print "%s ifc needs to be repopulated" % (sip_uri,) homestead.put_filter_criteria(sip_uri, ifcs.generate_ifcs(utils.sip_uri_to_domain(sip_uri)), put_ifc_callback) else: inc_resp_count() def put_ifc_callback(response, sip_uri=sip_uri): """ Handle put response, report errors, issue a final GET to check we succeeded. """ global num_responses, num_requests, errors print "%s Put ifc response %s" % (sip_uri, response.code) if response.code // 100 != 2: print >> sys.stderr, "%s Bad response %s" % (sip_uri, response) errors += 1 homestead.get_filter_criteria(sip_uri, check_callback) def get_digest_callback(response, sip_uri=sip_uri): """ Handle response to initial GET, only PUT new data if there wasn't anything there. """ global num_responses, num_requests print "%s Get digest response %s" % (sip_uri, response.code) if response.code == 404: print "%s digest needs to be repopulated" % (sip_uri,) homestead.put_password(sip_uri, sip_uri, utils.generate_sip_password(), post_digest_callback) else: inc_resp_count() def post_digest_callback(response, sip_uri=sip_uri): """ Handle post response, report errors, issue a final GET to check we succeeded. """ global num_responses, num_requests, errors print "%s Post digest response %s" % (sip_uri, response.code) if response.code // 100 != 2: print >> sys.stderr, "%s Bad response %s" % (sip_uri, response) errors += 1 homestead.get_digest(sip_uri, check_callback) def check_callback(response, sip_uri=sip_uri): """ Handle response to the check request, reports errors. """ global num_responses, num_requests, errors print "%s Check response %s" % (sip_uri, response.code) if response.code != 200: print >> sys.stderr, "%s Bad response %s" % (sip_uri, response) errors += 1 inc_resp_count() num_requests += 2 homestead.get_digest(sip_uri, get_digest_callback) homestead.get_filter_criteria(sip_uri, get_ifc_callback) print "Finished queuing requests."