Exemple #1
0
    def GET(self):
        query = web.ctx.query

        if not validate_query_string(query):
            logger.info('GET newnode - Invalid query string "%s"', query)
            return 'error: Invalid query string "{}".\n'.format(query)

        nodeid = query.lstrip('?').upper()

        if not validate_node_id_string(nodeid):
            logger.error('GET newnode - Invalid node ID string "%s".', nodeid)
            return 'error: Invalid node ID string "{}".\n'.format(nodeid)

        logger.info('GET newnode - Preparing to register "%s".', nodeid)

        node_dir = os.path.join(ssl_nodes_dir, 'node_' + nodeid)

        ##### Got node_id #####
        logger.info('GET newnode - Generating credentials for "{}".'.format(nodeid))

        with resource_lock:
            subprocess.call([
                os.path.join(script_path, 'create_client_cert.sh'),
                'node-{}'.format(nodeid.lower()),
                os.path.join('nodes/', 'node_' + nodeid),  # BUG create_client_cert.sh already prefixes path...
            ])

            append_to_authorized_keys_file(read_file(os.path.join(node_dir, 'key_rsa.pub')))

        privkey = read_file(os.path.join(node_dir, 'key.pem'))
        cert = read_file(os.path.join(node_dir, 'cert.pem'))
        key_rsa_pub_file_content = read_file(os.path.join(node_dir, 'key_rsa.pub'))

        db = Mysql( host="beehive-mysql",
                        user="******",
                        passwd="waggle",
                        db="waggle")

        mysql_row_node = db.get_node(nodeid)

        if not mysql_row_node:
            port=db.createNewNode(nodeid)
            if not port:
                print "Error: Node creation failed"
                return "Error: Node creation failed"
            mysql_row_node = db.get_node(nodeid)

        port = int(db.find_port(nodeid))

        if not port:
            logger.error("Error: port number not found !?")
            return "Error: port number not found !?"

        token = generate_token_from_key_and_cert(key=privkey, cert=cert)

        return '{key}\n{cert}\nTOKEN={token}\nPORT={ssh_port}\n{ssh_key}\n'.format(
            key=privkey,
            cert=cert,
            token=token,
            ssh_port=port,
            ssh_key=key_rsa_pub_file_content)
    def GET(self):
        logger.info("GET /newnode ")
        query = web.ctx.query

        if not validate_query_string(query):
            logger.info('GET newnode - Invalid query string "%s"', query)
            return 'error: Invalid query string "{}".\n'.format(query)

        nodeid = query.lstrip('?').upper()

        if not validate_node_id_string(nodeid):
            logger.error('GET newnode - Invalid node ID string "%s".', nodeid)
            return 'error: Invalid node ID string "{}".\n'.format(nodeid)

        logger.info('GET newnode - Preparing to register "%s".', nodeid)

        logger.info("connecting to {} {}".format(mysql_host, mysql_db))
        # check if credentials are already in database
        db = Mysql(host=mysql_host,
                   user=mysql_user,
                   passwd=mysql_passwd,
                   db=mysql_db)

        node_credentials = db.get_node_credentials(nodeid)
        #logger.info("node_credentials:", node_credentials)
        if not node_credentials:
            try:
                generate_credentials(db, nodeid)
            except Exception as e:
                return "error: {}".format(str(e))

            try:
                node_credentials = db.get_node_credentials(nodeid)
            except Exception as e:
                return "error: {}".format(str(e))

        if not node_credentials:
            return "error: Could not create credentials"

        mysql_row_node = db.get_node(nodeid)

        if not mysql_row_node:
            port = db.createNewNode(nodeid)
            if not port:
                logger.error("Error: Node creation failed")
                raise Exception("Node creation failed")
            mysql_row_node = db.get_node(nodeid)

        port = int(db.find_port(nodeid))

        if not port:
            logger.error("Error: port number not found !?")
            raise Exception("port number not found !?")

        #logger("A")
        #logger("node_credentials", node_credentials)
        try:
            rsa_private_key = node_credentials['rsa_private_key']
            rsa_public_key = node_credentials['rsa_public_key']
            signed_client_certificate = node_credentials['signed_client_certificate']
        except Exception as e:
            return 'error: credential incomplete'
        #logger("rsa_private_key:", len(rsa_private_key))
        #logger("rsa_public_key:", len(rsa_public_key))
        #logger("signed_client_certificate:", len(signed_client_certificate))

        #setup_rabbitmq_user_for_nodeid(nodeid)

        #logger("port", port)

        # logger("B", '{key}\n{cert}\nPORT={ssh_port}'.format(
        #    key=rsa_private_key, cert=signed_client_certificate, ssh_port=port) )
        # removed TOKEN={token}\n
        return_content = '{key}\n{cert}\nPORT={ssh_port}\n{ssh_key}\n'.format(
            key=rsa_private_key,
            cert=signed_client_certificate,
            ssh_port=port,
            ssh_key=rsa_public_key)

        return return_content