From 0d806bd170791f9c45f88420681eb2f01f11b39e Mon Sep 17 00:00:00 2001 From: Roy Date: Tue, 27 Aug 2024 12:25:22 +0300 Subject: [PATCH 01/25] installed keycloak and added configurations --- docker-compose.yml | 32 +++++++++++++++++++ pom.xml | 5 +++ .../config/SecurityConfig.java | 19 +++++++++++ src/main/resources/application.properties | 2 ++ 4 files changed, 58 insertions(+) create mode 100644 docker-compose.yml create mode 100644 src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfig.java diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..d37b031 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,32 @@ +version: '3.8' +services: + keycloak-db: + container_name: keycloak-db + image: postgres:16.2 + ports: + - "5432:5432" + environment: + POSTGRES_DB: keycloak + POSTGRES_USER: postgres + POSTGRES_PASSWORD: 1 + volumes: + - ./data:/var/lib/postgresql/data # Maps the local directory ./data to the container’s data directory for data persistence. + - ./docker/init.sql:/docker-entrypoint-initdb.d/init.sql + keycloak: + container_name: keycloak + image: quay.io/keycloak/keycloak:24.0.1 + command: [ "start-dev", "--import-realm" ] + environment: + DB_VENDOR: POSTGRES + DB_ADDR: postgres + POSTGRES_DB: keycloak + POSTGRES_USER: postgres + POSTGRES_PASSWORD: 1 + KEYCLOAK_ADMIN: admin + KEYCLOAK_ADMIN_PASSWORD: admin + ports: + - "8181:8080" + volumes: + - ./docker/keycloak/realms/:/opt/keycloak/data/import/ + depends_on: + - keycloak-db diff --git a/pom.xml b/pom.xml index eb6e47e..82cd691 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,11 @@ spring-boot-starter-test test + + + org.springframework.boot + spring-boot-starter-oauth2-resource-server + diff --git a/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfig.java b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfig.java new file mode 100644 index 0000000..b391068 --- /dev/null +++ b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfig.java @@ -0,0 +1,19 @@ +package com.MTAPizza.Sympoll.api_gateway_service.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.Customizer; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.web.SecurityFilterChain; + +@Configuration +public class SecurityConfig { + @Bean + public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception { + return httpSecurity.authorizeHttpRequests(authorize -> authorize + .anyRequest().authenticated()) + .oauth2ResourceServer(oauth2 -> oauth2.jwt(Customizer.withDefaults())) + .build(); + } +} + diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 32073c1..e100453 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -6,3 +6,5 @@ poll.route.uri=http://poll-service:8082 user.route.uri=http://user-service:8083 vote.route.uri=http://vote-service:8084 group.route.uri=http://group-service:8085 + +spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8181/realms/sympoll-security-realm From 0444b2ccc31771eadfcf6735cf857e8ba177f081 Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Tue, 3 Sep 2024 18:20:40 +0300 Subject: [PATCH 02/25] - Set up docker compose --- .gitignore | 4 ++- docker-compose.yml | 37 +++++++++++++++++++++-- docker/init.sql | 9 ++++++ src/main/resources/application.properties | 2 +- 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 docker/init.sql diff --git a/.gitignore b/.gitignore index cd72f48..932136f 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,6 @@ hs_err_pid* replay_pid* -target \ No newline at end of file +target + +data/ \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index d37b031..eb62ee8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,10 +1,34 @@ -version: '3.8' services: + user-db: + image: postgres:16.2 + container_name: user-db + ports: + - "5432:5432" # Maps 5433 externally to 5432 internally + environment: + POSTGRES_DB: userdb + POSTGRES_USER: postgres + POSTGRES_PASSWORD: 1 + volumes: + - ./data:/var/lib/postgresql/data + - ./docker/init.sql:/docker-entrypoint-initdb.d/init.sql + networks: + - sympoll-network + + user-service: + image: ghcr.io/sympoll/user-service/sympoll-user-service:latest + container_name: user-service-dc + ports: + - "8083:8083" + depends_on: + - user-db + networks: + - sympoll-network + keycloak-db: container_name: keycloak-db image: postgres:16.2 ports: - - "5432:5432" + - "5400:5432" environment: POSTGRES_DB: keycloak POSTGRES_USER: postgres @@ -12,6 +36,9 @@ services: volumes: - ./data:/var/lib/postgresql/data # Maps the local directory ./data to the container’s data directory for data persistence. - ./docker/init.sql:/docker-entrypoint-initdb.d/init.sql + networks: + - sympoll-network + keycloak: container_name: keycloak image: quay.io/keycloak/keycloak:24.0.1 @@ -30,3 +57,9 @@ services: - ./docker/keycloak/realms/:/opt/keycloak/data/import/ depends_on: - keycloak-db + networks: + - sympoll-network + +networks: + sympoll-network: + driver: bridge \ No newline at end of file diff --git a/docker/init.sql b/docker/init.sql new file mode 100644 index 0000000..5ff8584 --- /dev/null +++ b/docker/init.sql @@ -0,0 +1,9 @@ +-- User Management Service Schema +CREATE TABLE users +( + user_id UUID PRIMARY KEY, + username VARCHAR(255) UNIQUE NOT NULL, + password_hash VARCHAR(255) NOT NULL, + email VARCHAR(255), + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 923c837..3954621 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -8,4 +8,4 @@ vote.route.uri=http://vote-service:8084 group.route.uri=http://group-service:8085 media.route.uri=http://media-service:8086 -spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8181/realms/sympoll-security-realm +spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8181/realms/sympoll-realm From cfd6e488609369159c6168d29a39a3de70bdf775 Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Tue, 3 Sep 2024 19:48:18 +0300 Subject: [PATCH 03/25] - Change configuration --- docker-compose.yml | 24 +++++++---------------- src/main/resources/application.properties | 2 +- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index eb62ee8..d9d0a71 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,26 +1,16 @@ services: - user-db: - image: postgres:16.2 - container_name: user-db + poll-service: + image: ghcr.io/sympoll/poll-service/sympoll-poll-service:latest + container_name: poll-service-dc ports: - - "5432:5432" # Maps 5433 externally to 5432 internally - environment: - POSTGRES_DB: userdb - POSTGRES_USER: postgres - POSTGRES_PASSWORD: 1 - volumes: - - ./data:/var/lib/postgresql/data - - ./docker/init.sql:/docker-entrypoint-initdb.d/init.sql + - "8082:8082" networks: - sympoll-network - user-service: - image: ghcr.io/sympoll/user-service/sympoll-user-service:latest - container_name: user-service-dc + api-gateway: + image: ghcr.io/sympoll/api-gateway-service/sympoll-api-gateway-service-test:latest ports: - - "8083:8083" - depends_on: - - user-db + - "8081:8081" networks: - sympoll-network diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3954621..aa9361c 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -8,4 +8,4 @@ vote.route.uri=http://vote-service:8084 group.route.uri=http://group-service:8085 media.route.uri=http://media-service:8086 -spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8181/realms/sympoll-realm +spring.security.oauth2.resourceserver.jwt.issuer-uri=http://keycloak:8181/realms/sympoll-realm From e997343a841a2c827e5bba3b06e6dfa0d799a6d5 Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Tue, 3 Sep 2024 20:00:50 +0300 Subject: [PATCH 04/25] - Update docker compose --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index d9d0a71..11ce5e1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -35,7 +35,7 @@ services: command: [ "start-dev", "--import-realm" ] environment: DB_VENDOR: POSTGRES - DB_ADDR: postgres + DB_ADDR: keycloak-db POSTGRES_DB: keycloak POSTGRES_USER: postgres POSTGRES_PASSWORD: 1 From f800925577f410200222d5d682bb5ba866ed60a7 Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Tue, 3 Sep 2024 20:09:15 +0300 Subject: [PATCH 05/25] - update keycloak port --- src/main/resources/application.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index aa9361c..7062de5 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -8,4 +8,4 @@ vote.route.uri=http://vote-service:8084 group.route.uri=http://group-service:8085 media.route.uri=http://media-service:8086 -spring.security.oauth2.resourceserver.jwt.issuer-uri=http://keycloak:8181/realms/sympoll-realm +spring.security.oauth2.resourceserver.jwt.issuer-uri=http://keycloak:8080/realms/sympoll-realm From ba47a16a49ff02a580db939ecac7dea9d8517583 Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Tue, 3 Sep 2024 20:25:02 +0300 Subject: [PATCH 06/25] - update keycloak port --- docker-compose.yml | 17 +---------------- src/main/resources/application.properties | 2 +- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 11ce5e1..f089f61 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,13 +7,6 @@ services: networks: - sympoll-network - api-gateway: - image: ghcr.io/sympoll/api-gateway-service/sympoll-api-gateway-service-test:latest - ports: - - "8081:8081" - networks: - - sympoll-network - keycloak-db: container_name: keycloak-db image: postgres:16.2 @@ -26,8 +19,6 @@ services: volumes: - ./data:/var/lib/postgresql/data # Maps the local directory ./data to the container’s data directory for data persistence. - ./docker/init.sql:/docker-entrypoint-initdb.d/init.sql - networks: - - sympoll-network keycloak: container_name: keycloak @@ -46,10 +37,4 @@ services: volumes: - ./docker/keycloak/realms/:/opt/keycloak/data/import/ depends_on: - - keycloak-db - networks: - - sympoll-network - -networks: - sympoll-network: - driver: bridge \ No newline at end of file + - keycloak-db \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 7062de5..2d1e254 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,7 +2,7 @@ server.port=8081 spring.application.name=api-gateway-service # Cluster path to the Services: -poll.route.uri=http://poll-service:8082 +poll.route.uri=http://localhost:8082 user.route.uri=http://user-service:8083 vote.route.uri=http://vote-service:8084 group.route.uri=http://group-service:8085 From d566f6e169337092d391fd208858ae1597488a50 Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Tue, 3 Sep 2024 20:25:43 +0300 Subject: [PATCH 07/25] - update docker compose --- docker-compose.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index f089f61..5d761a8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,8 +4,6 @@ services: container_name: poll-service-dc ports: - "8082:8082" - networks: - - sympoll-network keycloak-db: container_name: keycloak-db From e7a28936ee1fb206d0984bc6b769dd9320f869b6 Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Tue, 3 Sep 2024 20:26:14 +0300 Subject: [PATCH 08/25] - update application properties --- src/main/resources/application.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 2d1e254..bc34305 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -8,4 +8,4 @@ vote.route.uri=http://vote-service:8084 group.route.uri=http://group-service:8085 media.route.uri=http://media-service:8086 -spring.security.oauth2.resourceserver.jwt.issuer-uri=http://keycloak:8080/realms/sympoll-realm +spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8181/realms/sympoll-realm From 4011e9782381211e9c72145e960394b4e967a56d Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Tue, 3 Sep 2024 20:50:35 +0300 Subject: [PATCH 09/25] - change app properties --- .gitignore | 3 +- docker-compose.yml | 45 +++++++++++++++-------- src/main/resources/application.properties | 4 +- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 932136f..b98fac5 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,5 @@ replay_pid* target -data/ \ No newline at end of file +data/ +volume-data/ \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 5d761a8..ac02cd3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,30 +4,39 @@ services: container_name: poll-service-dc ports: - "8082:8082" + networks: + - sympoll-network - keycloak-db: - container_name: keycloak-db - image: postgres:16.2 + api-gateway: + image: ghcr.io/sympoll/api-gateway-service/sympoll-api-gateway-service-test:latest ports: - - "5400:5432" + - "8081:8081" + networks: + - sympoll-network + + keycloak-mysql: + container_name: keycloak-mysql + image: mysql:8 environment: - POSTGRES_DB: keycloak - POSTGRES_USER: postgres - POSTGRES_PASSWORD: 1 + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: keycloak + MYSQL_USER: keycloak + MYSQL_PASSWORD: password volumes: - - ./data:/var/lib/postgresql/data # Maps the local directory ./data to the container’s data directory for data persistence. - - ./docker/init.sql:/docker-entrypoint-initdb.d/init.sql + - ./volume-data/mysql_keycloak_data:/var/lib/mysql + networks: + - sympoll-network keycloak: container_name: keycloak image: quay.io/keycloak/keycloak:24.0.1 command: [ "start-dev", "--import-realm" ] environment: - DB_VENDOR: POSTGRES - DB_ADDR: keycloak-db - POSTGRES_DB: keycloak - POSTGRES_USER: postgres - POSTGRES_PASSWORD: 1 + DB_VENDOR: MYSQL + DB_ADDR: mysql + DB_DATABASE: keycloak + DB_USER: keycloak + DB_PASSWORD: password KEYCLOAK_ADMIN: admin KEYCLOAK_ADMIN_PASSWORD: admin ports: @@ -35,4 +44,10 @@ services: volumes: - ./docker/keycloak/realms/:/opt/keycloak/data/import/ depends_on: - - keycloak-db \ No newline at end of file + - keycloak-mysql + networks: + - sympoll-network + +networks: + sympoll-network: + driver: bridge \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index bc34305..7062de5 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,10 +2,10 @@ server.port=8081 spring.application.name=api-gateway-service # Cluster path to the Services: -poll.route.uri=http://localhost:8082 +poll.route.uri=http://poll-service:8082 user.route.uri=http://user-service:8083 vote.route.uri=http://vote-service:8084 group.route.uri=http://group-service:8085 media.route.uri=http://media-service:8086 -spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8181/realms/sympoll-realm +spring.security.oauth2.resourceserver.jwt.issuer-uri=http://keycloak:8080/realms/sympoll-realm From 5579379de5d48eb6cea4fd9a68d9bc9007a44551 Mon Sep 17 00:00:00 2001 From: Roy Date: Wed, 4 Sep 2024 16:31:24 +0300 Subject: [PATCH 10/25] Added log message --- .../com/MTAPizza/Sympoll/api_gateway_service/routes/Routes.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/routes/Routes.java b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/routes/Routes.java index 8edda9c..b678f58 100644 --- a/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/routes/Routes.java +++ b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/routes/Routes.java @@ -34,6 +34,7 @@ public class Routes { @Bean public RouterFunction pollServiceRoute() { log.info("Received a poll route request"); + log.info("Keycloak url: {}", "${spring.security.oauth2.resourceserver.jwt.issuer-uri}"); return GatewayRouterFunctions.route("poll-service") .route(RequestPredicates.path("api/poll/**"), HandlerFunctions.http(pollRouteUri)) .build(); From 0802e1cf3e5c4362dcbe84a805e36ea03af9a0b9 Mon Sep 17 00:00:00 2001 From: Roy Date: Wed, 4 Sep 2024 16:39:24 +0300 Subject: [PATCH 11/25] test --- .../Sympoll/api_gateway_service/routes/Routes.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/routes/Routes.java b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/routes/Routes.java index b678f58..551ae4f 100644 --- a/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/routes/Routes.java +++ b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/routes/Routes.java @@ -64,11 +64,11 @@ public RouterFunction voteServiceRoute() { .build(); } - @Bean - public RouterFunction mediaServiceRoute() { - log.info("Received a media route request"); - return GatewayRouterFunctions.route("media-service") - .route(RequestPredicates.path("api/media/**"), HandlerFunctions.http(mediaRouteUri)) - .build(); - } +// @Bean +// public RouterFunction mediaServiceRoute() { +// log.info("Received a media route request"); +// return GatewayRouterFunctions.route("media-service") +// .route(RequestPredicates.path("api/media/**"), HandlerFunctions.http(mediaRouteUri)) +// .build(); +// } } From c3e996f9c66a676048879e18a0afead2951fec87 Mon Sep 17 00:00:00 2001 From: Roy Date: Wed, 4 Sep 2024 16:57:40 +0300 Subject: [PATCH 12/25] test --- .../api_gateway_service/routes/Routes.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/routes/Routes.java b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/routes/Routes.java index 551ae4f..8edda9c 100644 --- a/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/routes/Routes.java +++ b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/routes/Routes.java @@ -34,7 +34,6 @@ public class Routes { @Bean public RouterFunction pollServiceRoute() { log.info("Received a poll route request"); - log.info("Keycloak url: {}", "${spring.security.oauth2.resourceserver.jwt.issuer-uri}"); return GatewayRouterFunctions.route("poll-service") .route(RequestPredicates.path("api/poll/**"), HandlerFunctions.http(pollRouteUri)) .build(); @@ -64,11 +63,11 @@ public RouterFunction voteServiceRoute() { .build(); } -// @Bean -// public RouterFunction mediaServiceRoute() { -// log.info("Received a media route request"); -// return GatewayRouterFunctions.route("media-service") -// .route(RequestPredicates.path("api/media/**"), HandlerFunctions.http(mediaRouteUri)) -// .build(); -// } + @Bean + public RouterFunction mediaServiceRoute() { + log.info("Received a media route request"); + return GatewayRouterFunctions.route("media-service") + .route(RequestPredicates.path("api/media/**"), HandlerFunctions.http(mediaRouteUri)) + .build(); + } } From 1f5b244c36a3482884946654e88209d16d57c46d Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Thu, 5 Sep 2024 13:11:23 +0300 Subject: [PATCH 13/25] - Added security config --- .idea/uiDesigner.xml | 124 ++++++++++++++++++ .../config/SecurityConfigDisabled.java | 20 +++ ...Config.java => SecurityConfigEnabled.java} | 6 +- src/main/resources/application.properties | 3 +- 4 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 .idea/uiDesigner.xml create mode 100644 src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigDisabled.java rename src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/{SecurityConfig.java => SecurityConfigEnabled.java} (76%) diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigDisabled.java b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigDisabled.java new file mode 100644 index 0000000..19f6f6c --- /dev/null +++ b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigDisabled.java @@ -0,0 +1,20 @@ +package com.MTAPizza.Sympoll.api_gateway_service.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.security.config.Customizer; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.web.SecurityFilterChain; + +@Configuration +@EnableWebSecurity +@Profile("auth-disabled") +public class SecurityConfigDisabled { + @Bean + public void configure(HttpSecurity httpSecurity) throws Exception { + httpSecurity.authorizeRequests().anyRequest().permitAll(); + } +} + diff --git a/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfig.java b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigEnabled.java similarity index 76% rename from src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfig.java rename to src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigEnabled.java index b391068..4e2b617 100644 --- a/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfig.java +++ b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigEnabled.java @@ -2,12 +2,16 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; import org.springframework.security.config.Customizer; import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.web.SecurityFilterChain; @Configuration -public class SecurityConfig { +@EnableWebSecurity +@Profile("auth-enabled") +public class SecurityConfigEnabled { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception { return httpSecurity.authorizeHttpRequests(authorize -> authorize diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 7062de5..6fac6e8 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,6 @@ server.port=8081 spring.application.name=api-gateway-service +spring.profiles.active=${ENABLE_AUTH:false} ? auth-enabled : auth-disabled # Cluster path to the Services: poll.route.uri=http://poll-service:8082 @@ -8,4 +9,4 @@ vote.route.uri=http://vote-service:8084 group.route.uri=http://group-service:8085 media.route.uri=http://media-service:8086 -spring.security.oauth2.resourceserver.jwt.issuer-uri=http://keycloak:8080/realms/sympoll-realm +spring.security.oauth2.resourceserver.jwt.issuer-uri=http://keycloak:8181/realms/sympoll-realm From b5de510d094ce92ccddd1a9b0ab319808f118b4b Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Thu, 5 Sep 2024 14:49:11 +0300 Subject: [PATCH 14/25] - Adjusted application properties --- src/main/resources/application-auth-disabled.properties | 0 src/main/resources/application-auth-enabled.properties | 2 ++ src/main/resources/application.properties | 6 ++---- 3 files changed, 4 insertions(+), 4 deletions(-) create mode 100644 src/main/resources/application-auth-disabled.properties create mode 100644 src/main/resources/application-auth-enabled.properties diff --git a/src/main/resources/application-auth-disabled.properties b/src/main/resources/application-auth-disabled.properties new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/application-auth-enabled.properties b/src/main/resources/application-auth-enabled.properties new file mode 100644 index 0000000..0b5ebe5 --- /dev/null +++ b/src/main/resources/application-auth-enabled.properties @@ -0,0 +1,2 @@ +# Configuration for authentication enabled +spring.security.oauth2.resourceserver.jwt.issuer-uri=http://keycloak:8181/realms/sympoll-realm \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6fac6e8..3d93d46 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,12 +1,10 @@ server.port=8081 spring.application.name=api-gateway-service -spring.profiles.active=${ENABLE_AUTH:false} ? auth-enabled : auth-disabled +spring.profiles.active=${ENABLE_AUTH:auth-enabled} # Cluster path to the Services: poll.route.uri=http://poll-service:8082 user.route.uri=http://user-service:8083 vote.route.uri=http://vote-service:8084 group.route.uri=http://group-service:8085 -media.route.uri=http://media-service:8086 - -spring.security.oauth2.resourceserver.jwt.issuer-uri=http://keycloak:8181/realms/sympoll-realm +media.route.uri=http://media-service:8086 \ No newline at end of file From 404e6356aae246f4c0b5a220075bd7bfd1a67fb9 Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Thu, 5 Sep 2024 15:17:36 +0300 Subject: [PATCH 15/25] - changed disabled security config --- .../config/SecurityConfigDisabled.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigDisabled.java b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigDisabled.java index 19f6f6c..9b74bdd 100644 --- a/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigDisabled.java +++ b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigDisabled.java @@ -12,9 +12,14 @@ @EnableWebSecurity @Profile("auth-disabled") public class SecurityConfigDisabled { + @Bean - public void configure(HttpSecurity httpSecurity) throws Exception { - httpSecurity.authorizeRequests().anyRequest().permitAll(); + public SecurityFilterChain configure(HttpSecurity httpSecurity) throws Exception { + httpSecurity + .authorizeRequests(authorizeRequests -> + authorizeRequests.anyRequest().permitAll() + ); + + return httpSecurity.build(); // Return the configured SecurityFilterChain } } - From 0402cee7bb5ff555547c338256b3a10c4ad9997f Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Sat, 7 Sep 2024 13:52:15 +0300 Subject: [PATCH 16/25] -changed issuer uri --- src/main/resources/application-auth-enabled.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/application-auth-enabled.properties b/src/main/resources/application-auth-enabled.properties index 0b5ebe5..1590c3a 100644 --- a/src/main/resources/application-auth-enabled.properties +++ b/src/main/resources/application-auth-enabled.properties @@ -1,2 +1,2 @@ -# Configuration for authentication enabled -spring.security.oauth2.resourceserver.jwt.issuer-uri=http://keycloak:8181/realms/sympoll-realm \ No newline at end of file +spring.security.oauth2.resourceserver.jwt.issuer-uri=http://auth.localhost/realms/sympoll-realm +spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://auth.localhost/realms/sympoll-realm/protocol/openid-connect/certs \ No newline at end of file From 1ca550ae8ed6211485047eba8ac49fea0bdf229d Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Sat, 7 Sep 2024 14:16:16 +0300 Subject: [PATCH 17/25] - Adjust to use reverse proxy --- src/main/resources/application-auth-enabled.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/application-auth-enabled.properties b/src/main/resources/application-auth-enabled.properties index 1590c3a..d9b5d3f 100644 --- a/src/main/resources/application-auth-enabled.properties +++ b/src/main/resources/application-auth-enabled.properties @@ -1,2 +1,2 @@ -spring.security.oauth2.resourceserver.jwt.issuer-uri=http://auth.localhost/realms/sympoll-realm -spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://auth.localhost/realms/sympoll-realm/protocol/openid-connect/certs \ No newline at end of file +spring.security.oauth2.resourceserver.jwt.issuer-uri=http://auth-reverse-proxy/realms/sympoll-realm +spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://auth-reverse-proxy/realms/sympoll-realm/protocol/openid-connect/certs \ No newline at end of file From 3f04987339cd7f415e75bba891261af17ec78438 Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Sat, 7 Sep 2024 14:38:28 +0300 Subject: [PATCH 18/25] - Display detailed logs --- src/main/resources/application-auth-enabled.properties | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/resources/application-auth-enabled.properties b/src/main/resources/application-auth-enabled.properties index d9b5d3f..c343da8 100644 --- a/src/main/resources/application-auth-enabled.properties +++ b/src/main/resources/application-auth-enabled.properties @@ -1,2 +1,9 @@ spring.security.oauth2.resourceserver.jwt.issuer-uri=http://auth-reverse-proxy/realms/sympoll-realm -spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://auth-reverse-proxy/realms/sympoll-realm/protocol/openid-connect/certs \ No newline at end of file +spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://auth-reverse-proxy/realms/sympoll-realm/protocol/openid-connect/certs +# Enable detailed logging for Spring Security +logging.level.org.springframework.security=DEBUG +# Enable detailed logging for OAuth2 resource server and JWT validation +logging.level.org.springframework.security.oauth2=DEBUG +logging.level.org.springframework.security.oauth2.server.resource=DEBUG +logging.level.org.springframework.web=DEBUG +logging.level.com.nimbusds=DEBUG \ No newline at end of file From e80c46ff5c3b7f663696d5d89892d98af9f53f9b Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Sat, 7 Sep 2024 14:50:01 +0300 Subject: [PATCH 19/25] - Adjust issuer uri again... --- src/main/resources/application-auth-enabled.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/application-auth-enabled.properties b/src/main/resources/application-auth-enabled.properties index c343da8..acd97da 100644 --- a/src/main/resources/application-auth-enabled.properties +++ b/src/main/resources/application-auth-enabled.properties @@ -1,5 +1,5 @@ -spring.security.oauth2.resourceserver.jwt.issuer-uri=http://auth-reverse-proxy/realms/sympoll-realm -spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://auth-reverse-proxy/realms/sympoll-realm/protocol/openid-connect/certs +spring.security.oauth2.resourceserver.jwt.issuer-uri=http://auth.localhost/realms/sympoll-realm +spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://auth.localhost/realms/sympoll-realm/protocol/openid-connect/certs # Enable detailed logging for Spring Security logging.level.org.springframework.security=DEBUG # Enable detailed logging for OAuth2 resource server and JWT validation From 04468451300248374db9e6811a0e4ea750b9ef8d Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Sat, 7 Sep 2024 14:55:28 +0300 Subject: [PATCH 20/25] - Adjust issuer uri again... --- src/main/resources/application-auth-enabled.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/application-auth-enabled.properties b/src/main/resources/application-auth-enabled.properties index acd97da..28dbd64 100644 --- a/src/main/resources/application-auth-enabled.properties +++ b/src/main/resources/application-auth-enabled.properties @@ -1,6 +1,6 @@ spring.security.oauth2.resourceserver.jwt.issuer-uri=http://auth.localhost/realms/sympoll-realm -spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://auth.localhost/realms/sympoll-realm/protocol/openid-connect/certs -# Enable detailed logging for Spring Security +# Use the reverse proxy to resolve and retrieve JWKs internally +spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://auth-reverse-proxy/realms/sympoll-realm/protocol/openid-connect/certs# Enable detailed logging for Spring Security logging.level.org.springframework.security=DEBUG # Enable detailed logging for OAuth2 resource server and JWT validation logging.level.org.springframework.security.oauth2=DEBUG From 61a23b850e2a3041bb6f2ae55f5529c881445fae Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Sat, 7 Sep 2024 14:59:51 +0300 Subject: [PATCH 21/25] - Adjust issuer uri again... --- src/main/resources/application-auth-enabled.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/application-auth-enabled.properties b/src/main/resources/application-auth-enabled.properties index 28dbd64..31ff3b9 100644 --- a/src/main/resources/application-auth-enabled.properties +++ b/src/main/resources/application-auth-enabled.properties @@ -1,6 +1,6 @@ spring.security.oauth2.resourceserver.jwt.issuer-uri=http://auth.localhost/realms/sympoll-realm # Use the reverse proxy to resolve and retrieve JWKs internally -spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://auth-reverse-proxy/realms/sympoll-realm/protocol/openid-connect/certs# Enable detailed logging for Spring Security +spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://auth-reverse-proxy/realms/sympoll-realm/protocol/openid-connect/certs logging.level.org.springframework.security=DEBUG # Enable detailed logging for OAuth2 resource server and JWT validation logging.level.org.springframework.security.oauth2=DEBUG From 6dcae1915ce7491c29ad759bf5378b98f7eaaa8b Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Sat, 7 Sep 2024 20:31:40 +0300 Subject: [PATCH 22/25] - Changed logging --- src/main/resources/application-auth-enabled.properties | 2 -- src/main/resources/application.properties | 5 ++++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/resources/application-auth-enabled.properties b/src/main/resources/application-auth-enabled.properties index 31ff3b9..a3946bb 100644 --- a/src/main/resources/application-auth-enabled.properties +++ b/src/main/resources/application-auth-enabled.properties @@ -1,9 +1,7 @@ spring.security.oauth2.resourceserver.jwt.issuer-uri=http://auth.localhost/realms/sympoll-realm # Use the reverse proxy to resolve and retrieve JWKs internally spring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://auth-reverse-proxy/realms/sympoll-realm/protocol/openid-connect/certs -logging.level.org.springframework.security=DEBUG # Enable detailed logging for OAuth2 resource server and JWT validation logging.level.org.springframework.security.oauth2=DEBUG logging.level.org.springframework.security.oauth2.server.resource=DEBUG -logging.level.org.springframework.web=DEBUG logging.level.com.nimbusds=DEBUG \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3d93d46..93f2094 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -7,4 +7,7 @@ poll.route.uri=http://poll-service:8082 user.route.uri=http://user-service:8083 vote.route.uri=http://vote-service:8084 group.route.uri=http://group-service:8085 -media.route.uri=http://media-service:8086 \ No newline at end of file +media.route.uri=http://media-service:8086 + +logging.level.org.springframework.web=DEBUG +logging.level.org.springframework.security=DEBUG From 6da12c1eadd721b39eefe20703f979306f60a218 Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Sat, 7 Sep 2024 20:40:26 +0300 Subject: [PATCH 23/25] - disable CRSF --- .../config/SecurityConfigDisabled.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigDisabled.java b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigDisabled.java index 9b74bdd..e039678 100644 --- a/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigDisabled.java +++ b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigDisabled.java @@ -3,10 +3,10 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; -import org.springframework.security.config.Customizer; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.web.SecurityFilterChain; +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; + @Configuration @EnableWebSecurity @@ -14,12 +14,10 @@ public class SecurityConfigDisabled { @Bean - public SecurityFilterChain configure(HttpSecurity httpSecurity) throws Exception { - httpSecurity - .authorizeRequests(authorizeRequests -> - authorizeRequests.anyRequest().permitAll() - ); - - return httpSecurity.build(); // Return the configured SecurityFilterChain + protected void configure(HttpSecurity http) throws Exception { + http + .csrf(AbstractHttpConfigurer::disable) // Updated way to disable CSRF + .authorizeRequests() + .anyRequest().permitAll(); // Allow all requests without authentication } } From 516ef82429521c78619225e357badabfe3a48f6a Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Sat, 7 Sep 2024 20:46:03 +0300 Subject: [PATCH 24/25] - disable CRSF --- .../config/SecurityConfigDisabled.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigDisabled.java b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigDisabled.java index e039678..266e60d 100644 --- a/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigDisabled.java +++ b/src/main/java/com/MTAPizza/Sympoll/api_gateway_service/config/SecurityConfigDisabled.java @@ -6,6 +6,7 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; +import org.springframework.security.web.SecurityFilterChain; @Configuration @@ -14,10 +15,12 @@ public class SecurityConfigDisabled { @Bean - protected void configure(HttpSecurity http) throws Exception { + public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http - .csrf(AbstractHttpConfigurer::disable) // Updated way to disable CSRF - .authorizeRequests() - .anyRequest().permitAll(); // Allow all requests without authentication + .csrf(AbstractHttpConfigurer::disable) // New method to disable CSRF + .authorizeRequests(auth -> auth + .anyRequest().permitAll()); // Allow all requests without authentication + + return http.build(); } } From d4315959b71efe2739aa1bfec94b75cf8d49cd90 Mon Sep 17 00:00:00 2001 From: Ronen Gelmanovich Date: Sat, 7 Sep 2024 20:55:18 +0300 Subject: [PATCH 25/25] - Fixed init.sql --- docker/init.sql | 1 - 1 file changed, 1 deletion(-) diff --git a/docker/init.sql b/docker/init.sql index 5ff8584..2c3d88e 100644 --- a/docker/init.sql +++ b/docker/init.sql @@ -3,7 +3,6 @@ CREATE TABLE users ( user_id UUID PRIMARY KEY, username VARCHAR(255) UNIQUE NOT NULL, - password_hash VARCHAR(255) NOT NULL, email VARCHAR(255), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); \ No newline at end of file