9d8776ad18245966269c7def24d8a0e1a334834e
[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 String identity;
40         private boolean useAAF = false;
41         
42         
43         
44         public String getIdentity() {
45                 return identity;
46         }
47
48
49         public void setIdentity(String identity) {
50                 this.identity = identity;
51         }
52
53
54         private String getCred( boolean wPwd ) {
55                 String mechIdProperty = null;
56                 String pwdProperty = null;
57                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
58                 AafDecrypt decryptor = new AafDecrypt();
59
60                 if ( ctype == ServiceType.AAF_Admin ) {
61                          mechIdProperty = "aaf.AdminUser";
62                          pwdProperty = "aaf.AdminPassword";
63                 } else if ( ctype == ServiceType.AAF_TopicMgr ){        
64                          mechIdProperty = "aaf.TopicMgrUser";
65                          pwdProperty = "aaf.TopicMgrPassword";
66                 } else {
67                         logger.error( "Unexpected case for AAF credential type: " + ctype );
68                         return null;
69                 }
70                 identity = p.getProperty( mechIdProperty, "noMechId@domain.netset.com" );
71
72                 String pwd = "";
73                 String encPwd = p.getProperty( pwdProperty, "notSet" );
74
75                 
76                 pwd = decryptor.decrypt(encPwd);
77                 
78                 if ( wPwd ) {
79                         return identity + ":" + pwd;
80                 } else {
81                         return identity;
82                 }
83                 
84                 
85         }
86         
87         
88         public AafService(ServiceType t ) {
89                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
90                 aafURL = p.getProperty( "aaf.URL", "https://authentication.domain.netset.com:8100/proxy/");
91                 initAafService( t );
92         }
93         public AafService( ServiceType t, String url ) {
94                 aafURL = url;
95                 initAafService( t );
96         }
97                 
98         private void initAafService( ServiceType t ) {
99                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
100                 useAAF= "true".equalsIgnoreCase(p.getProperty("UseAAF", "false"));
101                 logger.info( "AafService initAafService: useAAF=" + useAAF);
102                 
103                 ctype = t;
104                 aaf = new AafConnection( getCred( true ) );
105         }
106         
107         public int addPerm(DmaapPerm perm) {
108                 logger.info( "entry: addPerm() "  );
109                 return doPost( perm, "authz/perm", 201);
110         }
111         public int addGrant(DmaapGrant grant ) {
112                 logger.info( "entry: addGrant() "  );
113                 return doPost( grant, "authz/role/perm", 201 );
114         }
115         public int addUserRole( AafUserRole ur ) {
116                 logger.info( "entry: addUserRole() "  );
117                 return doPost( ur, "authz/userRole", 201 );
118         }
119
120         public int delGrant( DmaapGrant grant ) {
121                 int rc = -1;
122                 logger.info( "entry: delGrant() "  );
123
124                 String pURL = aafURL + "authz/role/:" + grant.getRole() + "/perm";
125                 
126                 if ( useAAF ) {
127                         rc = aaf.delAaf( grant, pURL );
128                 } else {
129                         rc = 200;
130                 }
131         switch( rc ) {
132         case 401:
133         case 403:
134                 errorLogger.error(DmaapbcLogMessageEnum.AAF_CREDENTIAL_ERROR,  getCred( false ) );
135                 System.exit(1);
136                 break;
137  
138         case 404:
139                 logger.warn( "Perm not found...ignore");
140                 break;
141                 
142         case 200:
143                 logger.info( "expected response" );
144                 break;
145         default :
146                 logger.error( "Unexpected response: " + rc );
147                 break;
148         }
149                 
150                 return rc;
151         }
152
153         public int addRole(AafRole role) {
154                 logger.info( "entry: addRole() "  );
155                 return doPost( role, "authz/role", 201 );
156         }
157
158         
159         
160         public int addNamespace(AafNamespace ns) {
161                 logger.info( "entry: addNamespace() "  );
162                 return doPost( ns, "authz/ns", 201 );
163         }
164
165         
166         private int doPost( AafObject obj, String uri, int expect ) {
167                 int rc = -1;
168                 logger.info( "entry: doPost() "  );
169                 String pURL = aafURL + uri;
170                 logger.info( "doPost: useAAF=" + useAAF );
171                 if ( useAAF ) {
172                         logger.info( "doPost: " + obj.toJSON());
173                         rc = aaf.postAaf( obj, pURL );
174                 } else {
175                         rc = expect;
176                 }
177         switch( rc ) {
178         case 401:
179         case 403:
180                 errorLogger.error(DmaapbcLogMessageEnum.AAF_CREDENTIAL_ERROR,  getCred( false ) );
181                 System.exit(1);
182         case 409:
183                 logger.warn( "Object for " + uri + " already exists. Possible conflict.");
184                 break;
185                 
186
187         default :
188                 if ( rc == expect ) {
189                         logger.info( "expected response: " + rc);
190                 } else {
191                         logger.error( "Unexpected response: " + rc );
192                 }
193                 break;
194         }
195         
196         return rc;
197         }
198 }