AT&T 2.0.19 Code drop, stage 3
[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         public final Access access;
50         public final ENV env;
51         private AAFConHttp aafCon;
52
53         public final String app_name;
54         public final String app_version;
55         public final String app_interface_version;
56         public final String ROOT_NS;
57
58     public AbsService(final Access access, final ENV env) throws CadiException {
59                 Define.set(access);
60                 ROOT_NS = Define.ROOT_NS();
61                 this.access = access;
62                 this.env = env;
63
64                 String component = access.getProperty(Config.AAF_COMPONENT, null);
65                 final String[] locator_deploy;
66                 
67                 if(component == null) {
68                         locator_deploy = null;
69                 } else {
70                         locator_deploy = Split.splitTrim(':', component);
71                 }
72                         
73                 if(component == null || locator_deploy==null || locator_deploy.length<2) {
74                         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)");
75                 }
76                 final String[] version = Split.splitTrim('.', locator_deploy[1]);
77                 if(version==null || version.length<2) {
78                         throw new CadiException("AAF Component Version must have at least Major.Minor version");
79                 }
80                 app_name = Define.varReplace(locator_deploy[0]);
81                 app_version = locator_deploy[1];
82                 app_interface_version = version[0]+'.'+version[1];
83                 
84                 // Print Cipher Suites Available
85                 if(access.willLog(Level.DEBUG)) {
86                         SSLContext context;
87                         try {
88                                 context = SSLContext.getDefault();
89                         } catch (NoSuchAlgorithmException e) {
90                                 throw new CadiException("SSLContext issue",e);
91                         }
92                         SSLSocketFactory sf = context.getSocketFactory();
93                         StringBuilder sb = new StringBuilder("Available Cipher Suites: ");
94                         boolean first = true;
95                         int count=0;
96                         for( String cs : sf.getSupportedCipherSuites()) {
97                                 if(first)first = false;
98                                 else sb.append(',');
99                                 sb.append(cs);
100                                 if(++count%4==0){sb.append('\n');}
101                         }
102                         access.log(Level.DEBUG,sb);
103                 }
104     }
105
106         public abstract Filter[] filters() throws CadiException,  LocatorException;
107
108
109     public abstract Registrant<ENV>[] registrants(final int port) throws CadiException, LocatorException;
110
111         // Lazy Instantiation
112     public synchronized AAFConHttp aafCon() throws CadiException, LocatorException {
113                 if(aafCon==null) {
114                         if(access.getProperty(Config.AAF_URL,null)!=null) {
115                                 aafCon = _newAAFConHttp();
116                         } else {
117                                 throw new CadiException("AAFCon cannot be constructed without " + Config.AAF_URL);
118                         }
119                 }
120                 return aafCon;
121     }
122     
123     /**
124      * Allow to be over ridden for special cases
125      * @return
126      * @throws LocatorException 
127      */
128     protected synchronized AAFConHttp _newAAFConHttp() throws CadiException, LocatorException {
129                 try {
130                         if(aafCon==null) {
131                                 aafCon = new AAFConHttp(access);
132                         } 
133                         return aafCon;
134                 } catch (APIException e) {
135                         throw new CadiException(e);
136                 }
137     }
138     
139     // This is a method, so we can overload for AAFAPI
140     public String aaf_url() {
141                 return access.getProperty(Config.AAF_URL, null);
142     }
143     
144         public Rcli<?> client() throws CadiException {
145                 return aafCon.client(Config.AAF_DEFAULT_VERSION);
146         }
147
148         public Rcli<?> clientAsUser(TaggedPrincipal p) throws CadiException {
149                 return aafCon.client(Config.AAF_DEFAULT_VERSION).forUser(
150                                 new HTransferSS(p,app_name, aafCon.securityInfo()));
151         }
152
153         public<RET> RET clientAsUser(TaggedPrincipal p,Retryable<RET> retryable) throws APIException, LocatorException, CadiException  {
154                         return aafCon.hman().best(new HTransferSS(p,app_name, aafCon.securityInfo()), retryable);
155         }
156 }