e1beb2872e6760ccc0c8cae87735f4686c853ab5
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / service / ApiService.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.service;
22
23 import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID;
24 import static com.att.eelf.configuration.Configuration.MDC_PARTNER_NAME;
25 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
26
27 import javax.xml.bind.DatatypeConverter;
28 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
29 import org.onap.dmaap.dbcapi.authentication.ApiPolicy;
30 import org.onap.dmaap.dbcapi.authentication.AuthenticationErrorException;
31 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
32 import org.onap.dmaap.dbcapi.model.ApiError;
33 import org.onap.dmaap.dbcapi.model.Dmaap;
34 import org.onap.dmaap.dbcapi.util.DmaapConfig;
35 import org.onap.dmaap.dbcapi.util.RandomString;
36 import org.slf4j.MDC;
37
38 public class ApiService extends BaseLoggingClass {
39
40     private String apiNamespace;
41     private String uri;
42     private String uriPath;
43     private String method;
44     private String authorization;
45     private String requestId;
46     private ApiError err;
47     private ApiPolicy apiPolicy;
48
49     public ApiService() {
50
51         err = new ApiError();
52         requestId = (new RandomString(10)).nextString();
53
54         if (apiNamespace == null) {
55             DmaapConfig p = (DmaapConfig) DmaapConfig.getConfig();
56             apiNamespace = p.getProperty("ApiNamespace", "org.openecomp.dmaapBC.api");
57             logger.info("config param usePE has been deprecated.  Use ApiPermission.Class property instead.");
58         }
59         apiPolicy = new ApiPolicy();
60
61         logger.info("apiNamespace=" + apiNamespace);
62     }
63
64     public ApiService setAuth(String auth) {
65         this.authorization = auth;
66         logger.info("setAuth:  authorization={} ", authorization);
67         return this;
68     }
69
70     private void setServiceName() {
71         String svcRequest = new String(this.method + " " + this.uriPath);
72         MDC.put(MDC_SERVICE_NAME, svcRequest);
73     }
74
75     public ApiService setHttpMethod(String httpMethod) {
76         this.method = httpMethod;
77         logger.info("setHttpMethod: method={} ", method);
78         setServiceName();
79         return this;
80     }
81
82     public ApiService setUriPath(String uriPath) {
83         this.uriPath = uriPath;
84         this.uri = setUriFromPath(uriPath);
85         logger.info("setUriPath: uriPath={} uri={}", uriPath, uri);
86         setServiceName();
87         return this;
88     }
89
90     private String setUriFromPath(String uriPath) {
91         int ch = uriPath.indexOf("/");
92         if (ch > 0) {
93             return ((String) uriPath.subSequence(0, ch));
94         } else {
95             return uriPath;
96         }
97     }
98
99     public ApiError getErr() {
100         return err;
101     }
102
103     public void checkAuthorization() throws Exception {
104
105         MDC.put(MDC_KEY_REQUEST_ID, requestId);
106
107         logger.info("request: uri={} method={} auth={}", uri, method, authorization);
108
109         if (uri == null || uri.isEmpty()) {
110             String errmsg = "No URI value provided ";
111             err.setMessage(errmsg);
112             logger.info(errmsg);
113             throw new AuthenticationErrorException();
114         }
115         if (method == null || method.isEmpty()) {
116             String errmsg = "No method value provided ";
117             err.setMessage(errmsg);
118             logger.info(errmsg);
119             throw new AuthenticationErrorException();
120         }
121         DmaapService dmaapService = new DmaapService();
122         Dmaap dmaap = dmaapService.getDmaap();
123         String env = dmaap.getDmaapName();
124
125         // special case during bootstrap of app when DMaaP environment may not be set.
126         // this allows us to authorize certain APIs used for initialization during this window.
127         if (env == null || env.isEmpty()) {
128             env = "boot";
129         }
130         if (!apiPolicy.getUseAuthClass()) {
131             return;  // skip authorization if not enabled
132         }
133         if (authorization == null || authorization.isEmpty()) {
134             String errmsg = "No basic authorization value provided ";
135             err.setMessage(errmsg);
136             logger.info(errmsg);
137             throw new AuthenticationErrorException();
138         }
139         String credentials = authorization.substring("Basic".length()).trim();
140         byte[] decoded = DatatypeConverter.parseBase64Binary(credentials);
141         String decodedString = new String(decoded);
142         String[] actualCredentials = decodedString.split(":");
143         String ID = actualCredentials[0];
144         String Password = actualCredentials[1];
145         MDC.put(MDC_PARTNER_NAME, ID);
146         try {
147
148             DmaapPerm p = new DmaapPerm(apiNamespace + "." + uri, env, method);
149             apiPolicy.check(ID, Password, p);
150         } catch (AuthenticationErrorException ae) {
151             String errmsg =
152                 "User " + ID + " failed authentication/authorization for " + apiNamespace + "." + uriPath + " " + env
153                     + " " + method;
154             logger.info(errmsg);
155             err.setMessage(errmsg);
156             throw ae;
157
158         }
159     }
160
161     public ApiService setRequestId(String requestId) {
162         if (requestId == null || requestId.isEmpty()) {
163             this.requestId = (new RandomString(10)).nextString();
164             logger.warn("X-ECOMP-RequestID not set in HTTP Header.  Setting RequestId value to: " + this.requestId);
165         } else {
166             this.requestId = requestId;
167         }
168         MDC.put(MDC_KEY_REQUEST_ID, this.requestId);
169         return this;
170     }
171 }
172