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.logic.job.MacroRunJobDelegate;
40  import org.apache.syncope.core.persistence.api.dao.AnySearchDAO;
41  import org.apache.syncope.core.persistence.jpa.attrvalue.validation.AlwaysTrueValidator;
42  import org.apache.syncope.core.persistence.jpa.attrvalue.validation.BasicValidator;
43  import org.apache.syncope.core.persistence.jpa.attrvalue.validation.BinaryValidator;
44  import org.apache.syncope.core.persistence.jpa.attrvalue.validation.EmailAddressValidator;
45  import org.apache.syncope.core.provisioning.api.ImplementationLookup;
46  import org.apache.syncope.core.provisioning.api.job.report.ReportJobDelegate;
47  import org.apache.syncope.core.provisioning.api.rules.AccountRule;
48  import org.apache.syncope.core.provisioning.api.rules.PasswordRule;
49  import org.apache.syncope.core.provisioning.api.rules.PullCorrelationRule;
50  import org.apache.syncope.core.provisioning.api.rules.PushCorrelationRule;
51  import org.apache.syncope.core.provisioning.java.job.ExpiredAccessTokenCleanup;
52  import org.apache.syncope.core.provisioning.java.job.ExpiredBatchCleanup;
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(MacroRunJobDelegate.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 
148             classNames = new HashSet<>();
149             classNames.add(LDAPMembershipPropagationActions.class.getName());
150             classNames.add(LDAPPasswordPropagationActions.class.getName());
151             classNames.add(DBPasswordPropagationActions.class.getName());
152             classNames.add(AzurePropagationActions.class.getName());
153             classNames.add(GoogleAppsPropagationActions.class.getName());
154             put(IdMImplementationType.PROPAGATION_ACTIONS, classNames);
155 
156             classNames = new HashSet<>();
157             classNames.add(LDAPPasswordPullActions.class.getName());
158             classNames.add(TestPullActions.class.getName());
159             classNames.add(LDAPMembershipPullActions.class.getName());
160             classNames.add(DBPasswordPullActions.class.getName());
161             put(IdMImplementationType.PULL_ACTIONS, classNames);
162 
163             classNames = new HashSet<>();
164             put(IdMImplementationType.PUSH_ACTIONS, classNames);
165 
166             classNames = new HashSet<>();
167             classNames.add(DummyPullCorrelationRule.class.getName());
168             put(IdMImplementationType.PULL_CORRELATION_RULE, classNames);
169 
170             classNames = new HashSet<>();
171             classNames.add(DummyPushCorrelationRule.class.getName());
172             put(IdMImplementationType.PUSH_CORRELATION_RULE, classNames);
173 
174             classNames = new HashSet<>();
175             classNames.add(BasicValidator.class.getName());
176             classNames.add(EmailAddressValidator.class.getName());
177             classNames.add(AlwaysTrueValidator.class.getName());
178             classNames.add(BinaryValidator.class.getName());
179             put(IdRepoImplementationType.VALIDATOR, classNames);
180 
181             classNames = new HashSet<>();
182             classNames.add(TestNotificationRecipientsProvider.class.getName());
183             put(IdRepoImplementationType.RECIPIENTS_PROVIDER, classNames);
184 
185             classNames = ITImplementationLookup.PROVISION_SORTER_CLASSES.stream().
186                     map(Class::getName).collect(Collectors.toSet());
187             put(IdMImplementationType.PROVISION_SORTER, classNames);
188 
189             classNames = ITImplementationLookup.COMMAND_CLASSES.stream().
190                     map(Class::getName).collect(Collectors.toSet());
191             put(IdRepoImplementationType.COMMAND, classNames);
192         }
193     };
194 
195     private final UserWorkflowAdapter uwf;
196 
197     private final AnySearchDAO anySearchDAO;
198 
199     private final EnableFlowableForTestUsers enableFlowableForTestUsers;
200 
201     private final ElasticsearchInit elasticsearchInit;
202 
203     private final OpenSearchInit openSearchInit;
204 
205     public ITImplementationLookup(
206             final UserWorkflowAdapter uwf,
207             final AnySearchDAO anySearchDAO,
208             final EnableFlowableForTestUsers enableFlowableForTestUsers,
209             final ElasticsearchInit elasticsearchInit,
210             final OpenSearchInit openSearchInit) {
211 
212         this.uwf = uwf;
213         this.anySearchDAO = anySearchDAO;
214         this.enableFlowableForTestUsers = enableFlowableForTestUsers;
215         this.elasticsearchInit = elasticsearchInit;
216         this.openSearchInit = openSearchInit;
217     }
218 
219     @Override
220     public int getOrder() {
221         return Integer.MAX_VALUE;
222     }
223 
224     @Override
225     public void load(final String domain, final DataSource datasource) {
226         // in case the Flowable extension is enabled, enable modifications for test users
227         if (enableFlowableForTestUsers != null && AopUtils.getTargetClass(uwf).getName().contains("Flowable")) {
228             AuthContextUtils.callAsAdmin(domain, () -> {
229                 enableFlowableForTestUsers.init(datasource);
230                 return null;
231             });
232         }
233 
234         // in case the Elasticsearch extension is enabled, reinit a clean index for all available domains
235         if (elasticsearchInit != null && AopUtils.getTargetClass(anySearchDAO).getName().contains("Elasticsearch")) {
236             AuthContextUtils.callAsAdmin(domain, () -> {
237                 elasticsearchInit.init();
238                 return null;
239             });
240         }
241 
242         // in case the OpenSearch extension is enabled, reinit a clean index for all available domains
243         if (openSearchInit != null && AopUtils.getTargetClass(anySearchDAO).getName().contains("OpenSearch")) {
244             AuthContextUtils.callAsAdmin(domain, () -> {
245                 openSearchInit.init();
246                 return null;
247             });
248         }
249     }
250 
251     @Override
252     public Set<String> getClassNames(final String type) {
253         return CLASS_NAMES.get(type);
254     }
255 
256     @Override
257     public Class<? extends ReportJobDelegate> getReportClass(final Class<? extends ReportConf> reportConfClass) {
258         return REPORT_CLASSES.get(reportConfClass);
259     }
260 
261     @Override
262     public Class<? extends AccountRule> getAccountRuleClass(
263             final Class<? extends AccountRuleConf> accountRuleConfClass) {
264 
265         return ACCOUNT_RULE_CLASSES.get(accountRuleConfClass);
266     }
267 
268     @Override
269     public Class<? extends PasswordRule> getPasswordRuleClass(
270             final Class<? extends PasswordRuleConf> passwordRuleConfClass) {
271 
272         return PASSWORD_RULE_CLASSES.get(passwordRuleConfClass);
273     }
274 
275     @Override
276     public Class<? extends PullCorrelationRule> getPullCorrelationRuleClass(
277             final Class<? extends PullCorrelationRuleConf> pullCorrelationRuleConfClass) {
278 
279         return PULL_CR_CLASSES.get(pullCorrelationRuleConfClass);
280     }
281 
282     @Override
283     public Class<? extends PushCorrelationRule> getPushCorrelationRuleClass(
284             final Class<? extends PushCorrelationRuleConf> pushCorrelationRuleConfClass) {
285 
286         return PUSH_CR_CLASSES.get(pushCorrelationRuleConfClass);
287     }
288 }