[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / main / java / org / onap / dmaap / dbcapi / authentication / AafLurAndFish.java
1 /*-
2  * ============LICENSE_START=======================================================
3   * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2018 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 package org.onap.dmaap.dbcapi.authentication;
22
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.util.Properties;
26 import org.onap.aaf.cadi.CadiException;
27 import org.onap.aaf.cadi.LocatorException;
28 import org.onap.aaf.cadi.PropAccess;
29 import org.onap.aaf.misc.env.APIException;
30 import org.onap.dmaap.dbcapi.aaf.AafLurService;
31 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
32 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
33 import org.onap.dmaap.dbcapi.util.DmaapConfig;
34
35
36 public class AafLurAndFish extends BaseLoggingClass implements ApiAuthorizationCheckInterface {
37         private AafLurService svc;
38         private static String apiNamespace;
39         private static final String ERROR="Error";
40
41         AafLurAndFish()  throws AuthenticationErrorException  {
42
43                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
44                 apiNamespace = p.getProperty( "ApiNamespace", "org.onap.dmaap-bc.api");
45
46                 String cadiprop = p.getProperty( "cadi.properties", "/opt/app/osaaf/local/org.onap.dmaap-bc.props");
47                 logger.info( "cadiprops in " + cadiprop );
48                 Properties props = new Properties();
49                 try {
50                         FileInputStream fis = new FileInputStream( cadiprop );
51                         try {
52                                 props.load( fis );
53                         } finally {
54                                 fis.close();
55                         }
56                 } catch ( IOException e ) {
57                         errorLogger.error( "Unable to load " + cadiprop );
58                         errorLogger.error(ERROR, e);
59                         throw new AuthenticationErrorException( );
60                 }
61                 try {
62                         PropAccess myAccess = new PropAccess( props );
63
64                         svc =  AafLurService.getInstance(myAccess);
65                 } catch (APIException | CadiException | LocatorException e ) {
66                         errorLogger.error(ERROR, e);
67                         errorLogger.error( e.toString() );
68                         throw new AuthenticationErrorException();
69                 }
70
71         }
72
73         public void check( String mechid, String pwd, DmaapPerm p ) throws AuthenticationErrorException {
74
75                 try {
76                     if (mechid.isEmpty() || pwd.isEmpty()) {
77                 throw new AuthenticationErrorException("No basic authorization value provided");
78             }
79
80                         if (!svc.checkPerm( apiNamespace, mechid, pwd, p )) {
81                                 throw new AuthenticationErrorException();
82                         }
83                 } catch ( IOException | CadiException  e ) {
84                         errorLogger.error(ERROR, e);
85                         errorLogger.error( e.toString() );
86                         throw new AuthenticationErrorException();
87                 }
88
89         }
90
91          public static void main(String[] args) throws Exception {
92                 AafLurAndFish alaf = new AafLurAndFish();
93                 DmaapPerm p = new DmaapPerm( "org.onap.dmaap-bc.api.dmaap", "boot", "GET");
94
95                 try {
96                         alaf.check("mmanager@people.osaaf.org", "demo123456!", p);
97                 } catch (AuthenticationErrorException aee ) {
98                                 errorLogger.error(aee.getMessage());
99                                 errorLogger.error( "Check failed for: " + p.toJSON());
100                         System.exit(-1);
101                 }
102                  errorLogger.info("Check succeeded for: " + p.toJSON());
103             }
104 }