4778affcee8114297e00ef5bac2bf9d757b07378
[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:8095/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
116         public int delGrant( DmaapGrant grant ) {
117                 int rc = -1;
118                 logger.info( "entry: delGrant() "  );
119
120                 String pURL = aafURL + "authz/role/:" + grant.getRole() + "/perm";
121                 
122                 if ( useAAF ) {
123                         rc = aaf.delAaf( grant, pURL );
124                 } else {
125                         rc = 200;
126                 }
127         switch( rc ) {
128         case 401:
129         case 403:
130                 errorLogger.error(DmaapbcLogMessageEnum.AAF_CREDENTIAL_ERROR,  getCred( false ) );
131                 System.exit(1);
132                 break;
133  
134         case 404:
135                 logger.warn( "Perm not found...ignore");
136                 break;
137                 
138         case 200:
139                 logger.info( "expected response" );
140                 break;
141         default :
142                 logger.error( "Unexpected response: " + rc );
143                 break;
144         }
145                 
146                 return rc;
147         }
148
149         public int addRole(AafRole role) {
150                 logger.info( "entry: addRole() "  );
151                 return doPost( role, "authz/role", 201 );
152         }
153
154         
155         
156         public int addNamespace(AafNamespace ns) {
157                 logger.info( "entry: addNamespace() "  );
158                 return doPost( ns, "authz/ns", 201 );
159         }
160
161         
162         private int doPost( AafObject obj, String uri, int expect ) {
163                 int rc = -1;
164                 logger.info( "entry: doPost() "  );
165                 String pURL = aafURL + uri;
166                 logger.info( "doPost: useAAF=" + useAAF );
167                 if ( useAAF ) {
168                         logger.info( "doPost: " + obj.toJSON());
169                         rc = aaf.postAaf( obj, pURL );
170                 } else {
171                         rc = expect;
172                 }
173         switch( rc ) {
174         case 401:
175         case 403:
176                 errorLogger.error(DmaapbcLogMessageEnum.AAF_CREDENTIAL_ERROR,  getCred( false ) );
177                 System.exit(1);
178         case 409:
179                 logger.warn( "Object for " + uri + " already exists. Possible conflict.");
180                 break;
181                 
182
183         default :
184                 if ( rc == expect ) {
185                         logger.info( "expected response: " + rc);
186                 } else {
187                         logger.error( "Unexpected response: " + rc );
188                 }
189                 break;
190         }
191         
192         return rc;
193         }
194 }