2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.service;
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;
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;
38 public class ApiService extends BaseLoggingClass {
40 private String apiNamespace;
42 private String uriPath;
43 private String method;
44 private String authorization;
45 private String requestId;
47 private ApiPolicy apiPolicy;
52 requestId = (new RandomString(10)).nextString();
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.");
59 apiPolicy = new ApiPolicy();
61 logger.info("apiNamespace=" + apiNamespace);
64 public ApiService setAuth(String auth) {
65 this.authorization = auth;
66 logger.info("setAuth: authorization={} ", authorization);
70 private void setServiceName() {
71 String svcRequest = new String(this.method + " " + this.uriPath);
72 MDC.put(MDC_SERVICE_NAME, svcRequest);
75 public ApiService setHttpMethod(String httpMethod) {
76 this.method = httpMethod;
77 logger.info("setHttpMethod: method={} ", method);
82 public ApiService setUriPath(String uriPath) {
83 this.uriPath = uriPath;
84 this.uri = setUriFromPath(uriPath);
85 logger.info("setUriPath: uriPath={} uri={}", uriPath, uri);
90 private String setUriFromPath(String uriPath) {
91 int ch = uriPath.indexOf("/");
93 return ((String) uriPath.subSequence(0, ch));
99 public ApiError getErr() {
104 public void setErr(ApiError err) {
108 public void setCode(int statusCode) {
109 err.setCode(statusCode);
113 public void setMessage(String string) {
114 err.setMessage(string);
118 public void setFields(String string) {
119 err.setFields(string);
122 public void checkAuthorization() throws Exception {
124 MDC.put(MDC_KEY_REQUEST_ID, requestId);
126 logger.info("request: uri={} method={} auth={}", uri, method, authorization);
128 if (uri == null || uri.isEmpty()) {
129 String errmsg = "No URI value provided ";
130 err.setMessage(errmsg);
132 throw new AuthenticationErrorException();
134 if (method == null || method.isEmpty()) {
135 String errmsg = "No method value provided ";
136 err.setMessage(errmsg);
138 throw new AuthenticationErrorException();
140 DmaapService dmaapService = new DmaapService();
141 Dmaap dmaap = dmaapService.getDmaap();
142 String env = dmaap.getDmaapName();
144 // special case during bootstrap of app when DMaaP environment may not be set.
145 // this allows us to authorize certain APIs used for initialization during this window.
146 if (env == null || env.isEmpty()) {
149 if (!apiPolicy.getUseAuthClass()) {
150 return; // skip authorization if not enabled
152 if (authorization == null || authorization.isEmpty()) {
153 String errmsg = "No basic authorization value provided ";
154 err.setMessage(errmsg);
156 throw new AuthenticationErrorException();
158 String credentials = authorization.substring("Basic".length()).trim();
159 byte[] decoded = DatatypeConverter.parseBase64Binary(credentials);
160 String decodedString = new String(decoded);
161 String[] actualCredentials = decodedString.split(":");
162 String ID = actualCredentials[0];
163 String Password = actualCredentials[1];
164 MDC.put(MDC_PARTNER_NAME, ID);
167 DmaapPerm p = new DmaapPerm(apiNamespace + "." + uri, env, method);
168 apiPolicy.check(ID, Password, p);
169 } catch (AuthenticationErrorException ae) {
171 "User " + ID + " failed authentication/authorization for " + apiNamespace + "." + uriPath + " " + env
174 err.setMessage(errmsg);
180 public ApiService setRequestId(String requestId) {
181 if (requestId == null || requestId.isEmpty()) {
182 this.requestId = (new RandomString(10)).nextString();
183 logger.warn("X-ECOMP-RequestID not set in HTTP Header. Setting RequestId value to: " + this.requestId);
185 this.requestId = requestId;
187 MDC.put(MDC_KEY_REQUEST_ID, this.requestId);