Obtain intrusion prevention rules for all computers of active tenants: Java Example

The following example iterates over the computers of each active tenant to obtain a list of Intrusion Prevention rules that are applied to each computer.

View source

// Key is tenant ID. Value is a list of computer rule IDs
Map<Integer, Map<Integer, ArrayList<Integer>>> tenantMap = new HashMap<>();

// Key is computer ID. Value is a list of rule IDs
Map<Integer, ArrayList<Integer>> computerRules = new HashMap<>();

// Obtain connection properties from local properties file
Properties properties = new Properties();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try (InputStream input = classLoader.getResourceAsStream("com/trendmicro/deepsecurity/docs/Resources/example.properties")) {
    properties.load(input);

    String primarySecretKey = properties.getProperty("secretkey");
    String primaryURL = properties.getProperty("url");

    // Configure the ApiClient
    ApiClient apiClient = Configuration.getDefaultApiClient();
    apiClient.setBasePath(primaryURL);
    ApiKeyAuth defaultAuthentication = (ApiKeyAuth)apiClient.getAuthentication("DefaultAuthentication");
    defaultAuthentication.setApiKey(primarySecretKey);

    // Search for Active tenants
    SearchCriteria searchCriteria = new SearchCriteria();
    searchCriteria.setFieldName("tenantState");
    searchCriteria.setChoiceValue("active");
    searchCriteria.setChoiceTest(SearchCriteria.ChoiceTestEnum.EQUAL);

    // Search filter
    SearchFilter searchFilter = new SearchFilter();
    searchFilter.addSearchCriteriaItem(searchCriteria);

    TenantsApi tenantsApi = new TenantsApi();
    Tenants tenants = tenantsApi.searchTenants(searchFilter, apiVersion);

    // Iterate the tenants
    for (Tenant tenant : tenants.getTenants()) {

        // Create an api key for the tenant
        ApiKey tenantKey = new ApiKey();
        tenantKey.setKeyName("Temporary Key");
        tenantKey.setRoleID(Integer.valueOf(1));
        tenantKey.setLocale(ApiKey.LocaleEnum.EN_US);
        tenantKey.setTimeZone("Asia/Tokyo");

        // Add the key to Deep Security Manager
        tenantKey = tenantsApi.generateTenantApiSecretKey(tenant.getID(), tenantKey, apiVersion);

        // Configure the ApiClient to use the tenant's secret key
        defaultAuthentication.setApiKey(tenantKey.getSecretKey());

        // Create a ComputersApi object for the tenant
        ComputersApi tnComputersApi = new ComputersApi();

        // Include Intrusion Prevention information in the returned Computer objects
        Expand expand = new Expand(Expand.OptionsEnum.INTRUSION_PREVENTION);

        // Iterate over the tenant computers
        Computers tenantComputers = tnComputersApi.listComputers(expand.list(), Boolean.FALSE, apiVersion);
        for (Computer tenantComputer : tenantComputers.getComputers()) {
            IntrusionPreventionComputerExtension intrusionPeventionComputerExtension = tenantComputer.getIntrusionPrevention();
            computerRules.put(tenantComputer.getID(), (ArrayList<Integer>)intrusionPeventionComputerExtension.getRuleIDs());
        }
        tenantMap.put(tenant.getID(), computerRules);
        // Configure the ApiClient to use the primary tenant's Secret Key
        defaultAuthentication.setApiKey(primarySecretKey);
    }
    return tenantMap;
}

Also see the Search Tenants and List Computers operations in the API Reference.