Java Code Examples for org.springframework.vault.client.VaultEndpoint#from()

The following examples show how to use org.springframework.vault.client.VaultEndpoint#from() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HashicorpVaultAliasService.java    From knox with Apache License 2.0 6 votes vote down vote up
@Override
public void init(GatewayConfig config, Map<String, String> options) throws ServiceLifecycleException {
  this.config = config;
  Map<String, String> remoteAliasServiceConfiguration = config.getRemoteAliasServiceConfiguration();
  Map<String, String> vaultConfiguration = new HashMap<>();
  for(Map.Entry<String, String> entry : remoteAliasServiceConfiguration.entrySet()) {
    if(entry.getKey().startsWith(VAULT_CONFIG_PREFIX)) {
      vaultConfiguration.put(entry.getKey(),
          entry.getValue());
    }
  }

  String vaultAddress = vaultConfiguration.get(VAULT_ADDRESS_KEY);
  String vaultSecretsEngine = vaultConfiguration.get(VAULT_SECRETS_ENGINE_KEY);
  vaultPathPrefix = getVaultPathPrefix(vaultConfiguration);

  VaultEndpoint vaultEndpoint;
  try {
    vaultEndpoint = VaultEndpoint.from(new URI(vaultAddress));
    ClientAuthentication vaultAuthentication = getClientAuthentication(vaultConfiguration);
    VaultTemplate vaultTemplate = new VaultTemplate(vaultEndpoint, vaultAuthentication);
    vault = vaultTemplate.opsForVersionedKeyValue(vaultSecretsEngine);
  } catch (Exception e) {
    throw new ServiceLifecycleException("Failed to init", e);
  }
}
 
Example 2
Source File: EnvironmentVaultConfiguration.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@Override
public VaultEndpoint vaultEndpoint() {

	String uri = getProperty("vault.uri");
	if (uri != null) {
		return VaultEndpoint.from(URI.create(uri));
	}

	throw new IllegalStateException("Vault URI (vault.uri) is null");
}
 
Example 3
Source File: VaultConfigurationUtil.java    From spring-cloud-vault with Apache License 2.0 5 votes vote down vote up
/**
 * Create a {@link VaultEndpoint} given {@link VaultProperties}.
 * @param vaultProperties the Vault properties.
 * @return the endpoint.
 */
static VaultEndpoint createVaultEndpoint(VaultProperties vaultProperties) {

	if (StringUtils.hasText(vaultProperties.getUri())) {
		return VaultEndpoint.from(URI.create(vaultProperties.getUri()));
	}

	VaultEndpoint vaultEndpoint = new VaultEndpoint();
	vaultEndpoint.setHost(vaultProperties.getHost());
	vaultEndpoint.setPort(vaultProperties.getPort());
	vaultEndpoint.setScheme(vaultProperties.getScheme());

	return vaultEndpoint;
}
 
Example 4
Source File: KubernetesHashicorpVaultClientAuthenticationProvider.java    From knox with Apache License 2.0 5 votes vote down vote up
private RestOperations getRestOperations(Map<String, String> properties) throws Exception {
  String vaultAddress = properties.get(HashicorpVaultAliasService.VAULT_ADDRESS_KEY);
  VaultEndpoint vaultEndpoint = VaultEndpoint.from(new URI(vaultAddress));
  VaultEndpointProvider vaultEndpointProvider = SimpleVaultEndpointProvider.of(vaultEndpoint);
  ClientOptions clientOptions = new ClientOptions();
  SslConfiguration sslConfiguration = SslConfiguration.unconfigured();
  ClientHttpRequestFactory clientHttpRequestFactory = ClientHttpRequestFactoryFactory.create(
      clientOptions, sslConfiguration);
  return VaultClients.createRestTemplate(vaultEndpointProvider, clientHttpRequestFactory);
}
 
Example 5
Source File: SpringVaultClientConfiguration.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Override
public VaultEndpoint vaultEndpoint() {

	URI baseUrl = UriComponentsBuilder.newInstance()
			.scheme(vaultProperties.getScheme()).host(vaultProperties.getHost())
			.port(vaultProperties.getPort()).build().toUri();

	return VaultEndpoint.from(baseUrl);
}
 
Example 6
Source File: VaultConfiguration.java    From vault-crd with Apache License 2.0 4 votes vote down vote up
@Override
public VaultEndpoint vaultEndpoint() {
    return VaultEndpoint.from(getVaultUrlWithoutPath(vaultUrl));
}
 
Example 7
Source File: VaultConfiguration.java    From vault-crd with Apache License 2.0 4 votes vote down vote up
@Override
public VaultEndpoint vaultEndpoint() {
    return VaultEndpoint.from(getVaultUrlWithoutPath(vaultUrl));
}
 
Example 8
Source File: HashicorpKeyVaultServiceFactory.java    From tessera with Apache License 2.0 4 votes vote down vote up
KeyVaultService create(
        Config config, EnvironmentVariableProvider envProvider, HashicorpKeyVaultServiceFactoryUtil util) {
    Objects.requireNonNull(config);
    Objects.requireNonNull(envProvider);
    Objects.requireNonNull(util);

    final String roleId = envProvider.getEnv(HASHICORP_ROLE_ID);
    final String secretId = envProvider.getEnv(HASHICORP_SECRET_ID);
    final String authToken = envProvider.getEnv(HASHICORP_TOKEN);

    if (roleId == null && secretId == null && authToken == null) {
        throw new HashicorpCredentialNotSetException(
                "Environment variables must be set to authenticate with Hashicorp Vault.  Set the "
                        + HASHICORP_ROLE_ID
                        + " and "
                        + HASHICORP_SECRET_ID
                        + " environment variables if using the AppRole authentication method.  Set the "
                        + HASHICORP_TOKEN
                        + " environment variable if using another authentication method.");
    } else if (isOnlyOneInputNull(roleId, secretId)) {
        throw new HashicorpCredentialNotSetException(
                "Only one of the "
                        + HASHICORP_ROLE_ID
                        + " and "
                        + HASHICORP_SECRET_ID
                        + " environment variables to authenticate with Hashicorp Vault using the AppRole method has been set");
    }

    KeyVaultConfig keyVaultConfig =
            Optional.ofNullable(config.getKeys())
                    .flatMap(k -> k.getKeyVaultConfig(KeyVaultType.HASHICORP))
                    .orElseThrow(
                            () ->
                                    new ConfigException(
                                            new RuntimeException(
                                                    "Trying to create Hashicorp Vault connection but no Vault configuration provided")));

    VaultEndpoint vaultEndpoint;

    try {
        URI uri = new URI(keyVaultConfig.getProperty("url").get());
        vaultEndpoint = VaultEndpoint.from(uri);
    } catch (URISyntaxException | NoSuchElementException | IllegalArgumentException e) {
        throw new ConfigException(new RuntimeException("Provided Hashicorp Vault url is incorrectly formatted", e));
    }

    SslConfiguration sslConfiguration = util.configureSsl(keyVaultConfig, envProvider);

    ClientOptions clientOptions = new ClientOptions();

    ClientHttpRequestFactory clientHttpRequestFactory =
            util.createClientHttpRequestFactory(clientOptions, sslConfiguration);

    ClientAuthentication clientAuthentication =
            util.configureClientAuthentication(
                    keyVaultConfig, envProvider, clientHttpRequestFactory, vaultEndpoint);

    SessionManager sessionManager = new SimpleSessionManager(clientAuthentication);
    VaultOperations vaultOperations = new VaultTemplate(vaultEndpoint, clientHttpRequestFactory, sessionManager);

    return new HashicorpKeyVaultService(new KeyValueOperationsDelegateFactory(vaultOperations));
}