Package org.springframework.vault.core
Class VaultTemplate
java.lang.Object
org.springframework.vault.core.VaultTemplate
- All Implemented Interfaces:
DisposableBean
,InitializingBean
,VaultOperations
public class VaultTemplate
extends Object
implements InitializingBean, VaultOperations, DisposableBean
This class encapsulates main Vault interaction.
VaultTemplate
will log into
Vault on initialization and use the token throughout the whole lifetime.- Author:
- Mark Paluch
- See Also:
-
Constructor Summary
ConstructorDescriptionVaultTemplate
(RestTemplateBuilder restTemplateBuilder) VaultTemplate
(RestTemplateBuilder restTemplateBuilder, SessionManager sessionManager) VaultTemplate
(VaultEndpoint vaultEndpoint) Create a newVaultTemplate
with aVaultEndpoint
.VaultTemplate
(VaultEndpointProvider endpointProvider, ClientHttpRequestFactory requestFactory) Create a newVaultTemplate
with aVaultEndpointProvider
,ClientHttpRequestFactory
andSessionManager
.VaultTemplate
(VaultEndpointProvider endpointProvider, ClientHttpRequestFactory requestFactory, SessionManager sessionManager) Create a newVaultTemplate
with aVaultEndpointProvider
,ClientHttpRequestFactory
andSessionManager
.VaultTemplate
(VaultEndpoint vaultEndpoint, ClientHttpRequestFactory clientHttpRequestFactory) VaultTemplate
(VaultEndpoint vaultEndpoint, ClientHttpRequestFactory clientHttpRequestFactory, SessionManager sessionManager) VaultTemplate
(VaultEndpoint vaultEndpoint, ClientAuthentication clientAuthentication) -
Method Summary
Modifier and TypeMethodDescriptionvoid
void
Delete a path.void
destroy()
protected RestTemplate
doCreateRestTemplate
(VaultEndpointProvider endpointProvider, ClientHttpRequestFactory requestFactory) Create aRestTemplate
to be used byVaultTemplate
for Vault communication givenVaultEndpointProvider
andClientHttpRequestFactory
.protected RestTemplate
doCreateSessionTemplate
(VaultEndpointProvider endpointProvider, ClientHttpRequestFactory requestFactory) Create a session-boundRestTemplate
to be used byVaultTemplate
for Vault communication givenVaultEndpointProvider
andClientHttpRequestFactory
for calls that require an authenticated context.<T> T
doWithSession
(RestOperationsCallback<T> sessionCallback) Executes a VaultRestOperationsCallback
.<T> T
doWithVault
(RestOperationsCallback<T> clientCallback) Executes a VaultRestOperationsCallback
.Enumerate keys from a Vault path.opsForKeyValue
(String path, VaultKeyValueOperationsSupport.KeyValueBackend apiVersion) ReturnVaultKeyValueOperations
.ReturnVaultPkiOperations
if the PKI backend is mounted on a different path thanpki
.opsForTransform
(String path) ReturnVaultTransformOperations
if the transit backend is mounted on a different path thantransform
.opsForTransit
(String path) ReturnVaultTransitOperations
if the transit backend is mounted on a different path thantransit
.ReturnVaultVersionedKeyValueOperations
.Read from a Vault path.<T> VaultResponseSupport<T>
Read from a secret backend.void
setSessionManager
(SessionManager sessionManager) Set theSessionManager
.Write to a Vault path.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.vault.core.VaultOperations
write
-
Constructor Details
-
VaultTemplate
Create a newVaultTemplate
with aVaultEndpoint
. This constructor does not use aClientAuthentication
mechanism. It is intended for usage with Vault Agent to inherit Vault Agent's authentication without using theauthentication token header
.- Parameters:
vaultEndpoint
- must not be null.- Since:
- 2.2.1
-
VaultTemplate
- Parameters:
vaultEndpoint
- must not be null.clientAuthentication
- must not be null.
-
VaultTemplate
public VaultTemplate(VaultEndpoint vaultEndpoint, ClientHttpRequestFactory clientHttpRequestFactory) Create a newVaultTemplate
with aVaultEndpoint
, andClientHttpRequestFactory
. This constructor does not use aClientAuthentication
mechanism. It is intended for usage with Vault Agent to inherit Vault Agent's authentication without using theauthentication token header
.- Parameters:
vaultEndpoint
- must not be null.clientHttpRequestFactory
- must not be null.- Since:
- 2.2.1
-
VaultTemplate
public VaultTemplate(VaultEndpoint vaultEndpoint, ClientHttpRequestFactory clientHttpRequestFactory, SessionManager sessionManager) - Parameters:
vaultEndpoint
- must not be null.clientHttpRequestFactory
- must not be null.sessionManager
- must not be null.
-
VaultTemplate
public VaultTemplate(VaultEndpointProvider endpointProvider, ClientHttpRequestFactory requestFactory) Create a newVaultTemplate
with aVaultEndpointProvider
,ClientHttpRequestFactory
andSessionManager
. This constructor does not use aClientAuthentication
mechanism. It is intended for usage with Vault Agent to inherit Vault Agent's authentication without using theauthentication token header
.- Parameters:
endpointProvider
- must not be null.requestFactory
- must not be null.- Since:
- 2.2.1
-
VaultTemplate
public VaultTemplate(VaultEndpointProvider endpointProvider, ClientHttpRequestFactory requestFactory, SessionManager sessionManager) Create a newVaultTemplate
with aVaultEndpointProvider
,ClientHttpRequestFactory
andSessionManager
.- Parameters:
endpointProvider
- must not be null.requestFactory
- must not be null.sessionManager
- must not be null.- Since:
- 1.1
-
VaultTemplate
Create a newVaultTemplate
through aRestTemplateBuilder
andSessionManager
. This constructor does not use aClientAuthentication
mechanism. It is intended for usage with Vault Agent to inherit Vault Agent's authentication without using theauthentication token header
.- Parameters:
restTemplateBuilder
- must not be null.- Since:
- 2.2.1
-
VaultTemplate
- Parameters:
restTemplateBuilder
- must not be null.sessionManager
- must not be null.- Since:
- 2.2
-
-
Method Details
-
doCreateRestTemplate
protected RestTemplate doCreateRestTemplate(VaultEndpointProvider endpointProvider, ClientHttpRequestFactory requestFactory) Create aRestTemplate
to be used byVaultTemplate
for Vault communication givenVaultEndpointProvider
andClientHttpRequestFactory
.VaultEndpointProvider
is used to contribute host and port details for relative URLs typically used by the Template API. Subclasses may override this method to customize theRestTemplate
.- Parameters:
endpointProvider
- must not be null.requestFactory
- must not be null.- Returns:
- the
RestTemplate
used for Vault communication. - Since:
- 2.1
-
doCreateSessionTemplate
protected RestTemplate doCreateSessionTemplate(VaultEndpointProvider endpointProvider, ClientHttpRequestFactory requestFactory) Create a session-boundRestTemplate
to be used byVaultTemplate
for Vault communication givenVaultEndpointProvider
andClientHttpRequestFactory
for calls that require an authenticated context.VaultEndpointProvider
is used to contribute host and port details for relative URLs typically used by the Template API. Subclasses may override this method to customize theRestTemplate
.- Parameters:
endpointProvider
- must not be null.requestFactory
- must not be null.- Returns:
- the
RestTemplate
used for Vault communication. - Since:
- 2.1
-
setSessionManager
Set theSessionManager
.- Parameters:
sessionManager
- must not be null.
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSet
in interfaceInitializingBean
-
destroy
- Specified by:
destroy
in interfaceDisposableBean
- Throws:
Exception
-
opsForKeyValue
public VaultKeyValueOperations opsForKeyValue(String path, VaultKeyValueOperationsSupport.KeyValueBackend apiVersion) Description copied from interface:VaultOperations
ReturnVaultKeyValueOperations
.- Specified by:
opsForKeyValue
in interfaceVaultOperations
- Parameters:
path
- the mount path, must not be empty or null.apiVersion
- API version to use, must not be null.- Returns:
- the operations interface to interact with the Vault Key/Value backend.
-
opsForVersionedKeyValue
Description copied from interface:VaultOperations
ReturnVaultVersionedKeyValueOperations
.- Specified by:
opsForVersionedKeyValue
in interfaceVaultOperations
- Parameters:
path
- the mount path- Returns:
- the operations interface to interact with the versioned Vault Key/Value (version 2) backend.
-
opsForPki
- Specified by:
opsForPki
in interfaceVaultOperations
- Returns:
- the operations interface to interact with the Vault PKI backend.
-
opsForPki
Description copied from interface:VaultOperations
ReturnVaultPkiOperations
if the PKI backend is mounted on a different path thanpki
.- Specified by:
opsForPki
in interfaceVaultOperations
- Parameters:
path
- the mount path- Returns:
- the operations interface to interact with the Vault PKI backend.
-
opsForSys
- Specified by:
opsForSys
in interfaceVaultOperations
- Returns:
- the operations interface administrative Vault access.
-
opsForToken
- Specified by:
opsForToken
in interfaceVaultOperations
- Returns:
- the operations interface to interact with Vault token.
-
opsForTransform
- Specified by:
opsForTransform
in interfaceVaultOperations
- Returns:
- the operations interface to interact with the Vault transform backend.
-
opsForTransform
Description copied from interface:VaultOperations
ReturnVaultTransformOperations
if the transit backend is mounted on a different path thantransform
.- Specified by:
opsForTransform
in interfaceVaultOperations
- Parameters:
path
- the mount path- Returns:
- the operations interface to interact with the Vault transform backend.
-
opsForTransit
- Specified by:
opsForTransit
in interfaceVaultOperations
- Returns:
- the operations interface to interact with the Vault transit backend.
-
opsForTransit
Description copied from interface:VaultOperations
ReturnVaultTransitOperations
if the transit backend is mounted on a different path thantransit
.- Specified by:
opsForTransit
in interfaceVaultOperations
- Parameters:
path
- the mount path- Returns:
- the operations interface to interact with the Vault transit backend.
-
opsForWrapping
- Specified by:
opsForWrapping
in interfaceVaultOperations
- Returns:
- the operations interface to interact with the Vault system/wrapping endpoints.
-
read
Description copied from interface:VaultOperations
Read from a Vault path. Reading data using this method is suitable for API calls/secret backends that do not require a request body.- Specified by:
read
in interfaceVaultOperations
- Parameters:
path
- must not be null.- Returns:
- the data. May be null if the path does not exist.
-
read
Description copied from interface:VaultOperations
Read from a secret backend. Reading data using this method is suitable for secret backends that do not require a request body.- Specified by:
read
in interfaceVaultOperations
- Parameters:
path
- must not be null.responseType
- must not be null.- Returns:
- the data. May be null if the path does not exist.
-
list
Description copied from interface:VaultOperations
Enumerate keys from a Vault path.- Specified by:
list
in interfaceVaultOperations
- Parameters:
path
- must not be null.- Returns:
- the data. May be null if the path does not exist.
-
write
Description copied from interface:VaultOperations
Write to a Vault path.- Specified by:
write
in interfaceVaultOperations
- Parameters:
path
- must not be null.body
- the body, may be null if absent.- Returns:
- the response, may be null.
-
delete
Description copied from interface:VaultOperations
Delete a path.- Specified by:
delete
in interfaceVaultOperations
- Parameters:
path
- must not be null.
-
doWithVault
Description copied from interface:VaultOperations
Executes a VaultRestOperationsCallback
. Allows to interact with Vault usingRestOperations
without requiring a session.- Specified by:
doWithVault
in interfaceVaultOperations
- Parameters:
clientCallback
- the request.- Returns:
- the
RestOperationsCallback
return value.
-
doWithSession
Description copied from interface:VaultOperations
Executes a VaultRestOperationsCallback
. Allows to interact with Vault in an authenticated session.- Specified by:
doWithSession
in interfaceVaultOperations
- Parameters:
sessionCallback
- the request.- Returns:
- the
RestOperationsCallback
return value.
-