|
| 1 | +/** |
| 2 | + * Copyright (C) 2015 Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.fabric8.kubernetes.log4j.lookup; |
| 17 | + |
| 18 | +import io.fabric8.kubernetes.client.Config; |
| 19 | +import org.apache.logging.log4j.util.PropertiesUtil; |
| 20 | + |
| 21 | +import java.time.Duration; |
| 22 | + |
| 23 | +/** |
| 24 | + * Obtains properties used to configure the Kubernetes client. |
| 25 | + */ |
| 26 | +class ClientProperties { |
| 27 | + |
| 28 | + private static final String[] PREFIXES = { "log4j2.kubernetes.client.", "spring.cloud.kubernetes.client." }; |
| 29 | + private static final String API_VERSION = "apiVersion"; |
| 30 | + private static final String CA_CERT_FILE = "caCertFile"; |
| 31 | + private static final String CA_CERT_DATA = "caCertData"; |
| 32 | + private static final String CLIENT_CERT_FILE = "clientCertFile"; |
| 33 | + private static final String CLIENT_CERT_DATA = "clientCertData"; |
| 34 | + private static final String CLIENT_KEY_FILE = "clientKeyFile"; |
| 35 | + private static final String CLIENT_KEY_DATA = "clientKeyData"; |
| 36 | + private static final String CLIENT_KEY_ALGO = "clientKeyAlgo"; |
| 37 | + private static final String CLIENT_KEY_PASSPHRASE = "clientKeyPassphrase"; |
| 38 | + private static final String CONNECTION_TIMEOUT = "connectionTimeout"; |
| 39 | + private static final String HTTP_PROXY = "httpProxy"; |
| 40 | + private static final String HTTPS_PROXY = "httpsProxy"; |
| 41 | + private static final String LOGGING_INTERVAL = "loggingInterval"; |
| 42 | + private static final String MASTER_URL = "masterUrl"; |
| 43 | + private static final String NAMESPACE = "namespace"; |
| 44 | + private static final String NO_PROXY = "noProxy"; |
| 45 | + private static final String PASSWORD = "password"; |
| 46 | + private static final String PROXY_USERNAME = "proxyUsername"; |
| 47 | + private static final String PROXY_PASSWORD = "proxyPassword"; |
| 48 | + private static final String REQUEST_TIMEOUT = "requestTimeout"; |
| 49 | + private static final String TRUST_CERTS = "trustCerts"; |
| 50 | + private static final String USERNAME = "username"; |
| 51 | + private static final String WATCH_RECONNECT_INTERVAL = "watchReconnectInterval"; |
| 52 | + private static final String WATCH_RECONNECT_LIMIT = "watchReconnectLimit"; |
| 53 | + |
| 54 | + private final PropertiesUtil props = PropertiesUtil.getProperties(); |
| 55 | + private final Config base; |
| 56 | + |
| 57 | + public ClientProperties(final Config base) { |
| 58 | + this.base = base; |
| 59 | + } |
| 60 | + |
| 61 | + public String getApiVersion() { |
| 62 | + return props.getStringProperty(PREFIXES, API_VERSION, base::getApiVersion); |
| 63 | + } |
| 64 | + |
| 65 | + public String getCaCertFile() { |
| 66 | + return props.getStringProperty(PREFIXES, CA_CERT_FILE, base::getCaCertFile); |
| 67 | + } |
| 68 | + |
| 69 | + public String getCaCertData() { |
| 70 | + return props.getStringProperty(PREFIXES, CA_CERT_DATA, base::getCaCertData); |
| 71 | + } |
| 72 | + |
| 73 | + public String getClientCertFile() { |
| 74 | + return props.getStringProperty(PREFIXES, CLIENT_CERT_FILE, base::getClientCertFile); |
| 75 | + } |
| 76 | + |
| 77 | + public String getClientCertData() { |
| 78 | + return props.getStringProperty(PREFIXES, CLIENT_CERT_DATA, base::getClientCertData); |
| 79 | + } |
| 80 | + |
| 81 | + public String getClientKeyFile() { |
| 82 | + return props.getStringProperty(PREFIXES, CLIENT_KEY_FILE, base::getClientKeyFile); |
| 83 | + } |
| 84 | + |
| 85 | + public String getClientKeyData() { |
| 86 | + return props.getStringProperty(PREFIXES, CLIENT_KEY_DATA, base::getClientKeyData); |
| 87 | + } |
| 88 | + |
| 89 | + public String getClientKeyAlgo() { |
| 90 | + return props.getStringProperty(PREFIXES, CLIENT_KEY_ALGO, base::getClientKeyAlgo); |
| 91 | + } |
| 92 | + |
| 93 | + public String getClientKeyPassphrase() { |
| 94 | + return props.getStringProperty(PREFIXES, CLIENT_KEY_PASSPHRASE, base::getClientKeyPassphrase); |
| 95 | + } |
| 96 | + |
| 97 | + public int getConnectionTimeout() { |
| 98 | + final Duration timeout = props.getDurationProperty(PREFIXES, CONNECTION_TIMEOUT, null); |
| 99 | + if (timeout != null) { |
| 100 | + return (int) timeout.toMillis(); |
| 101 | + } |
| 102 | + return base.getConnectionTimeout(); |
| 103 | + } |
| 104 | + |
| 105 | + public String getHttpProxy() { |
| 106 | + return props.getStringProperty(PREFIXES, HTTP_PROXY, base::getHttpProxy); |
| 107 | + } |
| 108 | + |
| 109 | + public String getHttpsProxy() { |
| 110 | + return props.getStringProperty(PREFIXES, HTTPS_PROXY, base::getHttpsProxy); |
| 111 | + } |
| 112 | + |
| 113 | + public int getLoggingInterval() { |
| 114 | + final Duration interval = props.getDurationProperty(PREFIXES, LOGGING_INTERVAL, null); |
| 115 | + if (interval != null) { |
| 116 | + return (int) interval.toMillis(); |
| 117 | + } |
| 118 | + return base.getLoggingInterval(); |
| 119 | + } |
| 120 | + |
| 121 | + public String getMasterUrl() { |
| 122 | + return props.getStringProperty(PREFIXES, MASTER_URL, base::getMasterUrl); |
| 123 | + } |
| 124 | + |
| 125 | + public String getNamespace() { |
| 126 | + return props.getStringProperty(PREFIXES, NAMESPACE, base::getNamespace); |
| 127 | + } |
| 128 | + |
| 129 | + public String[] getNoProxy() { |
| 130 | + final String result = props.getStringProperty(PREFIXES, NO_PROXY, null); |
| 131 | + if (result != null) { |
| 132 | + return result.replace("\\s", "").split(","); |
| 133 | + } |
| 134 | + return base.getNoProxy(); |
| 135 | + } |
| 136 | + |
| 137 | + public String getPassword() { |
| 138 | + return props.getStringProperty(PREFIXES, PASSWORD, base::getPassword); |
| 139 | + } |
| 140 | + |
| 141 | + public String getProxyUsername() { |
| 142 | + return props.getStringProperty(PREFIXES, PROXY_USERNAME, base::getProxyUsername); |
| 143 | + } |
| 144 | + |
| 145 | + public String getProxyPassword() { |
| 146 | + return props.getStringProperty(PREFIXES, PROXY_PASSWORD, base::getProxyPassword); |
| 147 | + } |
| 148 | + |
| 149 | + public int getRequestTimeout() { |
| 150 | + final Duration interval = props.getDurationProperty(PREFIXES, REQUEST_TIMEOUT, null); |
| 151 | + if (interval != null) { |
| 152 | + return (int) interval.toMillis(); |
| 153 | + } |
| 154 | + return base.getRequestTimeout(); |
| 155 | + } |
| 156 | + |
| 157 | + public Boolean isTrustCerts() { |
| 158 | + return props.getBooleanProperty(PREFIXES, TRUST_CERTS, base::isTrustCerts); |
| 159 | + } |
| 160 | + |
| 161 | + public String getUsername() { |
| 162 | + return props.getStringProperty(PREFIXES, USERNAME, base::getUsername); |
| 163 | + } |
| 164 | + |
| 165 | + public int getWatchReconnectInterval() { |
| 166 | + final Duration interval = props.getDurationProperty(PREFIXES, WATCH_RECONNECT_INTERVAL, null); |
| 167 | + if (interval != null) { |
| 168 | + return (int) interval.toMillis(); |
| 169 | + } |
| 170 | + return base.getWatchReconnectInterval(); |
| 171 | + } |
| 172 | + |
| 173 | + public int getWatchReconnectLimit() { |
| 174 | + final Duration interval = props.getDurationProperty(PREFIXES, WATCH_RECONNECT_LIMIT, null); |
| 175 | + if (interval != null) { |
| 176 | + return (int) interval.toMillis(); |
| 177 | + } |
| 178 | + return base.getWatchReconnectLimit(); |
| 179 | + } |
| 180 | +} |
0 commit comments