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.sra;
20  
21  import org.apache.syncope.common.keymaster.client.api.ServiceOps;
22  import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
23  import org.apache.syncope.common.keymaster.client.api.startstop.KeymasterStart;
24  import org.apache.syncope.common.keymaster.client.api.startstop.KeymasterStop;
25  import org.apache.syncope.sra.actuate.SRASessions;
26  import org.apache.syncope.sra.actuate.SyncopeCoreHealthIndicator;
27  import org.apache.syncope.sra.actuate.SyncopeSRAInfoContributor;
28  import org.apache.syncope.sra.security.CsrfRouteMatcher;
29  import org.apache.syncope.sra.security.LogoutRouteMatcher;
30  import org.apache.syncope.sra.security.PublicRouteMatcher;
31  import org.springframework.beans.factory.annotation.Qualifier;
32  import org.springframework.boot.autoconfigure.SpringBootApplication;
33  import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
34  import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
35  import org.springframework.boot.builder.SpringApplicationBuilder;
36  import org.springframework.boot.context.properties.EnableConfigurationProperties;
37  import org.springframework.cache.CacheManager;
38  import org.springframework.cloud.gateway.route.Route;
39  import org.springframework.cloud.gateway.route.RouteLocator;
40  import org.springframework.context.ConfigurableApplicationContext;
41  import org.springframework.context.annotation.Bean;
42  import org.springframework.web.server.WebExceptionHandler;
43  import reactor.core.publisher.Flux;
44  
45  @SpringBootApplication(proxyBeanMethods = false)
46  @EnableConfigurationProperties(SRAProperties.class)
47  public class SyncopeSRAApplication {
48  
49      public static void main(final String[] args) {
50          new SpringApplicationBuilder(SyncopeSRAApplication.class).
51                  properties("spring.config.name:sra").
52                  build().run(args);
53      }
54  
55      @Bean
56      public LogoutRouteMatcher logoutRouteMatcher() {
57          return new LogoutRouteMatcher();
58      }
59  
60      @Bean
61      public PublicRouteMatcher publicRouteMatcher() {
62          return new PublicRouteMatcher();
63      }
64  
65      @Bean
66      public CsrfRouteMatcher csrfRouteMatcher(final PublicRouteMatcher publicRouteMatcher) {
67          return new CsrfRouteMatcher(publicRouteMatcher);
68      }
69  
70      @ConditionalOnMissingBean
71      @Bean
72      public RouteProvider routeProvider(final ConfigurableApplicationContext ctx,
73              final ServiceOps serviceOps,
74              final SRAProperties props) {
75          return new RouteProvider(
76                  serviceOps,
77                  ctx,
78                  props.getAnonymousUser(),
79                  props.getAnonymousKey(),
80                  props.isUseGZIPCompression());
81      }
82  
83      @ConditionalOnMissingBean
84      @Bean
85      public RouteLocator routes(@Qualifier("routeProvider") final RouteProvider routeProvider) {
86          return () -> Flux.fromIterable(routeProvider.fetch()).map(Route.AbstractBuilder::build);
87      }
88  
89      @ConditionalOnMissingBean
90      @Bean
91      public SRASessions sraSessionsActuatorEndpoint(final CacheManager cacheManager) {
92          return new SRASessions(cacheManager);
93      }
94  
95      @ConditionalOnMissingBean
96      @Bean
97      public SyncopeCoreHealthIndicator syncopeCoreHealthIndicator(final ServiceOps serviceOps,
98              final SRAProperties props) {
99          return new SyncopeCoreHealthIndicator(
100                 serviceOps,
101                 props.getAnonymousUser(),
102                 props.getAnonymousKey(),
103                 props.isUseGZIPCompression());
104     }
105 
106     @ConditionalOnMissingBean
107     @Bean
108     public SyncopeSRAInfoContributor syncopeSRAInfoContributor() {
109         return new SyncopeSRAInfoContributor();
110     }
111 
112     @ConditionalOnProperty(
113             prefix = "keymaster", name = "enableAutoRegistration", havingValue = "true", matchIfMissing = true)
114     @Bean
115     public KeymasterStart keymasterStart() {
116         return new KeymasterStart(NetworkService.Type.SRA);
117     }
118 
119     @ConditionalOnProperty(
120             prefix = "keymaster", name = "enableAutoRegistration", havingValue = "true", matchIfMissing = true)
121     @Bean
122     public KeymasterStop keymasterStop() {
123         return new KeymasterStop(NetworkService.Type.SRA);
124     }
125 
126     @Bean
127     public WebExceptionHandler syncopeSRAWebExceptionHandler(
128             @Qualifier("routeProvider") final RouteProvider routeProvider,
129             final SRAProperties props) {
130         return new SyncopeSRAWebExceptionHandler(routeProvider, props);
131     }
132 }