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