<commit_before>/**
 * Copyright 2016 Pinterest, Inc.
 *
 * 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.pinterest.teletraan.config;

import com.pinterest.deployservice.common.DBConfigReader;
import com.pinterest.deployservice.common.KnoxDBKeyReader;
import com.pinterest.deployservice.db.DatabaseUtil;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import org.apache.commons.dbcp.BasicDataSource;
import org.hibernate.validator.constraints.NotEmpty;
import org.apache.commons.codec.digest.DigestUtils;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;

@JsonTypeName("zkmysql")
public class ZKMysqlDataSourceFactory implements DataSourceFactory {
    @NotEmpty
    @JsonProperty
    private String replicaSet;

    @NotEmpty
    @JsonProperty
    private String role;

    @JsonProperty
    private String pool;

    @JsonProperty
    private boolean useProxy;

    @JsonProperty
    private String proxyHost;

    @JsonProperty
    private int proxyPort;

    @JsonProperty
    private boolean useMTLS;

    @JsonProperty
    private String spiffePrefix;

    @JsonProperty
    private String domainSuffix;

    @JsonProperty
    private String defaultMtlsPasswd;

    @JsonProperty
    private String trustUrl;

    @JsonProperty
    private String trustType;

    @JsonProperty
    private String trustPasswd;

    @JsonProperty
    private String clientUrl;

    @JsonProperty
    private String clientType;

    @JsonProperty
    private String clientPasswd;

    public String getReplicaSet() {
        return replicaSet;
    }

    public void setReplicaSet(String replicaSet) {
        this.replicaSet = replicaSet;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getPool() {
        return pool;
    }

    public void setPool(String pool) {
        this.pool = pool;
    }

    public BasicDataSource build() throws Exception {
        // use proxyHost:proxyPort if useProxy for database connection
        // otherwise utilize replicaSet
        String host;
        int port;
        if (this.useProxy) {
            host = this.proxyHost;
            port = this.proxyPort;
        } else {
            // get from local ZK maintained DB config file
            DBConfigReader reader = new DBConfigReader();
            host = reader.getHost(replicaSet);
            port = reader.getPort(replicaSet);
        }

        if (this.useMTLS) {
            String userName = getUserNameFromSpiffeId(this.spiffePrefix);
            String password = <PASSWORD>;
            Map<String, String> proxyConnectionProps = ImmutableMap.<String, String>builder()
                // ssl properties
                .put("sslMode", "VERIFY_CA" )
                .put("trustCertificateKeyStoreUrl", this.trustUrl )
                .put("trustCertificateKeyStoreType", this.trustType )
                .put("trustCertificateKeyStorePassword", <PASSWORD> )
                .put("clientCertificateKeyStoreUrl", this.clientUrl )
                .put("clientCertificateKeyStoreType", this.clientType )
                .put("clientCertificateKeyStorePassword",  <PASSWORD> )
                .build();
            host = this.replicaSet;
            // we don't need the replica number in the host; 
            // if in the configuration we input the number in the replica, we have to remove it.
            if (host.length() > 3) {
                String replicaSetNumber = replicaSet.substring(replicaSet.length() - 3);<commit_msg>fix a typo (#1057)<commit_after>/**
 * Copyright 2016 Pinterest, Inc.
 *
 * 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.pinterest.teletraan.config;

import com.pinterest.deployservice.common.DBConfigReader;
import com.pinterest.deployservice.common.KnoxDBKeyReader;
import com.pinterest.deployservice.db.DatabaseUtil;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import org.apache.commons.dbcp.BasicDataSource;
import org.hibernate.validator.constraints.NotEmpty;
import org.apache.commons.codec.digest.DigestUtils;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;

@JsonTypeName("zkmysql")
public class ZKMysqlDataSourceFactory implements DataSourceFactory {
    @NotEmpty
    @JsonProperty
    private String replicaSet;

    @NotEmpty
    @JsonProperty
    private String role;

    @JsonProperty
    private String pool;

    @JsonProperty
    private boolean useProxy;

    @JsonProperty
    private String proxyHost;

    @JsonProperty
    private int proxyPort;

    @JsonProperty
    private boolean useMTLS;

    @JsonProperty
    private String spiffePrefix;

    @JsonProperty
    private String domainSuffix;

    @JsonProperty
    private String defaultMtlsPasswd;

    @JsonProperty
    private String trustUrl;

    @JsonProperty
    private String trustType;

    @JsonProperty
    private String trustPasswd;

    @JsonProperty
    private String clientUrl;

    @JsonProperty
    private String clientType;

    @JsonProperty
    private String clientPasswd;

    public String getReplicaSet() {
        return replicaSet;
    }

    public void setReplicaSet(String replicaSet) {
        this.replicaSet = replicaSet;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getPool() {
        return pool;
    }

    public void setPool(String pool) {
        this.pool = pool;
    }

    public BasicDataSource build() throws Exception {
        // use proxyHost:proxyPort if useProxy for database connection
        // otherwise utilize replicaSet
        String host;
        int port;
        if (this.useProxy) {
            host = this.proxyHost;
            port = this.proxyPort;
        } else {
            // get from local ZK maintained DB config file
            DBConfigReader reader = new DBConfigReader();
            host = reader.getHost(replicaSet);
            port = reader.getPort(replicaSet);
        }

        if (this.useMTLS) {
            String userName = getUserNameFromSpiffeId(this.spiffePrefix);
            String password = <PASSWORD>;
            Map<String, String> proxyConnectionProps = ImmutableMap.<String, String>builder()
                // ssl properties
                .put("sslMode", "VERIFY_CA" )
                .put("trustCertificateKeyStoreUrl", this.trustUrl )
                .put("trustCertificateKeyStoreType", this.trustType )
                .put("trustCertificateKeyStorePassword", <PASSWORD> )
                .put("clientCertificateKeyStoreUrl", this.clientUrl )
                .put("clientCertificateKeyStoreType", this.clientType )
                .put("clientCertificateKeyStorePassword", <PASSWORD> )
                .build();
            host = this.replicaSet;
            // we don't need the replica number in the host; 
            // if in the configuration we input the number in the replica, we have to remove it.
            if (host.length() > 3) {
                String replicaSetNumber = replicaSet.substring(replicaSet.length() - 3);
{'fraction_non_alphanumeric': 0.07880889739296819, 'fraction_numerical': 0.0028701267639320736, 'mean_word_length': 2.653560506771516, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5017874', 'n_tokens_mistral': 2288, 'n_tokens_neox': 2171, 'n_words': 638}