Skip to content

Commit

Permalink
Merge pull request #26 from Lukinhasssss/feature/authentication-with-…
Browse files Browse the repository at this point in the history
…keycloak

Automated PR from feature/authentication-with-keycloak
  • Loading branch information
Lukinhasssss committed Jun 2, 2024
2 parents 6692605 + 091eab6 commit 61d76eb
Show file tree
Hide file tree
Showing 28 changed files with 326 additions and 250 deletions.
14 changes: 12 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
SPRING_PROFILES_ACTIVE=development
SPRING_PROFILES_ACTIVE=production

ELASTICSEARCH_URIS=http://localhost:9200
ELASTICSEARCH_USERNAME=elastic
ELASTICSEARCH_PASSWORD=changeme

KEYCLOAK_REALM=codeflix
KEYCLOAK_HOST=http://localhost:18080
KEYCLOAK_HOST=http://keycloak.internal:8443
KEYCLOAK_CLIENT_ID=admin-do-catalogo
KEYCLOAK_CLIENT_SECRET=AMs8T4dChEF5KCgRZiYP2nAsQVgm4MUy

BASE_URL=http://localhost:8080

KAFKA_BOOTSTRAP_SERVERS=localhost:9092
4 changes: 2 additions & 2 deletions .run/Catálogo de Vídeos.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<envs>
<env name="KEYCLOAK_CLIENT_ID" value="admin-do-catalogo" />
<env name="KEYCLOAK_CLIENT_SECRET" value="Td5scdSINqzhib0ket0UFUnvEPP6KAal" />
<env name="KEYCLOAK_CLIENT_SECRET" value="AMs8T4dChEF5KCgRZiYP2nAsQVgm4MUy" />
</envs>
<option name="MAIN_CLASS_NAME" value="com.lukinhasssss.catalogo.infrastructure.CatalogoDeVideosKt" />
<module name="catalogo-de-videos.infrastructure.main" />
<shortenClasspath name="NONE" />
<option name="VM_PARAMETERS" value="-Dspring.profiles.active=development" />
<option name="VM_PARAMETERS" value="-Dspring.profiles.active=sandbox" />
<method v="2">
<option name="Make" enabled="true" />
</method>
Expand Down
30 changes: 17 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,34 @@ RUN gradle bootJar
# build runtime
FROM eclipse-temurin:21-jre-alpine

# Install curl
RUN apk add --no-cache curl

ARG JAR_FILE=/app/build/libs/app*.jar

COPY --from=builder $JAR_FILE /app.jar

# Download do OpenTelemetry Java Agent
#RUN wget -O /opentelemetry-javaagent.jar https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v1.31.0/opentelemetry-javaagent.jar
#RUN wget -O /opentelemetry-javaagent.jar https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar
RUN wget -O /opentelemetry-javaagent.jar https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v2.4.0/opentelemetry-javaagent.jar

# Download do Elastic APM Java Agent
#RUN wget -O /apm-agent.jar https://repo1.maven.org/maven2/co/elastic/apm/elastic-apm-agent/1.46.0/elastic-apm-agent-1.46.0.jar
#COPY --from=docker.elastic.co/observability/apm-agent-java:latest /usr/agent/elastic-apm-agent.jar /apm-agent.jar
#RUN wget -O /apm-agent.jar https://repo1.maven.org/maven2/co/elastic/apm/elastic-apm-agent/1.49.0/elastic-apm-agent-1.49.0.jar
COPY --from=docker.elastic.co/observability/apm-agent-java:latest /usr/agent/elastic-apm-agent.jar /apm-agent.jar

RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring

ENTRYPOINT [ "java", \
# "-javaagent:/apm-agent.jar", \
# "-Delastic.apm.service_name=admin-do-catalogo", \
# "-Delastic.apm.server_url=http://apm-codeflix:8200", \
# "-Delastic.apm.environment=codeflix", \
# "-Delastic.apm.application_packages=com.lukinhasssss", \
# "-javaagent:/opentelemetry-javaagent.jar", \
# "-Dotel.service.name=admin-do-catalogo", \
# "-Dotel.exporter.otlp.endpoint=http://otel-collector-codeflix:4318", \
# "-Dotel.exporter.otlp.protocol=http/protobuf", \
"-javaagent:/apm-agent.jar", \
"-Delastic.apm.service_name=catalogo-de-videos", \
"-Delastic.apm.server_url=http://apm-codeflix:8200", \
"-Delastic.apm.application_packages=com.lukinhasssss", \
"-Delastic.apm.capture_body=all", \
"-Delastic.apm.environment=codeflix", \
"-javaagent:/opentelemetry-javaagent.jar", \
"-Dotel.service.name=catalogo-de-videos", \
"-Dotel.exporter.otlp.endpoint=http://jaeger-codeflix:4318", \
"-Dotel.trace.exporter=zipkin", \
"-Dotel.trace.exporter.zipkin.endpoint=http://jaeger-codeflix:9411/api/v2/spans", \
"-jar", "/app.jar" \
]
2 changes: 1 addition & 1 deletion config/detekt/detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ style:
maxLineLength: 140
excludePackageStatements: true
excludeImportStatements: true
excludeCommentStatements: false
excludeCommentStatements: true
excludeRawStrings: true
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**', '**/*.kts']
MayBeConst:
Expand Down
154 changes: 154 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
name: catalogo-de-videos

services:
catalogo-de-videos:
container_name: catalogo-de-videos
build:
context: .
dockerfile: Dockerfile
# image: lukinhasssss/catalogo-de-videos
ports:
- "8081:8081"
restart: on-failure
depends_on:
kafka-catalogo-de-videos:
condition: service_healthy
env_file:
- .env
labels:
filebeat_collector: true
networks:
- catalogo-de-videos
- kafka-catalogo-de-videos
- keycloak-codeflix
- elasticsearch-codeflix

zookeeper-catalogo-de-videos:
container_name: zookeeper-catalogo-de-videos
image: confluentinc/cp-zookeeper:latest
environment:
ZOOKEEPER_CLIENT_PORT: 2181
healthcheck:
test: [ "CMD", "zookeeper-shell", "localhost:2181", "ls", "/" ]
interval: 10s
timeout: 5s
retries: 3
networks:
- kafka-catalogo-de-videos

kafka-catalogo-de-videos:
container_name: kafka-catalogo-de-videos
image: confluentinc/cp-kafka:latest
depends_on:
zookeeper-catalogo-de-videos:
condition: service_healthy
ports:
- "9092:9092"
- "9094:9094"
environment:
KAFKA_BROKER_ID: 1
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper-catalogo-de-videos:2181
KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL
KAFKA_LISTENERS: INTERNAL://:9092,OUTSIDE://:9094
KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka-catalogo-de-videos:9092,OUTSIDE://localhost:9094
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,OUTSIDE:PLAINTEXT
volumes:
- kafka-catalogo-de-videos:/var/lib/kafka/data
healthcheck:
test: [ "CMD", "nc", "-z", "localhost", "9092" ]
interval: 10s
timeout: 5s
retries: 3
networks:
- kafka-catalogo-de-videos

control-center-catalogo-de-videos:
container_name: control-center-catalogo-de-videos
image: confluentinc/cp-enterprise-control-center:6.0.1
hostname: control-center
depends_on:
kafka-catalogo-de-videos:
condition: service_healthy
ports:
- "9021:9021"
environment:
CONTROL_CENTER_BOOTSTRAP_SERVERS: kafka-catalogo-de-videos:9092
CONTROL_CENTER_ZOOKEEPER_CONNECT: zookeeper-catalogo-de-videos:2181
CONTROL_CENTER_REPLICATION_FACTOR: 1
CONTROL_CENTER_CONNECT_CLUSTER: http://kafka-connect-catalogo-de-videos:8083
PORT: 9021
healthcheck:
test: [ "CMD", "nc", "-z", "localhost", "9021" ]
interval: 10s
timeout: 5s
retries: 3
networks:
- kafka-catalogo-de-videos

kafka-connect-catalogo-de-videos:
container_name: kafka-connect-catalogo-de-videos
image: confluentinc/cp-kafka-connect-base:6.0.1
ports:
- "8083:8083"
environment:
CONNECT_BOOTSTRAP_SERVERS: kafka-catalogo-de-videos:9092 # Tive que adicionar o host no arquivo /etc/hosts para que a aplicacao conseguisse acessar corretamente
CONNECT_REST_PORT: 8083
CONNECT_GROUP_ID: kafka-connect-catalogo-de-videos
CONNECT_REST_ADVERTISED_HOST_NAME: "kafka-connect-catalogo-de-videos"
CONNECT_CONFIG_STORAGE_TOPIC: _connect-configs
CONNECT_OFFSET_STORAGE_TOPIC: _connect-offsets
CONNECT_STATUS_STORAGE_TOPIC: _connect-status
CONNECT_KEY_CONVERTER: org.apache.kafka.connect.storage.StringConverter
CONNECT_VALUE_CONVERTER: org.apache.kafka.connect.json.JsonConverter
CONNECT_INTERNAL_KEY_CONVERTER: org.apache.kafka.connect.json.JsonConverter
CONNECT_INTERNAL_VALUE_CONVERTER: org.apache.kafka.connect.json.JsonConverter
CONNECT_LOG4J_ROOT_LOGLEVEL: "INFO"
CONNECT_LOG4J_LOGGERS: "org.apache.kafka.connect.runtime.rest=WARN,org.reflections=ERROR"
CONNECT_LOG4J_APPENDER_STDOUT_LAYOUT_CONVERSIONPATTERN: "[%d] %p %X{connector.context}%m (%c:%L)%n"
CONNECT_CONFIG_STORAGE_REPLICATION_FACTOR: "1"
CONNECT_OFFSET_STORAGE_REPLICATION_FACTOR: "1"
CONNECT_STATUS_STORAGE_REPLICATION_FACTOR: "1"
# # Optional settings to include to support Confluent Control Center
CONNECT_PRODUCER_INTERCEPTOR_CLASSES: "io.confluent.monitoring.clients.interceptor.MonitoringProducerInterceptor"
CONNECT_CONSUMER_INTERCEPTOR_CLASSES: "io.confluent.monitoring.clients.interceptor.MonitoringConsumerInterceptor"
# ---------------
CONNECT_PLUGIN_PATH: /usr/share/java,/usr/share/confluent-hub-components,/data/connect-jars
# If you want to use the Confluent Hub installer to d/l component, but make them available
# when running this offline, spin up the stack once and then run :
# docker cp kafka-connect:/usr/share/confluent-hub-components ./data/connect-jars
volumes:
- kafka-connect-catalogo-de-videos:/data
# In the command section, $ are replaced with $$ to avoid the error 'Invalid interpolation format for "command" option'
command:
- bash
- -c
- |
echo "Installing Connector"
confluent-hub install --no-prompt debezium/debezium-connector-postgresql:latest
#
echo "Launching Kafka Connect worker"
/etc/confluent/docker/run &
#
sleep infinity
networks:
- kafka-catalogo-de-videos
- postgresql-admin-do-catalogo

networks:
catalogo-de-videos:
external: true
kafka-catalogo-de-videos:
external: true
postgresql-admin-do-catalogo:
external: true
keycloak-codeflix:
external: true
elasticsearch-codeflix:
external: true

volumes:
kafka-catalogo-de-videos:
external: true
kafka-connect-catalogo-de-videos:
external: true
7 changes: 4 additions & 3 deletions infrastructure/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ dependencies {

implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("io.micrometer:micrometer-registry-prometheus:1.10.5")
implementation("ch.qos.logback:logback-classic:1.4.14")
implementation("ch.qos.logback:logback-core:1.4.14")
implementation("io.opentelemetry:opentelemetry-exporter-otlp:1.38.0")
implementation("ch.qos.logback:logback-classic:1.5.6")
implementation("ch.qos.logback:logback-core:1.5.6")
implementation("net.logstash.logback:logstash-logback-encoder:7.3")

implementation("com.google.guava:guava:33.0.0-jre")
Expand Down Expand Up @@ -96,7 +97,7 @@ dependencies {
dependencyManagement {
imports {
mavenBom("io.github.resilience4j:resilience4j-bom:2.2.0")
mavenBom("org.springframework.cloud:spring-cloud-dependencies:2023.0.0")
mavenBom("org.springframework.cloud:spring-cloud-dependencies:2023.0.2")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CategoryRestClient(
override fun categoryOfId(categoryId: String?): Category? = doGet(categoryId) {
getClientCredentials.retrieve().let { token ->
restClient.get()
.uri("/{id}", categoryId)
.uri("/categories/{id}", categoryId)
.header(HttpHeaders.AUTHORIZATION, "Bearer $token")
.retrieve()
.onStatus(isNotFound(), notFoundHandler(categoryId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.lukinhasssss.catalogo.infrastructure.configuration.annotations.Genres
import com.lukinhasssss.catalogo.infrastructure.configuration.annotations.Keycloak
import com.lukinhasssss.catalogo.infrastructure.configuration.annotations.Videos
import com.lukinhasssss.catalogo.infrastructure.configuration.properties.RestClientProperties
import io.micrometer.observation.ObservationRegistry
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
Expand All @@ -19,7 +20,11 @@ import org.springframework.web.client.RestClient
class RestClientConfig {

companion object {
fun restClient(properties: RestClientProperties, objectMapper: ObjectMapper) = with(properties) {
fun restClient(
properties: RestClientProperties,
objectMapper: ObjectMapper,
observationRegistry: ObservationRegistry
) = with(properties) {
val factory = JdkClientHttpRequestFactory()
factory.setReadTimeout(readTimeout)

Expand All @@ -31,6 +36,8 @@ class RestClientConfig {
converters.add(jsonConverter(objectMapper))
converters.add(FormHttpMessageConverter())
}
.observationRegistry(observationRegistry)
// .observationConvention(RestClientObservationConfig())
.build()
}

Expand Down Expand Up @@ -65,27 +72,31 @@ class RestClientConfig {
@Keycloak
fun keycloakHttpClient(
@Keycloak properties: RestClientProperties,
objectMapper: ObjectMapper
) = restClient(properties, objectMapper)
objectMapper: ObjectMapper,
observationRegistry: ObservationRegistry
) = restClient(properties, objectMapper, observationRegistry)

@Bean
@Categories
fun categoryHttpClient(
@Categories properties: RestClientProperties,
objectMapper: ObjectMapper
) = restClient(properties, objectMapper)
objectMapper: ObjectMapper,
observationRegistry: ObservationRegistry
) = restClient(properties, objectMapper, observationRegistry)

@Bean
@Genres
fun genreHttpClient(
@Genres properties: RestClientProperties,
objectMapper: ObjectMapper
) = restClient(properties, objectMapper)
objectMapper: ObjectMapper,
observationRegistry: ObservationRegistry
) = restClient(properties, objectMapper, observationRegistry)

@Bean
@Videos
fun videoHttpClient(
@Videos properties: RestClientProperties,
objectMapper: ObjectMapper
) = restClient(properties, objectMapper)
objectMapper: ObjectMapper,
observationRegistry: ObservationRegistry
) = restClient(properties, objectMapper, observationRegistry)
}
Loading

0 comments on commit 61d76eb

Please sign in to comment.