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() {
103 public void checkAuthorization() throws Exception {
105 MDC.put(MDC_KEY_REQUEST_ID, requestId);
107 logger.info("request: uri={} method={} auth={}", uri, method, authorization);
109 if (uri == null || uri.isEmpty()) {
110 String errmsg = "No URI value provided ";
111 err.setMessage(errmsg);
113 throw new AuthenticationErrorException();
115 if (method == null || method.isEmpty()) {
116 String errmsg = "No method value provided ";
117 err.setMessage(errmsg);
119 throw new AuthenticationErrorException();
121 DmaapService dmaapService = new DmaapService();
122 Dmaap dmaap = dmaapService.getDmaap();
123 String env = dmaap.getDmaapName();
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()) {
130 if (!apiPolicy.getUseAuthClass()) {
131 return; // skip authorization if not enabled
133 if (authorization == null || authorization.isEmpty()) {
134 String errmsg = "No basic authorization value provided ";
135 err.setMessage(errmsg);
137 throw new AuthenticationErrorException();
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);
148 DmaapPerm p = new DmaapPerm(apiNamespace + "." + uri, env, method);
149 apiPolicy.check(ID, Password, p);
150 } catch (AuthenticationErrorException ae) {
152 "User " + ID + " failed authentication/authorization for " + apiNamespace + "." + uriPath + " " + env
155 err.setMessage(errmsg);
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);
166 this.requestId = requestId;
168 MDC.put(MDC_KEY_REQUEST_ID, this.requestId);