6fce97a6d858b72ee76fcd0a8c224909c6b351fd
[aaf/cadi.git] / aaf / src / main / java / com / att / cadi / aaf / v2_0 / AAFConDME2.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cadi.aaf.v2_0;\r
24 \r
25 import java.io.IOException;\r
26 import java.net.ConnectException;\r
27 import java.net.URI;\r
28 import java.net.URISyntaxException;\r
29 import java.security.GeneralSecurityException;\r
30 import java.security.Principal;\r
31 import java.util.Properties;\r
32 \r
33 import com.att.aft.dme2.api.DME2Client;\r
34 import com.att.aft.dme2.api.DME2Exception;\r
35 import com.att.aft.dme2.api.DME2Manager;\r
36 import com.att.cadi.CadiException;\r
37 import com.att.cadi.LocatorException;\r
38 import com.att.cadi.PropAccess;\r
39 import com.att.cadi.SecuritySetter;\r
40 import com.att.cadi.client.Rcli;\r
41 import com.att.cadi.client.Retryable;\r
42 import com.att.cadi.config.Config;\r
43 import com.att.cadi.config.SecurityInfoC;\r
44 import com.att.cadi.dme2.DME2BasicAuth;\r
45 import com.att.cadi.dme2.DME2TransferSS;\r
46 import com.att.cadi.dme2.DME2x509SS;\r
47 import com.att.cadi.dme2.DRcli;\r
48 import com.att.cadi.principal.BasicPrincipal;\r
49 import com.att.inno.env.APIException;\r
50 \r
51 public class AAFConDME2 extends AAFCon<DME2Client>{\r
52         private DME2Manager manager;\r
53         private boolean isProxy;\r
54         private URI initURI;\r
55 \r
56         public AAFConDME2(PropAccess access) throws CadiException, GeneralSecurityException, IOException{\r
57                 super(access,Config.AAF_URL,new SecurityInfoC<DME2Client> (access));\r
58                 manager = newManager(access);\r
59                 setIsProxy();\r
60         }\r
61         \r
62         public AAFConDME2(PropAccess access, String url) throws CadiException, GeneralSecurityException, IOException{\r
63                 super(access,url,new SecurityInfoC<DME2Client> (access));\r
64                 manager = newManager(access);\r
65                 setIsProxy();\r
66         }\r
67 \r
68         public AAFConDME2(PropAccess access, SecurityInfoC<DME2Client> si) throws CadiException {\r
69                 super(access,Config.AAF_URL,si);\r
70                 manager = newManager(access);\r
71                 setIsProxy();\r
72         }\r
73 \r
74         public AAFConDME2(PropAccess access, String url, SecurityInfoC<DME2Client> si) throws CadiException {\r
75                 super(access,url,si);\r
76                 manager = newManager(access);\r
77                 setIsProxy();\r
78         }\r
79 \r
80         /**\r
81         *  Construct a Connector based on the AAF one.  This is for remote access to OTHER than AAF,\r
82         *  but using Credentials, etc\r
83         */ \r
84         private AAFConDME2(AAFCon<DME2Client> aafcon, String url) throws CadiException {\r
85                 super(aafcon);\r
86                 try {\r
87                         initURI = new URI(url);\r
88                 } catch (URISyntaxException e) {\r
89                         throw new CadiException(e);\r
90                 }\r
91                 manager = newManager(access);\r
92         }\r
93         \r
94         /**\r
95         *  Create a Connector based on the AAF one.  This is for remote access to OTHER than AAF,\r
96         *  but using Credentials, etc\r
97         */ \r
98         public AAFCon<DME2Client> clone(String url) throws CadiException {\r
99                 return new AAFConDME2(this,url);\r
100         }\r
101         \r
102         private void setIsProxy() {\r
103                 String str;\r
104                 if((str=access.getProperty(Config.AAF_URL, null))!=null) {\r
105                         isProxy = str.contains("service=com.att.authz.authz-gw/version=");\r
106                 }\r
107         }\r
108 \r
109         private DME2Manager newManager(PropAccess access) throws CadiException {\r
110                 Properties props = access.getDME2Properties();\r
111                 // Critical that TLS Settings not ignored\r
112                 try {\r
113                         return new DME2Manager("AAFCon",props);\r
114                 } catch (DME2Exception e) {\r
115                         throw new CadiException(e);\r
116                 }\r
117         }\r
118 \r
119 \r
120         /* (non-Javadoc)\r
121          * @see com.att.cadi.aaf.v2_0.AAFCon#basicAuth(java.lang.String, java.lang.String)\r
122          */\r
123         @Override\r
124         public SecuritySetter<DME2Client> basicAuth(String user, String password) throws CadiException {\r
125                 if(password.startsWith("enc:???")) {\r
126                         try {\r
127                                 password = access.decrypt(password, true);\r
128                         } catch (IOException e) {\r
129                                 throw new CadiException("Error Decrypting Password",e);\r
130                         }\r
131                 }\r
132 \r
133                 try {\r
134                         return set(new DME2BasicAuth(user,password,si));\r
135                 } catch (IOException e) {\r
136                         throw new CadiException("Error setting up DME2BasicAuth",e);\r
137                 }\r
138         }\r
139 \r
140         /* (non-Javadoc)\r
141          * @see com.att.cadi.aaf.v2_0.AAFCon#rclient(java.net.URI, com.att.cadi.SecuritySetter)\r
142          */\r
143         @Override\r
144         protected Rcli<DME2Client> rclient(URI uri, SecuritySetter<DME2Client> ss) {\r
145                 DRcli dc = new DRcli(uri, ss);\r
146                 dc.setProxy(isProxy);\r
147                 dc.setManager(manager);\r
148                 return dc;\r
149         }\r
150 \r
151         @Override\r
152         public SecuritySetter<DME2Client> transferSS(Principal principal) throws CadiException {\r
153                 try {\r
154                         return principal==null?ss:new DME2TransferSS(principal, app, si);\r
155                 } catch (IOException e) {\r
156                         throw new CadiException("Error creating DME2TransferSS",e);\r
157                 }\r
158         }\r
159 \r
160         @Override\r
161         public SecuritySetter<DME2Client> basicAuthSS(BasicPrincipal principal) throws CadiException {\r
162                 try {\r
163                         return new DME2BasicAuth(principal,si);\r
164                 } catch (IOException e) {\r
165                         throw new CadiException("Error creating DME2BasicAuth",e);\r
166                 }\r
167 \r
168         }\r
169 \r
170         @Override\r
171         public SecuritySetter<DME2Client> x509Alias(String alias) throws CadiException {\r
172                 try {\r
173                         presetProps(access, alias);\r
174                         return new DME2x509SS(alias,si);\r
175                 } catch (Exception e) {\r
176                         throw new CadiException("Error creating DME2x509SS",e);\r
177                 }\r
178         }\r
179 \r
180         @Override\r
181         public <RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException {\r
182                 // NOTE: DME2 had Retry Logic embedded lower.  \r
183                 try {\r
184                         return (retryable.code(rclient(initURI,ss)));\r
185                 } catch (ConnectException e) {\r
186                         // DME2 should catch\r
187                         try {\r
188                                 manager.refresh();\r
189                         } catch (Exception e1) {\r
190                                 throw new CadiException(e1);\r
191                         }\r
192                         throw new CadiException(e);\r
193                 }\r
194         }\r
195         \r
196         public static void presetProps(PropAccess access, String alias) throws IOException {\r
197                 System.setProperty(Config.AFT_DME2_CLIENT_SSL_CERT_ALIAS, alias);\r
198                 if(System.getProperty(Config.AFT_DME2_CLIENT_IGNORE_SSL_CONFIG)==null) {\r
199                         access.getDME2Properties();\r
200                 }\r
201 \r
202         }\r
203 \r
204         /* (non-Javadoc)\r
205          * @see com.att.cadi.aaf.v2_0.AAFCon#initURI()\r
206          */\r
207         @Override\r
208         protected URI initURI() {\r
209                 return initURI;\r
210         }\r
211 \r
212         /* (non-Javadoc)\r
213          * @see com.att.cadi.aaf.v2_0.AAFCon#setInitURI(java.lang.String)\r
214          */\r
215         @Override\r
216         protected void setInitURI(String uriString) throws CadiException {\r
217                 try {\r
218                         initURI = new URI(uriString);\r
219                 } catch (URISyntaxException e) {\r
220                         throw new CadiException(e);\r
221                 }\r
222         }\r
223 }\r