<commit_before>/*
 * Copyright 2015-2017 Hewlett Packard Enterprise Development LP.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.cafaudit.auditmonkey;

import java.util.Arrays;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MonkeyConfig
{
    private static final Logger LOG = LoggerFactory.getLogger(MonkeyConfig.class);
    
    // Audit
    private String auditMode;
    
    // Elasticsearch
    private String esClustername;
    private String esHostname;
    private int esPort;
    private String esHostnameAndPort;
    
    // Audit WebService
    private String wsHostname;
    private int wsPort;
    private String wsHostnameAndPort;
    
    // Audit Events
    private String tenantId;
    private String correlationId;
    private String userId;

    // Monkey
    private String monkeyMode;
    private int numOfEvents;
    private int numOfThreads;
    
    
    public MonkeyConfig() {
        
        LOG.info("Setting up Audit Monkey Configuration from supplied Environment Variables");

        readAuditProp();
        
        readElasticsearchProps();
        
        readWebServiceProps();        
        
        readAuditEventProps();
        
        readMonkeyProps();        
        
        
        LOG.info(this.toString());
    }

    private void readMonkeyProps()
    {
        /*
         * Monkey
         */
        monkeyMode = System.getProperty(MonkeyConstants.CAF_AUDIT_MONKEY_MODE, System.getenv(MonkeyConstants.CAF_AUDIT_MONKEY_MODE));
        if(null == monkeyMode || monkeyMode.isEmpty()) {
            monkeyMode = MonkeyConstants.CAF_AUDIT_STANDARD_MONKEY;
            LOG.info("No [" + MonkeyConstants.CAF_AUDIT_MONKEY_MODE + "] supplied defaulting to [" + monkeyMode + "]");
        } else if (!isMonkeyMode(monkeyMode))  {
            String errorMsg = "The " + MonkeyConstants.CAF_AUDIT_MODE + " supplied [" + monkeyMode + "] does not match the available modes [" + Arrays.toString(MonkeyConstants.ARRAY_OF_MONKEYS) + "]"; 
            LOG.error(errorMsg);
            throw new RuntimeException(errorMsg);
        }

        String numOfEventsStr = System.getProperty(MonkeyConstants.CAF_AUDIT_MONKEY_NUM_OF_EVENTS, System.getenv(MonkeyConstants.CAF_AUDIT_MONKEY_NUM_OF_EVENTS));
        if(null == numOfEventsStr || numOfEventsStr.isEmpty()) {
            numOfEvents = 1; 
            LOG.warn("No [" + MonkeyConstants.CAF_AUDIT_MONKEY_NUM_OF_EVENTS + "] supplied defaulting to [" + numOfEvents + "]");
        }
        else {
            numOfEvents = Integer.parseInt(numOfEventsStr);
            if(numOfEvents <= 0) {
                numOfEvents = 1;
                LOG.warn("Invalid [" + MonkeyConstants.CAF_AUDIT_MONKEY_NUM_OF_EVENTS + "] supplied defaulting to [" + numOfEvents + "]");
            }
        }
        
        String numOfThreadsStr = System.getProperty(MonkeyConstants.CAF_AUDIT_MONKEY_NUM_OF_THREADS, System.getenv(MonkeyConstants.CAF_AUDIT_MONKEY_NUM_OF_THREADS));
        if(null == numOfThreadsStr || numOfThreadsStr.isEmpty()) {
            numOfThreads = 1; 
            LOG.info("No [" + MonkeyConstants.CAF_AUDIT_MONKEY_NUM_OF_THREADS + "] supplied defaulting to [" + numOfThreads + "]");
        }
        else {
            numOfThreads = Integer.parseInt(numOfThreadsStr);
            if(numOfThreads <= 0) {
                numOfThreads = 1;
                LOG.warn("Invalid [" + MonkeyConstants.CAF_AUDIT_MONKEY_NUM_OF_THREADS + "] supplied defaulting to [" + numOfThreads + "]");
            }
        }
    }

    private void readAuditEventProps()
    {
        /*
         * Audit Events
         */
        tenantId = System.getProperty(MonkeyConstants.CAF_AUDIT_TENANT_ID, System.getenv(MonkeyConstants.CAF_AUDIT_TENANT_ID));
        if(null == tenantId || tenantId.isEmpty()) {
            tenantId = "acmecorp";
            LOG.info("No [" + MonkeyConstants.CAF_AUDIT_TENANT_ID + "] supplied defaulting to [" + tenantId + "]");
        }
        
        correlationId = System.getProperty(MonkeyConstants.CAF_AUDIT_CORRELATION_ID, System.getenv(MonkeyConstants.CAF_AUDIT_CORRELATION_ID));
        if(null == correlationId || correlationId.isEmpty()) {
            correlationId = UUID.randomUUID().toString();
            LOG.info("No [" + MonkeyConstants.CAF_AUDIT_CORRELATION_ID + "] supplied defaulting to [" + correlationId + "]");
        }
        
        userId = System.getProperty(MonkeyConstants.CAF_AUDIT_USER_ID, System.getenv(MonkeyConstants.CAF_AUDIT_USER_ID));
        if(null == userId || userId.isEmpty()) {
            userId = "<EMAIL>";
            LOG.info("No [" + MonkeyConstants.CAF_AUDIT_USER_ID + "] supplied defaulting to [" + userId + "]");
        }
    }

    private void readWebServiceProps()
    {
        /*
         * WebService
         */
        wsHostname = System.getProperty(MonkeyConstants.WS_HOSTNAME, System.getenv(MonkeyConstants.WS_HOSTNAME));
        if(null == wsHostname || wsHostname.isEmpty()) {
            wsHostname = "192.168.56.10";
            LOG.info("No [" + MonkeyConstants.WS_HOSTNAME + "] supplied defaulting to [" + wsHostname + "]");
        }
        
        String wsPortStr = System.getProperty(MonkeyConstants.WS_PORT, System.getenv(MonkeyConstants.WS_PORT));
        if(null == wsPortStr || wsPortStr.isEmpty()) {
            wsPort = 25080;
            LOG.info("No [" + MonkeyConstants.WS_PORT + "] supplied defaulting to [" + wsPort + "]");            
        } 
        else {
            wsPort = Integer.parseInt(wsPortStr);
            if(wsPort == 0 || wsPort < 0 || wsPort > 9999) {
                wsPort = 25080;
                LOG.warn("Invalid [" + MonkeyConstants.WS_PORT + "] supplied defaulting to [" + wsPort + "]");
            }
        }
        
        wsHostnameAndPort = String.format("%s:%s", wsHostname, wsPort);
        LOG.debug("Audit WebService Hostname and Port set to [" + wsHostnameAndPort + "]");
    }
<commit_msg>Bug in the validation of the Web Service Port<commit_after>/*
 * Copyright 2015-2017 Hewlett Packard Enterprise Development LP.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.cafaudit.auditmonkey;

import java.util.Arrays;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MonkeyConfig
{
    private static final Logger LOG = LoggerFactory.getLogger(MonkeyConfig.class);
    
    // Audit
    private String auditMode;
    
    // Elasticsearch
    private String esClustername;
    private String esHostname;
    private int esPort;
    private String esHostnameAndPort;
    
    // Audit WebService
    private String wsHostname;
    private int wsPort;
    private String wsHostnameAndPort;
    
    // Audit Events
    private String tenantId;
    private String correlationId;
    private String userId;

    // Monkey
    private String monkeyMode;
    private int numOfEvents;
    private int numOfThreads;
    
    
    public MonkeyConfig() {
        
        LOG.info("Setting up Audit Monkey Configuration from supplied Environment Variables");

        readAuditProp();
        
        readElasticsearchProps();
        
        readWebServiceProps();        
        
        readAuditEventProps();
        
        readMonkeyProps();        
        
        
        LOG.info(this.toString());
    }

    private void readMonkeyProps()
    {
        /*
         * Monkey
         */
        monkeyMode = System.getProperty(MonkeyConstants.CAF_AUDIT_MONKEY_MODE, System.getenv(MonkeyConstants.CAF_AUDIT_MONKEY_MODE));
        if(null == monkeyMode || monkeyMode.isEmpty()) {
            monkeyMode = MonkeyConstants.CAF_AUDIT_STANDARD_MONKEY;
            LOG.info("No [" + MonkeyConstants.CAF_AUDIT_MONKEY_MODE + "] supplied defaulting to [" + monkeyMode + "]");
        } else if (!isMonkeyMode(monkeyMode))  {
            String errorMsg = "The " + MonkeyConstants.CAF_AUDIT_MODE + " supplied [" + monkeyMode + "] does not match the available modes [" + Arrays.toString(MonkeyConstants.ARRAY_OF_MONKEYS) + "]"; 
            LOG.error(errorMsg);
            throw new RuntimeException(errorMsg);
        }

        String numOfEventsStr = System.getProperty(MonkeyConstants.CAF_AUDIT_MONKEY_NUM_OF_EVENTS, System.getenv(MonkeyConstants.CAF_AUDIT_MONKEY_NUM_OF_EVENTS));
        if(null == numOfEventsStr || numOfEventsStr.isEmpty()) {
            numOfEvents = 1; 
            LOG.warn("No [" + MonkeyConstants.CAF_AUDIT_MONKEY_NUM_OF_EVENTS + "] supplied defaulting to [" + numOfEvents + "]");
        }
        else {
            numOfEvents = Integer.parseInt(numOfEventsStr);
            if(numOfEvents <= 0) {
                numOfEvents = 1;
                LOG.warn("Invalid [" + MonkeyConstants.CAF_AUDIT_MONKEY_NUM_OF_EVENTS + "] supplied defaulting to [" + numOfEvents + "]");
            }
        }
        
        String numOfThreadsStr = System.getProperty(MonkeyConstants.CAF_AUDIT_MONKEY_NUM_OF_THREADS, System.getenv(MonkeyConstants.CAF_AUDIT_MONKEY_NUM_OF_THREADS));
        if(null == numOfThreadsStr || numOfThreadsStr.isEmpty()) {
            numOfThreads = 1; 
            LOG.info("No [" + MonkeyConstants.CAF_AUDIT_MONKEY_NUM_OF_THREADS + "] supplied defaulting to [" + numOfThreads + "]");
        }
        else {
            numOfThreads = Integer.parseInt(numOfThreadsStr);
            if(numOfThreads <= 0) {
                numOfThreads = 1;
                LOG.warn("Invalid [" + MonkeyConstants.CAF_AUDIT_MONKEY_NUM_OF_THREADS + "] supplied defaulting to [" + numOfThreads + "]");
            }
        }
    }

    private void readAuditEventProps()
    {
        /*
         * Audit Events
         */
        tenantId = System.getProperty(MonkeyConstants.CAF_AUDIT_TENANT_ID, System.getenv(MonkeyConstants.CAF_AUDIT_TENANT_ID));
        if(null == tenantId || tenantId.isEmpty()) {
            tenantId = "acmecorp";
            LOG.info("No [" + MonkeyConstants.CAF_AUDIT_TENANT_ID + "] supplied defaulting to [" + tenantId + "]");
        }
        
        correlationId = System.getProperty(MonkeyConstants.CAF_AUDIT_CORRELATION_ID, System.getenv(MonkeyConstants.CAF_AUDIT_CORRELATION_ID));
        if(null == correlationId || correlationId.isEmpty()) {
            correlationId = UUID.randomUUID().toString();
            LOG.info("No [" + MonkeyConstants.CAF_AUDIT_CORRELATION_ID + "] supplied defaulting to [" + correlationId + "]");
        }
        
        userId = System.getProperty(MonkeyConstants.CAF_AUDIT_USER_ID, System.getenv(MonkeyConstants.CAF_AUDIT_USER_ID));
        if(null == userId || userId.isEmpty()) {
            userId = "<EMAIL>";
            LOG.info("No [" + MonkeyConstants.CAF_AUDIT_USER_ID + "] supplied defaulting to [" + userId + "]");
        }
    }

    private void readWebServiceProps()
    {
        /*
         * WebService
         */
        wsHostname = System.getProperty(MonkeyConstants.WS_HOSTNAME, System.getenv(MonkeyConstants.WS_HOSTNAME));
        if(null == wsHostname || wsHostname.isEmpty()) {
            wsHostname = "192.168.56.10";
            LOG.info("No [" + MonkeyConstants.WS_HOSTNAME + "] supplied defaulting to [" + wsHostname + "]");
        }
        
        String wsPortStr = System.getProperty(MonkeyConstants.WS_PORT, System.getenv(MonkeyConstants.WS_PORT));
        if(null == wsPortStr || wsPortStr.isEmpty()) {
            wsPort = 25080;
            LOG.info("No [" + MonkeyConstants.WS_PORT + "] supplied defaulting to [" + wsPort + "]");            
        } 
        else {
            wsPort = Integer.parseInt(wsPortStr);
            if(wsPort == 0 || wsPort < 0 || wsPort > 99999) {
                wsPort = 25080;
                LOG.warn("Invalid [" + MonkeyConstants.WS_PORT + "] supplied defaulting to [" + wsPort + "]");
            }
        }
        
        wsHostnameAndPort = String.format("%s:%s", wsHostname, wsPort);
        LOG.debug("Audit WebService Hostname and Port set to [" + wsHostnameAndPort + "]");
    }

{'fraction_non_alphanumeric': 0.10484426417693427, 'fraction_numerical': 0.007117165378434224, 'mean_word_length': 2.4838709677419355, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4233455', 'n_tokens_mistral': 4013, 'n_tokens_neox': 3848, 'n_words': 756}