Replace ATT headers
[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 org.apache.log4j.Logger;
31 import org.onap.dmaap.datarouter.authz.AuthorizationResponse;
32 import org.onap.dmaap.datarouter.authz.Authorizer;
33 import org.onap.dmaap.datarouter.authz.impl.AuthzResource.ResourceType;
34
35 /** Authorizer for the provisioning API for Data Router R1
36  *
37  * @author J. F. Lucas
38  *
39  */
40 public class ProvAuthorizer implements Authorizer {
41
42     private Logger log;
43     private ProvDataProvider provData;
44
45     private static final String SUBJECT_HEADER = "X-DMAAP-DR-ON-BEHALF-OF";  // HTTP header carrying requester identity
46     private static final String SUBJECT_HEADER_GROUP = "X-DMAAP-DR-ON-BEHALF-OF-GROUP";  // HTTP header carrying requester identity  by group Rally : US708115
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 = Logger.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
83         boolean decision = false;
84
85         // Extract interesting parts of the HTTP request
86         String method = request.getMethod();
87         AuthzResource resource = new AuthzResource(request.getRequestURI());
88         String subject = (request.getHeader(SUBJECT_HEADER));         // identity of the requester
89         String subjectgroup = (request.getHeader(SUBJECT_HEADER_GROUP)); // identity of the requester by group Rally : US708115
90
91         log.trace("Method: " + method + " -- Type: " + resource.getType() + " -- Id: " + resource.getId() +
92                 " -- Subject: " + subject);
93
94         // Choose authorization method based on the resource type
95         ResourceType resourceType = resource.getType();
96         if (resourceType != null) {
97
98             switch (resourceType) {
99
100             case FEEDS_COLLECTION:
101                 decision = allowFeedsCollectionAccess(resource, method, subject, subjectgroup);
102                 break;
103
104             case SUBS_COLLECTION:
105                 decision = allowSubsCollectionAccess(resource, method, subject, subjectgroup);
106                 break;
107
108             case FEED:
109                 decision = allowFeedAccess(resource, method, subject, subjectgroup);
110                 break;
111
112             case SUB:
113                 decision = allowSubAccess(resource, method, subject, subjectgroup);
114                 break;
115
116             default:
117                 decision = false;
118                 break;
119             }
120         }
121         log.debug("Exit decide(): "  + method + "|" + resourceType + "|" + resource.getId() + "|" + subject + " ==> " + decision);
122
123         return new AuthRespImpl(decision);
124     }
125
126     private boolean allowFeedsCollectionAccess(AuthzResource resource,    String method, String subject, String subjectgroup) {
127
128         // Allow GET or POST unconditionally
129         return method != null && ("GET".equalsIgnoreCase(method) || "POST".equalsIgnoreCase(method));
130     }
131
132     private boolean allowSubsCollectionAccess(AuthzResource resource, String method, String subject, String subjectgroup) {
133
134         // Allow GET or POST unconditionally
135         return method != null && ("GET".equalsIgnoreCase(method) || "POST".equalsIgnoreCase(method));
136     }
137
138     private boolean allowFeedAccess(AuthzResource resource, String method,    String subject, String subjectgroup) {
139         boolean decision = false;
140
141         // Allow GET, PUT, or DELETE if requester (subject) is the owner (publisher) of the feed
142         if ( method != null && ("GET".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method) ||
143                         "DELETE".equalsIgnoreCase(method))) {
144
145             String owner = provData.getFeedOwner(resource.getId());
146             decision = (owner != null) && owner.equals(subject);
147
148             //Verifying by group Rally : US708115
149             if(subjectgroup != null) {
150                 String feedowner = provData.getGroupByFeedGroupId(subject, resource.getId());
151                 decision = (feedowner != null) && feedowner.equals(subjectgroup);
152             }
153         }
154
155         return decision;
156     }
157
158     private boolean allowSubAccess(AuthzResource resource, String method, String subject, String subjectgroup) {
159         boolean decision = false;
160
161         // Allow GET, PUT, or DELETE if requester (subject) is the owner of the subscription (subscriber)
162         if (method != null && ("GET".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method) ||
163                         "DELETE".equalsIgnoreCase(method) || "POST".equalsIgnoreCase(method))) {
164
165             String owner = provData.getSubscriptionOwner(resource.getId());
166             decision = (owner != null) && owner.equals(subject);
167
168             //Verifying by group Rally : US708115
169             if(subjectgroup != null) {
170                 String feedowner = provData.getGroupBySubGroupId(subject, resource.getId());
171                 decision = (feedowner != null) && feedowner.equals(subjectgroup);
172             }
173         }
174
175         return decision;
176     }
177
178 }