Skip to content

Commit

Permalink
Add support for Python clients (#165)
Browse files Browse the repository at this point in the history
- Changes vault auto-config to only add port number when it is not 443
- Adds example links to readme
- Clarifies port configuration in case of Docker container

{patch}

Signed-off-by: Esta Nagy <[email protected]>
  • Loading branch information
nagyesta authored May 14, 2022
1 parent d1e7461 commit 2b0f808
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 7 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,24 @@ In this case the issue is probably just exposing the `8443` port of the containe
when starting the container should do the trick.
[Example](lowkey-vault-docker/build.gradle#L61)

In case you need to change both the internal and the external port numbers, you can try using the ```LOWKEY_ARGS``` environment variable with
```--server.port=<portNumber>``` such as:

```shell
export LOWKEY_ARGS="--server.port=443"
docker run --rm --name lowkey -e LOWKEY_ARGS -d -p 443:443 nagyesta/lowkey-vault:1.4.0
```

##### Using Testcontainers

This issue should not happen while using Testcontainers. See examples under [Lowkey Vault Testcontainers](lowkey-vault-testcontainers/README.md).

# Example projects

1. [Java](https://github.com/nagyesta/lowkey-vault-example)
2. [.Net](https://github.com/nagyesta/lowkey-vault-example-dotnet)
3. [Python](https://github.com/nagyesta/lowkey-vault-example-python)

# Limitations

- Some encryption/signature algorithms are not supported. Please refer to the ["Features"](#features) section for the up-to-date list of supported algorithms.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.nagyesta.lowkeyvault;

import com.github.nagyesta.lowkeyvault.context.util.VaultUriUtil;
import com.github.nagyesta.lowkeyvault.service.vault.VaultService;
import com.github.nagyesta.lowkeyvault.service.vault.impl.VaultServiceImpl;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -10,7 +11,6 @@
import org.springframework.util.StringUtils;
import org.springframework.web.filter.CommonsRequestLoggingFilter;

import java.net.URI;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream;
Expand All @@ -30,19 +30,18 @@ public VaultService vaultService() {
final VaultServiceImpl service = new VaultServiceImpl();
log.info("Starting up vault with port: {} , auto-registering vaults: '{}'", port, autoRegisterVaults);
Stream.of(
"https://localhost:" + port,
"https://default.lowkey-vault:" + port,
"https://default.lowkey-vault.local:" + port
VaultUriUtil.vaultUri("localhost", port),
VaultUriUtil.vaultUri("default.lowkey-vault", port),
VaultUriUtil.vaultUri("default.lowkey-vault.local", port)
)
.map(URI::create).forEach(service::create);
.forEach(service::create);
Optional.ofNullable(autoRegisterVaults)
.filter(StringUtils::hasText)
.map(StringUtils::commaDelimitedListToStringArray)
.map(array -> Arrays.stream(array)
.filter(StringUtils::hasText)
.map(vaultName -> StringUtils.trimTrailingCharacter(vaultName, '/'))
.map(vaultName -> "https://" + vaultName + ".localhost:" + port)
.map(URI::create))
.map(vaultName -> VaultUriUtil.vaultUri(vaultName + ".localhost", port)))
.orElse(Stream.of()).forEach(service::create);
log.info("Vaults registered!");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.nagyesta.lowkeyvault.context.util;

import lombok.NonNull;

import java.net.URI;

public final class VaultUriUtil {

private static final int DEFAULT_HTTPS_PORT = 443;
private static final String HTTPS_SCHEME = "https://";

private VaultUriUtil() {
throw new IllegalCallerException("Utility cannot be instantiated.");
}

public static URI vaultUri(@NonNull final String hostname, final int optionalPort) {
final StringBuilder builder = new StringBuilder(HTTPS_SCHEME).append(hostname);
if (optionalPort != DEFAULT_HTTPS_PORT) {
builder.append(":").append(optionalPort);
}
return URI.create(builder.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.github.nagyesta.lowkeyvault.context.util;

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 java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.stream.Stream;

class VaultUriUtilTest {

@SuppressWarnings("checkstyle:MagicNumber")
public static Stream<Arguments> validSource() {
return Stream.<Arguments>builder()
.add(Arguments.of("localhost", 443, URI.create("https://localhost")))
.add(Arguments.of("localhost", 8443, URI.create("https://localhost:8443")))
.add(Arguments.of("localhost", 8444, URI.create("https://localhost:8444")))
.add(Arguments.of("lowkey-vault.local", 443, URI.create("https://lowkey-vault.local")))
.add(Arguments.of("lowkey-vault.local", 8080, URI.create("https://lowkey-vault.local:8080")))
.add(Arguments.of("lowkey-vault.local", 8443, URI.create("https://lowkey-vault.local:8443")))
.build();
}

@Test
void testConstructorShouldThrowExceptionWhenCalled() throws NoSuchMethodException {
//given
final Constructor<VaultUriUtil> constructor = VaultUriUtil.class.getDeclaredConstructor();
constructor.setAccessible(true);

//when
Assertions.assertThrows(InvocationTargetException.class, constructor::newInstance);

//then + exception
}

@ParameterizedTest
@MethodSource("validSource")
void testVaultUriShouldOmitPortNumberWhenCalledWithDefault(final String host, final int port, final URI expectedUri) {
//given

//when
final URI actual = VaultUriUtil.vaultUri(host, port);

//then
Assertions.assertEquals(expectedUri, actual);
}

@SuppressWarnings("ConstantConditions")
@Test
void testVaultUriShouldThrowExceptionWhenCalledWithNull() {
//given

//when
Assertions.assertThrows(IllegalArgumentException.class, () -> VaultUriUtil.vaultUri(null, 1));

//then + exception
}
}

0 comments on commit 2b0f808

Please sign in to comment.