AaflurService.java: Fixed sonar issues
[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
41 /*
42  * this service uses the AAF Lur object to lookup identities and perms
43  */
44 public class AafLurService extends BaseLoggingClass {
45         
46         static Logger logger = Logger.getLogger(AafLurService.class.getName());
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                         logger.error(e);
70                         e.printStackTrace();
71                         throw e;
72                 } 
73                 try {
74                         aafLur = aafcon.newLur();
75                 } catch ( CadiException  e) {
76                         appLogger.error( "Failure of newLur(): " + e.getMessage() );
77                         errorLogger.error( "Failure of newLur(): " + e.getMessage() );
78                         logger.error(e);
79                         e.printStackTrace();
80                         throw e;
81                 } 
82                 aafAuthn = aafcon.newAuthn( aafLur ); 
83         }
84         
85         public static synchronized AafLurService getInstance( Access myAccess ) throws APIException, CadiException, LocatorException{
86                 if ( singleton == null ) {
87                         singleton = new AafLurService();
88                         try {
89                                 init( myAccess );
90                         } catch (APIException | CadiException | LocatorException e) {
91                                 // TODO Auto-generated catch block
92                                 logger.error(e);
93                                 e.printStackTrace();
94                                 throw e;
95                         } 
96                         
97                 }
98                 return singleton;
99         }
100         
101
102         public boolean checkPerm(String ns, String fqi, String pwd, DmaapPerm p) throws IOException, CadiException {
103
104                 boolean rc = false;
105                 
106                 if ( aafAuthn == null ) {
107                         appLogger.error( "AafLurService: aafAuthn not set as expected.");
108                         return rc;
109                 }
110                 
111                 String ok = aafAuthn.validate( fqi,  pwd );
112                 if ( ok != null ) {
113                         appLogger.info( "FAILED validation of fqi=" + fqi + "with response:" + ok );
114                         return rc;
115                 }       
116                 
117                 Principal principal = new UnAuthPrincipal( fqi );
118                 // if we pass ns as first arg to AAFPermission constructor it gets prpended to the instance...
119                 // as in ns|instance|type|action.   we don't want that.
120                 Permission aafPerm = new AAFPermission( null, p.getPermission(), p.getPtype(), p.getAction());
121                 if ( aafLur == null ) {
122                         appLogger.error( "AafLurService: aafLur not set as expected.");
123                         return rc;
124                 }
125                 rc =  aafLur.fish( principal, aafPerm );
126                 boolean flag = true;
127                 if (rc == flag ) {
128                         return rc;
129                 }
130                 
131                 List<Permission> perms = new ArrayList<Permission>();
132                 aafLur.fishAll( principal,  perms);
133                 String key = aafPerm.getKey();
134                 for ( Permission prm: perms ) {
135                         if ( prm.getKey().equals( key )) {
136                                 appLogger.info( principal + " has MATCHING perm " + prm.getKey() );
137                         } else {
138                                 appLogger.info( principal + " has non-matching perm " + prm.getKey() );
139                         }
140                 }
141                 
142                 
143                 return rc;
144         
145                 
146         }
147 }