View Javadoc
1   /**
2    * This Source Code Form is subject to the terms of the Mozilla Public
3    * License, v. 2.0. If a copy of the MPL was not distributed with this
4    * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5    *
6    * If it is not possible or desirable to put the notice in a particular
7    * file, then You may include the notice in a location (such as a LICENSE
8    * file in a relevant directory) where a recipient would be likely to look
9    * for such a notice.
10  
11   * 
12   */
13   
14  /*  ---------------------------------------------------------------------------
15   *  U.S. Government, Department of the Army
16   *  Army Materiel Command
17   *  Research Development Engineering Command
18   *  Communications Electronics Research Development and Engineering Center
19   *  ---------------------------------------------------------------------------
20   */
21  
22  package org.miloss.fgsms.agentcore;
23  
24  import java.util.Map;
25  import javax.xml.ws.BindingProvider;
26  import org.miloss.fgsms.common.Utility;
27  import org.miloss.fgsms.services.interfaces.datacollector.AddMachineAndProcessDataRequestMsg;
28  import org.miloss.fgsms.services.interfaces.datacollector.AddMachineAndProcessDataResponseMsg;
29  import org.miloss.fgsms.services.interfaces.policyconfiguration.SetProcessListByMachineRequestMsg;
30  import org.miloss.fgsms.services.interfaces.policyconfiguration.SetProcessListByMachineResponseMsg;
31  import org.apache.log4j.Level;
32  import org.miloss.fgsms.common.Logger;;
33  
34  /**
35   *  Provides some useful functions for building agents.. All
36   * functions use the fgsms-agent.properties file within this JAR for
37   * configuration purposes All functions support discovery mechanisms, retry
38   * counts, failover/roundrobin
39   *
40   * @author AO
41   */
42  public class OSAgentHelper extends HelperBase{
43  
44      static ConfigLoader cfg = null;
45  
46      private static void Init() throws ConfigurationException {
47          if (cfg == null) {
48              cfg = new ConfigLoader();
49          }
50  
51      }
52      static Logger log = Logger.getLogger(org.miloss.fgsms.common.Constants.LoggerName);
53  
54  
55      /**
56       * Sends the current machine configuration information to the fgsms PCS
57       * service If it cannot be reached, null is returned.
58       *
59       * @param req
60       * @return
61       * @throws ConfigurationException
62       */
63      public static SetProcessListByMachineResponseMsg SetMachineInfo(SetProcessListByMachineRequestMsg req) throws ConfigurationException {
64          if (cfg == null) {
65              Init();
66          }
67          discoverEndpoints();
68          req.setClassification(cfg.classlevel);
69  
70          try {
71  
72              return Send(req);
73          } catch (Exception ex) {
74              log.log(Level.ERROR, "could not retrieve a policy for the specified url", ex);
75              return null;
76          }
77      }
78  
79      private static SetProcessListByMachineResponseMsg Send(SetProcessListByMachineRequestMsg req) throws ConfigurationException {
80          if (cfg == null) {
81              Init();
82          }
83          discoverEndpoints();
84          int retrycount = 0;
85          int urlcount = 0;
86          switch (cfg.PCSalgo) {
87              case FAILOVER:
88                  retrycount = 0;
89                  urlcount = 0;
90                  while ((retrycount < cfg.PCSRetryCount)) {
91  
92                      urlcount = 0;
93                      while (urlcount < cfg.PCS_URLS.size()) {
94                          try { // Call Web Service Operation
95  
96                              BindingProvider bp = (BindingProvider) cfg.pcsport;
97                              Map<String, Object> context = bp.getRequestContext();
98                              context.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, cfg.PCS_URLS.get(urlcount));
99                              if (cfg.mode_ == org.miloss.fgsms.common.Constants.AuthMode.UsernamePassword) {
100                                 context.put(BindingProvider.USERNAME_PROPERTY, cfg.username);
101                                 context.put(BindingProvider.PASSWORD_PROPERTY, Utility.DE(cfg.password));
102                             }
103                             if (Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.trustStore")) && !Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.trustStorePassword"))) {
104                                 context.put("javax.net.ssl.trustStorePassword", Utility.DE(cfg.getJavaxtruststorepass()));
105                                 context.put("javax.net.ssl.trustStore", Utility.DE(cfg.getJavaxtruststore()));
106                             }
107                             try {
108                                 if (cfg.mode_ == org.miloss.fgsms.common.Constants.AuthMode.PKI) {
109                                     if (Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.keyStore")) && !Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.keyStorePassword"))) {
110                                         context.put("javax.net.ssl.keyStorePassword", Utility.DE(cfg.getJavaxkeystorepass()));
111                                         context.put("javax.net.ssl.keyStore", Utility.DE(cfg.getJavaxkeystore()));
112                                     }
113                                 }
114 
115                             } catch (Exception ex) {
116                                 log.log(Level.FATAL, "error caught when referencing (get or set) System.properties for SSL communication. Check to ensure that this is enabled in your JAAS managemer", ex);
117                             }
118                             //  ApacheCxfSslHelper.DoCXF(cfg.pcsport, cfg);
119 
120 
121                             //JAXB.marshal(req, System.out);
122                             //send it
123                             SetProcessListByMachineResponseMsg servicePolicy = cfg.pcsport.setProcessListByMachine(req);
124                             cfg.classlevel = servicePolicy.getClassification();
125                             return servicePolicy;
126 
127 
128                         } catch (Exception ex) {
129                             log.log(Level.WARN, "fgsms unable to fetch policy from PCS at " + cfg.PCS_URLS.get(urlcount) + " will retry " + (cfg.PCSRetryCount - retrycount) + " times." + ex.getMessage(), ex);
130                         }
131                         urlcount++;
132                     }
133                     retrycount++;
134                 }
135 
136                 log.log(Level.FATAL, "fgsms unable to fetch policy from any of the PCS[" + cfg.PCS_URLS.size() + "] URLs, retry count exceeded. Falling back to default policy");
137                 return null;
138             case ROUNDROBIN:
139                 retrycount = 0;
140                 urlcount = 0;
141                 while ((retrycount < cfg.PCSRetryCount)) {
142 
143                     urlcount = 0;
144                     while (urlcount < cfg.PCS_URLS.size()) {
145                         try { // Call Web Service Operation
146 
147                             BindingProvider bp = (BindingProvider) cfg.pcsport;
148                             Map<String, Object> context = bp.getRequestContext();
149                             //    context.remove(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
150                             context.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, cfg.PCS_URLS.get(urlcount));
151 //                            StubExt sec = (StubExt) pcsport;
152                             if (cfg.mode_ == org.miloss.fgsms.common.Constants.AuthMode.UsernamePassword) {
153 //                                sec.setSecurityConfig("/META-INF/fgsms-username-config.xml");
154                                 context.put(BindingProvider.USERNAME_PROPERTY, cfg.username);
155                                 context.put(BindingProvider.PASSWORD_PROPERTY, Utility.DE(cfg.password));
156                             }
157                             if (cfg.mode_ == org.miloss.fgsms.common.Constants.AuthMode.PKI) {
158                                 if (Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.keyStore")) && !Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.keyStorePassword"))) {
159                                     System.setProperty("javax.net.ssl.keyStorePassword", Utility.DE(cfg.getJavaxkeystorepass()));
160                                     System.setProperty("javax.net.ssl.keyStore", Utility.DE(cfg.getJavaxkeystore()));
161                                 }
162                             }
163                             if (Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.trustStore")) && !Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.trustStorePassword"))) {
164                                 System.setProperty("javax.net.ssl.trustStorePassword", Utility.DE(cfg.getJavaxtruststorepass()));
165                                 System.setProperty("javax.net.ssl.trustStore", Utility.DE(cfg.getJavaxtruststore()));
166                             }
167                             SetProcessListByMachineResponseMsg servicePolicy = cfg.pcsport.setProcessListByMachine(req);
168                             cfg.classlevel = servicePolicy.getClassification();
169                             return servicePolicy;
170                         } catch (Exception ex) {
171                             log.log(Level.WARN, "fgsms unable to fetch policy from PCS at " + cfg.PCS_URLS.get(urlcount) + " will retry " + (cfg.PCSRetryCount - retrycount) + " times." + ex.getMessage(), ex);
172                         }
173                         urlcount++;
174                     }
175                     retrycount++;
176                 }
177 
178                 log.log(Level.FATAL, "fgsms unable to fetch policy from any of the PCS[" + cfg.PCS_URLS.size() + "] URLs, retry count exceeded.");
179                 return null;
180 
181         }
182         return null;
183     }
184 
185     public static AddMachineAndProcessDataResponseMsg AddMachineAndProcessDataRequestMsg(AddMachineAndProcessDataRequestMsg req) throws ConfigurationException {
186         if (cfg == null) {
187             Init();
188         }
189         discoverEndpoints();
190         int retrycount = 0;
191         int urlcount = 0;
192         switch (cfg.DCSalgo) {
193             case FAILOVER:
194                 retrycount = 0;
195                 urlcount = 0;
196                 while ((retrycount < cfg.DCSRetryCount)) {
197 
198                     urlcount = 0;
199                     while (urlcount < cfg.DCS_URLS.size()) {
200                         try { // Call Web Service Operation
201 
202                             BindingProvider bp = (BindingProvider) cfg.dcsport;
203                             Map<String, Object> context = bp.getRequestContext();
204                             //    context.remove(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
205                             context.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, cfg.DCS_URLS.get(urlcount));
206                             //TODO test to see if this works in other environments where the JbossWS stack isn't available.
207                             //                        StubExt sec = (StubExt) pcsport;
208                             if (cfg.mode_ == org.miloss.fgsms.common.Constants.AuthMode.UsernamePassword) {
209                                 //                        sec.setSecurityConfig("/META-INF/fgsms-username-config.xml");
210                                 context.put(BindingProvider.USERNAME_PROPERTY, cfg.username);
211                                 context.put(BindingProvider.PASSWORD_PROPERTY, Utility.DE(cfg.password));
212                             }
213                             if (cfg.mode_ == org.miloss.fgsms.common.Constants.AuthMode.PKI) {
214                                 if (Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.keyStore")) && !Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.keyStorePassword"))) {
215                                     System.setProperty("javax.net.ssl.keyStorePassword", Utility.DE(cfg.getJavaxkeystorepass()));
216                                     System.setProperty("javax.net.ssl.keyStore", Utility.DE(cfg.getJavaxkeystore()));
217                                 }
218                             }
219                             if (Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.trustStore")) && !Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.trustStorePassword"))) {
220                                 System.setProperty("javax.net.ssl.trustStorePassword", Utility.DE(cfg.getJavaxtruststorepass()));
221                                 System.setProperty("javax.net.ssl.trustStore", Utility.DE(cfg.getJavaxtruststore()));
222                             }
223                             //send it
224                             AddMachineAndProcessDataResponseMsg res = cfg.dcsport.addMachineAndProcessData(req);
225                             cfg.classlevel = res.getClassification();
226                             return res;
227 
228 
229                         } catch (Exception ex) {
230                             log.log(Level.WARN, "fgsms unable to add data at the DCS at " + cfg.DCS_URLS.get(urlcount) + " will retry " + (cfg.DCSRetryCount - retrycount) + " times." + ex.getMessage(), ex);
231                         }
232                         urlcount++;
233                     }
234                     retrycount++;
235                 }
236 
237                 log.log(Level.FATAL, "fgsms unable to add data at the from any of the DCS[" + cfg.DCS_URLS.size() + "] URLs, retry count exceeded.");
238                 return null;
239             case ROUNDROBIN:
240                 retrycount = 0;
241                 urlcount = 0;
242                 while ((retrycount < cfg.DCSRetryCount)) {
243 
244                     urlcount = 0;
245                     while (urlcount < cfg.DCS_URLS.size()) {
246                         try { // Call Web Service Operation
247 
248                             BindingProvider bp = (BindingProvider) cfg.dcsport;
249                             Map<String, Object> context = bp.getRequestContext();
250                             //    context.remove(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
251                             context.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, cfg.DCS_URLS.get(urlcount));
252               //              context.put(BindingProviderProperties.CONNECT_TIMEOUT, 10000);
253                             if (cfg.mode_ == org.miloss.fgsms.common.Constants.AuthMode.UsernamePassword) {
254                                 context.put(BindingProvider.USERNAME_PROPERTY, cfg.username);
255                                 context.put(BindingProvider.PASSWORD_PROPERTY, Utility.DE(cfg.password));
256                             }
257                             if (cfg.mode_ == org.miloss.fgsms.common.Constants.AuthMode.PKI) {
258                                 if (Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.keyStore")) && !Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.keyStorePassword"))) {
259                                     System.setProperty("javax.net.ssl.keyStorePassword", Utility.DE(cfg.getJavaxkeystorepass()));
260                                     System.setProperty("javax.net.ssl.keyStore", Utility.DE(cfg.getJavaxkeystore()));
261                                 }
262                             }
263                             if (Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.trustStore")) && !Utility.stringIsNullOrEmpty(System.getProperty("javax.net.ssl.trustStorePassword"))) {
264                                 System.setProperty("javax.net.ssl.trustStorePassword", Utility.DE(cfg.getJavaxtruststorepass()));
265                                 System.setProperty("javax.net.ssl.trustStore", Utility.DE(cfg.getJavaxtruststore()));
266                             }
267                             //send it
268                             AddMachineAndProcessDataResponseMsg servicePolicy = cfg.dcsport.addMachineAndProcessData(req);
269                             cfg.classlevel = servicePolicy.getClassification();
270                             return servicePolicy;
271                         } catch (Exception ex) {
272                             log.log(Level.WARN, "fgsms unable to add data at the DCS at " + cfg.DCS_URLS.get(urlcount) + " will retry " + (cfg.DCSRetryCount - retrycount) + " times." + ex.getMessage(), ex);
273                         }
274                         urlcount++;
275                     }
276                     retrycount++;
277                 }
278 
279                 log.log(Level.FATAL, "fgsms unable to add data from any of the DCS[" + cfg.DCS_URLS.size() + "] URLs, retry count exceeded.");
280                 return null;
281 
282         }
283         return null;
284     }
285 }