@Override public RulePolicyRepresentation toRepresentation(Policy policy, AuthorizationProvider authorization) { RulePolicyRepresentation representation = new RulePolicyRepresentation(); representation.setArtifactGroupId(policy.getConfig().get("mavenArtifactGroupId")); representation.setArtifactId(policy.getConfig().get("mavenArtifactId")); representation.setArtifactVersion(policy.getConfig().get("mavenArtifactVersion")); representation.setScannerPeriod(policy.getConfig().get("scannerPeriod")); representation.setScannerPeriodUnit(policy.getConfig().get("scannerPeriodUnit")); representation.setSessionName(policy.getConfig().get("sessionName")); representation.setModuleName(policy.getConfig().get("moduleName")); return representation; }
private boolean isInvalid(Date actualDate, int timeConstant, String configName, Policy policy) { Calendar calendar = Calendar.getInstance(); calendar.setTime(actualDate); int dateField = calendar.get(timeConstant); if (Calendar.MONTH == timeConstant) { dateField++; } String start = policy.getConfig().get(configName); if (start != null) { String end = policy.getConfig().get(configName + "End"); if (end != null) { if (dateField < Integer.parseInt(start) || dateField > Integer.parseInt(end)) { return true; } } else { if (dateField != Integer.parseInt(start)) { return true; } } } return false; }
private Map<String, Object>[] getRoles(Policy policy) { String roles = policy.getConfig().get("roles"); if (roles != null) { try { return JsonSerialization.readValue(roles.getBytes(), Map[].class); } catch (IOException e) { throw new RuntimeException("Could not parse roles [" + roles + "] from policy config [" + policy.getName() + ".", e); } } return new Map[] {}; } }
static String[] getUsers(Policy policy) { String users = policy.getConfig().get("users"); if (users != null) { try { return JsonSerialization.readValue(users.getBytes(), String[].class); } catch (IOException e) { throw new RuntimeException("Could not parse users [" + users + "] from policy config [" + policy.getName() + ".", e); } } return new String[0]; } }
private String[] getClients(Policy policy) { String clients = policy.getConfig().get("clients"); if (clients != null) { try { return JsonSerialization.readValue(clients.getBytes(), String[].class); } catch (IOException e) { throw new RuntimeException("Could not parse clients [" + clients + "] from policy config [" + policy.getName() + "].", e); } } return new String[]{}; } }
DroolsPolicy(KieServices ks, Policy associatedPolicy) { String groupId = associatedPolicy.getConfig().get("mavenArtifactGroupId"); String artifactId = associatedPolicy.getConfig().get("mavenArtifactId"); String version = associatedPolicy.getConfig().get("mavenArtifactVersion"); String scannerPeriod = associatedPolicy.getConfig().get("scannerPeriod"); String scannerPeriodUnit = associatedPolicy.getConfig().get("scannerPeriodUnit"); this.sessionName = associatedPolicy.getConfig().get("sessionName"); this.kc = ks.newKieContainer(ks.newReleaseId(groupId, artifactId, version)); this.kcs = ks.newKieScanner(this.kc); this.kcs.start(toMillis(scannerPeriod, scannerPeriodUnit)); KieSession session = this.kc.newKieSession(this.sessionName); if (session == null) { throw new RuntimeException("Could not obtain session with name [" + this.sessionName + "]."); } session.dispose(); }
@Override public GroupPolicyRepresentation toRepresentation(Policy policy, AuthorizationProvider authorization) { GroupPolicyRepresentation representation = new GroupPolicyRepresentation(); representation.setGroupsClaim(policy.getConfig().get("groupsClaim")); try { representation.setGroups(getGroupsDefinition(policy.getConfig())); } catch (IOException cause) { throw new RuntimeException("Failed to deserialize groups", cause); } return representation; }
@Override public RolePolicyRepresentation toRepresentation(Policy policy, AuthorizationProvider authorization) { RolePolicyRepresentation representation = new RolePolicyRepresentation(); try { representation.setRoles(new HashSet<>(Arrays.asList(JsonSerialization.readValue(policy.getConfig().get("roles"), RolePolicyRepresentation.RoleDefinition[].class)))); } catch (IOException cause) { throw new RuntimeException("Failed to deserialize roles", cause); } return representation; }
@Override public ResourcePermissionRepresentation toRepresentation(Policy policy, AuthorizationProvider authorization) { ResourcePermissionRepresentation representation = new ResourcePermissionRepresentation(); representation.setResourceType(policy.getConfig().get("defaultResourceType")); return representation; }
@Override public JSPolicyRepresentation toRepresentation(Policy policy, AuthorizationProvider authorization) { JSPolicyRepresentation representation = new JSPolicyRepresentation(); representation.setCode(policy.getConfig().get("code")); return representation; }
private void updateResourceType(Policy policy, ResourcePermissionRepresentation representation) { if (representation != null) { //TODO: remove this check once we migrate to new API if (ResourcePermissionRepresentation.class.equals(representation.getClass())) { ResourcePermissionRepresentation resourcePermission = ResourcePermissionRepresentation.class.cast(representation); Map<String, String> config = new HashMap(policy.getConfig()); config.compute("defaultResourceType", (key, value) -> { String resourceType = resourcePermission.getResourceType(); return resourceType != null ? resourcePermission.getResourceType() : null; }); policy.setConfig(config); } } }
@Override public UserPolicyRepresentation toRepresentation(Policy policy, AuthorizationProvider authorization) { UserPolicyRepresentation representation = new UserPolicyRepresentation(); try { representation.setUsers(JsonSerialization.readValue(policy.getConfig().get("users"), Set.class)); } catch (IOException cause) { throw new RuntimeException("Failed to deserialize roles", cause); } return representation; }
@Override public Map<String, String> getConfig() { if (isUpdated()) return updated.getConfig(); return cached.getConfig(modelSupplier); }
private ScriptModel getScriptModel(final Policy policy, final RealmModel realm, final ScriptingProvider scripting) { String scriptName = policy.getName(); String scriptCode = policy.getConfig().get("code"); String scriptDescription = policy.getDescription(); //TODO lookup script by scriptId instead of creating it every time return scripting.createScript(realm.getId(), ScriptModel.TEXT_JAVASCRIPT, scriptName, scriptCode, scriptDescription); }
@Override public Policy getDelegateForUpdate() { if (updated == null) { updated = modelSupplier.get(); String defaultResourceType = updated.getConfig().get("defaultResourceType"); cacheSession.registerPolicyInvalidation(cached.getId(), cached.getName(), cached.getResourcesIds(modelSupplier), cached.getScopesIds(modelSupplier), defaultResourceType, cached.getResourceServerId()); if (updated == null) throw new IllegalStateException("Not found in database"); } return updated; }
@Override public TimePolicyRepresentation toRepresentation(Policy policy, AuthorizationProvider authorization) { TimePolicyRepresentation representation = new TimePolicyRepresentation(); Map<String, String> config = policy.getConfig(); representation.setDayMonth(config.get("dayMonth")); representation.setDayMonthEnd(config.get("dayMonthEnd")); representation.setMonth(config.get("month")); representation.setMonthEnd(config.get("monthEnd")); representation.setYear(config.get("year")); representation.setYearEnd(config.get("yearEnd")); representation.setHour(config.get("hour")); representation.setHourEnd(config.get("hourEnd")); representation.setMinute(config.get("minute")); representation.setMinuteEnd(config.get("minuteEnd")); representation.setNotBefore(config.get("nbf")); representation.setNotOnOrAfter(config.get("noa")); return representation; }
@Override public void delete(String id) { if (id == null) return; Policy policy = findById(id, null); if (policy == null) return; cache.invalidateObject(id); Set<String> resources = policy.getResources().stream().map(resource -> resource.getId()).collect(Collectors.toSet()); ResourceServer resourceServer = policy.getResourceServer(); Set<String> resourceTypes = getResourceTypes(resources, resourceServer.getId()); String defaultResourceType = policy.getConfig().get("defaultResourceType"); if (Objects.nonNull(defaultResourceType)) { resourceTypes.add(defaultResourceType); } Set<String> scopes = policy.getScopes().stream().map(scope -> scope.getId()).collect(Collectors.toSet()); invalidationEvents.add(PolicyRemovedEvent.create(id, policy.getName(), resources, resourceTypes, scopes, resourceServer.getId())); cache.policyRemoval(id, policy.getName(), resources, resourceTypes, scopes, resourceServer.getId(), invalidations); getPolicyStoreDelegate().delete(id); }
private void updatePolicy(Policy policy, TimePolicyRepresentation representation) { String nbf = representation.getNotBefore(); String noa = representation.getNotOnOrAfter(); if (nbf != null && noa != null) { validateFormat(nbf); validateFormat(noa); } Map<String, String> config = new HashMap(policy.getConfig()); config.compute("nbf", (s, s2) -> nbf != null ? nbf : null); config.compute("noa", (s, s2) -> noa != null ? noa : null); config.compute("dayMonth", (s, s2) -> representation.getDayMonth() != null ? representation.getDayMonth() : null); config.compute("dayMonthEnd", (s, s2) -> representation.getDayMonthEnd() != null ? representation.getDayMonthEnd() : null); config.compute("month", (s, s2) -> representation.getMonth() != null ? representation.getMonth() : null); config.compute("monthEnd", (s, s2) -> representation.getMonthEnd() != null ? representation.getMonthEnd() : null); config.compute("year", (s, s2) -> representation.getYear() != null ? representation.getYear() : null); config.compute("yearEnd", (s, s2) -> representation.getYearEnd() != null ? representation.getYearEnd() : null); config.compute("hour", (s, s2) -> representation.getHour() != null ? representation.getHour() : null); config.compute("hourEnd", (s, s2) -> representation.getHourEnd() != null ? representation.getHourEnd() : null); config.compute("minute", (s, s2) -> representation.getMinute() != null ? representation.getMinute() : null); config.compute("minuteEnd", (s, s2) -> representation.getMinuteEnd() != null ? representation.getMinuteEnd() : null); policy.setConfig(config); }
String notBefore = policy.getConfig().get("nbf"); if (notBefore != null && !"".equals(notBefore)) { if (actualDate.before(dateFormat.parse(format(notBefore)))) { String notOnOrAfter = policy.getConfig().get("noa"); if (notOnOrAfter != null && !"".equals(notOnOrAfter)) { if (actualDate.after(dateFormat.parse(format(notOnOrAfter)))) {