update link to upper-constraints.txt
[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 jakarta.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     // HTTP header carrying requester identity  by group Rally : US708115
46     private static final String SUBJECT_HEADER_GROUP = "X-DMAAP-DR-ON-BEHALF-OF-GROUP";
47
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 a 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         boolean decision = false;
84         // Extract interesting parts of the HTTP request
85         String method = request.getMethod();
86         AuthzResource resource = new AuthzResource(request.getRequestURI());
87         String subject = (request.getHeader(SUBJECT_HEADER));
88         String subjectgroup = (request.getHeader(SUBJECT_HEADER_GROUP));
89
90         log.trace("Method: " + method + " -- Type: " + resource.getType() + " -- Id: " + resource.getId()
91                           + " -- Subject: " + subject);
92         // Choose authorization method based on the resource type
93         ResourceType resourceType = resource.getType();
94         if (resourceType != null) {
95             switch (resourceType) {
96                 case FEEDS_COLLECTION:
97                     decision = allowFeedsCollectionAccess(method);
98                     break;
99                 case SUBS_COLLECTION:
100                     decision = allowSubsCollectionAccess(method);
101                     break;
102                 case FEED:
103                     decision = allowFeedAccess(resource, method, subject, subjectgroup);
104                     break;
105                 case SUB:
106                     decision = allowSubAccess(resource, method, subject, subjectgroup);
107                     break;
108                 default:
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)
132                                         || "DELETE".equalsIgnoreCase(method))) {
133
134             String owner = provData.getFeedOwner(resource.getId());
135             decision = (owner != null) && owner.equals(subject);
136             //Verifying by group Rally : US708115
137             if (subjectgroup != null) {
138                 String feedOwner = provData.getGroupByFeedGroupId(subject, resource.getId());
139                 decision = (feedOwner != null) && feedOwner.equals(subjectgroup);
140             }
141         }
142         return decision;
143     }
144
145     private boolean allowSubAccess(AuthzResource resource, String method, String subject, String subjectgroup) {
146         boolean decision = false;
147
148         // Allow GET, PUT, or DELETE if requester (subject) is the owner of the subscription (subscriber)
149         if (method != null && ("GET".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method)
150                                        || "DELETE".equalsIgnoreCase(method) || "POST".equalsIgnoreCase(method))) {
151
152             String owner = provData.getSubscriptionOwner(resource.getId());
153             decision = (owner != null) && owner.equals(subject);
154
155             //Verifying by group Rally : US708115
156             if (subjectgroup != null) {
157                 String feedowner = provData.getGroupBySubGroupId(subject, resource.getId());
158                 decision = (feedowner != null) && feedowner.equals(subjectgroup);
159             }
160         }
161
162         return decision;
163     }
164
165 }