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