Improved Perm and Grant logging
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / aaf / AafService.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 org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
24 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
25 import org.onap.dmaap.dbcapi.util.DmaapConfig;
26
27 /*
28  * this service uses the AAF REST API endpoints to provision values in AAF
29  */
30 public class AafService extends BaseLoggingClass {
31         public enum ServiceType {
32                 AAF_Admin,
33                 AAF_TopicMgr
34         }
35         
36         private AafConnection aaf;
37         private ServiceType ctype;
38         private String aafURL ;
39         private boolean useAAF = false;
40         
41         
42         private String getCred( boolean wPwd ) {
43                 String mechIdProperty = null;
44                 String pwdProperty = null;
45                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
46                 AafDecrypt decryptor = new AafDecrypt();
47
48                 if ( ctype == ServiceType.AAF_Admin ) {
49                          mechIdProperty = "aaf.AdminUser";
50                          pwdProperty = "aaf.AdminPassword";
51                 } else if ( ctype == ServiceType.AAF_TopicMgr ){        
52                          mechIdProperty = "aaf.TopicMgrUser";
53                          pwdProperty = "aaf.TopicMgrPassword";
54                 } else {
55                         logger.error( "Unexpected case for AAF credential type: " + ctype );
56                         return null;
57                 }
58                 String user = p.getProperty( mechIdProperty, "noMechId@domain.netset.com" );
59
60                 String pwd = "";
61                 String encPwd = p.getProperty( pwdProperty, "notSet" );
62
63                 
64                 pwd = decryptor.decrypt(encPwd);
65                 
66                 if ( wPwd ) {
67                         return user + ":" + pwd;
68                 } else {
69                         return user;
70                 }
71                 
72                 
73         }
74         
75         public AafService(ServiceType t ) {
76                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
77                 aafURL = p.getProperty( "aaf.URL", "https://authentication.domain.netset.com:8095/proxy/");
78                 initAafService( t );
79         }
80         public AafService( ServiceType t, String url ) {
81                 aafURL = url;
82                 initAafService( t );
83         }
84                 
85         private void initAafService( ServiceType t ) {
86                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
87                 useAAF= "true".equalsIgnoreCase(p.getProperty("UseAAF", "false"));
88                 logger.info( "AafService initAafService: useAAF=" + useAAF);
89                 
90                 ctype = t;
91                 aaf = new AafConnection( getCred( true ) );
92         }
93         
94         public int addPerm(DmaapPerm perm) {
95
96                 int rc = -1;
97                 logger.info( "entry: addPerm() "  );
98                 String pURL = aafURL + "authz/perm";
99                 logger.info( "addPerm=" + useAAF );
100                 if ( useAAF ) {
101                         logger.info( "addPerm: " + perm.toJSON());
102                         rc = aaf.postAaf( perm, pURL );
103                 } else {
104                         rc = 201;
105                 }
106         switch( rc ) {
107         case 401:
108         case 403:
109                 errorLogger.error(DmaapbcLogMessageEnum.AAF_CREDENTIAL_ERROR,  getCred( false ) );
110                 System.exit(1);
111         case 409:
112                 logger.warn( "Perm already exists. Possible conflict.");
113                 break;
114                 
115         case 201:
116                 logger.info( "expected response: " + rc);
117                 break;
118         default :
119                 logger.error( "Unexpected response: " + rc );
120                 break;
121         }
122                 
123                 return rc;
124         }
125         public int addGrant(DmaapGrant grant ) {
126
127                 int rc = -1;
128                 logger.info( "entry: addGrant() "  );
129
130                 String pURL = aafURL + "authz/role/perm";
131                 logger.info( "addGrant: useAAF=" + useAAF );
132                 if ( useAAF ) {
133                         logger.info( "addGrant: " + grant.toJSON() );
134                         rc = aaf.postAaf( grant, pURL );
135                 } else {
136                         rc = 201;
137                 }
138                 
139         switch( rc ) {
140         case 401:
141         case 403:
142                 errorLogger.error(DmaapbcLogMessageEnum.AAF_CREDENTIAL_ERROR,  getCred( false ) );
143                 System.exit(1);
144                 break;
145
146         case 409:
147                 logger.warn( "Perm already exists. Possible conflict.");
148                 break;
149                 
150         case 201:
151                 logger.info( "expected response" );
152                 break;
153         default :
154                 logger.error( "Unexpected response: " + rc );
155                 break;
156         }
157                 
158                 return rc;
159         }
160
161         public int delGrant( DmaapGrant grant ) {
162                 int rc = -1;
163                 logger.info( "entry: delGrant() "  );
164
165                 String pURL = aafURL + "authz/role/:" + grant.getRole() + "/perm";
166                 
167                 if ( useAAF ) {
168                         rc = aaf.delAaf( grant, pURL );
169                 } else {
170                         rc = 200;
171                 }
172         switch( rc ) {
173         case 401:
174         case 403:
175                 errorLogger.error(DmaapbcLogMessageEnum.AAF_CREDENTIAL_ERROR,  getCred( false ) );
176                 System.exit(1);
177                 break;
178  
179         case 404:
180                 logger.warn( "Perm not found...ignore");
181                 break;
182                 
183         case 200:
184                 logger.info( "expected response" );
185                 break;
186         default :
187                 logger.error( "Unexpected response: " + rc );
188                 break;
189         }
190                 
191                 return rc;
192         }
193
194
195 }