Release patch 2.0.4
[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  * Modifications Copyright (C) 2019 IBM.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.dmaap.dbcapi.aaf;
23
24 import java.io.IOException;
25 import java.security.Principal;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import org.apache.log4j.Logger;
30 import org.onap.aaf.cadi.Access;
31 import org.onap.aaf.cadi.CadiException;
32 import org.onap.aaf.cadi.LocatorException;
33 import org.onap.aaf.cadi.Permission;
34 import org.onap.aaf.cadi.aaf.AAFPermission;
35 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
36 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
37 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
38 import org.onap.aaf.cadi.principal.UnAuthPrincipal;
39 import org.onap.aaf.misc.env.APIException;
40 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
41
42 /*
43  * this service uses the AAF Lur object to lookup identities and perms
44  */
45 public class AafLurService extends BaseLoggingClass {
46
47         static Logger log = Logger.getLogger(AafLurService.class.getName());
48
49
50         private static AAFConHttp aafcon;
51         private static AAFLurPerm aafLur;
52         private static AAFAuthn<?> aafAuthn;
53
54
55         /*
56          * singleton pattern suggested by AAF
57          */
58         private static AafLurService singleton;
59         private AafLurService() {}
60
61
62
63         private static void init( Access myAccess ) throws APIException, CadiException, LocatorException {
64                 appLogger.info( "myAccess=" + myAccess );
65                 try {
66                         aafcon = new AAFConHttp( myAccess );
67                 } catch ( CadiException | LocatorException e) {
68                         appLogger.error( "Failure of AAFConHttp: " + e.getMessage() );
69                         errorLogger.error( "Failure of AAFConHttp: " + e.getMessage() );
70                         log.error(e);
71
72                         throw e;
73                 }
74                 try {
75                         aafLur = aafcon.newLur();
76                 } catch ( CadiException  e) {
77                         appLogger.error( "Failure of newLur(): " + e.getMessage() );
78                         errorLogger.error( "Failure of newLur(): " + e.getMessage() );
79                         log.error(e);
80
81                         throw e;
82                 }
83                 aafAuthn = aafcon.newAuthn( aafLur );
84         }
85
86         public static synchronized AafLurService getInstance( Access myAccess ) throws APIException, CadiException, LocatorException{
87                 if ( singleton == null ) {
88                         singleton = new AafLurService();
89                         try {
90                                 init( myAccess );
91                         } catch (APIException | CadiException | LocatorException e) {
92
93                                 log.error(e);
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<>();
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                 return rc;
143
144         }
145 }