def standalone():
    if len(sys.argv) != 2:
        print USAGE
        return
    in_filename = sys.argv[1]
    in_filename_prefix = string.replace(in_filename, ".csv", "")
    out_filename = "%s.auto.csv" % (in_filename_prefix, )
    print "Autocompleting bulk provisioning users in %s..." % (in_filename, )
    try:
        with open(in_filename, 'rb') as in_file:
            with open(out_filename, 'w') as out_file:
                csv_reader = csv.reader(in_file)
                csv_writer = csv.writer(out_file)
                for row in csv_reader:
                    if len(row) == 1:
                        # Default private SIP ID to public SIP ID, stripping any sip: prefix
                        row.append(utils.sip_public_id_to_private(row[0]))
                    if len(row) == 2:
                        # Default realm to domain of public SIP ID
                        row.append(utils.sip_uri_to_domain(row[0]))
                    if len(row) == 3:
                        # Missing password - generate a random one
                        #row.append(utils.create_secure_human_readable_id(48))
                        row.append("7kkzTyGW")
                    csv_writer.writerow(row)
        print "Autocompleted bulk provisioning users written to %s" % (
            out_filename, )
    except IOError as e:
        print "Failed to read/write to %s:" % (e.filename, )
        traceback.print_exc()
Example #2
0
def standalone():
    if len(sys.argv) != 2:
        print USAGE
        return
    in_filename = sys.argv[1]
    in_filename_prefix = string.replace(in_filename, ".csv", "")
    out_filename = "%s.auto.csv" % (in_filename_prefix,)
    print "Autocompleting bulk provisioning users in %s..." % (in_filename,)
    try:
        with open(in_filename, 'rb') as in_file:
            with open(out_filename, 'w') as out_file:
                csv_reader = csv.reader(in_file)
                csv_writer = csv.writer(out_file)
                for row in csv_reader:
                    if len(row) == 1:
                        # Default private SIP ID to public SIP ID, stripping any sip: prefix
                        row.append(utils.sip_public_id_to_private(row[0]))
                    if len(row) == 2:
                        # Default realm to system default
                        row.append(settings.SIP_DIGEST_REALM)
                    if len(row) == 3:
                        # Missing password - generate a random one
                        row.append(utils.create_secure_human_readable_id(48))
                    csv_writer.writerow(row)
        print "Autocompleted bulk provisioning users written to %s" % (out_filename,)
    except IOError as e:
        print "Failed to read/write to %s:" % (e.filename,)
        traceback.print_exc();
Example #3
0
 def _on_get_publics_success(private_id, responses):
     _log.debug("Got related public ids")
     parsed_body = json.loads(responses[0].body)
     public_ids = parsed_body["associated_public_ids"]
     if (utils.sip_public_id_to_private(sip_uri) == private_id and
         len(public_ids) > 1):
         # Do not permit deletion of an original public identity if others exist
         # otherwise another may claim the same private id from the pool
         _log.error("Failed to delete default public ID %s while other IDs are attached to the same private ID" % sip_uri);
         _log.error("Other public IDs are %s" % ", ".join(public_ids))
         on_failure(responses[0])
     else:
         # Only delete the digest if there is only a single private identity
         # associated with our sip_uri (i.e. this is the last public id)
         delete_digest = (len(public_ids) == 1)
         _delete_number(db_sess, sip_uri, private_id, delete_digest, on_success, on_failure)
Example #4
0
 def _on_get_publics_success(private_id, responses):
     _log.debug("Got related public ids")
     parsed_body = json.loads(responses[0].body)
     public_ids = parsed_body["associated_public_ids"]
     if (utils.sip_public_id_to_private(sip_uri) == private_id
             and len(public_ids) > 1):
         # Do not permit deletion of an original public identity if others exist
         # otherwise another may claim the same private id from the pool
         _log.error(
             "Failed to delete default public ID %s while other IDs are attached to the same private ID"
             % sip_uri)
         _log.error("Other public IDs are %s" % ", ".join(public_ids))
         on_failure(responses[0])
     else:
         # Only delete the digest if there is only a single private identity
         # associated with our sip_uri (i.e. this is the last public id)
         delete_digest = (len(public_ids) == 1)
         _delete_number(db_sess, sip_uri, private_id, delete_digest,
                        on_success, on_failure, force_delete)
Example #5
0
 def get(self, public_id):
     try:
         result = yield self.cass.get(column_family=self.table, 
                                      key=public_id, 
                                      column=self.column)
         ifc = result.column.value
     except NotFoundException, e:
         if not settings.HSS_ENABLED: 
             # No HSS
             raise HTTPError(404)
         # IFC not in Cassandra, attempt to fetch from HSS
         try:
             # TODO For now we assume to same public to private id mapping as ellis
             private_id = utils.sip_public_id_to_private(public_id)
             ifc = yield self.application.hss_gateway.get_ifc(private_id, public_id)
         except HSSNotFound:
             raise HTTPError(404)
         # Have result from HSS, store in Cassandra
         yield self.cass.insert(column_family=self.table, 
                                key=public_id, 
                                column=self.column, 
                                value=ifc)
Example #6
0
 def get(self, public_id):
     try:
         result = yield self.cass.get(column_family=self.table,
                                      key=public_id,
                                      column=self.column)
         ifc = result.column.value
     except NotFoundException, e:
         if not settings.HSS_ENABLED:
             # No HSS
             raise HTTPError(404)
         # IFC not in Cassandra, attempt to fetch from HSS
         try:
             # TODO For now we assume to same public to private id mapping as ellis
             private_id = utils.sip_public_id_to_private(public_id)
             ifc = yield self.application.hss_gateway.get_ifc(
                 private_id, public_id)
         except HSSNotFound:
             raise HTTPError(404)
         # Have result from HSS, store in Cassandra
         yield self.cass.insert(column_family=self.table,
                                key=public_id,
                                column=self.column,
                                value=ifc)
Example #7
0
    def post(self, username):
        """Allocate a phone number."""
        _log.debug("Number allocation API call (PSTN = %s)", self.get_argument('pstn', 'false'))
        user_id = self.get_and_check_user_id(username)
        db_sess = self.db_session()
        pstn = self.get_argument('pstn', 'false') == 'true'
        private_id = self.get_argument('private_id', None)
        try:
            number_id = numbers.allocate_number(db_sess, user_id, pstn)
            sip_uri = numbers.get_number(db_sess, number_id, user_id)
            self.sip_uri = sip_uri
            _log.debug("SIP URI %s", sip_uri)
            # FIXME We shouldn't commit until we know XDM/HS have succeeded but
            # if we hold the transaction open we can deadlock
            # * Request 1 comes in and allocates a number, kicks off requests
            #   to XDM/Homestead, has transaction open, returns thread to Tornado
            # * Request 2 comes in, allocates same number, can't lock it for
            #   update because Request 1 is holding it.  Blocks.
            # * Request 1 gets response but the thread is tied up
            # * Request 2 SQL transaction times out.
            # * Request 1 probably completes..
            db_sess.commit()
        except NotFound:
            # FIXME email operator to tell them we're out of numbers!
            _log.warning("No available numbers")
            raise HTTPError(httplib.SERVICE_UNAVAILABLE,
                            "No available numbers")

        # Work out the response we'll send if the upstream requests
        # are successful.
        number = utils.sip_uri_to_phone_number(sip_uri)
        pretty_number = utils.format_phone_number(number)
        self.__response = {"sip_uri": sip_uri,
                           "sip_username": number,
                           "number": number,
                           "pstn": pstn,
                           "formatted_number": pretty_number,
                           "number_id": number_id.hex}

        # Generate a random password and store it in Homestead.
        _log.debug("Populating other servers...")
        self._request_group = HTTPCallbackGroup(self._on_post_success,
                                                self._on_post_failure)

        public_callback = self._request_group.callback()

        if private_id == None:
            # No private id was provided, so we need to create a new
            # digest in Homestead
            private_id = utils.sip_public_id_to_private(sip_uri)
            sip_password = utils.generate_sip_password()
            homestead.create_private_id(private_id,
                                        sip_password,
                                        self._request_group.callback())
            self.__response["sip_password"] = sip_password

        # Associate the new public identity with the private identity in Homestead
        # and store the iFCs in homestead.
        homestead.create_public_id(private_id,
                                   sip_uri,
                                   ifcs.generate_ifcs(settings.SIP_DIGEST_REALM),
                                   public_callback)

        self.__response["private_id"] = private_id

        # Concurrently, store the default simservs in XDM.
        with open(settings.XDM_DEFAULT_SIMSERVS_FILE) as xml_file:
            default_xml = xml_file.read()
        xdm.put_simservs(sip_uri, default_xml, self._request_group.callback())
Example #8
0
    def post(self, username, sip_uri):
        """Allocate a phone number."""
        _log.debug("Specific number allocation API call (%s)", sip_uri)
        self.is_admin_request()
        user_id = self.get_and_check_user_id(username)
        db_sess = self.db_session()
        pstn = self.get_argument("pstn", "false").lower() == "true"
        private_id = self.get_argument("private_id", None)
        new_private_id = self.get_argument("new_private_id", "false").lower() == "true"
        try:
            number_id = uuid.UUID(numbers.get_sip_uri_number_id(db_sess, sip_uri))
        except NotFound:
            # This SIP URI is not currently in the pool, so add it
            number_id = numbers.add_number_to_pool(db_sess, sip_uri, False, True)
        numbers.allocate_specific_number(db_sess, user_id, number_id)
        self.sip_uri = sip_uri
        db_sess.commit()

        # Work out the response we'll send if the upstream requests
        # are successful.
        number = utils.sip_uri_to_phone_number(sip_uri)
        pretty_number = format_phone_number(number)
        self.__response = {
            "sip_uri": sip_uri,
            "sip_username": number,
            "number": number,
            "pstn": pstn,
            "formatted_number": pretty_number,
            "number_id": number_id.hex,
        }

        # Generate a random password and store it in Homestead.
        _log.debug("Populating other servers...")
        self._request_group = HTTPCallbackGroup(self._on_post_success, self._on_post_failure)

        public_callback = self._request_group.callback()

        if private_id == None:
            # No private id was provided, so we need to create a new
            # digest in Homestead
            private_id = utils.sip_public_id_to_private(sip_uri)
            new_private_id = True

        if new_private_id:
            sip_password = utils.generate_sip_password()
            _log.debug("About to create private ID at Homestead")
            homestead.create_private_id(
                private_id, utils.sip_uri_to_domain(sip_uri), sip_password, self._request_group.callback()
            )
            _log.debug("Created private ID at Homestead")

            self.__response["sip_password"] = sip_password
        self.__response["sip_username"] = private_id

        # Associate the new public identity with the private identity in Homestead
        # and store the iFCs in homestead.
        homestead.create_public_id(
            private_id, sip_uri, ifcs.generate_ifcs(utils.sip_uri_to_domain(sip_uri)), public_callback
        )

        self.__response["private_id"] = private_id

        # Concurrently, store the default simservs in XDM.
        with open(settings.XDM_DEFAULT_SIMSERVS_FILE) as xml_file:
            default_xml = xml_file.read()
        xdm.put_simservs(sip_uri, default_xml, self._request_group.callback())
Example #9
0
    def post(self, username, sip_uri):  # pragma: no cover
        """Allocate a phone number."""
        _log.debug("Specific number allocation API call (%s)", sip_uri)
        self.is_admin_request()
        user_id = self.get_and_check_user_id(username)
        db_sess = self.db_session()
        pstn = self.get_argument('pstn', 'false').lower() == 'true'
        private_id = self.get_argument('private_id', None)
        new_private_id = self.get_argument('new_private_id',
                                           'false').lower() == 'true'
        try:
            number_id = uuid.UUID(
                numbers.get_sip_uri_number_id(db_sess, sip_uri))
        except NotFound:
            # This SIP URI is not currently in the pool, so add it
            number_id = numbers.add_number_to_pool(db_sess, sip_uri, False,
                                                   True)
        numbers.allocate_specific_number(db_sess, user_id, number_id)
        self.sip_uri = sip_uri
        db_sess.commit()

        # Work out the response we'll send if the upstream requests
        # are successful.
        number = utils.sip_uri_to_phone_number(sip_uri)
        pretty_number = format_phone_number(number)
        self.__response = {
            "sip_uri": sip_uri,
            "sip_username": number,
            "number": number,
            "pstn": pstn,
            "formatted_number": pretty_number,
            "number_id": number_id.hex
        }

        # Generate a random password and store it in Homestead.
        _log.debug("Populating other servers...")
        self._request_group = HTTPCallbackGroup(self._on_post_success,
                                                self._on_post_failure)

        public_callback = self._request_group.callback()

        if private_id == None:
            # No private id was provided, so we need to create a new
            # digest in Homestead
            private_id = utils.sip_public_id_to_private(sip_uri)
            new_private_id = True

        if new_private_id:
            sip_password = utils.generate_sip_password()
            _log.debug("About to create private ID at Homestead")
            homestead.create_private_id(private_id,
                                        utils.sip_uri_to_domain(sip_uri),
                                        sip_password,
                                        self._request_group.callback())
            _log.debug("Created private ID at Homestead")

            self.__response["sip_password"] = sip_password
        self.__response["sip_username"] = private_id

        # Associate the new public identity with the private identity in Homestead
        # and store the iFCs in homestead.
        homestead.create_public_id(
            private_id, sip_uri,
            ifcs.generate_ifcs(utils.sip_uri_to_domain(sip_uri)),
            public_callback)

        self.__response["private_id"] = private_id

        # Concurrently, store the default simservs in XDM.
        xdm.put_simservs(sip_uri, simservs.default_simservs(),
                         self._request_group.callback())
Example #10
0
    def post(self, username):
        """Allocate a phone number."""
        _log.debug("Number allocation API call (PSTN = %s)",
                   self.get_argument('pstn', 'false'))
        user_id = self.get_and_check_user_id(username)
        db_sess = self.db_session()
        pstn = self.get_argument('pstn', 'false').lower() == 'true'
        private_id = self.get_argument('private_id', None)
        try:
            number_id = numbers.allocate_number(db_sess, user_id, pstn)
            sip_uri = numbers.get_number(db_sess, number_id, user_id)
            self.sip_uri = sip_uri
            _log.debug("SIP URI %s", sip_uri)
            # FIXME We shouldn't commit until we know XDM/HS have succeeded but
            # if we hold the transaction open we can deadlock
            # * Request 1 comes in and allocates a number, kicks off requests
            #   to XDM/Homestead, has transaction open, returns thread to Tornado
            # * Request 2 comes in, allocates same number, can't lock it for
            #   update because Request 1 is holding it.  Blocks.
            # * Request 1 gets response but the thread is tied up
            # * Request 2 SQL transaction times out.
            # * Request 1 probably completes..
            db_sess.commit()
        except NotFound:  # pragma: no cover
            # FIXME email operator to tell them we're out of numbers!
            db_sess.rollback()
            _log.warning("No available numbers")
            raise HTTPError(httplib.SERVICE_UNAVAILABLE,
                            "No available numbers")

        # Work out the response we'll send if the upstream requests
        # are successful.
        number = utils.sip_uri_to_phone_number(sip_uri)
        pretty_number = format_phone_number(number)
        self.__response = {
            "sip_uri": sip_uri,
            "sip_username": number,
            "number": number,
            "pstn": pstn,
            "formatted_number": pretty_number,
            "number_id": number_id.hex
        }

        # Generate a random password and store it in Homestead.
        _log.debug("Populating other servers...")
        self._request_group = HTTPCallbackGroup(self._on_post_success,
                                                self._on_post_failure)

        public_callback = self._request_group.callback()

        if private_id == None:
            # No private id was provided, so we need to create a new
            # digest in Homestead
            private_id = utils.sip_public_id_to_private(sip_uri)
            sip_password = utils.generate_sip_password()
            _log.debug("About to create private ID at Homestead")
            homestead.create_private_id(private_id,
                                        utils.sip_uri_to_domain(sip_uri),
                                        sip_password,
                                        self._request_group.callback())
            _log.debug("Created private ID at Homestead")
            self.__response["sip_password"] = sip_password

        # Associate the new public identity with the private identity in Homestead
        # and store the iFCs in homestead.
        homestead.create_public_id(
            private_id, sip_uri,
            ifcs.generate_ifcs(utils.sip_uri_to_domain(sip_uri)),
            public_callback)

        self.__response["private_id"] = private_id

        # Concurrently, store the default simservs in XDM.
        xdm.put_simservs(sip_uri, simservs.default_simservs(),
                         self._request_group.callback())