AT&T 2.0.19 Code drop, stage 2
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / lur / aaf / test1 / MultiThreadPermHit.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.cadi.lur.aaf.test1;
23
24 import java.security.Principal;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.onap.aaf.cadi.Permission;
29 import org.onap.aaf.cadi.PropAccess;
30 import org.onap.aaf.cadi.aaf.AAFPermission;
31 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
32 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
33 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
34 import org.onap.aaf.cadi.config.Config;
35 import org.onap.aaf.cadi.locator.PropertyLocator;
36 import org.onap.aaf.stillNeed.TestPrincipal;
37
38 public class MultiThreadPermHit {
39         public static void main(String args[]) {
40                 // Link or reuse to your Logging mechanism
41                 PropAccess myAccess = new PropAccess(); // 
42                 
43                 // 
44                 try {
45                         AAFConHttp con = new AAFConHttp(myAccess,new PropertyLocator("https://mithrilcsp.sbc.com:8100"));
46                         
47                         // AAFLur has pool of DME clients as needed, and Caches Client lookups
48                         final AAFLurPerm aafLur = con.newLur();
49                         aafLur.setDebug("m12345@aaf.att.com");
50
51                         // Note: If you need both Authn and Authz construct the following:
52                         AAFAuthn<?> aafAuthn = con.newAuthn(aafLur);
53                         
54                         // Do not set Mech ID until after you construct AAFAuthn,
55                         // because we initiate  "401" info to determine the Realm of 
56                         // of the service we're after.
57                         final String id = myAccess.getProperty(Config.AAF_APPID,null);
58                         final String pass = myAccess.decrypt(myAccess.getProperty(Config.AAF_APPPASS,null),false);
59                         if(id!=null && pass!=null) {
60                                 try {
61                                         
62                                         // Normally, you obtain Principal from Authentication System.
63         //                              // For J2EE, you can ask the HttpServletRequest for getUserPrincipal()
64         //                              // If you use CADI as Authenticator, it will get you these Principals from
65         //                              // CSP or BasicAuth mechanisms.
66         //                              String id = "cluster_admin@gridcore.att.com";
67         //
68         //                              // If Validate succeeds, you will get a Null, otherwise, you will a String for the reason.
69                                         String ok;
70                                         ok = aafAuthn.validate(id, pass,null /* use AuthzTrans or HttpServlet, if you have it */);
71                                         if(ok!=null) {
72                                                 System.out.println(ok);
73                                         }
74
75                                         List<Permission> pond = new ArrayList<Permission>();
76                                         for(int i=0;i<20;++i) {
77                                                 pond.clear();
78                                                 aafLur.fishAll(new TestPrincipal(i+id), pond);
79                                                 if(ok!=null && i%1000==0) {
80                                                         System.out.println(i + " " + ok);
81                                                 }
82                                         }
83
84                                         for(int i=0;i<1000000;++i) {
85                                                 ok = aafAuthn.validate( i+ id, "wrongPass",null /* use AuthzTrans or HttpServlet, if you have it */);
86                                                 if(ok!=null && i%1000==0) {
87                                                         System.out.println(i + " " + ok);
88                                                 }
89                                         }
90         
91                                         final AAFPermission perm = new AAFPermission("org.osaaf.aaf.access","*","*");
92                                         
93                                         // Now you can ask the LUR (Local Representative of the User Repository about Authorization
94                                         // With CADI, in J2EE, you can call isUserInRole("org.osaaf.mygroup|mytype|write") on the Request Object 
95                                         // instead of creating your own LUR
96                                         final Principal p = new TestPrincipal(id);
97                                         for(int i=0;i<4;++i) {
98                                                 if(aafLur.fish(p, perm)) {
99                                                         System.out.println("Yes, " + id + " has permission for " + perm.getKey());
100                                                 } else {
101                                                         System.out.println("No, " + id + " does not have permission for " + perm.getKey());
102                                                 }
103                                         }
104         
105         
106                                         // Or you can all for all the Permissions available
107                                         List<Permission> perms = new ArrayList<Permission>();
108         
109                                         
110                                         aafLur.fishAll(p,perms);
111                                         System.out.println("Perms for " + id);
112                                         for(Permission prm : perms) {
113                                                 System.out.println(prm.getKey());
114                                         }
115                                         
116                                         System.out.println("Press any key to continue");
117                                         System.in.read();
118                                         
119                                         for(int j=0;j<5;++j) {
120                                                 new Thread(new Runnable() {
121                                                         @Override
122                                                         public void run() {
123                                                                 for(int i=0;i<20;++i) {
124                                                                         if(aafLur.fish(p, perm)) {
125                                                                                 System.out.println("Yes, " + id + " has permission for " + perm.getKey());
126                                                                         } else {
127                                                                                 System.out.println("No, " + id + " does not have permission for " + perm.getKey());
128                                                                         }
129                                                                 }
130                                                         }
131                                                 }).start();
132                                         }
133         
134                                         
135                                 } finally {
136                                         aafLur.destroy();
137                                 }
138                         } else { // checked on IDs
139                                 System.err.println(Config.AAF_APPID + " and/or " + Config.AAF_APPPASS + " are not set.");
140                         }
141                 } catch (Exception e) {
142                         e.printStackTrace();
143                 }
144         }
145 }