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