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.core.logic.audit;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import org.apache.logging.log4j.Level;
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.core.LoggerContext;
26  import org.apache.logging.log4j.core.config.LoggerConfig;
27  import org.apache.syncope.common.lib.types.AuditLoggerName;
28  import org.apache.syncope.core.logic.IdRepoLogicContext;
29  import org.apache.syncope.core.persistence.api.DomainHolder;
30  import org.apache.syncope.ext.opensearch.client.OpenSearchIndexManager;
31  import org.springframework.boot.autoconfigure.AutoConfigureBefore;
32  import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
33  import org.springframework.context.annotation.Bean;
34  import org.springframework.context.annotation.Configuration;
35  
36  @AutoConfigureBefore(IdRepoLogicContext.class)
37  @Configuration(proxyBeanMethods = false)
38  public class OpenSearchLogicContext {
39  
40      @ConditionalOnMissingBean(name = { "defaultAuditAppenders", "openSearchDefaultAuditAppenders" })
41      @Bean
42      public List<AuditAppender> defaultAuditAppenders(
43              final DomainHolder domainHolder,
44              final OpenSearchIndexManager openSearchIndexManager) {
45  
46          List<AuditAppender> auditAppenders = new ArrayList<>();
47  
48          LoggerContext logCtx = (LoggerContext) LogManager.getContext(false);
49          domainHolder.getDomains().forEach((domain, dataSource) -> {
50              AuditAppender appender = new OpenSearchAuditAppender(domain, openSearchIndexManager);
51  
52              LoggerConfig logConf = new LoggerConfig(AuditLoggerName.getAuditLoggerName(domain), null, false);
53              logConf.addAppender(appender.getTargetAppender(), Level.DEBUG, null);
54              logConf.setLevel(Level.DEBUG);
55              logCtx.getConfiguration().addLogger(logConf.getName(), logConf);
56  
57              auditAppenders.add(appender);
58          });
59  
60          return auditAppenders;
61      }
62  }