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