Ejemplo n.º 1
0
def main():
    """Create CAI dump files from fake data."""
    logger.enable_console_log()
    config = InventoryConfig(gcp_api_mocks.ORGANIZATION_ID, '', {}, '',
                             {'enabled': False})
    service_config = TestServiceConfig('sqlite', config)
    config.set_service_config(service_config)

    resources = []
    iam_policies = []
    with MemoryStorage() as storage:
        progresser = NullProgresser()
        with gcp_api_mocks.mock_gcp():
            run_crawler(storage, progresser, config, parallel=False)
            for item in storage.mem.values():
                (resource, iam_policy) = convert_item_to_assets(item)
                if resource:
                    resources.append(resource)
                if iam_policy:
                    iam_policies.append(iam_policy)

    with open(ADDITIONAL_RESOURCES_FILE, 'r') as f:
        for line in f:
            if line.startswith('#'):
                continue
            resources.append(line.strip())

    with open(ADDITIONAL_IAM_POLCIIES_FILE, 'r') as f:
        for line in f:
            if line.startswith('#'):
                continue
            iam_policies.append(line.strip())

    write_data(resources, RESOURCE_DUMP_FILE)
    write_data(iam_policies, IAM_POLICY_DUMP_FILE)
Ejemplo n.º 2
0
def serve(endpoint,
          services,
          forseti_db_connect_string,
          config_file_path,
          log_level,
          enable_console_log,
          max_workers=32,
          wait_shutdown_secs=3):
    """Instantiate the services and serves them via gRPC.

    Args:
        endpoint (str): the server channel endpoint
        services (list): services to register on the server
        forseti_db_connect_string (str): Forseti database string
        config_file_path (str): Path to Forseti configuration file.
        log_level (str): Sets the threshold for Forseti's logger.
        enable_console_log (bool): Enable console logging.
        max_workers (int): maximum number of workers for the crawler
        wait_shutdown_secs (int): seconds to wait before shutdown

    Raises:
        Exception: No services to start
    """

    # Configuring log level for the application
    logger.set_logger_level_from_config(log_level)

    if enable_console_log:
        logger.enable_console_log()

    factories = []
    for service in services:
        factories.append(SERVICE_MAP[service])

    if not factories:
        raise Exception('No services to start.')

    # Server config service is always started.
    factories.append(SERVICE_MAP['server'])

    config = ServiceConfig(forseti_config_file_path=config_file_path,
                           forseti_db_connect_string=forseti_db_connect_string,
                           endpoint=endpoint)
    config.update_configuration()

    server = grpc.server(futures.ThreadPoolExecutor(max_workers))
    for factory in factories:
        factory(config).create_and_register_service(server)

    server.add_insecure_port(endpoint)
    server.start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            server.stop(wait_shutdown_secs).wait()
            return