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