Example usage for org.springframework.vault.core VaultOperations doWithSession

List of usage examples for org.springframework.vault.core VaultOperations doWithSession

Introduction

In this page you can find the example usage for org.springframework.vault.core VaultOperations doWithSession.

Prototype

@Nullable
<T> T doWithSession(RestOperationsCallback<T> sessionCallback) throws VaultException, RestClientException;

Source Link

Document

Executes a Vault RestOperationsCallback .

Usage

From source file:example.helloworld.CubbyholeAuthenticationTests.java

/**
 * Write some data to Vault before Vault can be used as {@link VaultPropertySource}.
 *//*from  w  ww  .j  a  va  2 s. co m*/
@BeforeClass
public static void beforeClass() {

    VaultOperations vaultOperations = new VaultTestConfiguration().vaultTemplate();
    vaultOperations.write("secret/myapp/configuration", Collections.singletonMap("configuration.key", "value"));

    VaultResponse response = vaultOperations.doWithSession(new RestOperationsCallback<VaultResponse>() {

        @Override
        public VaultResponse doWithRestOperations(RestOperations restOperations) {

            HttpHeaders headers = new HttpHeaders();
            headers.add("X-Vault-Wrap-TTL", "10m");

            return restOperations.postForObject("auth/token/create", new HttpEntity<Object>(headers),
                    VaultResponse.class);
        }
    });

    // Response Wrapping requires Vault 0.6.0+
    Map<String, String> wrapInfo = response.getWrapInfo();
    initialToken = VaultToken.of(wrapInfo.get("token"));
}