745e339d4532da65736265de77e8cc2fa102cf65
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / authz / impl / ProvAuthorizer.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 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  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24 package org.onap.dmaap.datarouter.authz.impl;
25
26 import java.util.Map;
27
28 import javax.servlet.http.HttpServletRequest;
29
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32 import org.onap.dmaap.datarouter.authz.AuthorizationResponse;
33 import org.onap.dmaap.datarouter.authz.Authorizer;
34 import org.onap.dmaap.datarouter.authz.impl.AuthzResource.ResourceType;
35
36 /** Authorizer for the provisioning API for Data Router R1
37  *
38  * @author J. F. Lucas
39  *
40  */
41 public class ProvAuthorizer implements Authorizer {
42
43     private EELFLogger log;
44     private ProvDataProvider provData;
45
46     private static final String SUBJECT_HEADER = "X-DMAAP-DR-ON-BEHALF-OF";  // HTTP header carrying requester identity
47     private static final String SUBJECT_HEADER_GROUP = "X-DMAAP-DR-ON-BEHALF-OF-GROUP";  // HTTP header carrying requester identity  by group Rally : US708115
48     /** Constructor. For the moment, do nothing special.  Make it a singleton?
49      *
50      */
51     public ProvAuthorizer(ProvDataProvider provData) {
52         this.provData = provData;
53         this.log = EELFManager.getInstance().getLogger(this.getClass());
54     }
55
56     /**
57      * Determine if the API request carried in the <code>request</code> parameter is permitted.
58      *
59      * @param request the HTTP request for which an authorization decision is needed
60      * @return an object implementing the <code>AuthorizationResponse</code> interface.  This object includes the
61      * permit/deny decision for the request and (after R1) supplemental information related to the response in the form
62      * of advice and obligations.
63      */
64     @Override
65     public AuthorizationResponse decide(HttpServletRequest request) {
66             return this.decide(request, null);
67     }
68
69     /**
70      * Determine if the API request carried in the <code>request</code> parameter, with additional attributes provided in
71      * the <code>additionalAttrs</code> parameter, is permitted.   <code>additionalAttrs</code> isn't used in R1.
72      *
73      * @param request the HTTP request for which an authorization decision is needed
74      * @param additionalAttrs additional attributes that the <code>Authorizer</code> can in making an authorization decision
75      * @return an object implementing the <code>AuthorizationResponse</code> interface.  This object includes the
76      * permit/deny decision for the request and (after R1) supplemental information related to the response in the form
77      * of advice and obligations.
78      */
79     @Override
80     public AuthorizationResponse decide(HttpServletRequest request,
81             Map<String, String> additionalAttrs) {
82         log.trace ("Entering decide()");
83
84         boolean decision = false;
85
86         // Extract interesting parts of the HTTP request
87         String method = request.getMethod();
88         AuthzResource resource = new AuthzResource(request.getRequestURI());
89         String subject = (request.getHeader(SUBJECT_HEADER));         // identity of the requester
90         String subjectgroup = (request.getHeader(SUBJECT_HEADER_GROUP)); // identity of the requester by group Rally : US708115
91
92         log.trace("Method: " + method + " -- Type: " + resource.getType() + " -- Id: " + resource.getId() +
93                 " -- Subject: " + subject);
94
95         // Choose authorization method based on the resource type
96         ResourceType resourceType = resource.getType();
97         if (resourceType != null) {
98
99             switch (resourceType) {
100
101             case FEEDS_COLLECTION:
102                 decision = allowFeedsCollectionAccess(resource, method, subject, subjectgroup);
103                 break;
104
105             case SUBS_COLLECTION:
106                 decision = allowSubsCollectionAccess(resource, method, subject, subjectgroup);
107                 break;
108
109             case FEED:
110                 decision = allowFeedAccess(resource, method, subject, subjectgroup);
111                 break;
112
113             case SUB:
114                 decision = allowSubAccess(resource, method, subject, subjectgroup);
115                 break;
116
117             default:
118                 decision = false;
119                 break;
120             }
121         }
122         log.debug("Exit decide(): "  + method + "|" + resourceType + "|" + resource.getId() + "|" + subject + " ==> " + decision);
123
124         return new AuthRespImpl(decision);
125     }
126
127     private boolean allowFeedsCollectionAccess(AuthzResource resource,    String method, String subject, String subjectgroup) {
128
129         // Allow GET or POST unconditionally
130         return method != null && ("GET".equalsIgnoreCase(method) || "POST".equalsIgnoreCase(method));
131     }
132
133     private boolean allowSubsCollectionAccess(AuthzResource resource, String method, String subject, String subjectgroup) {
134
135         // Allow GET or POST unconditionally
136         return method != null && ("GET".equalsIgnoreCase(method) || "POST".equalsIgnoreCase(method));
137     }
138
139     private boolean allowFeedAccess(AuthzResource resource, String method,    String subject, String subjectgroup) {
140         boolean decision = false;
141
142         // Allow GET, PUT, or DELETE if requester (subject) is the owner (publisher) of the feed
143         if ( method != null && ("GET".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method) ||
144                         "DELETE".equalsIgnoreCase(method))) {
145
146             String owner = provData.getFeedOwner(resource.getId());
147             decision = (owner != null) && owner.equals(subject);
148
149             //Verifying by group Rally : US708115
150             if(subjectgroup != null) {
151                 String feedowner = provData.getGroupByFeedGroupId(subject, resource.getId());
152                 decision = (feedowner != null) && feedowner.equals(subjectgroup);
153             }
154         }
155
156         return decision;
157     }
158
159     private boolean allowSubAccess(AuthzResource resource, String method, String subject, String subjectgroup) {
160         boolean decision = false;
161
162         // Allow GET, PUT, or DELETE if requester (subject) is the owner of the subscription (subscriber)
163         if (method != null && ("GET".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method) ||
164                         "DELETE".equalsIgnoreCase(method) || "POST".equalsIgnoreCase(method))) {
165
166             String owner = provData.getSubscriptionOwner(resource.getId());
167             decision = (owner != null) && owner.equals(subject);
168
169             //Verifying by group Rally : US708115
170             if(subjectgroup != null) {
171                 String feedowner = provData.getGroupBySubGroupId(subject, resource.getId());
172                 decision = (feedowner != null) && feedowner.equals(subjectgroup);
173             }
174         }
175
176         return decision;
177     }
178
179 }