2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
23 package org.onap.aaf.auth.cm;
25 import java.lang.reflect.Constructor;
27 import java.util.Map.Entry;
28 import java.util.TreeMap;
30 import javax.servlet.Filter;
32 import org.onap.aaf.auth.cache.Cache;
33 import org.onap.aaf.auth.cache.Cache.Dated;
34 import org.onap.aaf.auth.cm.api.API_Artifact;
35 import org.onap.aaf.auth.cm.api.API_Cert;
36 import org.onap.aaf.auth.cm.ca.CA;
37 import org.onap.aaf.auth.cm.facade.Facade1_0;
38 import org.onap.aaf.auth.cm.facade.FacadeFactory;
39 import org.onap.aaf.auth.cm.mapper.Mapper.API;
40 import org.onap.aaf.auth.cm.service.CMService;
41 import org.onap.aaf.auth.cm.service.Code;
42 import org.onap.aaf.auth.dao.CassAccess;
43 import org.onap.aaf.auth.dao.cass.LocateDAO;
44 import org.onap.aaf.auth.direct.DirectLocatorCreator;
45 import org.onap.aaf.auth.direct.DirectRegistrar;
46 import org.onap.aaf.auth.env.AuthzEnv;
47 import org.onap.aaf.auth.env.AuthzTrans;
48 import org.onap.aaf.auth.env.AuthzTransFilter;
49 import org.onap.aaf.auth.rserv.HttpMethods;
50 import org.onap.aaf.auth.server.AbsService;
51 import org.onap.aaf.auth.server.JettyServiceStarter;
52 import org.onap.aaf.auth.server.Log4JLogIt;
53 import org.onap.aaf.cadi.Access;
54 import org.onap.aaf.cadi.Access.Level;
55 import org.onap.aaf.cadi.CadiException;
56 import org.onap.aaf.cadi.LocatorException;
57 import org.onap.aaf.cadi.PropAccess;
58 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
59 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
60 import org.onap.aaf.cadi.aaf.v2_0.AAFTrustChecker;
61 import org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator;
62 import org.onap.aaf.cadi.config.Config;
63 import org.onap.aaf.cadi.register.Registrant;
64 import org.onap.aaf.misc.env.APIException;
65 import org.onap.aaf.misc.env.Data;
66 import org.onap.aaf.misc.env.Env;
67 import org.onap.aaf.misc.env.util.Split;
69 import com.datastax.driver.core.Cluster;
71 public class AAF_CM extends AbsService<AuthzEnv, AuthzTrans> {
73 private static final String USER_PERMS = "userPerms";
74 private static final Map<String,CA> certAuths = new TreeMap<>();
75 public static Facade1_0 facade1_0; // this is the default Facade
76 public static Facade1_0 facade1_0_XML; // this is the XML Facade
77 public static Map<String, Dated> cacheUser;
78 public static AAFAuthn<?> aafAuthn;
79 public static AAFLurPerm aafLurPerm;
80 public final Cluster cluster;
81 public final LocateDAO locateDAO;
82 public static AuthzEnv envLog;
86 public CMService getService() {
90 * Construct AuthzAPI with all the Context Supporting Routes that Authz needs
96 * @throws APIException
98 public AAF_CM(AuthzEnv env) throws Exception {
99 super(env.access(),env);
100 aafLurPerm = aafCon().newLur();
101 // Note: If you need both Authn and Authz construct the following:
102 aafAuthn = aafCon().newAuthn(aafLurPerm);
104 String aafEnv = env.getProperty(Config.AAF_ENV);
106 throw new APIException("aaf_env needs to be set");
109 // Initialize Facade for all uses
110 AuthzTrans trans = env.newTrans();
112 cluster = org.onap.aaf.auth.dao.CassAccess.cluster(env,null);
113 locateDAO = new LocateDAO(trans,cluster,CassAccess.KEYSPACE);
115 // Have AAFLocator object Create DirectLocators for Location needs
116 AbsAAFLocator.setCreator(new DirectLocatorCreator(env, locateDAO));
118 // Load Supported Certificate Authorities by property
119 // Note: Some will be dynamic Properties, so we need to look through all
120 for (Entry<Object, Object> es : env.access().getProperties().entrySet()) {
121 String key = es.getKey().toString();
122 if (key.startsWith(CA.CM_CA_PREFIX)) {
123 int idx = key.indexOf('.');
124 if (idx==key.lastIndexOf('.')) { // else it's a regular property
125 env.log(Level.INIT, "Loading Certificate Authority Module: " + key.substring(idx+1));
126 String[] segs = Split.split(',', env.getProperty(key));
128 String[][] multiParams = new String[segs.length-1][];
129 for (int i=0;i<multiParams.length;++i) {
130 multiParams[i]=Split.split(';',segs[1+i]);
132 @SuppressWarnings("unchecked")
133 Class<CA> cac = (Class<CA>)Class.forName(segs[0]);
134 Constructor<CA> cons = cac.getConstructor(new Class<?>[] {
135 Access.class,String.class,String.class,String[][].class
137 Object pinst[] = new Object[4];
139 pinst[1]= key.substring(idx+1);
141 pinst[3] = multiParams;
142 CA ca = cons.newInstance(pinst);
143 certAuths.put(ca.getName(),ca);
148 if (certAuths.size()==0) {
149 throw new APIException("No Certificate Authorities have been configured in CertMan");
152 service = getService();
153 if(service == null) {
154 service = new CMService(trans, this);
156 // note: Service knows how to shutdown Cluster on Shutdown, etc. See Constructor
157 facade1_0 = FacadeFactory.v1_0(this,trans, service,Data.TYPE.JSON); // Default Facade
158 facade1_0_XML = FacadeFactory.v1_0(this,trans,service,Data.TYPE.XML);
162 if (cacheUser == null) {
163 cacheUser = Cache.obtain(USER_PERMS);
164 Cache.startCleansing(env, USER_PERMS);
168 ////////////////////////////////////////////////////////////////////////////
170 ////////////////////////////////////////////////////////////////////////
172 API_Artifact.init(this);
174 StringBuilder sb = new StringBuilder();
175 trans.auditTrail(2, sb);
176 trans.init().log(sb);
179 public CA getCA(String key) {
180 return certAuths.get(key);
185 * Setup XML and JSON implementations for each supported Version type
187 * We do this by taking the Code passed in and creating clones of these with the appropriate Facades and properties
188 * to do Versions and Content switches
191 public void route(HttpMethods meth, String path, API api, Code code) throws Exception {
192 String version = "1.0";
193 // Get Correct API Class from Mapper
194 Class<?> respCls = facade1_0.mapper().getClass(api);
195 if (respCls==null) throw new Exception("Unknown class associated with " + api.getClass().getName() + ' ' + api.name());
196 // setup Application API HTML ContentTypes for JSON and Route
197 String application = applicationJSON(respCls, version);
198 route(env,meth,path,code,application,"application/json;version="+version,"*/*");
200 // setup Application API HTML ContentTypes for XML and Route
201 application = applicationXML(respCls, version);
202 route(env,meth,path,code.clone(facade1_0_XML),application,"application/xml;version="+version);
204 // Add other Supported APIs here as created
207 public void routeAll(HttpMethods meth, String path, API api, Code code) throws Exception {
208 route(env,meth,path,code,""); // this will always match
212 public Filter[] _filters(Object ... additionalTafLurs) throws CadiException, LocatorException {
214 return new Filter[] {
215 new AuthzTransFilter(env,aafCon(),
216 new AAFTrustChecker((Env)env),
219 } catch (NumberFormatException e) {
220 throw new CadiException("Invalid Property information", e);
224 @SuppressWarnings("unchecked")
226 public Registrant<AuthzEnv>[] registrants(final int port) throws CadiException, LocatorException {
227 return new Registrant[] {
228 new DirectRegistrar(access,locateDAO,app_name,app_version,port)
232 public void destroy() {
234 locateDAO.close(env.newTransNoAvg());
238 public static void main(final String[] args) {
241 Log4JLogIt logIt = new Log4JLogIt(args, "cm");
242 PropAccess propAccess = new PropAccess(logIt,args);
244 AAF_CM service = new AAF_CM(new AuthzEnv(propAccess));
245 JettyServiceStarter<AuthzEnv,AuthzTrans> jss = new JettyServiceStarter<AuthzEnv,AuthzTrans>(service);
247 } catch (Exception e) {
248 envLog.error().log(e);