12b19d293a3e5e39a4868b56cbaffc4a8e9a044c
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / server / AbsService.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.server;
23
24 import java.security.NoSuchAlgorithmException;
25
26 import javax.net.ssl.SSLContext;
27 import javax.net.ssl.SSLSocketFactory;
28 import javax.servlet.Filter;
29
30 import org.onap.aaf.auth.common.Define;
31 import org.onap.aaf.auth.rserv.RServlet;
32 import org.onap.aaf.cadi.Access;
33 import org.onap.aaf.cadi.Access.Level;
34 import org.onap.aaf.cadi.CadiException;
35 import org.onap.aaf.cadi.LocatorException;
36 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
37 import org.onap.aaf.cadi.client.Rcli;
38 import org.onap.aaf.cadi.client.Retryable;
39 import org.onap.aaf.cadi.config.Config;
40 import org.onap.aaf.cadi.http.HTransferSS;
41 import org.onap.aaf.cadi.principal.TaggedPrincipal;
42 import org.onap.aaf.cadi.register.Registrant;
43 import org.onap.aaf.cadi.util.Split;
44 import org.onap.aaf.misc.env.APIException;
45 import org.onap.aaf.misc.env.Trans;
46 import org.onap.aaf.misc.env.impl.BasicEnv;
47
48 public abstract class AbsService<ENV extends BasicEnv, TRANS extends Trans> extends RServlet<TRANS> {
49         protected static final String AAF_LOG4J_PREFIX = "aaf_log4j_prefix";
50         public final Access access;
51         public final ENV env;
52         private AAFConHttp aafCon;
53
54         public final String app_name;
55         public final String app_version;
56         public final String app_interface_version;
57         public final String ROOT_NS;
58
59     public AbsService(final Access access, final ENV env) throws CadiException {
60                 Define.set(access);
61                 ROOT_NS = Define.ROOT_NS();
62                 this.access = access;
63                 this.env = env;
64
65                 String component = access.getProperty(Config.AAF_COMPONENT, null);
66                 final String[] locator_deploy;
67                 
68                 if(component == null) {
69                         locator_deploy = null;
70                 } else {
71                         locator_deploy = Split.splitTrim(':', component);
72                 }
73                         
74                 if(component == null || locator_deploy==null || locator_deploy.length<2) {
75                         throw new CadiException("AAF Component must include the " + Config.AAF_COMPONENT + " property, <fully qualified service name>:<full deployed version (i.e. 2.1.3.13)");
76                 }
77                 final String[] version = Split.splitTrim('.', locator_deploy[1]);
78                 if(version==null || version.length<2) {
79                         throw new CadiException("AAF Component Version must have at least Major.Minor version");
80                 }
81                 app_name = Define.varReplace(locator_deploy[0]);
82                 app_version = locator_deploy[1];
83                 app_interface_version = version[0]+'.'+version[1];
84                 
85                 // Print Cipher Suites Available
86                 if(access.willLog(Level.DEBUG)) {
87                         SSLContext context;
88                         try {
89                                 context = SSLContext.getDefault();
90                         } catch (NoSuchAlgorithmException e) {
91                                 throw new CadiException("SSLContext issue",e);
92                         }
93                         SSLSocketFactory sf = context.getSocketFactory();
94                         StringBuilder sb = new StringBuilder("Available Cipher Suites: ");
95                         boolean first = true;
96                         int count=0;
97                         for( String cs : sf.getSupportedCipherSuites()) {
98                                 if(first)first = false;
99                                 else sb.append(',');
100                                 sb.append(cs);
101                                 if(++count%4==0){sb.append('\n');}
102                         }
103                         access.log(Level.DEBUG,sb);
104                 }
105     }
106
107         public abstract Filter[] filters() throws CadiException,  LocatorException;
108
109
110     public abstract Registrant<ENV>[] registrants(final int port) throws CadiException, LocatorException;
111
112         // Lazy Instantiation
113     public synchronized AAFConHttp aafCon() throws CadiException, LocatorException {
114                 if(aafCon==null) {
115                         if(access.getProperty(Config.AAF_URL,null)!=null) {
116                                 aafCon = _newAAFConHttp();
117                         } else {
118                                 throw new CadiException("AAFCon cannot be constructed without " + Config.AAF_URL);
119                         }
120                 }
121                 return aafCon;
122     }
123     
124     /**
125      * Allow to be over ridden for special cases
126      * @return
127      * @throws LocatorException 
128      */
129     protected synchronized AAFConHttp _newAAFConHttp() throws CadiException, LocatorException {
130                 try {
131                         if(aafCon==null) {
132                                 aafCon = new AAFConHttp(access);
133                         } 
134                         return aafCon;
135                 } catch (APIException e) {
136                         throw new CadiException(e);
137                 }
138     }
139     
140     // This is a method, so we can overload for AAFAPI
141     public String aaf_url() {
142                 return access.getProperty(Config.AAF_URL, null);
143     }
144     
145         public Rcli<?> client() throws CadiException {
146                 return aafCon.client(Config.AAF_DEFAULT_VERSION);
147         }
148
149         public Rcli<?> clientAsUser(TaggedPrincipal p) throws CadiException {
150                 return aafCon.client(Config.AAF_DEFAULT_VERSION).forUser(
151                                 new HTransferSS(p,app_name, aafCon.securityInfo()));
152         }
153
154         public<RET> RET clientAsUser(TaggedPrincipal p,Retryable<RET> retryable) throws APIException, LocatorException, CadiException  {
155                         return aafCon.hman().best(new HTransferSS(p,app_name, aafCon.securityInfo()), retryable);
156         }
157         
158         protected static final String getArg(final String tag, final String args[], final String def) {
159                 String value = def;
160                 String tagEQ = tag + '=';
161                 for(String arg : args) {
162                         if(arg.startsWith(tagEQ)) {
163                                 value = arg.substring(tagEQ.length());
164                         }
165                 }
166                 return value;
167         }
168 }