001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.kubernetes;
018
019import java.time.Duration;
020
021import org.apache.logging.log4j.util.PropertiesUtil;
022
023import io.fabric8.kubernetes.client.Config;
024
025/**
026 * Obtains properties used to configure the Kubernetes client.
027 */
028public class KubernetesClientProperties {
029
030    private static final String[] PREFIXES = {"log4j2.kubernetes.client.", "spring.cloud.kubernetes.client."};
031    private static final String API_VERSION = "apiVersion";
032    private static final String CA_CERT_FILE = "caCertFile";
033    private static final String CA_CERT_DATA = "caCertData";
034    private static final String CLIENT_CERT_FILE = "clientCertFile";
035    private static final String CLIENT_CERT_DATA = "clientCertData";
036    private static final String CLIENT_KEY_FILE = "clientKeyFile";
037    private static final String CLIENT_KEY_DATA = "cientKeyData";
038    private static final String CLIENT_KEY_ALGO = "clientKeyAlgo";
039    private static final String CLIENT_KEY_PASSPHRASE = "clientKeyPassphrase";
040    private static final String CONNECTION_TIMEOUT = "connectionTimeout";
041    private static final String HTTP_PROXY = "httpProxy";
042    private static final String HTTPS_PROXY = "httpsProxy";
043    private static final String LOGGING_INTERVAL = "loggingInterval";
044    private static final String MASTER_URL = "masterUrl";
045    private static final String NAMESPACE = "namespace";
046    private static final String NO_PROXY = "noProxy";
047    private static final String PASSWORD = "password";
048    private static final String PROXY_USERNAME = "proxyUsername";
049    private static final String PROXY_PASSWORD = "proxyPassword";
050    private static final String REQUEST_TIMEOUT = "requestTimeout";
051    private static final String ROLLING_TIMEOUT = "rollingTimeout";
052    private static final String TRUST_CERTS = "trustCerts";
053    private static final String USERNAME = "username";
054    private static final String WATCH_RECONNECT_INTERVAL = "watchReconnectInterval";
055    private static final String WATCH_RECONNECT_LIMIT = "watchReconnectLimit";
056
057    private PropertiesUtil props = PropertiesUtil.getProperties();
058    private final Config base;
059
060    public KubernetesClientProperties(Config base) {
061        this.base = base;
062    }
063
064
065    public String getApiVersion() {
066        return props.getStringProperty(PREFIXES, API_VERSION, base::getApiVersion);
067    }
068    public String getCaCertFile() {
069        return props.getStringProperty(PREFIXES, CA_CERT_FILE, base::getCaCertFile);
070    }
071
072    public String getCaCertData() {
073        return props.getStringProperty(PREFIXES, CA_CERT_DATA, base::getCaCertData);
074    }
075
076    public String getClientCertFile() {
077        return props.getStringProperty(PREFIXES, CLIENT_CERT_FILE, base::getClientCertFile);
078    }
079
080    public String getClientCertData() {
081        return props.getStringProperty(PREFIXES, CLIENT_CERT_DATA, base::getClientCertData);
082    }
083
084    public String getClientKeyFile() {
085        return props.getStringProperty(PREFIXES, CLIENT_KEY_FILE, base::getClientKeyFile);
086    }
087
088    public String getClientKeyData() {
089        return props.getStringProperty(PREFIXES, CLIENT_KEY_DATA, base::getClientKeyData);
090    }
091
092    public String getClientKeyAlgo() {
093        return props.getStringProperty(PREFIXES, CLIENT_KEY_ALGO, base::getClientKeyAlgo);
094    }
095
096    public String getClientKeyPassphrase() {
097        return props.getStringProperty(PREFIXES, CLIENT_KEY_PASSPHRASE, base::getClientKeyPassphrase);
098    }
099
100    public int getConnectionTimeout() {
101        Duration timeout = props.getDurationProperty(PREFIXES, CONNECTION_TIMEOUT, null);
102        if (timeout != null) {
103            return (int) timeout.toMillis();
104        }
105        return base.getConnectionTimeout();
106    }
107
108    public String getHttpProxy() {
109        return props.getStringProperty(PREFIXES, HTTP_PROXY, base::getHttpProxy);
110    }
111
112    public String getHttpsProxy() {
113        return props.getStringProperty(PREFIXES, HTTPS_PROXY, base::getHttpsProxy);
114    }
115
116    public int getLoggingInterval() {
117        Duration interval = props.getDurationProperty(PREFIXES, LOGGING_INTERVAL, null);
118        if (interval != null) {
119            return (int) interval.toMillis();
120        }
121        return base.getLoggingInterval();
122    }
123
124    public String getMasterUrl() {
125        return props.getStringProperty(PREFIXES, MASTER_URL, base::getMasterUrl);
126    }
127
128    public String getNamespace() {
129        return props.getStringProperty(PREFIXES, NAMESPACE, base::getNamespace);
130    }
131
132    public String[] getNoProxy() {
133        String result = props.getStringProperty(PREFIXES, NO_PROXY, null);
134        if (result != null) {
135            return result.replace("\\s", "").split(",");
136        }
137        return base.getNoProxy();
138    }
139
140    public String getPassword() {
141        return props.getStringProperty(PREFIXES, PASSWORD, base::getPassword);
142    }
143
144    public String getProxyUsername() {
145        return props.getStringProperty(PREFIXES, PROXY_USERNAME, base::getProxyUsername);
146    }
147
148    public String getProxyPassword() {
149        return props.getStringProperty(PREFIXES, PROXY_PASSWORD, base::getProxyPassword);
150    }
151
152    public int getRequestTimeout() {
153        Duration interval = props.getDurationProperty(PREFIXES, REQUEST_TIMEOUT, null);
154        if (interval != null) {
155            return (int) interval.toMillis();
156        }
157        return base.getRequestTimeout();
158    }
159
160    public long getRollingTimeout() {
161        Duration interval = props.getDurationProperty(PREFIXES, ROLLING_TIMEOUT, null);
162        if (interval != null) {
163            return interval.toMillis();
164        }
165        return base.getRollingTimeout();
166    }
167
168    public Boolean isTrustCerts() {
169        return props.getBooleanProperty(PREFIXES, TRUST_CERTS, base::isTrustCerts);
170    }
171
172    public String getUsername() {
173        return props.getStringProperty(PREFIXES, USERNAME, base::getUsername);
174    }
175
176    public int getWatchReconnectInterval() {
177        Duration interval = props.getDurationProperty(PREFIXES, WATCH_RECONNECT_INTERVAL, null);
178        if (interval != null) {
179            return (int) interval.toMillis();
180        }
181        return base.getWatchReconnectInterval();
182    }
183
184    public int getWatchReconnectLimit() {
185        Duration interval = props.getDurationProperty(PREFIXES, WATCH_RECONNECT_LIMIT, null);
186        if (interval != null) {
187            return (int) interval.toMillis();
188        }
189        return base.getWatchReconnectLimit();
190    }
191}