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