/** * Remove registered service. * * @param service the service */ protected void removeRegisteredService(final RegisteredService service) { this.serviceMap.remove(service.getId()); }
@Override public void update(final RegisteredService service) { this.serviceMap.put(service.getId(), service); }
@Override public RegisteredService findServiceById(final long id) { return this.registeredServices.stream().filter(r -> r.getId() == id).findFirst().orElse(null); }
private static String getBackupMetadataFilenamePrefix(final AbstractResource metadataResource, final RegisteredService service) { return service.getName() .concat("-") .concat(String.valueOf(service.getId())) .concat("-") .concat(metadataResource.getFilename()) .concat("-"); }
@Override public RegisteredService save(final RegisteredService registeredService) { if (registeredService.getId() == RegisteredService.INITIAL_IDENTIFIER_VALUE) { registeredService.setId(findHighestId() + 1); } val svc = findServiceById(registeredService.getId()); if (svc != null) { this.registeredServices.remove(svc); } this.registeredServices.add(registeredService); return registeredService; }
/** * Method creates a filename to store the service. * * @param service - Service to be stored. * @param extension - extension to use for the file. * @return - String representing file name. */ @Override public String build(final RegisteredService service, final String extension) { return StringUtils.remove(service.getName(), ' ') + '-' + service.getId() + '.' + extension; } }
@Override public RegisteredService getRegisteredServiceFromCacheIfAny(final RegisteredService service, final long id, final ServiceRegistry serviceRegistry) { return getRegisteredServiceFromCacheByPredicate(service, value -> value.getValue().getId() == id, serviceRegistry); }
@Override public void accept(final File file) { LOGGER.debug("New service definition [{}] was modified. Locating service entry from cache...", file); val newServices = serviceRegistryDao.load(file); newServices.stream() .filter(Objects::nonNull) .forEach(newService -> { val oldService = serviceRegistryDao.findServiceById(newService.getId()); if (!newService.equals(oldService)) { LOGGER.debug("Updating service definitions with [{}]", newService); serviceRegistryDao.publishEvent(new CasRegisteredServicePreSaveEvent(this, newService)); serviceRegistryDao.update(newService); serviceRegistryDao.publishEvent(new CasRegisteredServiceSavedEvent(this, newService)); } else { LOGGER.debug("Service [{}] loaded from [{}] is identical to the existing entry. Entry may have already been saved " + "in the event processing pipeline", newService.getId(), file.getName()); } }); } }
/** * Evaluate proxied service if needed. * * @param service the service * @param ticketGrantingTicket the ticket granting ticket * @param registeredService the registered service */ protected void evaluateProxiedServiceIfNeeded(final Service service, final TicketGrantingTicket ticketGrantingTicket, final RegisteredService registeredService) { val proxiedBy = ticketGrantingTicket.getProxiedBy(); if (proxiedBy != null) { LOGGER.debug("Ticket-granting ticket is proxied by [{}]. Locating proxy service in registry...", proxiedBy.getId()); val proxyingService = this.servicesManager.findServiceBy(proxiedBy); if (proxyingService != null) { LOGGER.debug("Located proxying service [{}] in the service registry", proxyingService); if (!proxyingService.getProxyPolicy().isAllowedToProxy()) { LOGGER.warn("Found proxying service [{}], but it is not authorized to fulfill the proxy attempt made by [{}]", proxyingService.getId(), service.getId()); throw new UnauthorizedProxyingException(UnauthorizedProxyingException.MESSAGE + registeredService.getId()); } } else { LOGGER.warn("No proxying service found. Proxy attempt by service [{}] (registered service [{}]) is not allowed.", service.getId(), registeredService.getId()); throw new UnauthorizedProxyingException(UnauthorizedProxyingException.MESSAGE + registeredService.getId()); } } else { LOGGER.trace("Ticket-granting ticket is not proxied by another service"); } }
@Override public void accept(final File file) { LOGGER.debug("New service definition [{}] was created. Locating service entry from cache...", file); val services = serviceRegistryDao.load(file); services.stream() .filter(Objects::nonNull) .forEach(service -> { if (serviceRegistryDao.findServiceById(service.getId()) != null) { LOG_SERVICE_DUPLICATE.accept(service); } LOGGER.trace("Updating service definitions with [{}]", service); serviceRegistryDao.publishEvent(new CasRegisteredServicePreSaveEvent(this, service)); serviceRegistryDao.update(service); serviceRegistryDao.publishEvent(new CasRegisteredServiceSavedEvent(this, service)); }); } }
/** * Load services that are provided by the DAO. */ @Override public Collection<RegisteredService> load() { LOGGER.trace("Loading services from [{}]", serviceRegistry.getName()); this.services = this.serviceRegistry.load() .stream() .collect(Collectors.toConcurrentMap(r -> { LOGGER.debug("Adding registered service [{}]", r.getServiceId()); return r.getId(); }, Function.identity(), (r, s) -> s == null ? r : s)); loadInternal(); publishEvent(new CasRegisteredServicesLoadedEvent(this, getAllServices())); evaluateExpiredServiceDefinitions(); LOGGER.info("Loaded [{}] service(s) from [{}].", this.services.size(), this.serviceRegistry.getName()); return services.values(); }
/** * Method returns a RegisteredService instance of the the submitted service that it can be viewed in the * online form before being accepted by an admin. * * @param response - HttpServletResponse * @param request - HttpServletRequest * @param id - id of service * @return - Array of RegisteredService * @throws VersionControlException - failed */ @GetMapping("pair/{id}") public RegisteredService[] changePair(final HttpServletResponse response, final HttpServletRequest request, final @PathVariable String id) throws VersionControlException { try (GitUtil git = repositoryFactory.from(request, response)) { val change = CasManagementUtils.fromJson(git.readObject(id)); val casUserProfile = casUserProfileFactory.from(request, response); val orig = managerFactory.from(request, casUserProfile).findServiceBy(change.getId()); return new RegisteredService[]{change, orig}; } catch (final IOException ex) { LOGGER.error(ex.getMessage(), ex); throw new VersionControlException(); } }
/** * Method will check if the updated service will cause a filename change and handle appropriately. * * @param service - the service */ @Override @SneakyThrows public void checkForRename(final RegisteredService service) { val existing = findServiceBy(service.getId()); if (existing != null) { val oldName = getNamingStrategy().build(existing, ".json"); val newName = getNamingStrategy().build(service, ".json"); if (!oldName.equals(newName)) { try (git) { git.move(oldName, newName); } } } }
private boolean findExistingMatchForService(final RegisteredService r) { if (StringUtils.isNotBlank(r.getServiceId())) { val match = this.serviceRegistry.findServiceById(r.getServiceId()); if (match != null) { LOGGER.warn("Skipping [{}] JSON service definition as a matching service [{}] is found in the registry", r.getName(), match.getName()); return true; } val match2 = this.serviceRegistry.findServiceByExactServiceId(r.getServiceId()); if (match2 != null) { LOGGER.warn("Skipping [{}] JSON service definition as a matching service [{}] is found in the registry", r.getName(), match2.getName()); return true; } } val match = this.serviceRegistry.findServiceById(r.getId()); if (match != null) { LOGGER.warn("Skipping [{}] JSON service definition as a matching id [{}] is found in the registry", r.getName(), match.getId()); return true; } return false; } }
@Override public int compareTo(final RegisteredService other) { return new CompareToBuilder() .append(getEvaluationOrder(), other.getEvaluationOrder()) .append(StringUtils.defaultIfBlank(getName(), StringUtils.EMPTY).toLowerCase(), StringUtils.defaultIfBlank(other.getName(), StringUtils.EMPTY).toLowerCase()) .append(getServiceId(), other.getServiceId()).append(getId(), other.getId()) .toComparison(); }
@Audit(action = "DELETE_SERVICE", actionResolverName = "DELETE_SERVICE_ACTION_RESOLVER", resourceResolverName = "DELETE_SERVICE_RESOURCE_RESOLVER") @Override public synchronized RegisteredService delete(final RegisteredService service) { if (service != null) { publishEvent(new CasRegisteredServicePreDeleteEvent(this, service)); this.serviceRegistry.delete(service); this.services.remove(service.getId()); deleteInternal(service); publishEvent(new CasRegisteredServiceDeletedEvent(this, service)); } return service; }
@Audit(action = "SAVE_SERVICE", actionResolverName = "SAVE_SERVICE_ACTION_RESOLVER", resourceResolverName = "SAVE_SERVICE_RESOURCE_RESOLVER") @Override public synchronized RegisteredService save(final RegisteredService registeredService, final boolean publishEvent) { publishEvent(new CasRegisteredServicePreSaveEvent(this, registeredService)); val r = this.serviceRegistry.save(registeredService); this.services.put(r.getId(), r); saveInternal(registeredService); if (publishEvent) { publishEvent(new CasRegisteredServiceSavedEvent(this, r)); } return r; }
/** * Creates a change for a modified file. * * @param git - GitUtil * @param entry - DiffEntry for the change * @return - Change */ @SuppressWarnings("DefaultCharset") @SneakyThrows public static Change createModifyChange(final GitUtil git, final DiffEntry entry) { val file = git.repoPath() + '/' + entry.getNewPath(); val json = new String(Files.readAllBytes(Paths.get(file))); val svc = CasManagementUtils.fromJson(json); return new Change(String.valueOf(svc.getId()), entry.getNewPath(), entry.getChangeType().toString(), svc.getName(), ObjectId.toString(entry.getOldId().toObjectId()), ObjectId.toString(entry.getNewId().toObjectId())); }
/** * Creates a change for a delete file. * * @param git - GitUtil * @param entry - DiffEntry for the change. * @return - Change */ @SneakyThrows public static Change createDeleteChange(final GitUtil git, final DiffEntry entry) { val json = git.readObject(entry.getOldId().toObjectId()); val svc = CasManagementUtils.fromJson(json); return new Change(String.valueOf(svc.getId()), entry.getOldPath(), DiffEntry.ChangeType.DELETE.toString(), svc.getName(), ObjectId.toString(entry.getOldId().toObjectId()), null); }
@Override public void write(final Kryo kryo, final Output output, final RegisteredService service) { kryo.writeObject(output, service.getServiceId()); kryo.writeObject(output, StringUtils.defaultIfEmpty(service.getName(), StringUtils.EMPTY)); kryo.writeObject(output, StringUtils.defaultIfEmpty(service.getDescription(), StringUtils.EMPTY)); kryo.writeObject(output, service.getId()); kryo.writeObject(output, service.getEvaluationOrder()); kryo.writeObject(output, ObjectUtils.defaultIfNull(service.getLogo(), getEmptyUrl())); kryo.writeObject(output, service.getLogoutType()); kryo.writeObject(output, ObjectUtils.defaultIfNull(service.getLogoutUrl(), StringUtils.EMPTY)); kryo.writeObject(output, new HashSet<>(service.getRequiredHandlers())); kryo.writeObject(output, StringUtils.defaultIfEmpty(service.getTheme(), StringUtils.EMPTY)); kryo.writeObject(output, StringUtils.defaultIfEmpty(service.getResponseType(), StringUtils.EMPTY)); writeObjectByReflection(kryo, output, ObjectUtils.defaultIfNull(service.getPublicKey(), new RegisteredServicePublicKeyImpl())); writeObjectByReflection(kryo, output, ObjectUtils.defaultIfNull(service.getProxyPolicy(), new RefuseRegisteredServiceProxyPolicy())); writeObjectByReflection(kryo, output, ObjectUtils.defaultIfNull(service.getAttributeReleasePolicy(), new ReturnAllowedAttributeReleasePolicy())); writeObjectByReflection(kryo, output, ObjectUtils.defaultIfNull(service.getUsernameAttributeProvider(), new DefaultRegisteredServiceUsernameProvider())); writeObjectByReflection(kryo, output, ObjectUtils.defaultIfNull(service.getAccessStrategy(), new DefaultRegisteredServiceAccessStrategy())); writeObjectByReflection(kryo, output, ObjectUtils.defaultIfNull(service.getMultifactorPolicy(), new DefaultRegisteredServiceMultifactorPolicy())); writeObjectByReflection(kryo, output, ObjectUtils.defaultIfNull(service.getContacts(), new ArrayList<>())); kryo.writeObject(output, StringUtils.defaultIfEmpty(service.getInformationUrl(), StringUtils.EMPTY)); kryo.writeObject(output, StringUtils.defaultIfEmpty(service.getPrivacyUrl(), StringUtils.EMPTY)); kryo.writeObject(output, new HashMap<>(service.getProperties())); }