View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.fit.core.reference;
20  
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.Map;
24  import java.util.Set;
25  import java.util.stream.Collectors;
26  import javax.sql.DataSource;
27  import org.apache.syncope.common.lib.policy.AccountRuleConf;
28  import org.apache.syncope.common.lib.policy.DefaultAccountRuleConf;
29  import org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf;
30  import org.apache.syncope.common.lib.policy.DefaultPullCorrelationRuleConf;
31  import org.apache.syncope.common.lib.policy.DefaultPushCorrelationRuleConf;
32  import org.apache.syncope.common.lib.policy.HaveIBeenPwnedPasswordRuleConf;
33  import org.apache.syncope.common.lib.policy.PasswordRuleConf;
34  import org.apache.syncope.common.lib.policy.PullCorrelationRuleConf;
35  import org.apache.syncope.common.lib.policy.PushCorrelationRuleConf;
36  import org.apache.syncope.common.lib.report.ReportConf;
37  import org.apache.syncope.common.lib.types.IdMImplementationType;
38  import org.apache.syncope.common.lib.types.IdRepoImplementationType;
39  import org.apache.syncope.core.persistence.api.dao.AnySearchDAO;
40  import org.apache.syncope.core.persistence.jpa.attrvalue.validation.AlwaysTrueValidator;
41  import org.apache.syncope.core.persistence.jpa.attrvalue.validation.BasicValidator;
42  import org.apache.syncope.core.persistence.jpa.attrvalue.validation.BinaryValidator;
43  import org.apache.syncope.core.persistence.jpa.attrvalue.validation.EmailAddressValidator;
44  import org.apache.syncope.core.provisioning.api.ImplementationLookup;
45  import org.apache.syncope.core.provisioning.api.job.report.ReportJobDelegate;
46  import org.apache.syncope.core.provisioning.api.rules.AccountRule;
47  import org.apache.syncope.core.provisioning.api.rules.PasswordRule;
48  import org.apache.syncope.core.provisioning.api.rules.PullCorrelationRule;
49  import org.apache.syncope.core.provisioning.api.rules.PushCorrelationRule;
50  import org.apache.syncope.core.provisioning.java.job.ExpiredAccessTokenCleanup;
51  import org.apache.syncope.core.provisioning.java.job.ExpiredBatchCleanup;
52  import org.apache.syncope.core.provisioning.java.job.MacroJobDelegate;
53  import org.apache.syncope.core.provisioning.java.propagation.AzurePropagationActions;
54  import org.apache.syncope.core.provisioning.java.propagation.DBPasswordPropagationActions;
55  import org.apache.syncope.core.provisioning.java.propagation.GoogleAppsPropagationActions;
56  import org.apache.syncope.core.provisioning.java.propagation.LDAPMembershipPropagationActions;
57  import org.apache.syncope.core.provisioning.java.propagation.LDAPPasswordPropagationActions;
58  import org.apache.syncope.core.provisioning.java.pushpull.DBPasswordPullActions;
59  import org.apache.syncope.core.provisioning.java.pushpull.DefaultProvisionSorter;
60  import org.apache.syncope.core.provisioning.java.pushpull.DefaultPullCorrelationRule;
61  import org.apache.syncope.core.provisioning.java.pushpull.DefaultPushCorrelationRule;
62  import org.apache.syncope.core.provisioning.java.pushpull.LDAPMembershipPullActions;
63  import org.apache.syncope.core.provisioning.java.pushpull.LDAPPasswordPullActions;
64  import org.apache.syncope.core.provisioning.java.pushpull.PullJobDelegate;
65  import org.apache.syncope.core.provisioning.java.pushpull.PushJobDelegate;
66  import org.apache.syncope.core.spring.policy.DefaultAccountRule;
67  import org.apache.syncope.core.spring.policy.DefaultPasswordRule;
68  import org.apache.syncope.core.spring.policy.HaveIBeenPwnedPasswordRule;
69  import org.apache.syncope.core.spring.security.AuthContextUtils;
70  import org.apache.syncope.core.workflow.api.UserWorkflowAdapter;
71  import org.springframework.aop.support.AopUtils;
72  
73  /**
74   * Static implementation providing information about the integration test environment.
75   */
76  public class ITImplementationLookup implements ImplementationLookup {
77  
78      private static final Map<Class<? extends ReportConf>, Class<? extends ReportJobDelegate>> REPORT_CLASSES =
79              Map.of(SampleReportConf.class, SampleReportJobDelegate.class);
80  
81      private static final Map<Class<? extends AccountRuleConf>, Class<? extends AccountRule>> ACCOUNT_RULE_CLASSES =
82              Map.of(
83                      TestAccountRuleConf.class, TestAccountRule.class,
84                      DefaultAccountRuleConf.class, DefaultAccountRule.class);
85  
86      private static final Map<Class<? extends PasswordRuleConf>, Class<? extends PasswordRule>> PASSWORD_RULE_CLASSES =
87              Map.of(
88                      TestPasswordRuleConf.class, TestPasswordRule.class,
89                      DefaultPasswordRuleConf.class, DefaultPasswordRule.class,
90                      HaveIBeenPwnedPasswordRuleConf.class, HaveIBeenPwnedPasswordRule.class);
91  
92      private static final Map<
93              Class<? extends PullCorrelationRuleConf>, Class<? extends PullCorrelationRule>> PULL_CR_CLASSES =
94              Map.of(
95                      DummyPullCorrelationRuleConf.class, DummyPullCorrelationRule.class,
96                      DefaultPullCorrelationRuleConf.class, DefaultPullCorrelationRule.class,
97                      LinkedAccountSamplePullCorrelationRuleConf.class, LinkedAccountSamplePullCorrelationRule.class);
98  
99      private static final Map<
100             Class<? extends PushCorrelationRuleConf>, Class<? extends PushCorrelationRule>> PUSH_CR_CLASSES =
101             Map.of(
102                     DummyPushCorrelationRuleConf.class, DummyPushCorrelationRule.class,
103                     DefaultPushCorrelationRuleConf.class, DefaultPushCorrelationRule.class);
104 
105     private static final Set<Class<?>> PROVISION_SORTER_CLASSES =
106             Set.of(DefaultProvisionSorter.class);
107 
108     private static final Set<Class<?>> COMMAND_CLASSES =
109             Set.of(TestCommand.class);
110 
111     private static final Map<String, Set<String>> CLASS_NAMES = new HashMap<>() {
112 
113         private static final long serialVersionUID = 3109256773218160485L;
114 
115         {
116             Set<String> classNames = new HashSet<>();
117             classNames.add(SampleReportJobDelegate.class.getName());
118             put(IdRepoImplementationType.REPORT_DELEGATE, classNames);
119 
120             classNames = ITImplementationLookup.ACCOUNT_RULE_CLASSES.values().stream().
121                     map(Class::getName).collect(Collectors.toSet());
122             put(IdRepoImplementationType.ACCOUNT_RULE, classNames);
123 
124             classNames = ITImplementationLookup.PASSWORD_RULE_CLASSES.values().stream().
125                     map(Class::getName).collect(Collectors.toSet());
126             put(IdRepoImplementationType.PASSWORD_RULE, classNames);
127 
128             classNames = new HashSet<>();
129             classNames.add(DateToDateItemTransformer.class.getName());
130             classNames.add(DateToLongItemTransformer.class.getName());
131             put(IdRepoImplementationType.ITEM_TRANSFORMER, classNames);
132 
133             classNames = new HashSet<>();
134             classNames.add(MacroJobDelegate.class.getName());
135             classNames.add(PullJobDelegate.class.getName());
136             classNames.add(PushJobDelegate.class.getName());
137             classNames.add(ExpiredAccessTokenCleanup.class.getName());
138             classNames.add(ExpiredBatchCleanup.class.getName());
139             classNames.add(TestSampleJobDelegate.class.getName());
140             put(IdRepoImplementationType.TASKJOB_DELEGATE, classNames);
141 
142             classNames = new HashSet<>();
143             put(IdMImplementationType.RECON_FILTER_BUILDER, classNames);
144 
145             classNames = new HashSet<>();
146             put(IdRepoImplementationType.LOGIC_ACTIONS, classNames);
147             classNames = new HashSet<>();
148             classNames.add(TestMacroActions.class.getName());
149             put(IdRepoImplementationType.MACRO_ACTIONS, classNames);
150 
151             classNames = new HashSet<>();
152             classNames.add(LDAPMembershipPropagationActions.class.getName());
153             classNames.add(LDAPPasswordPropagationActions.class.getName());
154             classNames.add(DBPasswordPropagationActions.class.getName());
155             classNames.add(AzurePropagationActions.class.getName());
156             classNames.add(GoogleAppsPropagationActions.class.getName());
157             put(IdMImplementationType.PROPAGATION_ACTIONS, classNames);
158 
159             classNames = new HashSet<>();
160             classNames.add(LDAPPasswordPullActions.class.getName());
161             classNames.add(TestPullActions.class.getName());
162             classNames.add(LDAPMembershipPullActions.class.getName());
163             classNames.add(DBPasswordPullActions.class.getName());
164             put(IdMImplementationType.PULL_ACTIONS, classNames);
165 
166             classNames = new HashSet<>();
167             put(IdMImplementationType.PUSH_ACTIONS, classNames);
168 
169             classNames = new HashSet<>();
170             classNames.add(DummyPullCorrelationRule.class.getName());
171             put(IdMImplementationType.PULL_CORRELATION_RULE, classNames);
172 
173             classNames = new HashSet<>();
174             classNames.add(DummyPushCorrelationRule.class.getName());
175             put(IdMImplementationType.PUSH_CORRELATION_RULE, classNames);
176 
177             classNames = new HashSet<>();
178             classNames.add(BasicValidator.class.getName());
179             classNames.add(EmailAddressValidator.class.getName());
180             classNames.add(AlwaysTrueValidator.class.getName());
181             classNames.add(BinaryValidator.class.getName());
182             put(IdRepoImplementationType.ATTR_VALUE_VALIDATOR, classNames);
183 
184             classNames = new HashSet<>();
185             classNames.add(TestNotificationRecipientsProvider.class.getName());
186             put(IdRepoImplementationType.RECIPIENTS_PROVIDER, classNames);
187 
188             classNames = ITImplementationLookup.PROVISION_SORTER_CLASSES.stream().
189                     map(Class::getName).collect(Collectors.toSet());
190             put(IdMImplementationType.PROVISION_SORTER, classNames);
191 
192             classNames = ITImplementationLookup.COMMAND_CLASSES.stream().
193                     map(Class::getName).collect(Collectors.toSet());
194             put(IdRepoImplementationType.COMMAND, classNames);
195         }
196     };
197 
198     private final UserWorkflowAdapter uwf;
199 
200     private final AnySearchDAO anySearchDAO;
201 
202     private final EnableFlowableForTestUsers enableFlowableForTestUsers;
203 
204     private final ElasticsearchInit elasticsearchInit;
205 
206     private final OpenSearchInit openSearchInit;
207 
208     public ITImplementationLookup(
209             final UserWorkflowAdapter uwf,
210             final AnySearchDAO anySearchDAO,
211             final EnableFlowableForTestUsers enableFlowableForTestUsers,
212             final ElasticsearchInit elasticsearchInit,
213             final OpenSearchInit openSearchInit) {
214 
215         this.uwf = uwf;
216         this.anySearchDAO = anySearchDAO;
217         this.enableFlowableForTestUsers = enableFlowableForTestUsers;
218         this.elasticsearchInit = elasticsearchInit;
219         this.openSearchInit = openSearchInit;
220     }
221 
222     @Override
223     public int getOrder() {
224         return Integer.MAX_VALUE;
225     }
226 
227     @Override
228     public void load(final String domain, final DataSource datasource) {
229         // in case the Flowable extension is enabled, enable modifications for test users
230         if (enableFlowableForTestUsers != null && AopUtils.getTargetClass(uwf).getName().contains("Flowable")) {
231             AuthContextUtils.callAsAdmin(domain, () -> {
232                 enableFlowableForTestUsers.init(datasource);
233                 return null;
234             });
235         }
236 
237         // in case the Elasticsearch extension is enabled, reinit a clean index for all available domains
238         if (elasticsearchInit != null && AopUtils.getTargetClass(anySearchDAO).getName().contains("Elasticsearch")) {
239             AuthContextUtils.callAsAdmin(domain, () -> {
240                 elasticsearchInit.init();
241                 return null;
242             });
243         }
244 
245         // in case the OpenSearch extension is enabled, reinit a clean index for all available domains
246         if (openSearchInit != null && AopUtils.getTargetClass(anySearchDAO).getName().contains("OpenSearch")) {
247             AuthContextUtils.callAsAdmin(domain, () -> {
248                 openSearchInit.init();
249                 return null;
250             });
251         }
252     }
253 
254     @Override
255     public Set<String> getClassNames(final String type) {
256         return CLASS_NAMES.get(type);
257     }
258 
259     @Override
260     public Class<? extends ReportJobDelegate> getReportClass(final Class<? extends ReportConf> reportConfClass) {
261         return REPORT_CLASSES.get(reportConfClass);
262     }
263 
264     @Override
265     public Class<? extends AccountRule> getAccountRuleClass(
266             final Class<? extends AccountRuleConf> accountRuleConfClass) {
267 
268         return ACCOUNT_RULE_CLASSES.get(accountRuleConfClass);
269     }
270 
271     @Override
272     public Class<? extends PasswordRule> getPasswordRuleClass(
273             final Class<? extends PasswordRuleConf> passwordRuleConfClass) {
274 
275         return PASSWORD_RULE_CLASSES.get(passwordRuleConfClass);
276     }
277 
278     @Override
279     public Class<? extends PullCorrelationRule> getPullCorrelationRuleClass(
280             final Class<? extends PullCorrelationRuleConf> pullCorrelationRuleConfClass) {
281 
282         return PULL_CR_CLASSES.get(pullCorrelationRuleConfClass);
283     }
284 
285     @Override
286     public Class<? extends PushCorrelationRule> getPushCorrelationRuleClass(
287             final Class<? extends PushCorrelationRuleConf> pushCorrelationRuleConfClass) {
288 
289         return PUSH_CR_CLASSES.get(pushCorrelationRuleConfClass);
290     }
291 }