JettyStartups to improve new Filters
[aaf/authz.git] / auth / auth-service / src / main / java / org / onap / aaf / auth / service / AAF_Service.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.service;
23
24 import javax.servlet.Filter;
25
26 import org.onap.aaf.auth.cache.Cache;
27 import org.onap.aaf.auth.dao.CassAccess;
28 import org.onap.aaf.auth.dao.hl.Question;
29 import org.onap.aaf.auth.direct.DirectAAFLur;
30 import org.onap.aaf.auth.direct.DirectAAFUserPass;
31 import org.onap.aaf.auth.direct.DirectCertIdentity;
32 import org.onap.aaf.auth.direct.DirectLocatorCreator;
33 import org.onap.aaf.auth.direct.DirectRegistrar;
34 import org.onap.aaf.auth.env.AuthzEnv;
35 import org.onap.aaf.auth.env.AuthzTrans;
36 import org.onap.aaf.auth.env.AuthzTransFilter;
37 import org.onap.aaf.auth.org.OrganizationFactory;
38 import org.onap.aaf.auth.rserv.HttpMethods;
39 import org.onap.aaf.auth.server.AbsService;
40 import org.onap.aaf.auth.server.JettyServiceStarter;
41 import org.onap.aaf.auth.server.Log4JLogIt;
42 import org.onap.aaf.auth.service.api.API_Api;
43 import org.onap.aaf.auth.service.api.API_Approval;
44 import org.onap.aaf.auth.service.api.API_Creds;
45 import org.onap.aaf.auth.service.api.API_Delegate;
46 import org.onap.aaf.auth.service.api.API_History;
47 import org.onap.aaf.auth.service.api.API_Mgmt;
48 import org.onap.aaf.auth.service.api.API_NS;
49 import org.onap.aaf.auth.service.api.API_Perms;
50 import org.onap.aaf.auth.service.api.API_Roles;
51 import org.onap.aaf.auth.service.api.API_User;
52 import org.onap.aaf.auth.service.api.API_UserRole;
53 import org.onap.aaf.auth.service.facade.AuthzFacadeFactory;
54 import org.onap.aaf.auth.service.facade.AuthzFacade_2_0;
55 import org.onap.aaf.auth.service.mapper.Mapper.API;
56 import org.onap.aaf.cadi.CadiException;
57 import org.onap.aaf.cadi.LocatorException;
58 import org.onap.aaf.cadi.PropAccess;
59 import org.onap.aaf.cadi.aaf.v2_0.AAFTrustChecker;
60 import org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator;
61 import org.onap.aaf.cadi.config.Config;
62 import org.onap.aaf.cadi.register.Registrant;
63 import org.onap.aaf.cadi.taf.basic.BasicHttpTaf;
64 import org.onap.aaf.cadi.util.FQI;
65 import org.onap.aaf.misc.env.APIException;
66 import org.onap.aaf.misc.env.Data;
67 import org.onap.aaf.misc.env.Env;
68
69 import com.datastax.driver.core.Cluster;
70
71 public class AAF_Service extends AbsService<AuthzEnv,AuthzTrans> {
72
73         private static final String ORGANIZATION = "Organization.";
74
75         public final Question question;
76         private AuthzFacade_2_0 facade;
77         private AuthzFacade_2_0 facade_XML;
78         private DirectAAFUserPass directAAFUserPass;
79         private final Cluster cluster;
80         //private final OAuthService oauthService;
81         
82         /**
83          * Construct AuthzAPI with all the Context Supporting Routes that Authz needs
84          * 
85          * @param env
86          * @param decryptor 
87          * @throws APIException 
88          */
89         public AAF_Service( final AuthzEnv env) throws Exception {
90                 super(env.access(), env);
91
92                 // Initialize Facade for all uses
93                 AuthzTrans trans = env.newTrans();
94
95                 cluster = org.onap.aaf.auth.dao.CassAccess.cluster(env,null);
96
97                 // Need Question for Security purposes (direct User/Authz Query in Filter)
98                 // Start Background Processing
99                 question = new Question(trans, cluster, CassAccess.KEYSPACE, true);
100                 DirectCertIdentity.set(question.certDAO);
101
102                 // Have AAFLocator object Create DirectLocators for Location needs
103                 AbsAAFLocator.setCreator(new DirectLocatorCreator(env, question.locateDAO));
104                 
105                 // Initialize Organizations... otherwise, first pass may miss
106                 int org_size = ORGANIZATION.length();
107                 for(String n : env.existingStaticSlotNames()) {
108                         if(n.startsWith(ORGANIZATION)) {
109                                 OrganizationFactory.obtain(env, n.substring(org_size));
110                         }
111                 }
112                 
113
114                 // For direct Introspection needs.
115                 //oauthService = new OAuthService(trans, question);
116                 
117                 facade = AuthzFacadeFactory.v2_0(env,trans,Data.TYPE.JSON,question);
118                 facade_XML = AuthzFacadeFactory.v2_0(env,trans,Data.TYPE.XML,question);
119
120                 directAAFUserPass = new DirectAAFUserPass(trans.env(),question);
121         
122                 // Print results and cleanup
123                 StringBuilder sb = new StringBuilder();
124                 trans.auditTrail(0, sb);
125                 if(sb.length()>0)env.init().log(sb);
126                 trans = null;
127                 sb = null;
128
129                 ////////////////////////////////////////////////////////////////////////////
130                 // Time Critical
131                 //  These will always be evaluated first
132                 ////////////////////////////////////////////////////////////////////////
133                 API_Creds.timeSensitiveInit(env, this, facade,directAAFUserPass);
134                 API_Perms.timeSensitiveInit(this, facade);
135                 ////////////////////////////////////////////////////////////////////////
136                 // Service APIs
137                 ////////////////////////////////////////////////////////////////////////
138                 API_Creds.init(this, facade);
139                 API_UserRole.init(this, facade);
140                 API_Roles.init(this, facade);
141                 API_Perms.init(this, facade);
142                 API_NS.init(this, facade);
143                 API_User.init(this, facade);
144                 API_Delegate.init(this,facade);
145                 API_Approval.init(this, facade);
146                 API_History.init(this, facade);
147
148                 ////////////////////////////////////////////////////////////////////////
149                 // Management APIs
150                 ////////////////////////////////////////////////////////////////////////
151                 // There are several APIs around each concept, and it gets a bit too
152                 // long in this class to create.  The initialization of these Management
153                 // APIs have therefore been pushed to StandAlone Classes with static
154                 // init functions
155                 API_Mgmt.init(this, facade);
156                 API_Api.init(this, facade);
157                 
158         }
159         
160         @Override
161         public Filter[] _filters(Object ... additionalTafLurs) throws CadiException, LocatorException {
162                 final String domain = FQI.reverseDomain(access.getProperty(Config.AAF_ROOT_NS,Config.AAF_ROOT_NS_DEF));
163                 try {
164                 Object[] atl=new Object[additionalTafLurs.length+2];
165                 atl[0]=new DirectAAFLur(env,question); // Note, this will be assigned by AuthzTransFilter to TrustChecker
166                         atl[1]=new BasicHttpTaf(env, directAAFUserPass,
167                                 domain,Long.parseLong(env.getProperty(Config.AAF_CLEAN_INTERVAL, Config.AAF_CLEAN_INTERVAL_DEF)),
168                                 false);
169
170                 if(additionalTafLurs.length>0) {
171                         System.arraycopy(additionalTafLurs, 0, atl, 2, additionalTafLurs.length);
172                 }
173                 
174                         return new Filter[] {
175                                 new AuthzTransFilter(env,aafCon(),
176                                 new AAFTrustChecker((Env)env),
177                                 atl
178                 )};
179                 } catch (NumberFormatException e) {
180                         throw new CadiException("Invalid Property information", e);
181                 }
182         }
183
184
185
186         @SuppressWarnings("unchecked")
187         @Override
188         public Registrant<AuthzEnv>[] registrants(final int port) throws CadiException {
189                 return new Registrant[] {
190                         new DirectRegistrar(access,question.locateDAO,app_name,app_interface_version,port)
191                 };
192         }
193
194         @Override
195         public void destroy() {
196                 Cache.stopTimer();
197                 if(cluster!=null) {
198                         cluster.close();
199                 }
200                 super.destroy();
201         }
202
203         
204         /**
205          * Setup XML and JSON implementations for each supported Version type
206          * 
207          * We do this by taking the Code passed in and creating clones of these with the appropriate Facades and properties
208          * to do Versions and Content switches
209          * 
210          */
211         public void route(HttpMethods meth, String path, API api, Code code) throws Exception {
212                 String version = "2.0";
213                 Class<?> respCls = facade.mapper().getClass(api); 
214                 if(respCls==null) throw new Exception("Unknown class associated with " + api.getClass().getName() + ' ' + api.name());
215                 String application = applicationJSON(respCls, version);
216
217                 route(env,meth,path,code,application,"application/json;version=2.0","*/*");
218                 application = applicationXML(respCls, version);
219                 route(env,meth,path,code.clone(facade_XML,false),application,"text/xml;version=2.0");
220         }
221
222         /**
223          * Start up AAF_Service as Jetty Service
224          */
225         public static void main(final String[] args) {
226                 try {
227                         Log4JLogIt logIt = new Log4JLogIt(args, "authz");
228                         PropAccess propAccess = new PropAccess(logIt,args);
229                         
230                         AbsService<AuthzEnv, AuthzTrans> service = new AAF_Service(new AuthzEnv(propAccess));
231                         JettyServiceStarter<AuthzEnv,AuthzTrans> jss = new JettyServiceStarter<AuthzEnv,AuthzTrans>(service);
232                         jss.start();
233                 } catch (Exception e) {
234                         e.printStackTrace();
235                 }
236         }
237 }