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_Lur2_0Call.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 static org.junit.Assert.assertEquals;
25
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.io.UnsupportedEncodingException;
29 import java.net.HttpURLConnection;
30 import java.security.Principal;
31 import java.util.Collection;
32 import java.util.Enumeration;
33 import java.util.Locale;
34 import java.util.Map;
35
36 import javax.servlet.AsyncContext;
37 import javax.servlet.DispatcherType;
38 import javax.servlet.RequestDispatcher;
39 import javax.servlet.ServletContext;
40 import javax.servlet.ServletException;
41 import javax.servlet.ServletInputStream;
42 import javax.servlet.ServletRequest;
43 import javax.servlet.ServletResponse;
44 import javax.servlet.http.Cookie;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47 import javax.servlet.http.HttpSession;
48 import javax.servlet.http.Part;
49
50 import org.junit.BeforeClass;
51 import org.junit.Test;
52 import org.onap.aaf.cadi.CadiException;
53 import org.onap.aaf.cadi.Lur;
54 import org.onap.aaf.cadi.Permission;
55 import org.onap.aaf.cadi.PropAccess;
56 import org.onap.aaf.cadi.Symm;
57 import org.onap.aaf.cadi.Taf.LifeForm;
58 import org.onap.aaf.cadi.aaf.AAFPermission;
59 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
60 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
61 import org.onap.aaf.cadi.aaf.v2_0.AAFTaf;
62 import org.onap.aaf.cadi.locator.DNSLocator;
63 import org.onap.aaf.cadi.lur.ConfigPrincipal;
64 import org.onap.aaf.cadi.lur.LocalPermission;
65 import org.onap.aaf.cadi.taf.TafResp;
66
67 public class JU_Lur2_0Call {
68         private static AAFConHttp aaf;
69         private static PropAccess access;
70
71         @BeforeClass
72         public static void setUpBeforeClass() throws Exception {
73                 access = new PropAccess();
74                 aaf = new AAFConHttp(access,new DNSLocator(access,"https","localhost","8100"));
75                 aaf.basicAuth("testid", "whatever");
76         }
77
78         @Test 
79         public void test() throws Exception {
80         
81                 AAFLurPerm aafLur = aaf.newLur();
82
83                 Principal pri = new ConfigPrincipal("testid@aaf.att.com","whatever");
84                 for (int i = 0; i < 10; ++i) {
85                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|myInstance|write"),true);
86                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|kumquat|write"),false);
87                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|myInstance|read"),true);
88                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|kumquat|read"),true);
89                         
90                         print(aafLur, pri, new AAFPermission("com.test.JU_Lur2_0Call.service","myInstance","write"),true);
91                         print(aafLur, pri, new AAFPermission("com.test.JU_Lur2_0Call.service","kumquat","write"),false);
92                         print(aafLur, pri, new AAFPermission("com.test.JU_Lur2_0Call.service","myInstance","read"),true);
93                         print(aafLur, pri, new AAFPermission("com.test.JU_Lur2_0Call.service","kumquat","read"),true);
94
95                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|!kum.*|read"),true);
96                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|myInstance|!wr*"),true);
97                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|myInstance"),true);
98
99                         print(aafLur, pri, new AAFPermission("com.test.JU_Lur2_0Call.service","!kum.*","read"),true);
100                         print(aafLur, pri, new AAFPermission("com.test.JU_Lur2_0Call.service","myInstance","!wr*"),true);
101
102                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|!kum[Qq]uat|read"),true);
103                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|!my[iI]nstance|!wr*"),true);
104                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|!my[iI]nstance|!wr*"),true);
105                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|myInstance|!wr*"),true);
106
107                         print(aafLur, pri, new AAFPermission("com.test.JU_Lur2_0Call.service","!kum[Qq]uat","read"),true);
108                         print(aafLur, pri, new AAFPermission("com.test.JU_Lur2_0Call.service","!my[iI]nstance","!wr*"),true);
109                         print(aafLur, pri, new AAFPermission("com.test.JU_Lur2_0Call.service","!my[iI]nstance","!wr*"),true);
110                         print(aafLur, pri, new AAFPermission("com.test.JU_Lur2_0Call.service","myInstance","!wr*"),true);
111                         
112
113                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|!my.nstance|!wr*"),true);
114                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|my.nstance|!wr*"),false);
115                         
116                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|my.nstance|!wr*"),false);
117                         
118                         //Maitrayee, aren't we going to have issues if we do RegExp with "."?
119                         //Is it too expensive to only do Reg Ex in presence of special characters, []{}*, etc? Not sure this helps for GRID.
120                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|kum.quat|read"),true);
121                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|!kum..uat|read"),true);
122                         
123                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|myInstance"),true); // ok if Stored Action is "*"
124                         
125                         // Key Evaluations
126                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|:myCluster:*:!my.*|write"),true); // ok if Stored Action is "*"
127                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|:myCluster:*|write"),false); // not ok if key lengths don't match "*"
128                         print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|:myCluster:*:myCF|write"),true); // ok if Stored Action is "*"
129                         print(aafLur, pri, new AAFPermission("com.test.JU_Lur2_0Call.service",":myCluster:*:!my.*","write"),true); // ok if Stored Action is "*"
130                         print(aafLur, pri, new AAFPermission("com.test.JU_Lur2_0Call.service",":myCluster:*:myCF","write"),true); // ok if Stored Action is "*"
131                         print(aafLur, pri, new AAFPermission("com.test.JU_Lur2_0Call.service",":myCluster:*","write"),false); // not ok if key lengths don't match
132                         
133                 }
134
135                 print(aafLur, pri, new LocalPermission("bogus"),false);
136
137 //              try {
138 //                      Thread.sleep(7000);
139 //              } catch (InterruptedException e) {
140 //                      e.printStackTrace();
141 //              }
142                 for (int i = 0; i < 10; ++i)
143                         print(aafLur, pri, new LocalPermission("supergroup"),false);
144
145                 System.out.println("All Done");
146         }
147         @Test
148         public void testTaf() throws Exception {
149                 AAFTaf<?> aaft = new AAFTaf<HttpURLConnection>(aaf,true);
150                 
151                 TafResp resp;
152                 // No Header
153                 resp = aaft.validate(LifeForm.CBLF, new Req(), null);
154                 assertEquals(TafResp.RESP.TRY_AUTHENTICATING, resp.isAuthenticated());
155
156                 String auth = "Basic " + Symm.base64.encode("testid:whatever");
157                 resp = aaft.validate(LifeForm.CBLF, new Req("Authorization",auth), null);
158                 assertEquals(TafResp.RESP.IS_AUTHENTICATED, resp.isAuthenticated());
159                 
160         }
161 //      @Test
162 //      public void testRole() throws CadiException {
163 //              TestAccess ta = new TestAccess();
164 //              AAFLurRole1_0 aafLur = new AAFLurRole1_0(
165 //                              ta,
166 ////                            "http://DME2RESOLVE/service=com.att.authz.AuthorizationService/version=1.0.0/envContext=UAT/routeOffer=BAU_SE",
167 //                              "http://DME2RESOLVE/service=com.att.authz.AuthorizationService/version=1.0.0/envContext=DEV/routeOffer=D1",
168 //                              "m12345", "m12345pass", 50000, // dme Time
169 //                              // 5*60000); // 5 minutes User Expiration
170 //                              50000, // 5 seconds after Expiration
171 //                              200); // High Count of items.. These do not take much memory
172 //
173 //              Principal pri = new ConfigPrincipal("xy1234","whatever");
174 //              for (int i = 0; i < 10; ++i) {
175 ////                    print(aafLur, pri, new LocalPermission("*|*|*|com.att.authz"));
176 //                      print(aafLur, pri, new LocalPermission("service|myInstance|write"),false);
177 //                      print(aafLur, pri, new LocalPermission("com.test.JU_Lur2_0Call.service|myInstance|write"),false);
178 //                      print(aafLur, pri, new LocalPermission("org.osaaf.cadi"),true);
179 //                      print(aafLur, pri, new LocalPermission("global"),true);
180 //                      print(aafLur, pri, new LocalPermission("kumquat"),false);
181 //              }
182 //
183 //              print(aafLur, pri, new LocalPermission("bogus"),false);
184 //
185 //              for (int i = 0; i < 10; ++i)
186 //                      print(aafLur, pri, new LocalPermission("supergroup"),false);
187 //
188 //              System.out.println("All Done");
189 //      }
190
191
192         private void print(Lur aafLur, Principal pri, Permission perm, boolean shouldBe)
193                         throws CadiException {
194                 long start = System.nanoTime();
195         
196                 // The Call
197                 boolean ok = aafLur.fish(pri, perm);
198         
199                 assertEquals(shouldBe,ok);
200                 float ms = (System.nanoTime() - start) / 1000000f;
201                 if (ok) {
202                         System.out.println("Yes, part of " + perm.getKey() + " (" + ms
203                                         + "ms)");
204                 } else {
205                         System.out.println("No, not part of " + perm.getKey() + " (" + ms
206                                         + "ms)");
207                 }
208         }
209
210         @SuppressWarnings("rawtypes")
211         public class Req implements HttpServletRequest {
212                 private String[] headers;
213
214                 public Req(String ... headers) {
215                         this.headers = headers;
216                 }
217
218                 public Object getAttribute(String name) {
219                         // TODO Auto-generated method stub
220                         return null;
221                 }
222
223                 @SuppressWarnings("unchecked")
224                 public Enumeration getAttributeNames() {
225                         // TODO Auto-generated method stub
226                         return null;
227                 }
228
229                 public String getCharacterEncoding() {
230                         // TODO Auto-generated method stub
231                         return null;
232                 }
233
234                 public void setCharacterEncoding(String env)
235                                 throws UnsupportedEncodingException {
236                         // TODO Auto-generated method stub
237                         
238                 }
239
240                 public int getContentLength() {
241                         // TODO Auto-generated method stub
242                         return 0;
243                 }
244
245                 public String getContentType() {
246                         // TODO Auto-generated method stub
247                         return null;
248                 }
249
250                 public ServletInputStream getInputStream() throws IOException {
251                         // TODO Auto-generated method stub
252                         return null;
253                 }
254
255                 public String getParameter(String name) {
256                         // TODO Auto-generated method stub
257                         return null;
258                 }
259
260                 @SuppressWarnings("unchecked")
261                 public Enumeration getParameterNames() {
262                         // TODO Auto-generated method stub
263                         return null;
264                 }
265
266                 public String[] getParameterValues(String name) {
267                         // TODO Auto-generated method stub
268                         return null;
269                 }
270
271                 @SuppressWarnings("unchecked")
272                 public Map getParameterMap() {
273                         // TODO Auto-generated method stub
274                         return null;
275                 }
276
277                 public String getProtocol() {
278                         // TODO Auto-generated method stub
279                         return null;
280                 }
281
282                 public String getScheme() {
283                         // TODO Auto-generated method stub
284                         return null;
285                 }
286
287                 public String getServerName() {
288                         // TODO Auto-generated method stub
289                         return null;
290                 }
291
292                 public int getServerPort() {
293                         // TODO Auto-generated method stub
294                         return 0;
295                 }
296
297                 public BufferedReader getReader() throws IOException {
298                         // TODO Auto-generated method stub
299                         return null;
300                 }
301
302                 public String getRemoteAddr() {
303                         // TODO Auto-generated method stub
304                         return null;
305                 }
306
307                 public String getRemoteHost() {
308                         // TODO Auto-generated method stub
309                         return null;
310                 }
311
312                 public void setAttribute(String name, Object o) {
313                         // TODO Auto-generated method stub
314                         
315                 }
316
317                 public void removeAttribute(String name) {
318                         // TODO Auto-generated method stub
319                         
320                 }
321
322                 public Locale getLocale() {
323                         // TODO Auto-generated method stub
324                         return null;
325                 }
326
327                 @SuppressWarnings("unchecked")
328                 public Enumeration getLocales() {
329                         // TODO Auto-generated method stub
330                         return null;
331                 }
332
333                 public boolean isSecure() {
334                         // TODO Auto-generated method stub
335                         return false;
336                 }
337
338                 public RequestDispatcher getRequestDispatcher(String path) {
339                         // TODO Auto-generated method stub
340                         return null;
341                 }
342
343                 public String getRealPath(String path) {
344                         // TODO Auto-generated method stub
345                         return null;
346                 }
347
348                 public int getRemotePort() {
349                         // TODO Auto-generated method stub
350                         return 0;
351                 }
352
353                 public String getLocalName() {
354                         // TODO Auto-generated method stub
355                         return null;
356                 }
357
358                 public String getLocalAddr() {
359                         // TODO Auto-generated method stub
360                         return null;
361                 }
362
363                 public int getLocalPort() {
364                         // TODO Auto-generated method stub
365                         return 0;
366                 }
367
368                 public String getAuthType() {
369                         // TODO Auto-generated method stub
370                         return null;
371                 }
372
373                 public Cookie[] getCookies() {
374                         // TODO Auto-generated method stub
375                         return null;
376                 }
377
378                 public long getDateHeader(String name) {
379                         // TODO Auto-generated method stub
380                         return 0;
381                 }
382
383                 public String getHeader(String name) {
384                         for(int i=1;i<headers.length;i=i+2) {
385                                 if(headers[i-1].equals(name)) return headers[i];
386                         }
387                         return null;
388                 }
389
390                 @SuppressWarnings("unchecked")
391                 public Enumeration getHeaders(String name) {
392                         // TODO Auto-generated method stub
393                         return null;
394                 }
395
396                 @SuppressWarnings("unchecked")
397                 public Enumeration getHeaderNames() {
398                         // TODO Auto-generated method stub
399                         return null;
400                 }
401
402                 public int getIntHeader(String name) {
403                         // TODO Auto-generated method stub
404                         return 0;
405                 }
406
407                 public String getMethod() {
408                         // TODO Auto-generated method stub
409                         return null;
410                 }
411
412                 public String getPathInfo() {
413                         // TODO Auto-generated method stub
414                         return null;
415                 }
416
417                 public String getPathTranslated() {
418                         // TODO Auto-generated method stub
419                         return null;
420                 }
421
422                 public String getContextPath() {
423                         // TODO Auto-generated method stub
424                         return null;
425                 }
426
427                 public String getQueryString() {
428                         // TODO Auto-generated method stub
429                         return null;
430                 }
431
432                 public String getRemoteUser() {
433                         // TODO Auto-generated method stub
434                         return null;
435                 }
436
437                 public boolean isUserInRole(String role) {
438                         // TODO Auto-generated method stub
439                         return false;
440                 }
441
442                 public Principal getUserPrincipal() {
443                         // TODO Auto-generated method stub
444                         return null;
445                 }
446
447                 public String getRequestedSessionId() {
448                         // TODO Auto-generated method stub
449                         return null;
450                 }
451
452                 public String getRequestURI() {
453                         // TODO Auto-generated method stub
454                         return null;
455                 }
456
457                 public StringBuffer getRequestURL() {
458                         // TODO Auto-generated method stub
459                         return null;
460                 }
461
462                 public String getServletPath() {
463                         // TODO Auto-generated method stub
464                         return null;
465                 }
466
467                 public HttpSession getSession(boolean create) {
468                         // TODO Auto-generated method stub
469                         return null;
470                 }
471
472                 public HttpSession getSession() {
473                         // TODO Auto-generated method stub
474                         return null;
475                 }
476
477                 public boolean isRequestedSessionIdValid() {
478                         // TODO Auto-generated method stub
479                         return false;
480                 }
481
482                 public boolean isRequestedSessionIdFromCookie() {
483                         // TODO Auto-generated method stub
484                         return false;
485                 }
486
487                 public boolean isRequestedSessionIdFromURL() {
488                         // TODO Auto-generated method stub
489                         return false;
490                 }
491
492                 public boolean isRequestedSessionIdFromUrl() {
493                         // TODO Auto-generated method stub
494                         return false;
495                 }
496
497                 @Override
498                 public ServletContext getServletContext() {
499                         // TODO Auto-generated method stub
500                         return null;
501                 }
502
503                 @Override
504                 public AsyncContext startAsync() throws IllegalStateException {
505                         // TODO Auto-generated method stub
506                         return null;
507                 }
508
509                 @Override
510                 public AsyncContext startAsync(ServletRequest servletRequest,
511                                 ServletResponse servletResponse) throws IllegalStateException {
512                         // TODO Auto-generated method stub
513                         return null;
514                 }
515
516                 @Override
517                 public boolean isAsyncStarted() {
518                         // TODO Auto-generated method stub
519                         return false;
520                 }
521
522                 @Override
523                 public boolean isAsyncSupported() {
524                         // TODO Auto-generated method stub
525                         return false;
526                 }
527
528                 @Override
529                 public AsyncContext getAsyncContext() {
530                         // TODO Auto-generated method stub
531                         return null;
532                 }
533
534                 @Override
535                 public DispatcherType getDispatcherType() {
536                         // TODO Auto-generated method stub
537                         return null;
538                 }
539
540                 @Override
541                 public boolean authenticate(HttpServletResponse response)
542                                 throws IOException, ServletException {
543                         // TODO Auto-generated method stub
544                         return false;
545                 }
546
547                 @Override
548                 public void login(String username, String password)
549                                 throws ServletException {
550                         // TODO Auto-generated method stub
551                         
552                 }
553
554                 @Override
555                 public void logout() throws ServletException {
556                         // TODO Auto-generated method stub
557                         
558                 }
559
560                 @Override
561                 public Collection<Part> getParts() throws IOException, ServletException {
562                         // TODO Auto-generated method stub
563                         return null;
564                 }
565
566                 @Override
567                 public Part getPart(String name) throws IOException, ServletException {
568                         // TODO Auto-generated method stub
569                         return null;
570                 }
571                 
572         }
573 }