311313145ebe07956411421f5fd1e9dcfbbe2ae9
[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 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 str = access.getProperty(Config.AAF_LOCATOR_ENTRIES, null);
66         String[] scomp = Split.splitTrim(',', str);
67         if(scomp.length==0) {
68             throw new CadiException(Config.AAF_LOCATOR_ENTRIES + " must be defined.");
69         } else {
70             str = ROOT_NS + '.' + scomp[0];
71         }
72         app_name = str;
73
74         str = access.getProperty(Config.AAF_LOCATOR_VERSION, null);
75         if(str==null) {
76             str = Defaults.AAF_VERSION;
77             env.setProperty(Config.AAF_LOCATOR_VERSION, str);
78         }
79         app_version = access.getProperty(Config.AAF_DEPLOYED_VERSION, str);
80
81         // Print Cipher Suites Available
82         if (access.willLog(Level.DEBUG)) {
83             SSLContext context;
84             try {
85                 context = SSLContext.getDefault();
86             } catch (NoSuchAlgorithmException e) {
87                 throw new CadiException("SSLContext issue",e);
88             }
89             SSLSocketFactory sf = context.getSocketFactory();
90             StringBuilder sb = new StringBuilder("Available Cipher Suites: ");
91             boolean first = true;
92             int count=0;
93             for ( String cs : sf.getSupportedCipherSuites()) {
94                 if (first)first = false;
95                 else sb.append(',');
96                 sb.append(cs);
97                 if (++count%4==0){sb.append('\n');}
98             }
99             access.log(Level.DEBUG,sb);
100         }
101     }
102
103     public void setProtocol(String proto) {
104         env.setProperty(Config.AAF_LOCATOR_PROTOCOL, proto);
105     }
106
107     public void setSubprotocol(String subproto) {
108         env.setProperty(Config.AAF_LOCATOR_SUBPROTOCOL, subproto);
109     }
110
111     protected abstract Filter[] _filters(Object ... additionalTafLurs) throws CadiException,  LocatorException;
112
113     /**
114      * Overload this method to add new TAF or LURs
115      *
116      * @return
117      * @throws CadiException
118      * @throws LocatorException
119      */
120     public Filter[] filters() throws CadiException,  LocatorException {
121         return _filters();
122     }
123
124     public abstract Registrant<ENV>[] registrants(final int actualPort) throws CadiException, LocatorException;
125
126     // Lazy Instantiation
127     public synchronized AAFConHttp aafCon() throws CadiException, LocatorException {
128             if (aafCon==null) {
129                 if (access.getProperty(Config.AAF_URL,null)!=null) {
130                     aafCon = _newAAFConHttp();
131                 } else {
132                     throw new CadiException("AAFCon cannot be constructed without " + Config.AAF_URL);
133                 }
134             }
135             return aafCon;
136     }
137
138     /**
139      * Allow to be over ridden for special cases
140      * @return
141      * @throws LocatorException
142      */
143         protected synchronized AAFConHttp _newAAFConHttp() throws CadiException, LocatorException {
144             if (aafCon==null) {
145                 aafCon = new AAFConHttp(access);
146             }
147             return aafCon;
148
149         }
150
151     // This is a method, so we can overload for AAFAPI
152     public String aaf_url() {
153             return access.getProperty(Config.AAF_URL, null);
154     }
155
156     public Rcli<?> client() throws CadiException {
157         return aafCon.client();
158     }
159
160     public Rcli<?> clientAsUser(TaggedPrincipal p) throws CadiException {
161         return aafCon.client().forUser(
162                 new HTransferSS(p,app_name, aafCon.securityInfo()));
163     }
164
165     public<RET> RET clientAsUser(TaggedPrincipal p,Retryable<RET> retryable) throws APIException, LocatorException, CadiException  {
166             return aafCon.hman().best(new HTransferSS(p,app_name, aafCon.securityInfo()), retryable);
167     }
168
169     protected static final String loadFromArgOrSystem(final Properties props, final String tag, final String args[], final String def) {
170         String tagEQ = tag + '=';
171         String value;
172         for (String arg : args) {
173             if (arg.startsWith(tagEQ)) {
174                 props.put(tag, value=arg.substring(tagEQ.length()));
175                 return value;
176             }
177         }
178         // check System.properties
179         value = System.getProperty(tag);
180         if (value!=null) {
181             props.put(tag, value);
182             return value;
183         }
184
185         if (def!=null) {
186             props.put(tag,def);
187         }
188         return def;
189     }
190 }