1 .. This work is licensed under a Creative Commons Attribution 4.0 International License.
2 .. http://creativecommons.org/licenses/by/4.0
4 Service Configuration - Connecting to AAF
5 ==========================================
12 • If you are a Servlet in a Container, use CADI Framework with AAF Plugin. It's very easy, and includes BasicAuth for Services.
14 • Technologies using Servlet Filters
15 • DME2 (and other Servlet Containers) can use Servlet Filters
16 • Any WebApp can plug in CADI as a Servlet Filter
17 • Jetty can attach a Servlet Filter with Code, or as WebApp
18 • Tomcat 7 has a "Valve" plugin, which is similar and supported
19 • Use the AAFLur Code directly (shown)
20 • All Java Technologies utilize Configuration to set what Security elements are required
21 • example: Global Login can be turned on/off, AAF Client needs information to connect to AAF Service
22 • There are several specialty cases, which AAF can work with, including embedding all properties in a Web.xml, but the essentials needed are:
24 • cadi.properties file (configured the same for all technologies)
25 • Encrypt passwords with included CADI technology, so that there are no Clear Text Passwords in Config Files (ASPR)
26 • See CADI Deployment on how to perform this with several different technologies.
27 • AAF Restfully (see RESTFul APIS)
29 IMPORTANT: If Direct RESTFul API is used, then it is the Client's responsibility to Cache and avoid making an AAF Service Calls too often
30 Example: A Tool like Cassandra will ask for Authentication hundreds of times a second for the same identity during a transaction. Calling the AAF Service for each would be slow for the client, and wasteful of Network and AAF Service Capacities.
31 Rogue Clients can and will be denied access to AAF.
34 J2EE (Servlet Filter) Method
35 ============================
37 1. Per J2EE design, the Filter will deny any unauthenticated HTTP/S call; the Servlet will not even be invoked.
38 a. Therefore, the Servlet can depend on any transaction making it to their code set is Authenticated.
39 b. Identity can be viewed based on the HttpServletRequest Object (request.getUserPrincipal() )
40 2. Per J2EE design, AAF Filter overloads the HttpServletRequest for a String related to "Role". (request.isUserInRole("...") )
41 a. For AAF, do not put in "Role", but the three parts of requested "Permission", separated by "|", i.e. "org.onap.aaf.myapp.myperm|myInstance|myAction".
42 3. NOT REQUIRED: An added benefit, but not required, is a JASPI like interface, where you can add an Annotation to your Servlet.
43 a. When used, no transaction will come into your code if the listed Permissions are not Granted to the Incoming Transaction.
44 b. This might be helpful for covering separate Management Servlet implementations.
49 =========================
51 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
52 HttpServletRequest request;
54 request = (HttpServletRequest)req;
55 } catch (ClassCastException e) {
56 throw new ServletException("Only serving HTTP today",e);
59 // Note: CADI is OVERLOADING the concept of "isUserInRole".. You need to think "doesUserHavePermssion()"
60 // Assume that you have CREATED and GRANTED An AAF Permission in YOUR Namespace
61 // Example Permission: "org.onap.aaf.myapp.myPerm * write"
63 // Think in your head, "Does user have write permission on any instance of org.onap.aaf.myapp.myPerm
64 if(request.isUserInRole("org.onap.aaf.myapp.myPerm|*|write")) {
65 // *** Do something here that someone with "myPerm write" permissions is allowed to do
67 // *** Do something reasonable if user is denied, like an Error Message
72 Here is a working TestServlet, where you can play with different Permissions that you own on the URL, i.e.:
73 https://<your machine:port>/caditest/testme?PERM=org.onap.aaf.myapp.myPerm|*|write
75 Sample Servlet (Working example)
76 ================================
77 package org.onap.aaf.cadi.debug;
78 import java.io.FileInputStream;
79 import java.io.IOException;
80 import java.net.InetAddress;
81 import java.net.UnknownHostException;
82 import java.util.HashMap;
84 import java.util.Map.Entry;
85 import java.util.Properties;
86 import javax.servlet.Servlet;
87 import javax.servlet.ServletConfig;
88 import javax.servlet.ServletException;
89 import javax.servlet.ServletRequest;
90 import javax.servlet.ServletResponse;
91 import javax.servlet.http.HttpServletRequest;
92 import org.eclipse.jetty.server.Server;
93 import org.eclipse.jetty.server.ServerConnector;
94 import org.eclipse.jetty.server.handler.ContextHandler;
95 import org.eclipse.jetty.servlet.FilterHolder;
96 import org.eclipse.jetty.servlet.FilterMapping;
97 import org.eclipse.jetty.servlet.ServletContextHandler;
98 import org.eclipse.jetty.servlet.ServletHandler;
99 import org.onap.aaf.cadi.filter.CadiFilter;
100 import org.onap.aaf.cadi.filter.RolesAllowed;
101 import org.onap.aaf.cadi.jetty.MiniJASPIWrap;
103 public class CSPServletTest {
104 public static void main(String[] args) {
105 // Go ahead and print Test reports in cadi-core first
107 String hostname=null;
109 hostname = InetAddress.getLocalHost().getHostName();
110 } catch (UnknownHostException e) {
114 Properties props = new Properties();
115 Map<String,String> map = new HashMap<String,String>();
117 FileInputStream fis = new FileInputStream("run/cadi.properties");
121 for( Entry<Object, Object> es : props.entrySet()) {
122 key = es.getKey().toString();
123 value = es.getValue().toString();
125 if(key.startsWith("AFT_") || key.startsWith("DME2")) {
126 System.setProperty(key,value);
132 } catch(IOException e) {
133 System.err.println("Cannot load run/cadi.properties");
136 String portStr = System.getProperty("port");
137 int port = portStr==null?8080:Integer.parseInt(portStr);
139 // Add ServletHolder(s) and Filter(s) to a ServletHandler
140 ServletHandler shand = new ServletHandler();
142 FilterHolder cfh = new FilterHolder(CadiFilter.class);
143 cfh.setInitParameters(map);
145 shand.addFilterWithMapping(cfh, "/*", FilterMapping.ALL);
146 shand.addServletWithMapping(new MiniJASPIWrap(MyServlet.class),"/*");
147 // call initialize after start
149 ContextHandler ch = new ServletContextHandler();
150 ch.setContextPath("/caditest");
151 ch.setHandler(shand);
152 for( Entry<Object,Object> es : props.entrySet()) {
153 ch.getInitParams().put(es.getKey().toString(), es.getValue().toString());
155 //ch.setErrorHandler(new MyErrorHandler());
157 // Create Server and Add Context Handler
158 final Server server = new Server();
159 ServerConnector http = new ServerConnector(server);
161 server.addConnector(http);
162 server.setHandler(ch);
168 System.out.println("To test, put http://"+ hostname + ':' + port + "/caditest/testme in a browser or 'curl'");
169 // if we were really a server, we'd block the main thread with this join...
171 // But... since we're a test service, we'll block on StdIn
172 System.out.println("Press <Return> to end service...");
175 System.out.println("All done, have a good day!");
176 } catch (Exception e) {
181 @RolesAllowed({"org.onap.aaf.myapp.myPerm|myInstance|myAction"})
182 public static class MyServlet implements Servlet {
183 private ServletConfig servletConfig;
185 public void init(ServletConfig config) throws ServletException {
186 servletConfig = config;
189 public ServletConfig getServletConfig() {
190 return servletConfig;
193 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
194 HttpServletRequest request;
196 request = (HttpServletRequest)req;
197 } catch (ClassCastException e) {
198 throw new ServletException("Only serving HTTP today",e);
201 res.getOutputStream().print("<html><header><title>CSP Servlet Test</title></header><body><h1>You're good to go!</h1><pre>" +
202 request.getUserPrincipal());
204 String perm = request.getParameter("PERM");
206 if(request.isUserInRole(perm)) {
207 if(perm.indexOf('|')<0)
208 res.getOutputStream().print("\nCongrats!, You are in Role " + perm);
210 res.getOutputStream().print("\nCongrats!, You have Permission " + perm);
212 if(perm.indexOf('|')<0)
213 res.getOutputStream().print("\nSorry, you are NOT in Role " + perm);
215 res.getOutputStream().print("\nSorry, you do NOT have Permission " + perm);
218 res.getOutputStream().print("</pre></body></html>");
222 public String getServletInfo() {
226 public void destroy() {
231 Java Direct (AAFLur) Method
232 ===========================
233 The AAFLur is the exact component used within all the Plugins mentioned above. It is written so that it can be called standalone as well, see the Example as follows
234 package org.onap.aaf.example;
236 import java.util.ArrayList;
237 import java.util.List;
238 import java.util.Properties;
240 import org.onap.aaf.cadi.Access;
241 import org.onap.aaf.cadi.Permission;
242 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
243 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
244 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
245 import org.onap.aaf.cadi.config.Config;
246 import org.onap.aaf.cadi.lur.aaf.AAFPermission;
247 import org.onap.aaf.cadi.lur.aaf.test.TestAccess;
249 public class ExamplePerm2_0 {
250 public static void main(String args[]) {
251 // Normally, these should be set in environment. Setting here for clarity
252 Properties props = System.getProperties();
253 props.setProperty("AFT_LATITUDE", "32.780140");
254 props.setProperty("AFT_LONGITUDE", "-96.800451");
255 props.setProperty("AFT_ENVIRONMENT", "AFTUAT");
256 props.setProperty(Config.AAF_URL,
257 "https://DME2RESOLVE/service=org.onap.aaf.authz.AuthorizationService/version=2.0/envContext=TEST/routeOffer=BAU_SE"
259 props.setProperty(Config.AAF_USER_EXPIRES,Integer.toString(5*60000)); // 5 minutes for found items to live in cache
260 props.setProperty(Config.AAF_HIGH_COUNT,Integer.toString(400)); // Maximum number of items in Cache);
261 props.setProperty(Config.CADI_KEYFILE,"keyfile"); //Note: Be sure to generate with java -jar <cadi_path>/lib/cadi-core*.jar keygen keyfile
262 // props.setProperty("DME2_EP_REGISTRY_CLASS","DME2FS");
263 // props.setProperty("AFT_DME2_EP_REGISTRY_FS_DIR","../../authz/dme2reg");
266 // Link or reuse to your Logging mechanism
267 Access myAccess = new TestAccess(); //
271 AAFCon<?> con = new AAFConDME2(myAccess);
273 // AAFLur has pool of DME clients as needed, and Caches Client lookups
274 AAFLurPerm aafLur = con.newLur();
275 // Note: If you need both Authn and Authz construct the following:
276 AAFAuthn<?> aafAuthn = con.newAuthn(aafLur);
278 // Do not set Mech ID until after you construct AAFAuthn,
279 // because we initiate "401" info to determine the Realm of
280 // of the service we're after.
281 con.basicAuth("xxxx@aaf.abc.com", "XXXXXX");
285 // Normally, you obtain Principal from Authentication System.
286 // For J2EE, you can ask the HttpServletRequest for getUserPrincipal()
287 // If you use CADI as Authenticator, it will get you these Principals from
288 // CSP or BasicAuth mechanisms.
289 String id = "xxxx@aaf.abc.com"; //"cluster_admin@gridcore.abc.com";
291 // If Validate succeeds, you will get a Null, otherwise, you will a String for the reason.
292 String ok = aafAuthn.validate(id, "XXXXXX");
293 if(ok!=null)System.out.println(ok);
295 ok = aafAuthn.validate(id, "wrongPass");
296 if(ok!=null)System.out.println(ok);
299 // AAF Style permissions are in the form
300 // Type, Instance, Action
301 AAFPermission perm = new AAFPermission("org.onap.aaf.grid.core.coh",":dev_cluster", "WRITE");
303 // Now you can ask the LUR (Local Representative of the User Repository about Authorization
304 // With CADI, in J2EE, you can call isUserInRole("org.onap.aaf.mygroup|mytype|write") on the Request Object
305 // instead of creating your own LUR
306 System.out.println("Does " + id + " have " + perm);
307 if(aafLur.fish(id, perm)) {
308 System.out.println("Yes, you have permission");
310 System.out.println("No, you don't have permission");
313 System.out.println("Does Bogus have " + perm);
314 if(aafLur.fish("Bogus", perm)) {
315 System.out.println("Yes, you have permission");
317 System.out.println("No, you don't have permission");
320 // Or you can all for all the Permissions available
321 List<Permission> perms = new ArrayList<Permission>();
323 aafLur.fishAll(id,perms);
324 for(Permission prm : perms) {
325 System.out.println(prm.getKey());
328 // It might be helpful in some cases to clear the User's identity from the Cache
333 } catch (Exception e) {
340 There are two current AAF Lurs which you can utilize:
341 • Org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm is the default, and will fish based on the Three-fold "Permission" standard in AAF
342 To run this code, you will need from a SWM deployment (org.onap.aaf.cadi:cadi, then soft link to jars needed):
343 • cadi-core-<version>.jar
344 • cadi-aaf-<version>-full.jar
347 <groupId>org.onap.aaf.cadi</groupId>
348 <artifactId>aaf-cadi-aaf</artifactId>
349 <version>THE_LATEST_VERSION</version>
350 <classifier>full</classifier>