@@ -21,29 +21,40 @@ import org.springframework.context.annotation.Bean
21
21
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
22
22
import org.springframework.scheduling.annotation.EnableScheduling
23
23
import org.springframework.stereotype.Component
24
+ import org.springframework.util.Base64Utils
24
25
import org.springframework.web.method.support.HandlerMethodArgumentResolver
25
26
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
26
27
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
27
- import java.io.File
28
+ import java.lang.IllegalArgumentException
29
+ import java.lang.IllegalStateException
28
30
import java.net.URI
29
31
30
32
@ConfigurationPropertiesScan
31
33
@EnableScheduling
32
34
@SpringBootApplication
33
35
class CodeGartenApplication (private val configProperties : ConfigProperties ) {
34
36
35
- private val cryptoUtils = CryptoUtils (System .getenv(configProperties.cipherKeyEnv)!! )
37
+ private val cryptoUtils: CryptoUtils
38
+
39
+ init {
40
+ val cipherKey = System .getenv(configProperties.cipherKeyEnv)
41
+ ? : throw IllegalStateException (" Missing ${configProperties.cipherKeyEnv} environment variable!" )
42
+
43
+ cryptoUtils = CryptoUtils (cipherKey)
44
+ }
36
45
37
46
@Bean
38
47
fun getJdbi (): Jdbi {
39
48
val jdbcConnectionString = System .getenv(configProperties.dbJdbcConnectionStringEnv)
40
49
val connectionUrl = System .getenv(configProperties.dbUrlConnectionStringEnv)
50
+ if (jdbcConnectionString == null && connectionUrl == null )
51
+ throw IllegalStateException (" Missing ${configProperties.dbJdbcConnectionStringEnv} or ${configProperties.dbUrlConnectionStringEnv} environment variable!" )
41
52
42
53
val connectionString: String =
43
54
if (jdbcConnectionString != null )
44
55
jdbcConnectionString
45
56
else {
46
- val dbUri = URI (connectionUrl!! )
57
+ val dbUri = URI (connectionUrl)
47
58
48
59
val username: String = dbUri.userInfo.split(" :" )[0 ]
49
60
val password: String = dbUri.userInfo.split(" :" )[1 ]
@@ -57,23 +68,36 @@ class CodeGartenApplication(private val configProperties: ConfigProperties) {
57
68
58
69
@Bean
59
70
fun getGithubInterface (): GitHubInterface {
60
- val mapper: ObjectMapper = Jackson2ObjectMapperBuilder ().build()
71
+ val ghAppPropertiesEnv = System .getenv(configProperties.gitHubAppPropertiesEnv)
72
+ ? : throw IllegalStateException (" Missing ${configProperties.gitHubAppPropertiesEnv} environment variable!" )
73
+ val ghAppPrivateKeyEnv = System .getenv(configProperties.gitHubAppPrivateKeyEnv)
74
+ ? : throw IllegalStateException (" Missing ${configProperties.gitHubAppPrivateKeyEnv} environment variable!" )
75
+
76
+ val ghAppPropertiesString = try {
77
+ Base64Utils .decodeFromString(ghAppPropertiesEnv)
78
+ } catch (ex: IllegalArgumentException ) {
79
+ throw IllegalArgumentException (" ${configProperties.gitHubAppPropertiesEnv} environment variable is not a valid Base64 string!" )
80
+ }
81
+
82
+ val ghAppPrivateKeyString = try {
83
+ String (Base64Utils .decodeFromString(ghAppPrivateKeyEnv))
84
+ } catch (ex: IllegalArgumentException ) {
85
+ throw IllegalArgumentException (" ${configProperties.gitHubAppPrivateKeyEnv} environment variable is not a valid Base64 string!" )
86
+ }
61
87
88
+ val mapper: ObjectMapper = Jackson2ObjectMapperBuilder ().build()
62
89
val ghAppProperties = mapper.readValue(
63
- File ( System .getenv(configProperties.gitHubAppPropertiesPathEnv) !! ) ,
90
+ ghAppPropertiesString ,
64
91
GitHubAppProperties ::class .java
65
92
)
66
93
67
94
return GitHubInterface (
68
95
ghAppProperties,
69
- cryptoUtils.readRsaPrivateKey(
70
- System .getenv(configProperties.gitHubAppPrivateKeyPemPath)!!
71
- ),
96
+ cryptoUtils.readRsaPrivateKey(ghAppPrivateKeyString),
72
97
mapper
73
98
)
74
99
}
75
100
76
-
77
101
@Bean
78
102
fun getCryptoUtils () = cryptoUtils
79
103
}
0 commit comments