Refactor Api Auth for AAF
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / aaf / AafLurService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 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 package org.onap.dmaap.dbcapi.aaf;
22
23 import java.io.IOException;
24 import java.security.Principal;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.apache.log4j.Logger;
29 import org.onap.aaf.cadi.Access;
30 import org.onap.aaf.cadi.CadiException;
31 import org.onap.aaf.cadi.LocatorException;
32 import org.onap.aaf.cadi.Permission;
33 import org.onap.aaf.cadi.aaf.AAFPermission;
34 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
35 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
36 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
37 import org.onap.aaf.cadi.principal.UnAuthPrincipal;
38 import org.onap.aaf.misc.env.APIException;
39 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
40 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
41 import org.onap.dmaap.dbcapi.util.DmaapConfig;
42
43 /*
44  * this service uses the AAF Lur object to lookup identities and perms
45  */
46 public class AafLurService extends BaseLoggingClass {
47
48         
49          private static AAFConHttp aafcon;
50          private static AAFLurPerm aafLur;
51          private static AAFAuthn<?> aafAuthn;
52
53         
54         /*
55          * singleton pattern suggested by AAF
56          */
57         private static AafLurService singleton;
58         private AafLurService() {}
59
60
61         
62         private static void init( Access myAccess ) throws APIException, CadiException, LocatorException {
63                 appLogger.info( "myAccess=" + myAccess );
64                 try {
65                         aafcon = new AAFConHttp( myAccess );
66                 } catch ( CadiException | LocatorException e) {
67                         appLogger.error( "Failure of AAFConHttp: " + e.getMessage() );
68                         errorLogger.error( "Failure of AAFConHttp: " + e.getMessage() );
69                         e.printStackTrace();
70                         throw e;
71                 } 
72                 try {
73                         aafLur = aafcon.newLur();
74                 } catch ( CadiException  e) {
75                         appLogger.error( "Failure of newLur(): " + e.getMessage() );
76                         errorLogger.error( "Failure of newLur(): " + e.getMessage() );
77                         e.printStackTrace();
78                         throw e;
79                 } 
80                 aafAuthn = aafcon.newAuthn( aafLur ); 
81         }
82         
83         public static synchronized AafLurService getInstance( Access myAccess ) throws APIException, CadiException, LocatorException{
84                 if ( singleton == null ) {
85                         singleton = new AafLurService();
86                         try {
87                                 init( myAccess );
88                         } catch (APIException | CadiException | LocatorException e) {
89                                 // TODO Auto-generated catch block
90                                 e.printStackTrace();
91                                 throw e;
92                         } 
93                         
94                 }
95                 return singleton;
96         }
97         
98
99         public boolean checkPerm(String ns, String fqi, String pwd, DmaapPerm p) throws IOException, CadiException {
100
101                 boolean rc = false;
102                 
103                 if ( aafAuthn == null ) {
104                         appLogger.error( "AafLurService: aafAuthn not set as expected.");
105                         return rc;
106                 }
107                 
108                 String ok = aafAuthn.validate( fqi,  pwd );
109                 if ( ok != null ) {
110                         appLogger.info( "FAILED validation of fqi=" + fqi + "with response:" + ok );
111                         return rc;
112                 }       
113                 
114                 Principal principal = new UnAuthPrincipal( fqi );
115                 // if we pass ns as first arg to AAFPermission constructor it gets prpended to the instance...
116                 // as in ns|instance|type|action.   we don't want that.
117                 Permission aafPerm = new AAFPermission( null, p.getPermission(), p.getPtype(), p.getAction());
118                 if ( aafLur == null ) {
119                         appLogger.error( "AafLurService: aafLur not set as expected.");
120                         return rc;
121                 }
122                 rc =  aafLur.fish( principal, aafPerm );
123                 if (rc == true ) return rc;
124                 
125                 List<Permission> perms = new ArrayList<Permission>();
126                 aafLur.fishAll( principal,  perms);
127                 String key = aafPerm.getKey();
128                 for ( Permission prm: perms ) {
129                         if ( prm.getKey().equals( key )) {
130                                 appLogger.info( principal + " has MATCHING perm " + prm.getKey() );
131                         } else {
132                                 appLogger.info( principal + " has non-matching perm " + prm.getKey() );
133                         }
134                 }
135                 
136                 
137                 return rc;
138         
139                 
140         }
141 }