Skip to content

Commit 94c1175

Browse files
Rodrigo Dos SantosRodrigo Dos Santos
authored andcommitted
Fixed consul issue - build authentication-service, build person-service, build user-service, build kotlin-service
1 parent b00c342 commit 94c1175

File tree

16 files changed

+175
-81
lines changed

16 files changed

+175
-81
lines changed

.github/workflows/kubernetes/deployment-authentication-service.yml

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ spec:
3232
spec:
3333
containers:
3434
- name: authentication-service
35-
image: fielcapao/microservices-design-patterns-authentication-service:latest #eu.gcr.io/api-project-646370370811/authentication-service #eu.gcr.io/api-project-646370370811/authentication-service:7f79c29fbc58052bb7d86bceeb7722b0185c66c4
36-
# imagePullPolicy: IfNotPresent
35+
image: authentication-service #fielcapao/microservices-design-patterns-authentication-service:latest #eu.gcr.io/api-project-646370370811/authentication-service #eu.gcr.io/api-project-646370370811/authentication-service:7f79c29fbc58052bb7d86bceeb7722b0185c66c4
36+
imagePullPolicy: Never
3737
resources:
3838
requests:
3939
memory: "256Mi"
@@ -45,7 +45,7 @@ spec:
4545
- name: SERVER_PORT
4646
value: "9999"
4747
- name: SPRING_PROFILES_ACTIVE
48-
value: dev
48+
value: prod
4949
- name: SPRING_DATA_MONGODB_URI
5050
value: mongodb://mongodb:27017
5151
- name: SPRING_DATA_MONGODB_DATABASE
@@ -58,12 +58,22 @@ spec:
5858
value: "false"
5959
- name: SPRING_CLOUD_KUBERNETES_CONFIG_NAMESPACE
6060
value: "default"
61-
- name: LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_SECURITY
62-
value: debug
63-
- name: CLIENT_ID
64-
value: ${CLIENT_ID}
65-
- name: CLIENT_SECRET
66-
value: ${CLIENT_SECRET}
61+
- name: LOGGING_LEVEL_ROOT
62+
value: trace
63+
# - name: CLIENT_ID
64+
# value: ${CLIENT_ID}
65+
# - name: CLIENT_SECRET
66+
# value: ${CLIENT_SECRET}
67+
- name: MANAGEMENT_ENDPOINTS_WEB_CORS_ALLOW_CREDENTIALS
68+
value: "false"
69+
- name: SPRING_MAIN_ALLOW_CIRCULAR_REFERENCES
70+
value: "true"
71+
- name: KEYSTORE_PASSWORD
72+
value: Cert202!
73+
- name: KEYSTORE
74+
value: file:/etc/ssl_certs/www.spendingbetter.com.jks
75+
- name: KEYSTORE_ALIAS
76+
value: spendingbetter
6777
ports:
6878
- containerPort: 9999
6979
livenessProbe:

authentication-common/src/main/java/com/microservice/authentication/autoconfigure/AuthenticationCommonConfiguration.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.microservice.authentication.autoconfigure;
22

3+
import java.security.KeyPair;
4+
import java.security.interfaces.RSAPublicKey;
35
import java.util.Date;
46
import java.util.HashMap;
57
import java.util.List;
@@ -20,6 +22,7 @@
2022
import org.springframework.context.annotation.Bean;
2123
import org.springframework.context.annotation.Configuration;
2224
import org.springframework.context.annotation.Primary;
25+
import org.springframework.context.annotation.Profile;
2326
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
2427
import org.springframework.core.io.FileSystemResource;
2528
import org.springframework.core.io.Resource;
@@ -125,12 +128,8 @@ public OAuth2AccessToken enhance(
125128
}
126129
converter.setVerifierKey(keyValue);
127130
} else if (jwt.getKeyStore() != null) {
128-
Resource keyStore = new FileSystemResource(jwt.getKeyStore().replaceFirst("file:", ""));
129-
char[] keyStorePassword = Base64DecodeUtil.decodePassword(jwt.getKeyStorePassword());
130-
KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(keyStore, keyStorePassword);
131-
132-
String keyAlias = jwt.getKeyAlias();
133-
converter.setKeyPair(keyStoreKeyFactory.getKeyPair(keyAlias, keyStorePassword));
131+
KeyPair keyPair = getKeyPair(authenticationProperties);
132+
converter.setKeyPair(keyPair);
134133
}
135134
if (!CollectionUtils.isEmpty(this.configurers)) {
136135
AnnotationAwareOrderComparator.sort(this.configurers);
@@ -141,6 +140,25 @@ public OAuth2AccessToken enhance(
141140
return converter;
142141
}
143142

143+
@Profile("prod")
144+
@Bean
145+
KeyPair getKeyPair(AuthenticationProperties authenticationProperties) {
146+
AuthenticationProperties.Jwt jwt = authenticationProperties.getJwt();
147+
Resource keyStore = new FileSystemResource(jwt.getKeyStore().replaceFirst("file:", ""));
148+
char[] keyStorePassword = Base64DecodeUtil.decodePassword(jwt.getKeyStorePassword());
149+
KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(keyStore, keyStorePassword);
150+
151+
String keyAlias = jwt.getKeyAlias();
152+
return keyStoreKeyFactory.getKeyPair(keyAlias, keyStorePassword);
153+
}
154+
155+
156+
@Profile("prod")
157+
@Bean
158+
RSAPublicKey publicKey(KeyPair keyPair) {
159+
return (RSAPublicKey) keyPair.getPublic();
160+
}
161+
144162
@Bean
145163
UserDetailsService sharedAuthenticationService(AuthenticationCommonRepository authenticationCommonRepository) {
146164
return new SharedAuthenticationServiceImpl(authenticationCommonRepository);

authentication-service/src/main/java/com/microservice/authentication/AuthenticationServiceApplication.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.microservice.authentication;
22

3-
import java.security.KeyPair;
3+
import java.util.Collections;
44
import java.util.Properties;
55
import java.util.UUID;
66

7-
import com.microservice.authentication.autoconfigure.AuthenticationProperties;
87
import com.microservice.authentication.common.model.Authentication;
8+
import com.microservice.authentication.common.model.Authority;
99
import com.microservice.authentication.common.repository.AuthenticationCommonRepository;
10-
import com.microservice.authentication.common.service.Base64DecodeUtil;
1110
import com.microservice.authentication.service.CustomLogoutSuccessHandler;
1211
import com.microservice.authentication.service.RedisTokenStoreService;
1312
import com.microservice.web.common.util.constants.DefaultUsers;
@@ -23,8 +22,6 @@
2322
import org.springframework.boot.info.GitProperties;
2423
import org.springframework.context.annotation.Bean;
2524
import org.springframework.context.annotation.Primary;
26-
import org.springframework.context.annotation.Profile;
27-
import org.springframework.core.io.FileSystemResource;
2825
import org.springframework.data.mongodb.core.MongoTemplate;
2926
import org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener;
3027
import org.springframework.data.redis.connection.RedisConnectionFactory;
@@ -34,7 +31,6 @@
3431
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
3532
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
3633
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
37-
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;
3834
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
3935
import org.springframework.session.web.http.CookieSerializer;
4036
import org.springframework.session.web.http.DefaultCookieSerializer;
@@ -52,16 +48,6 @@ public static void main(String[] args) {
5248
SpringApplication.run(AuthenticationServiceApplication.class, args);
5349
}
5450

55-
@Profile("prod")
56-
@ConditionalOnMissingBean
57-
@Bean
58-
KeyPair keyPair(AuthenticationProperties properties) {
59-
AuthenticationProperties.Jwt jwt = properties.getJwt();
60-
char[] password = Base64DecodeUtil.decodePassword(jwt.getKeyStorePassword());
61-
KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(new FileSystemResource(jwt.getKeyStore().replaceFirst("file:", "")), password);
62-
return keyStoreKeyFactory.getKeyPair(jwt.getKeyAlias());
63-
}
64-
6551
@ConditionalOnProperty(prefix = "configuration", name = "initialLoad", havingValue = "true", matchIfMissing = true)
6652
@Bean
6753
CommandLineRunner runner(AuthenticationCommonRepository authenticationCommonRepository,
@@ -80,6 +66,19 @@ CommandLineRunner runner(AuthenticationCommonRepository authenticationCommonRepo
8066
authentication = mongoTemplate.save(authentication, "users_login");
8167
log.debug("Created Default Authentication: {}", authentication);
8268
}
69+
if (authenticationCommonRepository.findByEmail("admin@gmail.com") == null) {
70+
Authentication authentication = Authentication.builder()
71+
.email("admin@gmail.com")
72+
.password(passwordEncoder.encode("P@ssword2020!"))
73+
.fullName("Admin")
74+
.enabled(true)
75+
.id(UUID.randomUUID().toString())
76+
.authorities(Collections.singletonList(new Authority("ROLE_ADMIN")))
77+
.build();
78+
log.debug("Creating admin authentication: {}", authentication);
79+
authentication = mongoTemplate.save(authentication, "users_login");
80+
log.debug("Created admin Authentication: {}", authentication);
81+
}
8382
};
8483
}
8584

authentication-service/src/main/java/com/microservice/authentication/config/SpringSecurityFormConfiguration.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.IOException;
44
import java.nio.charset.StandardCharsets;
5-
import java.security.KeyPair;
65
import java.security.interfaces.RSAPublicKey;
76
import java.util.Collections;
87
import java.util.Map;
@@ -146,8 +145,7 @@ public JwtDecoder jwtDecoder(AuthenticationProperties properties) {
146145
SecretKeySpec secretKeySpec = new SecretKeySpec(jwt.getKeyValue().getBytes(StandardCharsets.UTF_8), "HS256");
147146
return NimbusJwtDecoder.withSecretKey(secretKeySpec).build();
148147
} else {
149-
KeyPair keyPair = getApplicationContext().getBean(KeyPair.class);
150-
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
148+
RSAPublicKey publicKey = getApplicationContext().getBean(RSAPublicKey.class);
151149
return NimbusJwtDecoder.withPublicKey(publicKey).build();
152150
}
153151
}

authentication-service/src/main/resources/application.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ opentracing:
1818
spring:
1919
web:
2020
skip-pattern: "/actuator.*|/api-docs.*|/swagger.*|.*\\.png|.*\\.css|.*\\.js|.*\\.html|/favicon.ico|/hystrix.stream"
21+
2122
---
2223
spring:
2324
config:
24-
import: consul:${consul_url:localhost:8500}
2525
activate:
2626
on-profile: consul
27+
import: consul:${consul_url:localhost:8500}
2728
cloud:
2829
consul:
2930
config:
3031
fail-fast: ${FAIL_FAST:true}
3132
format: yaml
33+
autoconfigure:
34+
exclude: org.springframework.cloud.consul.config.ConsulConfigAutoConfiguration

authentication-service/src/test/java/com/microservice/authentication/AuthenticationServiceApplicationIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public void shouldReturnOkWithToken() throws Exception {
308308
.expirationTime(Date.from(ZonedDateTime.now().plusMinutes(1).toInstant()))
309309
.issueTime(new Date())
310310
.notBeforeTime(new Date())
311-
.claim("authorities", Collections.singletonList("ROLE_ADMIN"))
311+
.claim("authorities", Collections.singletonList("ADMIN"))
312312
.jwtID(UUID.randomUUID().toString())
313313
.issuer("jwt")
314314
.build();

edge-server/src/main/java/com/springboot/edgeserver/config/AuthenticationZuulFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public Object run() {
4949
log.debug("User is not authenticated: {}", authentication.getName());
5050
context.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
5151
context.setResponseBody(String.format("To access(%s) user must be authenticated!", request.getRequestURI()));
52-
} else if (authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).anyMatch("ROLE_ADMIN"::equals)) {
52+
} else if (authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).anyMatch("ADMIN"::equals)) {
5353
log.debug("User has admin role: {}", authentication.getAuthorities());
5454
context.addZuulRequestHeader("X-WEBAUTH-USER", "admin");
5555
} else {

kotlin-service/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@
9797
<version>5.0.0</version>
9898
</dependency>
9999

100+
<dependency>
101+
<groupId>com.github.javafaker</groupId>
102+
<artifactId>javafaker</artifactId>
103+
<version>1.0.2</version>
104+
</dependency>
105+
100106
<!-- Test dependencies -->
101107
<dependency>
102108
<groupId>org.springframework.boot</groupId>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,50 @@
11
package com.microservice.kotlin.config
22

3+
import com.github.javafaker.Faker
4+
import com.microservice.kotlin.model.Task
35
import com.microservice.kotlin.repository.TaskRepository
6+
import org.slf4j.LoggerFactory
7+
import org.springframework.beans.factory.annotation.Value
8+
import org.springframework.boot.CommandLineRunner
9+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
410
import org.springframework.context.annotation.Bean
511
import org.springframework.context.annotation.Configuration
612
import org.springframework.context.annotation.Primary
713
import org.springframework.data.mongodb.config.EnableMongoAuditing
814
import org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener
915
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories
1016
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean
17+
import java.util.stream.IntStream
1118

1219
@Configuration
1320
@EnableMongoAuditing
1421
@EnableMongoRepositories(basePackageClasses = [TaskRepository::class])
1522
class ServiceConfiguration {
23+
private val log = LoggerFactory.getLogger(javaClass)
24+
25+
var faker: Faker = Faker()
26+
1627
@Bean
1728
fun validatingMongoEventListener(validator: LocalValidatorFactoryBean): ValidatingMongoEventListener = ValidatingMongoEventListener(validator)
1829

1930
@Primary
2031
@Bean
2132
fun validator(): LocalValidatorFactoryBean = LocalValidatorFactoryBean()
33+
34+
@ConditionalOnProperty(prefix = "load.data", name = ["tasks"], havingValue = "true")
35+
@Bean
36+
fun runner(
37+
@Value("\${load.data.tasks.total:20}") total: Int?,
38+
taskRepository: TaskRepository
39+
): CommandLineRunner? {
40+
return CommandLineRunner {
41+
if (taskRepository.count() == 0L) {
42+
val book = faker.book()
43+
IntStream.range(0, total!!).forEach {
44+
val task = Task(name = book.genre())
45+
log.info("task: {}", taskRepository.save(task))
46+
}
47+
}
48+
}
49+
}
2250
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
spring:
22
application:
33
name: kotlin-service
4-
cloud:
5-
consul:
6-
config:
7-
fail-fast: ${FAIL_FAST:true}
8-
format: yaml
94
main:
105
allow-bean-definition-overriding: true
11-
config:
12-
import: consul:${consul_url:localhost:8500}
136
management:
147
endpoints:
158
web:
@@ -22,3 +15,17 @@ opentracing:
2215
spring:
2316
web:
2417
skip-pattern: "/actuator.*|/api-docs.*|/swagger.*|.*\\.png|.*\\.css|.*\\.js|.*\\.html|/favicon.ico|/hystrix.stream"
18+
19+
---
20+
spring:
21+
config:
22+
activate:
23+
on-profile: consul
24+
import: consul:${consul_url:localhost:8500}
25+
cloud:
26+
consul:
27+
config:
28+
fail-fast: ${FAIL_FAST:true}
29+
format: yaml
30+
autoconfigure:
31+
exclude: org.springframework.cloud.consul.config.ConsulConfigAutoConfiguration

0 commit comments

Comments
 (0)