AT&T 2.0.19 Code drop, stage 2
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / lur / aaf / test / JU_MultiThreadPermHit.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 2017 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.test;
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 JU_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);
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                                                 Principal p = new TestPrincipal(i+id);
79                                                 aafLur.fishAll(p, pond);
80                                                 if(ok!=null && i%1000==0) {
81                                                         System.out.println(i + " " + ok);
82                                                 }
83                                         }
84
85                                         for(int i=0;i<1000000;++i) {
86                                                 ok = aafAuthn.validate( i+ id, "wrongPass");
87                                                 if(ok!=null && i%1000==0) {
88                                                         System.out.println(i + " " + ok);
89                                                 }
90                                         }
91         
92                                         final AAFPermission perm = new AAFPermission("org.osaaf.aaf.access","*","*");
93                                         
94                                         // Now you can ask the LUR (Local Representative of the User Repository about Authorization
95                                         // With CADI, in J2EE, you can call isUserInRole("org.osaaf.mygroup|mytype|write") on the Request Object 
96                                         // instead of creating your own LUR
97                                         for(int i=0;i<4;++i) {
98                                                 Principal p = new TestPrincipal(i+id);
99
100                                                 if(aafLur.fish(p, perm)) {
101                                                         System.out.println("Yes, " + id + " has permission for " + perm.getKey());
102                                                 } else {
103                                                         System.out.println("No, " + id + " does not have permission for " + perm.getKey());
104                                                 }
105                                         }
106         
107         
108                                         // Or you can all for all the Permissions available
109                                         List<Permission> perms = new ArrayList<Permission>();
110         
111                                         Principal p = new TestPrincipal(id);
112                                         aafLur.fishAll(p,perms);
113                                         System.out.println("Perms for " + id);
114                                         for(Permission prm : perms) {
115                                                 System.out.println(prm.getKey());
116                                         }
117                                         
118                                         System.out.println("Press any key to continue");
119                                         System.in.read();
120                                         
121                                         for(int j=0;j<5;++j) {
122                                                 new Thread(new Runnable() {
123                                                         @Override
124                                                         public void run() {
125                                                                 for(int i=0;i<20;++i) {
126                                                                         Principal p = new TestPrincipal(id);
127                                                                         if(aafLur.fish(p, perm)) {
128                                                                                 System.out.println("Yes, " + id + " has permission for " + perm.getKey());
129                                                                         } else {
130                                                                                 System.out.println("No, " + id + " does not have permission for " + perm.getKey());
131                                                                         }
132                                                                 }
133                                                         }
134                                                 }).start();
135                                         }
136         
137                                         
138                                 } finally {
139                                         aafLur.destroy();
140                                 }
141                         } else { // checked on IDs
142                                 System.err.println(Config.AAF_APPID + " and/or " + Config.AAF_APPPASS + " are not set.");
143                         }
144                 } catch (Exception e) {
145                         e.printStackTrace();
146                 }
147         }
148 }