View Javadoc
1   package cn.home1.cloud.config.server;
2   
3   import static org.apache.commons.lang3.StringUtils.isBlank;
4   import static org.apache.commons.lang3.StringUtils.isNotBlank;
5   import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE;
6   import static org.springframework.web.bind.annotation.RequestMethod.GET;
7   import static org.springframework.web.bind.annotation.RequestMethod.POST;
8   
9   import cn.home1.cloud.config.server.security.ConfigSecurity;
10  import cn.home1.cloud.config.server.ssh.DeployKey;
11  
12  import lombok.SneakyThrows;
13  import lombok.extern.slf4j.Slf4j;
14  
15  import org.eclipse.jgit.api.TransportConfigCallback;
16  import org.springframework.beans.factory.annotation.Autowired;
17  import org.springframework.boot.SpringApplication;
18  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
19  import org.springframework.boot.autoconfigure.SpringBootApplication;
20  import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
21  import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
22  import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
23  import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
24  import org.springframework.cloud.config.server.EnableConfigServer;
25  import org.springframework.cloud.config.server.config.ConfigServerHealthIndicator;
26  import org.springframework.cloud.config.server.environment.EnvironmentRepository;
27  import org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentProperties;
28  import org.springframework.cloud.config.server.ssh.FileBasedSshTransportConfigCallback;
29  import org.springframework.cloud.config.server.ssh.PropertiesBasedSshTransportConfigCallback;
30  import org.springframework.context.annotation.Bean;
31  import org.springframework.context.annotation.Configuration;
32  import org.springframework.core.env.Environment;
33  import org.springframework.http.HttpStatus;
34  import org.springframework.http.ResponseEntity;
35  import org.springframework.web.bind.annotation.RequestMapping;
36  import org.springframework.web.bind.annotation.RequestParam;
37  import org.springframework.web.bind.annotation.ResponseBody;
38  import org.springframework.web.bind.annotation.RestController;
39  
40  import java.io.File;
41  
42  /**
43   * see: {@link org.springframework.cloud.config.server.environment.EnvironmentController}
44   * see: {@link org.springframework.cloud.config.server.config.ConfigServerMvcConfiguration}
45   */
46  @RestController
47  @SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
48  @Slf4j
49  public class ConfigServer {
50  
51      private static final DeployKey DEPLOY_KEY;
52  
53      static {
54          // set default profiles
55          final String profilesFromEnv = System.getenv("SPRING_PROFILES_ACTIVE");
56          final String profilesFromProperty = System.getProperty("spring.profiles.active", "");
57          if (isBlank(profilesFromEnv) && isBlank(profilesFromProperty)) {
58              System.setProperty("spring.profiles.active", "port_nonsecure");
59          }
60  
61          // set deploy key
62          final String deployKeyFromEnv = System.getenv("SPRING_CLOUD_CONFIG_SERVER_GIT_DEPLOYKEY");
63          final String deployKeyFromProperty = System.getProperty("spring.cloud.config.server.git.deploy-key", "");
64          final String deployKeyLocation;
65          if (isNotBlank(deployKeyFromEnv)) {
66              deployKeyLocation = deployKeyFromEnv;
67          } else if (isNotBlank(deployKeyFromProperty)) {
68              deployKeyLocation = deployKeyFromProperty;
69          } else {
70              deployKeyLocation = System.getProperty("user.home") + "/.ssh/id_rsa";
71          }
72  
73          final File deployKeyFile = new File(DeployKey.getPrivateKeyPath(deployKeyLocation));
74          if (deployKeyFile.exists() && deployKeyFile.canRead()) {
75              DEPLOY_KEY = new DeployKey(deployKeyLocation);
76              DEPLOY_KEY.setUp(null);
77          } else {
78              DEPLOY_KEY = null;
79          }
80      }
81  
82      @Autowired
83      private Environment environment;
84      @Autowired
85      private ConfigSecurity configSecurity;
86  
87      public static void main(final String... args) {
88          SpringApplication.run(ConfigServer.class, args);
89      }
90  
91      @ResponseBody
92      @RequestMapping(path = {"/", "${spring.cloud.config.server.prefix:}/"}, method = GET)
93      public String index() {
94          return "Visit https://github.com/cloud-ready/spring-cloud-config-server for more info.";
95      }
96  
97      @ResponseBody
98      @RequestMapping(path = "${spring.cloud.config.server.prefix:}/deployKeyPublic", method = GET)
99      public ResponseEntity<String> getDeployKeyPublic() {
100         final ResponseEntity<String> responseEntity;
101         if (ConfigServer.DEPLOY_KEY != null) {
102             responseEntity = new ResponseEntity<>(ConfigServer.DEPLOY_KEY.getPublicKey(), HttpStatus.OK);
103         } else {
104             responseEntity = new ResponseEntity<>("", HttpStatus.NOT_FOUND);
105         }
106         return responseEntity;
107     }
108 
109     @SneakyThrows
110     @ResponseBody
111     @RequestMapping(path = "${spring.cloud.config.server.prefix:}/encryptParentPassword", method = POST,
112         consumes = APPLICATION_FORM_URLENCODED_VALUE)
113     public String encryptParentPassword(
114         @RequestParam("application") final String application,
115         @RequestParam("parentApplication") final String parentApplication,
116         @RequestParam("parentPassword") final String parentPassword
117     ) {
118         return this.configSecurity.encryptParentPassword(application, parentApplication, parentPassword);
119     }
120 
121     @Configuration
122     public static class HealthIndicatorConfiguration {
123 
124         @Bean
125         @ConditionalOnProperty(value = "spring.cloud.config.server.health.enabled", matchIfMissing = true)
126         public ConfigServerHealthIndicator configServerHealthIndicator(final EnvironmentRepository repository) {
127             return new ConfigServerHealthIndicator(repository);
128         }
129     }
130 
131     @Configuration
132     @EnableAutoConfiguration
133     @EnableConfigServer
134     //@EnableEurekaClient
135     @EnableDiscoveryClient
136     protected class ConfigServerConfiguration {
137 
138         /**
139          * see: {@link org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepositoryFactory}
140          */
141         @ConditionalOnMissingBean(TransportConfigCallback.class)
142         @Bean
143         public TransportConfigCallback propertiesBasedSshTransportCallback(final MultipleJGitEnvironmentProperties sshUriProperties) {
144             if (ConfigServer.DEPLOY_KEY != null) {
145                 DEPLOY_KEY.setUp(sshUriProperties);
146                 return new PropertiesBasedSshTransportConfigCallback(sshUriProperties);
147             } else {
148                 return new FileBasedSshTransportConfigCallback(sshUriProperties);
149             }
150         }
151     }
152 }