Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
feat: working spring workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenLoc committed Mar 5, 2024
1 parent 089aa3c commit af8226e
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 14 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/springBootTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: build-and-release

on:
push:
branches: [ main ]
pull_request:
workflow_dispatch:

env:
_JAVA_OPTS: "-Xmx2g"
GRADLE_OPTS: "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=2 -Dorg.gradle.console=plain"

jobs:
verify:
runs-on: ubuntu-latest
timeout-minutes: 30
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: corretto
java-version: 21
cache: gradle
- uses: taiki-e/install-action@v2
with:
tool: just@1,hurl@4
- run: just verify
1 change: 1 addition & 0 deletions showcases/springboot/api_tests/hurl.env.test
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target=http://localhost:8000
token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6IjEyMDkxMDkyOTAifQ.eyJhdWQiOlsibG9jYWwubXlfZG9tYWluLmNvbSJdLCJhenAiOiJkMTlYcDhEWEkxQVE4ZUNjeDdweHM5bkZiUTRUMHUwdCIsImVtYWlsIjoiam9obi5kb2VAb3B0cmF2aXMuY29tIiwiZXhwIjozMjUwMzY3NjQwMCwiaWF0IjowLCJpc3MiOiJodHRwczovL2F1dGgubXlfZG9tYWluLmNvbS8iLCJteV9vdGhlcl9zcGVjaWFsX2NsYWltIjoiaGVsbG8gd29ybGQiLCJuYW1lIjoiSm9obiIsInN1YiI6IjBSWHFvQ3ZzZlQ3NDludmpjIn0.TcAcntSyoXa1DMxxY8lxaOUTMcLVldrQXXM1jzAExhQ
2 changes: 1 addition & 1 deletion showcases/springboot/api_tests/implemented/protected.hurl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# wait for healthy
GET {{target}}/api/protected

Authorization: Bearer {{token}}


HTTP 200
1 change: 1 addition & 0 deletions showcases/springboot/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ services:
service:
image: hurlspring:latest
environment:
- 'JWT_SECRET=CTRKew35ltwdWhGv9WF10lJ06oYBZKzACYhANx7QXPZpvBvCNZbq161xHg2rKhcp'
- 'SERVER_PORT=8000'
ports:
- '8000:8000'
Expand Down
16 changes: 8 additions & 8 deletions showcases/springboot/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,20 @@ up c="": build
docker-compose down
docker-compose up {{c}}

verify: test build-and-run-api-test lint
just build-and-run-api-test
verify: test build-and-run-api-test

# Run all tests
test:
./gradlew test

# Run linters
lint:
./gradlew detekt

# run api tests
build-and-run-api-test: build (up "-d") api-test
build-and-run-api-test: (up "-d") api-test
docker-compose down


wait-for-api:
hurl api_tests/implemented/healthz.hurl --retry 60 {{hurl_opts}}
hurl api_tests/implemented/healthz.hurl --retry 60 {{hurl_opts}} --very-verbose


# run acceptance tests against the running test stack
Expand All @@ -43,4 +39,8 @@ api-test-not-implemented *args: wait-for-api
install-hurl:
curl --location --remote-name https://github.com/Orange-OpenSource/hurl/releases/download/4.1.0/hurl_4.1.0_amd64.deb
sudo apt update && sudo apt install ./hurl_4.1.0_amd64.deb
rm hurl_4.1.0_amd64.deb
rm hurl_4.1.0_amd64.deb


@replace-token:
cd token_creation && just replace
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.example.hurlspringboot

import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import org.springframework.security.oauth2.jwt.Jwt
import org.springframework.security.web.SecurityFilterChain
import org.springframework.security.oauth2.jwt.JwtDecoders;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher
import org.springframework.security.web.util.matcher.OrRequestMatcher
import java.time.Instant
import java.util.function.Consumer

@Configuration
class JWTSecurityConfig {
Expand All @@ -24,8 +28,8 @@ class JWTSecurityConfig {
fun filterChain(http: HttpSecurity): SecurityFilterChain {
return http
.authorizeHttpRequests { authorizeRequests ->
authorizeRequests.requestMatchers("/api/healthz").permitAll()
.anyRequest().authenticated()
authorizeRequests.requestMatchers(unauthenticatedMatcher).permitAll()
authorizeRequests.anyRequest().authenticated()
}
.oauth2ResourceServer { oauth2ResourceServer: OAuth2ResourceServerConfigurer<HttpSecurity?> ->
oauth2ResourceServer
Expand All @@ -39,12 +43,20 @@ class JWTSecurityConfig {
}
}.build()
}

companion object {
private val unauthenticatedMatcher = OrRequestMatcher(
AntPathRequestMatcher("/api/healthz"),
)
}
}



class LocalJwtDecoder(private val hmac256Secret: String): JwtDecoder{
override fun decode(token: String?): Jwt {
val verified = JWT.require(Algorithm.HMAC256(hmac256Secret)).build().verify(token)
return Jwt.withTokenValue(verified.token).build()
return Jwt(verified.token, Instant.now(), Instant.now(), mapOf("test" to "test"), mapOf("test" to "test"))
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
server.port=${SERVER_PORT:#8000}
spring.security.oauth2.resourceserver.jwt.hmac256Secret=${ISSUER:#{null}}
spring.security.oauth2.resourceserver.jwt.issuer-uri=${JWT_SECRET:#{null}}
spring.security.oauth2.resourceserver.jwt.issuer-uri=${ISSUER:#{null}}
spring.security.oauth2.resourceserver.jwt.hmac256Secret=${JWT_SECRET:#{null}}
20 changes: 20 additions & 0 deletions showcases/springboot/token_creation/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

secret := "CTRKew35ltwdWhGv9WF10lJ06oYBZKzACYhANx7QXPZpvBvCNZbq161xHg2rKhcp"

@create-token secret=secret:
jwt encode --kid "1209109290" --secret={{secret}} "$(cat payload.json)"

@raw:
just create-token

replace:
just replace-token $( just raw )

replace-token token:
sed -i~ '/^token=/s/=.*/={{token}}/' ./../api_tests/hurl.env.test

token choose="":
just create-token | jwt decode - -j | jq .payload"{{choose}}"

install-dev-tools:
cargo install jwt-cli
11 changes: 11 additions & 0 deletions showcases/springboot/token_creation/payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"my_other_special_claim": "hello world",
"email": "john.doe@optravis.com",
"name": "John",
"iss": "https://auth.my_domain.com/",
"sub": "0RXqoCvsfT749nvjc",
"aud": ["local.my_domain.com"],
"iat": 0,
"exp": 32503676400,
"azp": "d19Xp8DXI1AQ8eCcx7pxs9nFbQ4T0u0t"
}

0 comments on commit af8226e

Please sign in to comment.