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