View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.kubernetes;
18  
19  import java.time.Duration;
20  
21  import org.apache.logging.log4j.util.PropertiesUtil;
22  
23  import io.fabric8.kubernetes.client.Config;
24  
25  /**
26   * Obtains properties used to configure the Kubernetes client.
27   */
28  public class KubernetesClientProperties {
29  
30      private static final String[] PREFIXES = {"log4j2.kubernetes.client.", "spring.cloud.kubernetes.client."};
31      private static final String API_VERSION = "apiVersion";
32      private static final String CA_CERT_FILE = "caCertFile";
33      private static final String CA_CERT_DATA = "caCertData";
34      private static final String CLIENT_CERT_FILE = "clientCertFile";
35      private static final String CLIENT_CERT_DATA = "clientCertData";
36      private static final String CLIENT_KEY_FILE = "clientKeyFile";
37      private static final String CLIENT_KEY_DATA = "cientKeyData";
38      private static final String CLIENT_KEY_ALGO = "clientKeyAlgo";
39      private static final String CLIENT_KEY_PASSPHRASE = "clientKeyPassphrase";
40      private static final String CONNECTION_TIMEOUT = "connectionTimeout";
41      private static final String HTTP_PROXY = "httpProxy";
42      private static final String HTTPS_PROXY = "httpsProxy";
43      private static final String LOGGING_INTERVAL = "loggingInterval";
44      private static final String MASTER_URL = "masterUrl";
45      private static final String NAMESPACE = "namespace";
46      private static final String NO_PROXY = "noProxy";
47      private static final String PASSWORD = "password";
48      private static final String PROXY_USERNAME = "proxyUsername";
49      private static final String PROXY_PASSWORD = "proxyPassword";
50      private static final String REQUEST_TIMEOUT = "requestTimeout";
51      private static final String ROLLING_TIMEOUT = "rollingTimeout";
52      private static final String TRUST_CERTS = "trustCerts";
53      private static final String USERNAME = "username";
54      private static final String WATCH_RECONNECT_INTERVAL = "watchReconnectInterval";
55      private static final String WATCH_RECONNECT_LIMIT = "watchReconnectLimit";
56  
57      private PropertiesUtil props = PropertiesUtil.getProperties();
58      private final Config base;
59  
60      public KubernetesClientProperties(Config base) {
61          this.base = base;
62      }
63  
64  
65      public String getApiVersion() {
66          return props.getStringProperty(PREFIXES, API_VERSION, base::getApiVersion);
67      }
68      public String getCaCertFile() {
69          return props.getStringProperty(PREFIXES, CA_CERT_FILE, base::getCaCertFile);
70      }
71  
72      public String getCaCertData() {
73          return props.getStringProperty(PREFIXES, CA_CERT_DATA, base::getCaCertData);
74      }
75  
76      public String getClientCertFile() {
77          return props.getStringProperty(PREFIXES, CLIENT_CERT_FILE, base::getClientCertFile);
78      }
79  
80      public String getClientCertData() {
81          return props.getStringProperty(PREFIXES, CLIENT_CERT_DATA, base::getClientCertData);
82      }
83  
84      public String getClientKeyFile() {
85          return props.getStringProperty(PREFIXES, CLIENT_KEY_FILE, base::getClientKeyFile);
86      }
87  
88      public String getClientKeyData() {
89          return props.getStringProperty(PREFIXES, CLIENT_KEY_DATA, base::getClientKeyData);
90      }
91  
92      public String getClientKeyAlgo() {
93          return props.getStringProperty(PREFIXES, CLIENT_KEY_ALGO, base::getClientKeyAlgo);
94      }
95  
96      public String getClientKeyPassphrase() {
97          return props.getStringProperty(PREFIXES, CLIENT_KEY_PASSPHRASE, base::getClientKeyPassphrase);
98      }
99  
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 }