2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.server;
24 import java.security.NoSuchAlgorithmException;
25 import java.util.Properties;
27 import javax.net.ssl.SSLContext;
28 import javax.net.ssl.SSLSocketFactory;
29 import javax.servlet.Filter;
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;
50 public abstract class AbsService<ENV extends BasicEnv, TRANS extends Trans> extends RServlet<TRANS> {
51 public final Access access;
53 private AAFConHttp aafCon;
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;
60 public AbsService(final Access access, final ENV env) throws CadiException {
62 ROOT_NS = Define.ROOT_NS();
66 String component = access.getProperty(Config.AAF_COMPONENT, null);
67 final String[] locator_deploy;
69 if (component == null) {
70 locator_deploy = null;
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");
77 locator_deploy[1]=locator_deploy[1].substring(0, snapshot);
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)");
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");
89 app_name = Define.varReplace(locator_deploy[0]);
90 app_version = locator_deploy[1];
91 app_interface_version = version[0]+'.'+version[1];
93 // Print Cipher Suites Available
94 if (access.willLog(Level.DEBUG)) {
97 context = SSLContext.getDefault();
98 } catch (NoSuchAlgorithmException e) {
99 throw new CadiException("SSLContext issue",e);
101 SSLSocketFactory sf = context.getSocketFactory();
102 StringBuilder sb = new StringBuilder("Available Cipher Suites: ");
103 boolean first = true;
105 for ( String cs : sf.getSupportedCipherSuites()) {
106 if (first)first = false;
109 if (++count%4==0){sb.append('\n');}
111 access.log(Level.DEBUG,sb);
115 protected abstract Filter[] _filters(Object ... additionalTafLurs) throws CadiException, LocatorException;
118 * Overload this method to add new TAF or LURs
121 * @throws CadiException
122 * @throws LocatorException
124 public Filter[] filters() throws CadiException, LocatorException {
128 public abstract Registrant<ENV>[] registrants(final int port) throws CadiException, LocatorException;
130 // Lazy Instantiation
131 public synchronized AAFConHttp aafCon() throws CadiException, LocatorException {
133 if (access.getProperty(Config.AAF_URL,null)!=null) {
134 aafCon = _newAAFConHttp();
136 throw new CadiException("AAFCon cannot be constructed without " + Config.AAF_URL);
143 * Allow to be over ridden for special cases
145 * @throws LocatorException
147 protected synchronized AAFConHttp _newAAFConHttp() throws CadiException, LocatorException {
149 aafCon = new AAFConHttp(access);
155 // This is a method, so we can overload for AAFAPI
156 public String aaf_url() {
157 return access.getProperty(Config.AAF_URL, null);
160 public Rcli<?> client() throws CadiException {
161 return aafCon.client(Config.AAF_DEFAULT_VERSION);
164 public Rcli<?> clientAsUser(TaggedPrincipal p) throws CadiException {
165 return aafCon.client(Config.AAF_DEFAULT_VERSION).forUser(
166 new HTransferSS(p,app_name, aafCon.securityInfo()));
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);
173 protected static final String loadFromArgOrSystem(final Properties props, final String tag, final String args[], final String def) {
174 String tagEQ = tag + '=';
176 for (String arg : args) {
177 if (arg.startsWith(tagEQ)) {
178 props.put(tag, value=arg.substring(tagEQ.length()));
182 // check System.properties
183 value = System.getProperty(tag);
185 props.put(tag, value);