Search in sources :

Example 1 with VaultOperations

use of org.springframework.vault.core.VaultOperations in project spring-vault by spring-projects.

the class KeyValueV2 method versionedApi.

void versionedApi() {
    // tag::versionedApi[]
    VaultOperations operations = new VaultTemplate(new VaultEndpoint());
    VaultVersionedKeyValueOperations versionedOperations = operations.opsForVersionedKeyValue("secret");
    Versioned.Metadata metadata = // <1>
    versionedOperations.put(// <1>
    "elvis", Collections.singletonMap("social-security-number", "409-52-2002"));
    // <2>
    Version version = metadata.getVersion();
    // <3>
    Versioned<Object> ssn = versionedOperations.get("elvis", Version.from(42));
    Versioned<SocialSecurityNumber> mappedSsn = // <4>
    versionedOperations.get(// <4>
    "elvis", Version.from(42), SocialSecurityNumber.class);
    Versioned<Map<String, String>> versioned = Versioned.create(// <5>
    Collections.singletonMap("social-security-number", "409-52-2002"), Version.from(42));
    versionedOperations.put("elvis", version);
// end::versionedApi[]
}
Also used : VaultTemplate(org.springframework.vault.core.VaultTemplate) Versioned(org.springframework.vault.support.Versioned) Version(org.springframework.vault.support.Versioned.Version) VaultVersionedKeyValueOperations(org.springframework.vault.core.VaultVersionedKeyValueOperations) VaultOperations(org.springframework.vault.core.VaultOperations) Map(java.util.Map) VaultEndpoint(org.springframework.vault.client.VaultEndpoint)

Example 2 with VaultOperations

use of org.springframework.vault.core.VaultOperations in project spring-vault by spring-projects.

the class KeyValueV2 method vaultOperations.

void vaultOperations() {
    // tag::vaultOperations[]
    VaultOperations operations = new VaultTemplate(new VaultEndpoint());
    operations.write("secret/data/elvis", Collections.singletonMap("data", Collections.singletonMap("social-security-number", "409-52-2002")));
    VaultResponse read = operations.read("secret/data/ykey");
    Map<String, String> data = (Map<String, String>) read.getRequiredData().get("data");
    data.get("social-security-number");
// end::vaultOperations[]
}
Also used : VaultTemplate(org.springframework.vault.core.VaultTemplate) VaultResponse(org.springframework.vault.support.VaultResponse) VaultOperations(org.springframework.vault.core.VaultOperations) Map(java.util.Map) VaultEndpoint(org.springframework.vault.client.VaultEndpoint)

Example 3 with VaultOperations

use of org.springframework.vault.core.VaultOperations in project spring-vault by spring-projects.

the class KeyValueV2 method keyValueApi.

void keyValueApi() {
    // tag::keyValueApi[]
    VaultOperations operations = new VaultTemplate(new VaultEndpoint());
    VaultKeyValueOperations keyValueOperations = operations.opsForKeyValue("secret", VaultKeyValueOperationsSupport.KeyValueBackend.KV_2);
    keyValueOperations.put("elvis", Collections.singletonMap("social-security-number", "409-52-2002"));
    VaultResponse read = keyValueOperations.get("elvis");
    read.getRequiredData().get("social-security-number");
// end::keyValueApi[]
}
Also used : VaultTemplate(org.springframework.vault.core.VaultTemplate) VaultResponse(org.springframework.vault.support.VaultResponse) VaultOperations(org.springframework.vault.core.VaultOperations) VaultEndpoint(org.springframework.vault.client.VaultEndpoint) VaultKeyValueOperations(org.springframework.vault.core.VaultKeyValueOperations)

Example 4 with VaultOperations

use of org.springframework.vault.core.VaultOperations in project spring-vault by spring-projects.

the class PKI method pkiApi.

void pkiApi() {
    // tag::pkiApi[]
    VaultOperations operations = new VaultTemplate(new VaultEndpoint());
    VaultPkiOperations pkiOperations = operations.opsForPki("pki");
    VaultCertificateRequest request = // <1>
    VaultCertificateRequest.builder().ttl(Duration.ofHours(48)).altNames(Arrays.asList("prod.dc-1.example.com", "prod.dc-2.example.com")).withIpSubjectAltName("1.2.3.4").commonName("hello.example.com").build();
    // <2>
    VaultCertificateResponse response = pkiOperations.issueCertificate("production", request);
    CertificateBundle certificateBundle = response.getRequiredData();
    // <3>
    KeyStore keyStore = certificateBundle.createKeyStore("my-keystore");
    // <4>
    KeySpec privateKey = certificateBundle.getPrivateKeySpec();
    X509Certificate certificate = certificateBundle.getX509Certificate();
    X509Certificate caCertificate = certificateBundle.getX509IssuerCertificate();
    // <5>
    pkiOperations.revoke(certificateBundle.getSerialNumber());
// end::pkiApi[]
}
Also used : VaultTemplate(org.springframework.vault.core.VaultTemplate) VaultCertificateRequest(org.springframework.vault.support.VaultCertificateRequest) VaultPkiOperations(org.springframework.vault.core.VaultPkiOperations) CertificateBundle(org.springframework.vault.support.CertificateBundle) KeySpec(java.security.spec.KeySpec) VaultOperations(org.springframework.vault.core.VaultOperations) VaultCertificateResponse(org.springframework.vault.support.VaultCertificateResponse) KeyStore(java.security.KeyStore) VaultEndpoint(org.springframework.vault.client.VaultEndpoint) X509Certificate(java.security.cert.X509Certificate)

Example 5 with VaultOperations

use of org.springframework.vault.core.VaultOperations in project spring-vault by spring-projects.

the class Token method tokenApi.

void tokenApi() {
    // tag::tokenApi[]
    VaultOperations operations = new VaultTemplate(new VaultEndpoint());
    VaultTokenOperations tokenOperations = operations.opsForToken();
    // <1>
    VaultTokenResponse tokenResponse = tokenOperations.create();
    VaultToken justAToken = tokenResponse.getToken();
    VaultTokenRequest tokenRequest = VaultTokenRequest.builder().withPolicy("policy-for-myapp").displayName("Access tokens for myapp").renewable().ttl(Duration.ofHours(1)).build();
    // <2>
    VaultTokenResponse appTokenResponse = tokenOperations.create(tokenRequest);
    VaultToken appToken = appTokenResponse.getToken();
    // <3>
    tokenOperations.renew(appToken);
    // <4>
    tokenOperations.revoke(appToken);
// end::tokenApi[]
}
Also used : VaultTemplate(org.springframework.vault.core.VaultTemplate) VaultTokenResponse(org.springframework.vault.support.VaultTokenResponse) VaultTokenRequest(org.springframework.vault.support.VaultTokenRequest) VaultTokenOperations(org.springframework.vault.core.VaultTokenOperations) VaultOperations(org.springframework.vault.core.VaultOperations) VaultToken(org.springframework.vault.support.VaultToken) VaultEndpoint(org.springframework.vault.client.VaultEndpoint)

Aggregations

VaultOperations (org.springframework.vault.core.VaultOperations)49 HashMap (java.util.HashMap)21 BeforeClass (org.junit.BeforeClass)19 VaultRule (org.springframework.cloud.vault.util.VaultRule)19 InetSocketAddress (java.net.InetSocketAddress)16 Before (org.junit.Before)10 VaultEndpoint (org.springframework.vault.client.VaultEndpoint)10 VaultTemplate (org.springframework.vault.core.VaultTemplate)10 VaultConfigTemplate (org.springframework.cloud.vault.config.VaultConfigTemplate)9 VaultResponse (org.springframework.vault.support.VaultResponse)5 BeforeAll (org.junit.jupiter.api.BeforeAll)4 File (java.io.File)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 Test (org.junit.Test)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 NoSuchElementException (java.util.NoSuchElementException)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 HttpHeaders (org.springframework.http.HttpHeaders)2