Skip to content

Commit

Permalink
cleanup/docker-compose-integration-tests (#3614)
Browse files Browse the repository at this point in the history
Mindre refaktorering for å fjerne forskjellige warnings.
  • Loading branch information
rfc3092 authored Sep 12, 2024
1 parent 93cda90 commit 0b6141d
Show file tree
Hide file tree
Showing 29 changed files with 268 additions and 271 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ Eller kjør:
```

## Virtuelt miljø
Kjør kommandoen:

Kjør kommandoen:
```aiexclude
> JWK=$(cat ./mocks/jwk.json) docker compose up --build
```
JWK=$(cat ./mocks/jwk.json) docker compose up --build
Evt. i PowerShell:
```aiexclude
> $env:JWK=(Get-Content -Path ./mocks/jwk.json -Raw) ; docker compose up --build
```

Deretter kan itegrasjonstester kjøres med kommandoen:

Deretter kan itegrasjonstester kjøres med kommandoen:
```
./gradlew iTest
> ./gradlew iTest
```

NB: Dette vil kun fungere hvis appen støtter itegrasjonstester.
NB: Dette vil kun fungere hvis appen støtter integrasjonstester.


## Kode generert av GitHub Copilot
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: "3.9" # optional since v1.27.0
services:
testnav-person-organisasjon-tilgang-service:
container_name: testnav-person-organisasjon-tilgang-service
Expand Down
2 changes: 1 addition & 1 deletion mocks/maskinporten-mock/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM ghcr.io/navikt/baseimages/temurin:21
LABEL maintainer="Team Dolly"

ADD /build/libs/app.jar /app/app.jar
COPY /build/libs/app.jar /app/app.jar

EXPOSE 8080
12 changes: 6 additions & 6 deletions mocks/maskinporten-mock/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ plugins {
dependencies {
implementation "com.auth0:java-jwt:$versions.jwt"

implementation 'no.nav.testnav.libs:security-core'
implementation 'no.nav.testnav.libs:reactive-core'
implementation "no.nav.testnav.libs:security-core"
implementation "no.nav.testnav.libs:reactive-core"

implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
implementation "org.springframework.boot:spring-boot-starter-security"
implementation "org.springframework.boot:spring-boot-starter-webflux"
implementation "org.springframework.boot:spring-boot-starter-oauth2-resource-server"
implementation "org.springdoc:springdoc-openapi-starter-webflux-ui:$versions.springdoc"

compileOnly 'jakarta.servlet:jakarta.servlet-api'
compileOnly "jakarta.servlet:jakarta.servlet-api"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package no.nav.testnav.mocks.maskinporten;

import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.reactive.config.EnableWebFlux;

import no.nav.testnav.libs.reactivecore.config.CoreConfig;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

@Import({
CoreConfig.class,
})
@EnableWebFlux
@SpringBootApplication
public class MaskinportenMockApplicationStarter {
public static void main(String[] args) {
SpringApplication.run(MaskinportenMockApplicationStarter.class, args);
}

@UtilityClass
public static class Utils {

@SneakyThrows
public static String loadJson(String path) {
try (final InputStreamReader stream = new InputStreamReader(new ClassPathResource(path).getInputStream(), StandardCharsets.UTF_8)) {
return new BufferedReader(stream)
.lines()
.collect(Collectors.joining("\n"));
}
}

}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package no.nav.testnav.mocks.tokendingsmock.config;
package no.nav.testnav.mocks.maskinporten.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import no.nav.testnav.libs.reactivecore.config.ApplicationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import no.nav.testnav.libs.reactivecore.config.ApplicationProperties;


@Configuration
public class OpenApiConfig {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.nav.testnav.mocks.tokendingsmock.config;
package no.nav.testnav.mocks.maskinporten.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -18,6 +18,9 @@ public class SecurityConfig {

@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
return http.csrf(ServerHttpSecurity.CsrfSpec::disable).build();
return http
.csrf(ServerHttpSecurity.CsrfSpec::disable)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package no.nav.testnav.mocks.maskinporten.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import no.nav.testnav.mocks.maskinporten.domain.AccessToken;
import no.nav.testnav.mocks.maskinporten.domain.Arguments;
import no.nav.testnav.mocks.maskinporten.service.JwtService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/mock")
public class MockController {

private final JwtService service;

@PostMapping(
value = "/token",
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public Mono<AccessToken> getToken(Arguments arguments) {
return Mono.just(service.createAccessToken(arguments.getAudience()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package no.nav.testnav.mocks.maskinporten.controller;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import no.nav.testnav.mocks.maskinporten.domain.AccessToken;
import no.nav.testnav.mocks.maskinporten.domain.Arguments;
import no.nav.testnav.mocks.maskinporten.service.JwtService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import static no.nav.testnav.mocks.maskinporten.MaskinportenMockApplicationStarter.Utils.loadJson;

@Slf4j
@RestController
@RequiredArgsConstructor
public class OauthAuthorizationServiceController {

private static final String JWKS;
private static final String WELL_KNOWN;

static {
JWKS = loadJson("static/jwks.json");
WELL_KNOWN = loadJson("static/well-known.json");
}

private final JwtService jwtService;

@GetMapping(value = "/jwks", produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<String> getJWKS() {
return Mono.just(JWKS);
}

@GetMapping(value = "/.well-known/oauth-authorization-server", produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<String> getWellKnown() {
return Mono.just(WELL_KNOWN);
}

@PostMapping(value = "/token", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<AccessToken> createToken(Arguments arguments) {
return Mono.just(jwtService.createAccessToken(arguments.getAudience()));
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.nav.testnav.mocks.tokendingsmock.domain;
package no.nav.testnav.mocks.maskinporten.domain;

import com.fasterxml.jackson.annotation.JsonProperty;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.nav.testnav.mocks.tokendingsmock.domain;
package no.nav.testnav.mocks.maskinporten.domain;

import lombok.Getter;
import lombok.Setter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,32 @@
package no.nav.testnav.mocks.tokendingsmock.service;
package no.nav.testnav.mocks.maskinporten.service;


import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.nimbusds.jose.jwk.RSAKey;
import lombok.SneakyThrows;
import org.springframework.core.io.ClassPathResource;
import no.nav.testnav.mocks.maskinporten.domain.AccessToken;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.interfaces.RSAPrivateKey;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import java.util.stream.Collectors;

import no.nav.testnav.mocks.tokendingsmock.domain.AccessToken;
import static no.nav.testnav.mocks.maskinporten.MaskinportenMockApplicationStarter.Utils.loadJson;

@Service
public class JwtService {

private static final String jwtSecret;
private static final String JWK;

static {
jwtSecret = loadJson("static/jwk.json");
}

private static String loadJson(String path) {
var resource = new ClassPathResource(path);
try (final InputStreamReader stream = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) {
return new BufferedReader(stream)
.lines().collect(Collectors.joining("\n"));

} catch (IOException e) {
throw new RuntimeException("Feil med paring av " + path + ".", e);
}
JWK = loadJson("static/jwk.json");
}

@SneakyThrows
public AccessToken createAccessToken(String audience) {

var date = Calendar.getInstance();
var expiresAt = date.getTimeInMillis() + (60 * 60 * 1000);
var builder = JWT
Expand All @@ -52,16 +37,14 @@ public AccessToken createAccessToken(String audience) {
.withAudience(audience)
.withJWTId(UUID.randomUUID().toString())
.withExpiresAt(new Date(expiresAt));

var privateKey = RSAKey.parse(jwtSecret).toPrivateKey();

var privateKey = RSAKey.parse(JWK).toPrivateKey();
return new AccessToken(
builder.sign(Algorithm.RSA256(null, (RSAPrivateKey) privateKey)),
"Bearer",
60 * 60 * 1000,
audience

);

}

}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 0b6141d

Please sign in to comment.