View Javadoc

1   /*
2    * Copyright (C) 2017-2019 Centre National d'Etudes Spatiales (CNES).
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 3.0 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17   * MA 02110-1301  USA
18   */
19  package fr.cnes.doi.settings;
20  
21  import fr.cnes.doi.utils.Utils;
22  import fr.cnes.doi.utils.spec.Requirement;
23  import fr.cnes.httpclient.HttpClientFactory.Type;
24  import fr.cnes.httpclient.configuration.ProxyConfiguration;
25  import fr.cnes.httpclient.configuration.ProxySPNegoJAASConfiguration;
26  import org.apache.logging.log4j.LogManager;
27  import org.apache.logging.log4j.Logger;
28  
29  /**
30   * Sets the proxy parameter based on DoiSettings.
31   *
32   * @author Jean-Christophe Malapert
33   */
34  @Requirement(reqId = Requirement.DOI_CONFIG_010, reqName = Requirement.DOI_CONFIG_010_NAME)
35  public final class ProxySettings {
36  
37      /**
38       * Application logger.
39       */
40      private static final Logger LOG = LogManager.getLogger(ProxySettings.class.getName());
41  
42      /**
43       * Proxy configuration - host
44       */
45      private String proxyHost;
46  
47      /**
48       * Proxy configuration - port
49       */
50      private String proxyPort;
51  
52      /**
53       * Proxy configuration - user
54       */
55      private String proxyUser;
56  
57      /**
58       * Proxy configuration - password
59       */
60      private String proxyPassword;
61  
62      /**
63       * Proxy configuration - password
64       */
65      private String nonProxyHosts;
66  
67      /**
68       * Proxy configuration enable / disable
69       */
70      private boolean proxySet = false;
71  
72      /**
73       * Proxy type.
74       */
75      private String proxyType;
76  
77      /**
78       * Service provider name for SPNEGO protocol.
79       */
80      private String proxySpn;
81  
82      /**
83       * Jaas file where configuration for SPNego is described.
84       */
85      private String proxyJaasFile;
86  
87      /**
88       * Jaas context, which is described in Jaas file.
89       */
90      private String proxyJaasCtx;
91  
92      /**
93       * Private constructor
94       */
95      private ProxySettings() {
96          LOG.traceEntry();
97          init();
98          LOG.traceExit();
99      }
100 
101     /**
102      * Access to unique INSTANCE of Settings
103      *
104      * @return the configuration instance.
105      */
106     public static ProxySettings getInstance() {
107         LOG.traceEntry();
108         return LOG.traceExit(ProxySettingsHolder.INSTANCE);
109     }
110 
111     /**
112      * Init the proxy setting
113      */
114     public void init() {
115         LOG.traceEntry();
116         final DoiSettings settings = DoiSettings.getInstance();
117         LOG.info("----- proxy parameters ----");
118 
119         this.proxyHost = settings.getString(Consts.SERVER_PROXY_HOST, "");
120         LOG.info("proxyHost : {}", this.proxyHost);
121 
122         this.proxyPort = settings.getString(Consts.SERVER_PROXY_PORT, "");
123         LOG.info("proxyPort : {}", this.proxyPort);
124 
125         this.proxyUser = settings.getString(Consts.SERVER_PROXY_LOGIN, "");
126         LOG.info("proxyUser : {}", this.proxyUser);
127 
128         this.proxyPassword = settings.getSecret(Consts.SERVER_PROXY_PWD);
129         LOG.info("proxyPassword : {}", Utils.transformPasswordToStars(this.proxyPassword));
130 
131         this.nonProxyHosts = settings.getString(Consts.SERVER_NONPROXY_HOSTS, "localhost");
132         LOG.info("nonProxyHosts : {}", this.nonProxyHosts);
133 
134         this.proxySet = !Type.NO_PROXY.toString().equals(settings.
135                 getString(Consts.SERVER_PROXY_TYPE));
136         LOG.info("proxySet : {}", this.proxySet);
137 
138         this.proxyType = settings.getString(Consts.SERVER_PROXY_TYPE, Type.NO_PROXY.name());
139         LOG.info("proxyType : {}", this.proxyType);
140 
141         this.proxySpn = settings.getString(Consts.SERVER_PROXY_JAAS_SPN, "");
142         LOG.info("proxy SPN : {}", this.proxySpn);
143 
144         this.proxyJaasFile = settings.getString(Consts.SERVER_PROXY_JAAS_FILE, "");
145         LOG.info("proxy JAAS file : {}", this.proxyJaasFile);
146 
147         this.proxyJaasCtx = settings.getString(Consts.SERVER_PROXY_JAAS_CONTEXT, "");
148         LOG.info("proxy JAAS ctx : {}", this.proxyJaasCtx);
149 
150         LOG.info("Proxy settings have been loaded");
151         LOG.info("--------------------------");
152 
153         LOG.traceExit();
154     }
155 
156     /**
157      * Gets the withProxy value
158      *
159      * @return the withProxy
160      */
161     public boolean isWithProxy() {
162         LOG.traceEntry();
163         return LOG.traceExit(this.proxySet);
164     }
165 
166     /**
167      * Returns true when the proxy needs an authentication otherwise false.
168      *
169      * @return true when the proxy needs an authentication otherwise false.
170      */
171     public boolean isAuthenticate() {
172         LOG.traceEntry();
173         return LOG.traceExit(
174                 !this.getProxyUser().isEmpty() && !this.getProxyPassword().isEmpty() && this.
175                 isWithProxy());
176     }
177 
178     /**
179      * Gets the proxyHost value
180      *
181      * @return the proxyHost
182      */
183     public String getProxyHost() {
184         LOG.traceEntry();
185         return LOG.traceExit(proxyHost);
186     }
187 
188     /**
189      * Gets the proxyPort value
190      *
191      * @return the proxyPort
192      */
193     public String getProxyPort() {
194         LOG.traceEntry();
195         return LOG.traceExit(proxyPort);
196     }
197 
198     /**
199      * Gets the proxyUser value
200      *
201      * @return the proxyUser
202      */
203     public String getProxyUser() {
204         LOG.traceEntry();
205         return LOG.traceExit(proxyUser);
206     }
207 
208     /**
209      * Gets the proxyPassword value
210      *
211      * @return the proxyPassword
212      */
213     public String getProxyPassword() {
214         LOG.traceEntry();
215         return LOG.traceExit(proxyPassword);
216     }
217 
218     /**
219      * Gets the nonproxyHosts value
220      *
221      * @return the nonproxyHosts
222      */
223     public String getNonProxyHosts() {
224         LOG.traceEntry();
225         return LOG.traceExit(nonProxyHosts);
226     }
227 
228     /**
229      * Returns get type of proxy
230      *
231      * @return the proxy type
232      */
233     public String getProxyType() {
234         LOG.traceEntry();
235         return LOG.traceExit(this.proxyType);
236     }
237 
238     /**
239      * Configure the proxy.
240      */
241     public void configureProxy() {
242         final Type type = Type.valueOf(this.getProxyType());
243         LOG.info("Starting with proxy : {}", this.proxySet);
244         if (this.proxySet) {
245             switch (type) {
246                 case PROXY_BASIC:
247                     LOG.info("Proxy with Basic authentication");
248                     ProxyConfiguration.HTTP_PROXY.setValue(this.getProxyHost() + ":" + this.
249                             getProxyPort());
250                     ProxyConfiguration.NO_PROXY.setValue(this.getNonProxyHosts());
251                     if (this.isAuthenticate()) {
252                         ProxyConfiguration.USERNAME.setValue(this.getProxyUser());
253                         ProxyConfiguration.PASSWORD.setValue(this.getProxyPassword());
254                     }
255                     break;
256                 case PROXY_SPNEGO_API:
257                     LOG.info("Proxy with SPNego using API");
258                     throw new IllegalArgumentException(
259                             "SPNego trough API not supported, use JAAS configuration file");
260                 case PROXY_SPNEGO_JAAS:
261                     LOG.info("Proxy with SPNego using JAAS configuration file");
262                     ProxySPNegoJAASConfiguration.HTTP_PROXY.setValue(
263                             this.getProxyHost() + ":" + this.getProxyPort());
264                     ProxySPNegoJAASConfiguration.NO_PROXY.setValue(this.getNonProxyHosts());
265                     ProxySPNegoJAASConfiguration.JAAS.setValue(this.proxyJaasFile);
266                     ProxySPNegoJAASConfiguration.JAAS_CONTEXT.setValue(this.proxyJaasCtx);
267                     ProxySPNegoJAASConfiguration.SERVICE_PROVIDER_NAME.setValue(this.proxySpn);
268                     break;
269                 default:
270                     throw new IllegalArgumentException("Proxy " + type + " is not implemented");
271             }
272         }
273     }
274 
275     /**
276      *
277      */
278     private static class ProxySettingsHolder {
279 
280         /**
281          * Unique Instance unique not pre-initiliaze
282          */
283         private static final ProxySettings INSTANCE = new ProxySettings();
284     }
285 
286 }