Skip to content

Commit 0a61395

Browse files
authored
#68 Add service discovery starter (#70)
Signed-off-by: vityaman <vityaman.dev@yandex.ru>
1 parent d81fa94 commit 0a61395

File tree

17 files changed

+100
-128
lines changed

17 files changed

+100
-128
lines changed

backend/config/crypto/ca.cnf

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ default_bits = 4096
33
prompt = no
44
default_md = sha256
55
distinguished_name = dn
6-
req_extensions = req_ext
76
x509_extensions = v3_ca
87

98
[dn]
@@ -19,3 +18,4 @@ subjectKeyIdentifier = hash
1918
authorityKeyIdentifier = keyid:always,issuer
2019
basicConstraints = critical, CA:true
2120
keyUsage = critical, digitalSignature, cRLSign, keyCertSign
21+

backend/config/crypto/keys.bash

+16-14
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ ENV="$2"
77

88
ALIAS="itmo-dating"
99
ALIAS_BACKEND="$ALIAS-backend"
10+
1011
VALIDITY=1
11-
BACKEND_INSTALL_PATH="foundation/src/main/resources/keystore"
12-
CONSUL_INSTALL_PATH="consul/config"
13-
GATEWAY_INSTALL_PATH="gateway/src/main/resources/keystore"
1412
PASSWORD="$ITMO_DATING_KEY_STORE_PASSWORD"
1513

14+
INTERNAL_INSTALL_PATH="src/main/resources/keystore"
15+
FOUNDATION_INSTALL_PATH="foundation/$INTERNAL_INSTALL_PATH"
16+
GATEWAY_INSTALL_PATH="gateway/$INTERNAL_INSTALL_PATH"
17+
STARTER_SERVICE_DISCOVERY_INSTALL_PATH="starter-service-discovery/$INTERNAL_INSTALL_PATH"
18+
CONSUL_INSTALL_PATH="consul/config"
19+
1620
function generate() {
1721
echo "Phase: Generate"
1822

@@ -81,14 +85,13 @@ function distribute() {
8185
echo "Phase: Distribute"
8286

8387
echo "Copying package to the backend..."
84-
copy "$BACKEND_INSTALL_PATH" "$ALIAS_BACKEND.p12"
85-
copy "$BACKEND_INSTALL_PATH" "$ALIAS_BACKEND.jks"
86-
copy "$BACKEND_INSTALL_PATH" "$ALIAS_BACKEND.crt"
88+
copy "$FOUNDATION_INSTALL_PATH" "$ALIAS_BACKEND.p12"
89+
90+
echo "Copying package to the starter-service-discovery..."
91+
copy "$STARTER_SERVICE_DISCOVERY_INSTALL_PATH" "$ALIAS_BACKEND.jks"
8792

8893
echo "Copying package to the gateway..."
8994
copy "$GATEWAY_INSTALL_PATH" "$ALIAS_BACKEND.p12"
90-
copy "$GATEWAY_INSTALL_PATH" "$ALIAS_BACKEND.jks"
91-
copy "$GATEWAY_INSTALL_PATH" "$ALIAS_BACKEND.crt"
9295

9396
echo "Copying keys to the consul..."
9497
copy "$CONSUL_INSTALL_PATH" "$ALIAS_BACKEND.key"
@@ -107,15 +110,14 @@ function remove() {
107110
function clear() {
108111
echo "Phase: Clear"
109112

110-
echo "Removing package from the backend..."
111-
remove "$BACKEND_INSTALL_PATH" "$ALIAS_BACKEND.p12"
112-
remove "$BACKEND_INSTALL_PATH" "$ALIAS_BACKEND.jks"
113-
remove "$BACKEND_INSTALL_PATH" "$ALIAS_BACKEND.crt"
113+
echo "Removing package from the foundation..."
114+
remove "$FOUNDATION_INSTALL_PATH" "$ALIAS_BACKEND.p12"
115+
116+
echo "Removing package from the starter-service-discovery..."
117+
remove "$STARTER_SERVICE_DISCOVERY_INSTALL_PATH" "$ALIAS_BACKEND.jks"
114118

115119
echo "Removing package from the gateway..."
116120
remove "$GATEWAY_INSTALL_PATH" "$ALIAS_BACKEND.p12"
117-
remove "$GATEWAY_INSTALL_PATH" "$ALIAS_BACKEND.jks"
118-
remove "$GATEWAY_INSTALL_PATH" "$ALIAS_BACKEND.crt"
119121

120122
echo "Removing keys from the consul..."
121123
remove "$CONSUL_INSTALL_PATH" "$ALIAS_BACKEND.key"

backend/foundation/build.gradle.kts

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ plugins {
55
}
66

77
dependencies {
8+
api(project(":starter-service-discovery"))
9+
810
api(libs.org.springframework.boot.spring.boot)
911
api(libs.org.springframework.boot.spring.boot.starter.webflux)
1012
api(libs.org.springframework.boot.spring.boot.starter.data.r2dbc)

backend/foundation/src/main/kotlin/ru/ifmo/se/dating/spring/SpringConfiguration.kt

-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package ru.ifmo.se.dating.spring
22

3-
import org.springframework.cloud.client.discovery.EnableDiscoveryClient
43
import org.springframework.context.annotation.Bean
54
import org.springframework.context.annotation.Configuration
65
import org.springframework.scheduling.annotation.EnableScheduling
76
import java.time.Clock
87

98
@Configuration
109
@EnableScheduling
11-
@EnableDiscoveryClient
1210
class SpringConfiguration {
1311
@Bean
1412
fun clock(): Clock = Clock.systemDefaultZone()

backend/foundation/src/main/kotlin/ru/ifmo/se/dating/spring/security/ssl/KeyStoreExtractor.kt

-36
This file was deleted.

backend/foundation/src/main/resources/application-foundation-test.yml

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
spring:
2+
config:
3+
import: application-service-discovery-test.yml
14
security:
25
auth:
36
token:

backend/foundation/src/main/resources/application-foundation.yml

+7-32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
spring:
2+
config:
3+
import: application-service-discovery.yml
4+
datasource:
5+
driver-class-name: org.postgresql.Driver
6+
liquibase:
7+
change-log: database/changelog.sql
18
server:
29
ssl:
310
enabled: true
@@ -6,38 +13,6 @@ server:
613
key-store-password: ${KEY_STORE_PASSWORD}
714
protocol: TLSv1.3
815
enabled-protocols: TLSv1.3
9-
spring:
10-
cloud:
11-
consul:
12-
scheme: https
13-
host: server.dc1.consul
14-
port: 8501
15-
tls:
16-
key-store-instance-type: JKS
17-
certificate-path: keystore/itmo-dating-backend.jks
18-
certificate-password: ${server.ssl.key-store-password}
19-
key-store-path: keystore/itmo-dating-backend.jks
20-
key-store-password: ${server.ssl.key-store-password}
21-
discovery:
22-
enabled: true
23-
scheme: https
24-
instance-id: ${spring.application.name}:${HOSTNAME}
25-
heartbeat:
26-
enabled: true
27-
datasource:
28-
driver-class-name: org.postgresql.Driver
29-
liquibase:
30-
change-log: database/changelog.sql
31-
management:
32-
health:
33-
consul:
34-
enabled: true
35-
diskspace:
36-
enabled: false
37-
endpoints:
38-
web:
39-
exposure:
40-
include: health
4116
springdoc:
4217
api-docs:
4318
path: /openapi

backend/gateway/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ plugins {
33
}
44

55
dependencies {
6+
implementation(project(":starter-service-discovery"))
7+
68
implementation(libs.org.springframework.boot.spring.boot)
7-
implementation(libs.org.springframework.boot.spring.boot.starter.actuator)
89
implementation(libs.org.springframework.cloud.spring.cloud.starter.gateway)
9-
implementation(libs.org.springframework.cloud.spring.cloud.starter.consul.discovery)
1010

1111
implementation(libs.org.springdoc.springdoc.openapi.starter.webflux.ui)
1212

backend/gateway/src/main/kotlin/ru/ifmo/se/dating/gateway/Application.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package ru.ifmo.se.dating.gateway
22

33
import org.springframework.boot.autoconfigure.SpringBootApplication
44
import org.springframework.boot.runApplication
5-
import org.springframework.cloud.client.discovery.EnableDiscoveryClient
5+
import org.springframework.context.annotation.ComponentScan
66

77
@SpringBootApplication
8-
@EnableDiscoveryClient
8+
@ComponentScan(basePackages = ["ru.ifmo.se.dating"])
99
class Application
1010

1111
fun main(args: Array<String>) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring:
2+
config:
3+
import: application-service-discovery-test.yml

backend/gateway/src/main/resources/application.yml

+10-34
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
server:
2-
ssl:
3-
enabled: true
4-
key-store-type: PKCS12
5-
key-store: classpath:keystore/itmo-dating-backend.p12
6-
key-store-password: ${KEY_STORE_PASSWORD}
7-
protocol: TLSv1.3
8-
enabled-protocols: TLSv1.3
91
spring:
2+
config:
3+
import: application-service-discovery.yml
104
application:
115
name: gateway
126
cloud:
@@ -97,32 +91,14 @@ spring:
9791
- Method=GET
9892
- Path=/api/locations
9993

100-
consul:
101-
scheme: https
102-
host: server.dc1.consul
103-
port: 8501
104-
tls:
105-
key-store-instance-type: JKS
106-
certificate-path: keystore/itmo-dating-backend.jks
107-
certificate-password: ${server.ssl.key-store-password}
108-
key-store-path: keystore/itmo-dating-backend.jks
109-
key-store-password: ${server.ssl.key-store-password}
110-
discovery:
111-
enabled: true
112-
scheme: https
113-
instance-id: ${spring.application.name}:${HOSTNAME}
114-
heartbeat:
115-
enabled: true
116-
management:
117-
health:
118-
consul:
119-
enabled: true
120-
diskspace:
121-
enabled: false
122-
endpoints:
123-
web:
124-
exposure:
125-
include: health
94+
server:
95+
ssl:
96+
enabled: true
97+
key-store-type: PKCS12
98+
key-store: classpath:keystore/itmo-dating-backend.p12
99+
key-store-password: ${KEY_STORE_PASSWORD}
100+
protocol: TLSv1.3
101+
enabled-protocols: TLSv1.3
126102
springdoc:
127103
swagger-ui:
128104
path: /swagger-ui.html

backend/settings.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ include(
1111
":authik",
1212
":matchmaker",
1313
":people",
14+
":starter-service-discovery",
1415
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
plugins {
2+
id("buildlogic.kotlin-library-conventions")
3+
kotlin("plugin.spring")
4+
}
5+
6+
dependencies {
7+
api(libs.org.springframework.boot.spring.boot.starter.actuator)
8+
api(libs.org.springframework.cloud.spring.cloud.starter.consul.discovery)
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package ru.ifmo.se.dating.spring.consul
2+
3+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient
4+
5+
@EnableDiscoveryClient
6+
class ConsulConfiguration

backend/gateway/src/main/kotlin/ru/ifmo/se/dating/gateway/KeyStoreExtractor.kt backend/starter-service-discovery/src/main/kotlin/ru/ifmo/se/dating/spring/consul/KeyStoreExtractor.kt

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
package ru.ifmo.se.dating.gateway
1+
package ru.ifmo.se.dating.spring.consul
22

33
import jakarta.annotation.PostConstruct
44
import org.springframework.beans.factory.annotation.Value
5+
import org.springframework.context.annotation.Profile
56
import org.springframework.stereotype.Component
67
import java.io.File
78
import java.nio.file.Files
89
import java.nio.file.Paths
910
import java.nio.file.StandardCopyOption
1011

11-
12+
@Profile("!test")
1213
@Component
1314
class KeyStoreExtractor(
1415
@Value("\${spring.cloud.consul.tls.certificate-path}")
@@ -24,8 +25,8 @@ class KeyStoreExtractor(
2425
}
2526

2627
private fun extract(path: String) {
27-
val inputStream = javaClass.classLoader.getResourceAsStream(certificatePath)!!
28-
File(certificatePath).parentFile?.mkdirs()
29-
Files.copy(inputStream, Paths.get(certificatePath), StandardCopyOption.REPLACE_EXISTING)
28+
val inputStream = javaClass.classLoader.getResourceAsStream(path)!!
29+
File(path).parentFile?.mkdirs()
30+
Files.copy(inputStream, Paths.get(path), StandardCopyOption.REPLACE_EXISTING)
3031
}
3132
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring:
2+
cloud:
3+
consul:
4+
enabled: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
spring:
2+
cloud:
3+
consul:
4+
scheme: https
5+
host: server.dc1.consul
6+
port: 8501
7+
tls:
8+
key-store-instance-type: JKS
9+
certificate-path: keystore/itmo-dating-backend.jks
10+
certificate-password: ${server.ssl.key-store-password}
11+
key-store-path: keystore/itmo-dating-backend.jks
12+
key-store-password: ${server.ssl.key-store-password}
13+
discovery:
14+
enabled: true
15+
scheme: https
16+
instance-id: ${spring.application.name}:${HOSTNAME}
17+
heartbeat:
18+
enabled: true
19+
management:
20+
health:
21+
consul:
22+
enabled: true
23+
diskspace:
24+
enabled: false
25+
endpoints:
26+
web:
27+
exposure:
28+
include: health

0 commit comments

Comments
 (0)