Skip to content

Commit

Permalink
Merge pull request #8 from ingsis-group-6/dev
Browse files Browse the repository at this point in the history
Merging developed service
  • Loading branch information
ricardolopezb authored Jul 13, 2023
2 parents 9d8ee52 + 5fc2507 commit 5b2d687
Show file tree
Hide file tree
Showing 50 changed files with 1,601 additions and 96 deletions.
14 changes: 11 additions & 3 deletions .github/workflows/dev-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ on:
push:
branches:
- develop
- dev
- refactor_put
jobs:
push_to_registry:
name: Push Docker image to GitHub Packages
Expand All @@ -13,6 +15,10 @@ jobs:
id: short-sha
with:
length: 8
- name: aber
run: echo ${{ secrets.TOKEN}}
- name: aber2
run: echo ${{ secrets.TOKEN}}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
Expand All @@ -24,10 +30,12 @@ jobs:
username: ingsis-group-6
password: ${{ secrets.TOKEN }}

- name: Push to GitHub Packages
id: docker_build
- name: Build and push
uses: docker/build-push-action@v4
with:
push: true
tags: |
ghcr.io/ingsis-group-6/snippet-manager-dev:latest
build-args: |
"USERNAME=${{ secrets.PUBLISH_USERNAME }}"
"TOKEN=${{ secrets.PUBLISH_TOKEN }}"
push: true
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
build
.idea
.gradle
.gradle
.env
.envrc
gradle.properties
10 changes: 9 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
FROM gradle:7.6.1-jdk17
COPY . /home/gradle/src
WORKDIR /home/gradle/src

ARG USERNAME
ENV USERNAME ${USERNAME}

ARG TOKEN
ENV TOKEN ${TOKEN}


RUN gradle build
EXPOSE 8080
EXPOSE 8081
ENTRYPOINT ["java","-jar","/home/gradle/src/build/libs/snippet-manager-0.0.1-SNAPSHOT.jar"]
61 changes: 51 additions & 10 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id 'org.springframework.boot' version '3.0.6'
id 'io.spring.dependency-management' version '1.1.0'
id 'org.jetbrains.kotlin.jvm' version '1.7.22'
id 'org.springframework.boot' version '2.7.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'org.jetbrains.kotlin.jvm' version '1.7.22'
id 'org.jetbrains.kotlin.plugin.spring' version '1.7.22'
id 'org.jetbrains.kotlin.plugin.jpa' version '1.7.22'
id 'java'
Expand All @@ -15,16 +15,57 @@ sourceCompatibility = '17'

repositories {
mavenCentral()
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/austral-ingsis/class-redis-streams")
credentials {
username = System.getenv("USERNAME")
password = System.getenv("TOKEN")
}
}
maven {
name = "Spring-mvc-redis-streams"
url = uri("https://maven.pkg.github.com/Snippet-Searcher/spring-mvc-redis-streams%22")
credentials {
username = System.getenv("USERNAME")
password = System.getenv("TOKEN")
}
}
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/ingsis-group-6/snippet-events")
credentials {
username = System.getenv("USERNAME")
password = System.getenv("TOKEN")
}
}
}

dependencies {
// implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
implementation 'org.jetbrains.kotlin:kotlin-reflect'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
implementation 'org.jetbrains.kotlin:kotlin-reflect'
implementation("io.github.cdimascio:java-dotenv:5.2.2")
runtimeOnly 'com.h2database:h2'
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
implementation 'org.springframework.metrics:spring-metrics:0.5.1.RELEASE'
implementation 'com.netflix.spectator:spectator-api:0.55.0'
implementation 'org.springframework:spring-aspects'
implementation 'io.projectreactor.kotlin:reactor-kotlin-extensions'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-reactor'

implementation 'org.ingsis.group6:snippet-events:0.0.5'


implementation("spring-mvc-redis-streams:lib:0.0.1-SNAPSHOT")
implementation("org.springframework.boot:spring-boot-starter-data-redis")

// implementation 'org.austral.ingsis:redis-streams:0.1.7'
// implementation 'org.springframework.boot:spring-boot-starter-data-redis-reactive'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-reactor'
}

tasks.withType(KotlinCompile) {
Expand Down
66 changes: 46 additions & 20 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
version: '3'

services:
snippet-manager-api:
container_name: "snippet-manager-api"
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
environment:
DB_USER: sa
DB_PASSWORD: password
DB_NAME: compose-db
DB_HOST: snippet_manager_db
DB_PORT: 5432

snippet_manager_db:
container_name: snippet_manager_db
image: postgres:12
db:
image: postgres:alpine
ports:
- "5432:5432"
- "5433:5433"
environment:
POSTGRES_USER: sa
POSTGRES_PASSWORD: password
POSTGRES_DB: compose-db
POSTGRES_PASSWORD: postgres
POSTGRES_DB: db
command: -p 5433

# datadog:
# image: datadog/agent
# container_name: "datadog-dev-env-manager"
# environment:
# - DD_API_KEY=${DD_API_KEY}
# - DD_SITE=us5.datadoghq.com
# - DD_LOGS_ENABLED=true
# - DD_APM_ENABLED=true
# volumes:
# - /var/run/docker.sock:/var/run/docker.sock
# - /proc/:/host/proc/:ro
# - /sys/fs/cgroup:/host/sys/fs/cgroup:ro

# redis:
# image: redis
# ports:
# - "6379:6379"

# app:
# build:
# context: .
# dockerfile: Dockerfile
# ports:
# - "8081:8081"
# environment:
# DD_API_KEY: ${DD_API_KEY}
# DB_HOST: db
# DB_PORT: 5433
# DB_NAME: db
# DB_USER: postgres
# DB_PASSWORD: postgres
# SHARE_URI: "http://localhost:8080/share"
# REDIS_STREAM_KEY: ingsis
# REDIS_HOST: localhost
# REDIS_PORT: 6379
# REDIS_STREAM_GROUP_LINT: lint-manager
# labels:
# com.datadoghq.ad.logs: '[{"source": "java", "service": "manager-api-dev", "log_processing_rules": [{"type": "multi_line", "name": "log_start_with_date", "pattern" : "\\d{4}-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])"}]}]'

2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gpr.user=ricardolopezb
gpr.key=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
Empty file modified gradlew
100644 → 100755
Empty file.
32 changes: 5 additions & 27 deletions src/main/kotlin/ingsis/snippetmanager/SnippetManagerApplication.kt
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
package ingsis.snippetmanager

import ingsis.snippetmanager.snippet.Snippet
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import java.time.LocalDate
import org.springframework.metrics.export.datadog.EnableDatadogMetrics

@SpringBootApplication
@RestController
class SnippetManagerApplication{

@GetMapping("/")
fun url(): String {
return "Go to /hello or /snippets";
}
@GetMapping("/hello")
fun hello(): List<String> {
return listOf("Hello", " World, ", "this is Numa", "!");
}
@SpringBootApplication
@EnableDatadogMetrics
class SnippetManagerApplication

@GetMapping("/snippets")
fun snippets():List<Snippet>{
return listOf(
Snippet(1, "Contenido 1", LocalDate.now()),
Snippet(2, "Contenido 2", LocalDate.now()),
Snippet(3, "Contenido 3", LocalDate.now()),
Snippet(4, "Contenido 4", LocalDate.now()),
);
}
}
fun main(args: Array<String>) {
runApplication<SnippetManagerApplication>(*args)
runApplication<SnippetManagerApplication>(*args)
}



16 changes: 16 additions & 0 deletions src/main/kotlin/ingsis/snippetmanager/auth/AudienceValidator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ingsis.snippetmanager.auth

import org.springframework.security.oauth2.core.OAuth2Error
import org.springframework.security.oauth2.core.OAuth2TokenValidator
import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult
import org.springframework.security.oauth2.jwt.Jwt


internal class AudienceValidator(private val audience: String) : OAuth2TokenValidator<Jwt> {
override fun validate(jwt: Jwt): OAuth2TokenValidatorResult {
val error = OAuth2Error("invalid_token", "The required audience is missing", null)
return if (jwt.audience.contains(audience)) {
OAuth2TokenValidatorResult.success()
} else OAuth2TokenValidatorResult.failure(error)
}
}
58 changes: 58 additions & 0 deletions src/main/kotlin/ingsis/snippetmanager/auth/SecurityConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ingsis.snippetmanager.auth

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator;
import org.springframework.security.oauth2.core.OAuth2TokenValidator;
import org.springframework.security.oauth2.jwt.*;
import org.springframework.security.web.SecurityFilterChain;


@EnableWebSecurity
class SecurityConfig {
@Value("\${auth0.audience}")
private val audience: String? = null

@Value("\${spring.security.oauth2.resourceserver.jwt.issuer-uri}")
private val issuer: String? = null
@Bean
@Throws(Exception::class)
fun filterChain(http: HttpSecurity): SecurityFilterChain {
/*
This is where we configure the security required for our endpoints and setup our app to serve as
an OAuth2 Resource Server, using JWT validation.
*/

http.authorizeRequests()
.mvcMatchers("/snippet").authenticated()
.mvcMatchers("/snippet/**").authenticated()
.mvcMatchers("/health").authenticated()
.mvcMatchers("/health/**").authenticated()
.mvcMatchers("/test").authenticated()
.mvcMatchers("/test/**").authenticated()
.mvcMatchers("/rule").authenticated()
.mvcMatchers("/rule/**").authenticated()
.mvcMatchers("/error").authenticated()
.mvcMatchers("/error/**").authenticated()
.and().cors()
.and().csrf().disable()
.oauth2ResourceServer().jwt();
return http.build();
}

@Bean
fun jwtDecoder(): JwtDecoder {
/*
By default, Spring Security does not validate the "aud" claim of the token, to ensure that this token is
indeed intended for our app. Adding our own validator is easy to do:
*/
val jwtDecoder = JwtDecoders.fromOidcIssuerLocation<JwtDecoder>(issuer) as NimbusJwtDecoder
val audienceValidator: OAuth2TokenValidator<Jwt> = AudienceValidator(audience!!)
val withIssuer: OAuth2TokenValidator<Jwt> = JwtValidators.createDefaultWithIssuer(issuer)
val withAudience: OAuth2TokenValidator<Jwt> = DelegatingOAuth2TokenValidator(withIssuer, audienceValidator)
jwtDecoder.setJwtValidator(withAudience)
return jwtDecoder
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ingsis.snippetmanager.domains.rule.adapter

import ingsis.snippetmanager.domains.rule.model.ComplianceState
import org.springframework.context.annotation.Bean
import org.springframework.stereotype.Component
import snippet.events.lint.LintResultStatus

@Component
class LintResultStatusToComplianceStateAdapter {
fun toComplianceState(lintResultStatus: LintResultStatus): ComplianceState {
return when (lintResultStatus) {
LintResultStatus.COMPLIANT -> ComplianceState.COMPLIANT
LintResultStatus.NON_COMPLIANT -> ComplianceState.NON_COMPLIANT
LintResultStatus.PENDING -> ComplianceState.PENDING
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ingsis.snippetmanager.domains.rule.controller

import ingsis.snippetmanager.domains.rule.dto.FormatterRulesDTO
import ingsis.snippetmanager.domains.rule.service.FormatterRulesService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.CrossOrigin
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController
import java.security.Principal

@RestController
@CrossOrigin("*")
class FormatterRulesController{

@Autowired
private var ruleService: FormatterRulesService

constructor(ruleService: FormatterRulesService){
this.ruleService = ruleService
}

@GetMapping("/rule/formatter")
fun getFormatterRulesByUserId(principal: Principal): ResponseEntity<FormatterRulesDTO>{
return ResponseEntity.ok(ruleService.getFormatterRulesByUserId(principal.name))
}

@PutMapping("/rule/formatter")
fun updateFormatterRules(@RequestBody formatterRules: FormatterRulesDTO, principal: Principal): ResponseEntity<FormatterRulesDTO>{
return ResponseEntity.ok(ruleService.updateFormatterRules(formatterRules, principal.name))
}
}
Loading

0 comments on commit 5b2d687

Please sign in to comment.