[AAF-21] Initial code import
[aaf/cadi.git] / cass / src / main / java / com / att / cadi / aaf / cass / AAFBase.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cadi.aaf.cass;\r
25 \r
26 import java.io.File;\r
27 import java.io.FileInputStream;\r
28 import java.io.InputStream;\r
29 import java.net.URL;\r
30 import java.util.HashSet;\r
31 import java.util.Properties;\r
32 import java.util.Set;\r
33 \r
34 import org.apache.cassandra.auth.DataResource;\r
35 import org.apache.cassandra.auth.IAuthenticator;\r
36 import org.apache.cassandra.config.DatabaseDescriptor;\r
37 import org.apache.cassandra.exceptions.ConfigurationException;\r
38 \r
39 import com.att.cadi.Access;\r
40 import com.att.cadi.Access.Level;\r
41 import com.att.cadi.Lur;\r
42 import com.att.cadi.SLF4JAccess;\r
43 import com.att.cadi.aaf.v2_0.AAFAuthn;\r
44 import com.att.cadi.aaf.v2_0.AAFCon;\r
45 import com.att.cadi.aaf.v2_0.AbsAAFLur;\r
46 import com.att.cadi.config.Config;\r
47 import com.att.cadi.lur.EpiLur;\r
48 import com.att.cadi.lur.LocalLur;\r
49 import com.att.cadi.aaf.AAFPermission;\r
50 \r
51 public abstract class AAFBase {\r
52         protected static final Set<IAuthenticator.Option> options;\r
53         protected static final Set<DataResource> dataResource;\r
54 \r
55         static {\r
56                 options = new HashSet<IAuthenticator.Option>();\r
57                 options.add(IAuthenticator.Option.PASSWORD);\r
58                 \r
59                 dataResource = new HashSet<DataResource>();\r
60                 dataResource.add(DataResource.columnFamily("system_auth", "credentials"));\r
61         }\r
62         \r
63         protected static Access access;\r
64         protected static LocalLur localLur;\r
65         protected static AAFCon<?> aafcon;\r
66         protected static AAFAuthn<?> aafAuthn;\r
67         protected static AbsAAFLur<AAFPermission> aafLur;\r
68         protected static String default_realm;\r
69     protected static String cluster_name;\r
70     protected static String perm_type;\r
71         private static boolean props_ok = false;\r
72         \r
73         /**\r
74          * If you use your own Access Class, this must be called before \r
75          * "setup()" is invoked by Cassandra.\r
76          * \r
77          * Otherwise, it will default to reading Properties CADI style.\r
78          * \r
79          * @param access\r
80          */\r
81         public static void setAccess(Access access) {\r
82                 AAFBase.access = access;\r
83         }\r
84 \r
85         \r
86     public void validateConfiguration() throws ConfigurationException {\r
87         setup();\r
88         if(!props_ok)  {\r
89                 throw new ConfigurationException("AAF not initialized");\r
90         }\r
91     }\r
92     \r
93         @SuppressWarnings("unchecked")\r
94         public synchronized void setup() {\r
95                 if(aafAuthn == null) {\r
96                         try {\r
97                                 if(access==null) {\r
98                                         String value = System.getProperty(Config.CADI_PROP_FILES, "cadi.properties");\r
99                                         Properties initial = new Properties();\r
100                                         URL cadi_props = ClassLoader.getSystemResource(value);\r
101                                         if(cadi_props == null) {\r
102                                                 File cp = new File(value);\r
103                                                 if(cp.exists()) {\r
104                                                         InputStream is = new FileInputStream(cp);\r
105                                                         try {\r
106                                                                 initial.load(is);\r
107                                                         } finally {\r
108                                                                 is.close();\r
109                                                         }\r
110                                                 } else {\r
111                                                         System.out.printf("%s does not exist as File or in Classpath\n",value);\r
112                                                         initial.setProperty(Config.CADI_PROP_FILES, value);\r
113                                                 }\r
114                                         } else {\r
115                                                 InputStream is = cadi_props.openStream();\r
116                                                 try {\r
117                                                         initial.load(is);\r
118                                                 } finally {\r
119                                                         is.close();\r
120                                                 }\r
121                                         }\r
122                                         access = new SLF4JAccess(initial);\r
123                                 }\r
124                                 props_ok = true;\r
125                                 if((perm_type = Config.logProp(access, "cass_group_name",null))==null) {\r
126                                         props_ok=false;\r
127                                 } else {\r
128                                         perm_type = perm_type + ".cass";\r
129                                 }\r
130                                 \r
131                                 if((cluster_name = Config.logProp(access,"cass_cluster_name",null))==null) {\r
132                                         if((cluster_name = DatabaseDescriptor.getClusterName())==null) {\r
133                                                 props_ok=false;\r
134                                         }\r
135                                 }\r
136 \r
137                                 if((default_realm = Config.logProp(access, Config.AAF_DEFAULT_REALM, null))==null) {\r
138                                         props_ok=false;\r
139                                 }\r
140                                 \r
141                                 if(props_ok==false) {\r
142                                         return;\r
143                                 }\r
144 \r
145                                 // AAFLur has pool of DME clients as needed, and Caches Client lookups\r
146                                 Lur lur = Config.configLur(access);\r
147                                 // Loop through to find AAFLur out of possible Lurs, to reuse AAFCon\r
148                                 if(lur instanceof EpiLur) {\r
149                                         EpiLur elur = (EpiLur)lur;\r
150                                         for(int i=0; (lur = elur.get(i))!=null;++i) {\r
151                                                 if(lur instanceof AbsAAFLur) {\r
152                                                         aafLur=(AbsAAFLur<AAFPermission>)lur;\r
153                                                         aafcon = aafLur.aaf;\r
154                                                         aafAuthn = aafLur.aaf.newAuthn(aafLur);\r
155                                                         break;\r
156                                                 } else if(lur instanceof LocalLur) {\r
157                                                         localLur = (LocalLur)lur;\r
158                                                 }\r
159                                         }\r
160                                 } else if(lur instanceof AbsAAFLur) {\r
161                                         aafLur=(AbsAAFLur<AAFPermission>)lur;\r
162                                         aafcon = aafLur.aaf;\r
163                                         aafAuthn = aafLur.aaf.newAuthn(aafLur);\r
164                                 }\r
165                                 if(aafAuthn==null) {\r
166                                         access.log(Level.INIT,"Failed to instantiate full AAF access");\r
167                                         props_ok = false;\r
168                                 }\r
169                         } catch (Exception e) {\r
170                                 aafAuthn=null;\r
171                                 if(access!=null)access.log(e, "Failed to initialize AAF");\r
172                                 props_ok = false;\r
173                         }\r
174                 }               \r
175         }\r
176 \r
177         public Set<DataResource> protectedResources() {\r
178                 access.log(Level.DEBUG, "Data Resource asked for: it's",dataResource.isEmpty()?"":"not","empty");\r
179                 return dataResource;\r
180         }\r
181         \r
182         public Set<IAuthenticator.Option> supportedOptions() {\r
183                 access.log(Level.DEBUG, "supportedOptions() called");\r
184                 return options;\r
185         }\r
186           \r
187         public Set<IAuthenticator.Option> alterableOptions() {\r
188                 access.log(Level.DEBUG, "alterableOptions() called");\r
189                 return options;\r
190         }\r
191 \r
192 \r
193 }\r