-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Testcontainers Module (#1288)
- Adds new endpoints serving the default keystore and keystore password - Adds new methods to the Testcontainers module to obtain the keystore and password - Adds new test cases to cover new functionality - Skips PR labeling from forks Resolves #1285 {minor} Signed-off-by: Esta Nagy <nagyesta@gmail.com>
- Loading branch information
Showing
11 changed files
with
243 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 0 additions & 34 deletions
34
.../main/java/com/github/nagyesta/lowkeyvault/controller/ManagedIdentityTokenController.java
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
...ault-app/src/main/java/com/github/nagyesta/lowkeyvault/controller/MetadataController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.github.nagyesta.lowkeyvault.controller; | ||
|
||
import com.github.nagyesta.lowkeyvault.model.TokenResponse; | ||
import lombok.NonNull; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.util.MimeTypeUtils; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
|
||
@Slf4j | ||
@RestController | ||
public class MetadataController { | ||
|
||
private final String tokenRealm; | ||
private final byte[] keyStoreContent; | ||
private final String keyStorePassword; | ||
|
||
public MetadataController( | ||
@NonNull @Value("${LOWKEY_TOKEN_REALM:assumed-identity}") final String tokenRealm, | ||
@NonNull @Value("${default-keystore-resource}") final String keyStoreResource, | ||
@NonNull @Value("${default-keystore-password}") final String keyStorePassword) throws IOException { | ||
this.tokenRealm = tokenRealm; | ||
this.keyStoreContent = new ClassPathResource(keyStoreResource).getContentAsByteArray(); | ||
this.keyStorePassword = keyStorePassword; | ||
} | ||
|
||
@GetMapping(value = {"/metadata/identity/oauth2/token", "/metadata/identity/oauth2/token/"}) | ||
public ResponseEntity<TokenResponse> getManagedIdentityToken(@RequestParam("resource") final URI resource) { | ||
final TokenResponse body = new TokenResponse(resource); | ||
log.info("Returning token: {}", body); | ||
return ResponseEntity.ok() | ||
.header(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=" + tokenRealm) | ||
.body(body); | ||
} | ||
|
||
@GetMapping(value = "/metadata/default-cert/lowkey-vault.p12", | ||
produces = MimeTypeUtils.APPLICATION_OCTET_STREAM_VALUE) | ||
public @ResponseBody byte[] getDefaultCertificateStoreContent() { | ||
log.info("Returning default certificate store."); | ||
return keyStoreContent; | ||
} | ||
|
||
@GetMapping(value = "/metadata/default-cert/password", | ||
produces = MimeTypeUtils.TEXT_PLAIN_VALUE) | ||
public @ResponseBody String getDefaultCertificateStorePassword() { | ||
log.info("Returning default certificate store password."); | ||
return keyStorePassword; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...c/test/java/com/github/nagyesta/lowkeyvault/controller/common/MetadataControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.github.nagyesta.lowkeyvault.controller.common; | ||
|
||
import com.github.nagyesta.lowkeyvault.controller.MetadataController; | ||
import com.github.nagyesta.lowkeyvault.model.TokenResponse; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
class MetadataControllerTest { | ||
|
||
private static final String KEY_STORE_PASSWORD = "changeit"; | ||
private static final String KEY_STORE_RESOURCE = "cert/keystore.p12"; | ||
private static final String REALM_NAME = "realm-name"; | ||
|
||
public static Stream<Arguments> nullProvider() { | ||
return Stream.<Arguments>builder() | ||
.add(Arguments.of(null, null, null)) | ||
.add(Arguments.of(REALM_NAME, null, null)) | ||
.add(Arguments.of(null, KEY_STORE_RESOURCE, null)) | ||
.add(Arguments.of(null, null, KEY_STORE_PASSWORD)) | ||
.add(Arguments.of(null, KEY_STORE_RESOURCE, KEY_STORE_PASSWORD)) | ||
.add(Arguments.of(REALM_NAME, null, KEY_STORE_PASSWORD)) | ||
.add(Arguments.of(REALM_NAME, KEY_STORE_RESOURCE, null)) | ||
.build(); | ||
} | ||
|
||
@Test | ||
void testGetManagedIdentityTokenShouldReturnTokenWhenCalled() throws IOException { | ||
//given | ||
final MetadataController underTest = new MetadataController(REALM_NAME, KEY_STORE_RESOURCE, KEY_STORE_PASSWORD); | ||
final URI resource = URI.create("https://localhost:8443/"); | ||
|
||
//when | ||
final ResponseEntity<TokenResponse> actual = underTest.getManagedIdentityToken(resource); | ||
|
||
//then | ||
Assertions.assertNotNull(actual); | ||
Assertions.assertEquals(HttpStatus.OK, actual.getStatusCode()); | ||
Assertions.assertNotNull(actual.getBody()); | ||
Assertions.assertEquals(resource, actual.getBody().resource()); | ||
Assertions.assertEquals("dummy", actual.getBody().accessToken()); | ||
Assertions.assertEquals(List.of("Basic realm=" + REALM_NAME), actual.getHeaders().get(HttpHeaders.WWW_AUTHENTICATE)); | ||
} | ||
|
||
@Test | ||
void testGetDefaultCertificateStoreContentShouldReturnResourceContents() throws IOException { | ||
//given | ||
final MetadataController underTest = new MetadataController(REALM_NAME, KEY_STORE_RESOURCE, KEY_STORE_PASSWORD); | ||
final byte[] expected = getResourceContent(); | ||
|
||
//when | ||
final byte[] actual = underTest.getDefaultCertificateStoreContent(); | ||
|
||
//then | ||
Assertions.assertNotNull(actual); | ||
Assertions.assertArrayEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
void testGetDefaultCertificateStorePasswordShouldReturnPassword() throws IOException { | ||
//given | ||
final MetadataController underTest = new MetadataController(REALM_NAME, KEY_STORE_RESOURCE, KEY_STORE_PASSWORD); | ||
|
||
//when | ||
final String actual = underTest.getDefaultCertificateStorePassword(); | ||
|
||
//then | ||
Assertions.assertEquals(KEY_STORE_PASSWORD, actual); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("nullProvider") | ||
void testConstructorShouldThrowExceptionWhenCalledWithNull(final String realm, final String resource, final String password) { | ||
//given | ||
|
||
//when | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> new MetadataController(realm, resource, password)); | ||
|
||
//then + exception | ||
} | ||
|
||
private byte[] getResourceContent() throws IOException { | ||
final URL url = getClass().getResource("/" + KEY_STORE_RESOURCE); | ||
if (url == null) { | ||
throw new IOException("Resource not found: " + KEY_STORE_RESOURCE); | ||
} | ||
//noinspection LocalCanBeFinal | ||
try (InputStream inputStream = url.openStream()) { | ||
return inputStream.readAllBytes(); | ||
} | ||
} | ||
} |
44 changes: 0 additions & 44 deletions
44
.../src/test/java/com/github/nagyesta/lowkeyvault/controller/common/TokenControllerTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.