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