View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.authorization.xacml.manager.impl;
23  
24  import org.opencastproject.authorization.xacml.manager.api.AclService;
25  import org.opencastproject.authorization.xacml.manager.api.AclServiceException;
26  import org.opencastproject.authorization.xacml.manager.api.ManagedAcl;
27  import org.opencastproject.elasticsearch.api.SearchIndexException;
28  import org.opencastproject.elasticsearch.api.SearchResult;
29  import org.opencastproject.elasticsearch.api.SearchResultItem;
30  import org.opencastproject.elasticsearch.index.ElasticsearchIndex;
31  import org.opencastproject.elasticsearch.index.objects.event.Event;
32  import org.opencastproject.elasticsearch.index.objects.event.EventSearchQuery;
33  import org.opencastproject.elasticsearch.index.objects.series.Series;
34  import org.opencastproject.elasticsearch.index.objects.series.SeriesSearchQuery;
35  import org.opencastproject.security.api.AccessControlList;
36  import org.opencastproject.security.api.Organization;
37  import org.opencastproject.security.api.SecurityService;
38  import org.opencastproject.security.api.User;
39  import org.opencastproject.util.NotFoundException;
40  
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  import java.util.List;
45  import java.util.Optional;
46  import java.util.function.Function;
47  
48  /** Organization bound impl. */
49  public final class AclServiceImpl implements AclService {
50    /** Logging utility */
51    private static final Logger logger = LoggerFactory.getLogger(AclServiceImpl.class);
52  
53    /** Context */
54    private final Organization organization;
55  
56    /** Service dependencies */
57    private final AclDb aclDb;
58    private final SecurityService securityService;
59  
60    /** The Elasticsearch indices */
61    protected ElasticsearchIndex index;
62  
63    public AclServiceImpl(Organization organization, AclDb aclDb, ElasticsearchIndex index,
64            SecurityService securityService) {
65      this.organization = organization;
66      this.aclDb = aclDb;
67      this.index = index;
68      this.securityService = securityService;
69    }
70  
71    @Override
72    public List<ManagedAcl> getAcls() {
73      return aclDb.getAcls(organization);
74    }
75  
76    @Override
77    public Optional<ManagedAcl> getAcl(long id) {
78      return aclDb.getAcl(organization, id);
79    }
80  
81    @Override
82    public boolean updateAcl(ManagedAcl acl) {
83      Optional<ManagedAcl> oldName = getAcl(acl.getId());
84      boolean updateAcl = aclDb.updateAcl(acl);
85      if (updateAcl) {
86        if (oldName.isPresent() && !(oldName.get().getName().equals(acl.getName()))) {
87          User user = securityService.getUser();
88          updateAclInIndex(oldName.get().getName(), acl.getName(), index, organization.getId(), user);
89        }
90      }
91      return updateAcl;
92    }
93  
94    @Override
95    public Optional<ManagedAcl> createAcl(AccessControlList acl, String name) {
96      // we don't need to update the Elasticsearch indices in this case
97      return aclDb.createAcl(organization, acl, name);
98    }
99  
100   @Override
101   public boolean deleteAcl(long id) throws AclServiceException, NotFoundException {
102     Optional<ManagedAcl> deletedAcl = getAcl(id);
103     if (aclDb.deleteAcl(organization, id)) {
104       if (deletedAcl.isPresent()) {
105         User user = securityService.getUser();
106         removeAclFromIndex(deletedAcl.get().getName(), index, organization.getId(), user);
107       }
108       return true;
109     }
110     throw new NotFoundException("Managed acl with id " + id + " not found.");
111   }
112 
113   /**
114    * Update the Managed ACL in the events and series in the Elasticsearch index.
115    *
116    * @param currentAclName
117    *         the current name of the managed acl
118    * @param newAclName
119    *         the new name of the managed acl
120    * @param index
121    *         the index to update
122    * @param orgId
123    *         the organization the managed acl belongs to
124    * @param user
125    *         the current user
126    */
127   private void updateAclInIndex(String currentAclName, String newAclName, ElasticsearchIndex index, String orgId,
128           User user) {
129     logger.debug("Update the events to change the managed acl name from '{}' to '{}'.", currentAclName, newAclName);
130     updateManagedAclForEvents(currentAclName, Optional.of(newAclName), index, orgId, user);
131 
132     logger.debug("Update the series to change the managed acl name from '{}' to '{}'.", currentAclName, newAclName);
133     updateManagedAclForSeries(currentAclName, Optional.of(newAclName), index, orgId, user);
134   }
135 
136   /**
137    * Remove the Managed ACL from the events and series in the Elasticsearch index.
138    *
139    * @param currentAclName
140    *         the current name of the managed acl
141    * @param index
142    *         the index to update
143    * @param orgId
144    *         the organization the managed acl belongs to
145    * @param user
146    *         the current user
147    */
148   private void removeAclFromIndex(String currentAclName, ElasticsearchIndex index, String orgId,
149           User user) {
150     logger.debug("Update the events to remove the managed acl name '{}'.", currentAclName);
151     updateManagedAclForEvents(currentAclName, Optional.empty(), index, orgId, user);
152 
153     logger.debug("Update the series to remove the managed acl name '{}'.", currentAclName);
154     updateManagedAclForSeries(currentAclName, Optional.empty(), index, orgId, user);
155   }
156 
157   /**
158    * Update or remove the Managed Acl for the series in the Elasticsearch index.
159    *
160    * @param currentAclName
161    *         the current name of the managed acl
162    * @param newAclNameOpt
163    * @param index
164    *         the index to update
165    * @param orgId
166    *         the organization the managed acl belongs to
167    * @param user
168    *         the current user
169    */
170   private void updateManagedAclForSeries(String currentAclName, Optional<String> newAclNameOpt,
171           ElasticsearchIndex index, String orgId, User user) {
172     SearchResult<Series> result;
173     try {
174       result = index.getByQuery(new SeriesSearchQuery(orgId, user).withoutActions()
175               .withManagedAcl(currentAclName));
176     } catch (SearchIndexException e) {
177       logger.error("Unable to find the series in org '{}' with current managed acl name '{}'", orgId, currentAclName,
178               e);
179       return;
180     }
181 
182     for (SearchResultItem<Series> seriesItem : result.getItems()) {
183       String seriesId = seriesItem.getSource().getIdentifier();
184 
185       Function<Optional<Series>, Optional<Series>> updateFunction = (Optional<Series> seriesOpt) -> {
186         if (seriesOpt.isPresent() && seriesOpt.get().getManagedAcl().equals(currentAclName)) {
187           Series series = seriesOpt.get();
188           series.setManagedAcl(newAclNameOpt.orElse(null));
189           return Optional.of(series);
190         }
191         return Optional.empty();
192       };
193 
194       try {
195         index.addOrUpdateSeries(seriesId, updateFunction, orgId, user);
196       } catch (SearchIndexException e) {
197         if (newAclNameOpt.isPresent()) {
198           logger.warn("Unable to update series'{}' from current managed acl '{}' to new managed acl name '{}'",
199                   seriesId, currentAclName, newAclNameOpt.get(), e);
200         } else {
201           logger.warn("Unable to update series '{}' to remove managed acl '{}'", seriesId, currentAclName, e);
202         }
203       }
204     }
205   }
206 
207   /**
208    * Update or remove the Managed Acl for the events in the Elasticsearch index.
209    *
210    * @param currentAclName
211    *         the current name of the managed acl
212    * @param newAclNameOpt
213    * @param index
214    *         the index to update
215    * @param orgId
216    *         the organization the managed acl belongs to
217    * @param user
218    *         the current user
219    */
220   private void updateManagedAclForEvents(String currentAclName, Optional<String> newAclNameOpt,
221           ElasticsearchIndex index, String orgId, User user) {
222     SearchResult<Event> result;
223     try {
224       result = index.getByQuery(new EventSearchQuery(orgId, user).withoutActions()
225               .withManagedAcl(currentAclName));
226     } catch (SearchIndexException e) {
227       logger.error("Unable to find the events in org '{}' with current managed acl name '{}' for event",
228               orgId, currentAclName, e);
229       return;
230     }
231 
232     for (SearchResultItem<Event> eventItem : result.getItems()) {
233       String eventId = eventItem.getSource().getIdentifier();
234 
235       Function<Optional<Event>, Optional<Event>> updateFunction = (Optional<Event> eventOpt) -> {
236         if (eventOpt.isPresent() && eventOpt.get().getManagedAcl().equals(currentAclName)) {
237           Event event = eventOpt.get();
238           event.setManagedAcl(newAclNameOpt.orElse(null));
239           return Optional.of(event);
240         }
241         return Optional.empty();
242       };
243 
244       try {
245         index.addOrUpdateEvent(eventId, updateFunction, orgId, user);
246       } catch (SearchIndexException e) {
247         if (newAclNameOpt.isPresent()) {
248           logger.warn(
249                   "Unable to update event '{}' from current managed acl '{}' to new managed acl name '{}'",
250                   eventId, currentAclName, newAclNameOpt.get(), e);
251         } else {
252           logger.warn("Unable to update event '{}' to remove managed acl '{}'", eventId, currentAclName, e);
253         }
254       }
255     }
256   }
257 }