Cleanup transit keys after test run.

Remove transit keys before/after testrun to leave a clean state.

See gh-53.
This commit is contained in:
Mark Paluch
2017-02-18 19:25:08 +01:00
parent d3491dd712
commit 7101a38ace
2 changed files with 76 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,8 +16,10 @@
package org.springframework.vault.core;
import java.util.Collections;
import java.util.List;
import org.apache.commons.codec.binary.Base64;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -27,13 +29,15 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.vault.support.VaultMount;
import org.springframework.vault.support.VaultResponse;
import org.springframework.vault.support.VaultTransitKeyConfiguration;
import org.springframework.vault.util.IntegrationTestSupport;
import org.springframework.vault.util.Version;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link VaultTemplate} using the {@code transit} backend.
*
*
* @author Mark Paluch
*/
@RunWith(SpringRunner.class)
@@ -50,10 +54,47 @@ public class VaultTemplateTransitIntegrationTests extends IntegrationTestSupport
if (!adminOperations.getMounts().containsKey("transit/")) {
adminOperations.mount("transit", VaultMount.create("transit"));
}
vaultOperations.write("transit/keys/mykey", null);
vaultOperations.write("transit/keys/derived",
Collections.singletonMap("derived", true));
removeKeys();
vaultOperations.write("transit/keys/mykey", null);
vaultOperations.write("transit/keys/derived",
Collections.singletonMap("derived", true));
}
@After
public void tearDown() {
removeKeys();
}
private void deleteKey(String keyName) {
try {
vaultOperations.opsForTransit().configureKey(keyName,
VaultTransitKeyConfiguration.builder().deletionAllowed(true).build());
}
catch (Exception e) {
}
try {
vaultOperations.opsForTransit().deleteKey(keyName);
}
catch (Exception e) {
}
}
private void removeKeys() {
if (prepare().getVersion().isGreaterThanOrEqualTo(Version.parse("0.6.4"))) {
List<String> keys = vaultOperations.opsForTransit().getKeys();
for (String keyName : keys) {
deleteKey(keyName);
}
}
else {
deleteKey("mykey");
deleteKey("derived");
}
}

View File

@@ -15,6 +15,9 @@
*/
package org.springframework.vault.core;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -51,26 +54,51 @@ public class VaultTransitTemplateIntegrationTests extends IntegrationTestSupport
@Before
public void before() {
transitOperations = vaultOperations.opsForTransit();
if (!vaultOperations.opsForSys().getMounts().containsKey("transit/")) {
vaultOperations.opsForSys().mount("transit", VaultMount.create("transit"));
}
removeKeys();
}
@After
public void tearDown() {
removeKeys();
}
private void deleteKey(String keyName) {
try {
transitOperations.configureKey("mykey", VaultTransitKeyConfiguration
transitOperations.configureKey(keyName, VaultTransitKeyConfiguration
.builder().deletionAllowed(true).build());
}
catch (Exception e) {
}
try {
transitOperations.deleteKey("mykey");
transitOperations.deleteKey(keyName);
}
catch (Exception e) {
}
}
private void removeKeys() {
if (prepare().getVersion().isGreaterThanOrEqualTo(Version.parse("0.6.4"))) {
List<String> keys = vaultOperations.opsForTransit().getKeys();
for (String keyName : keys) {
deleteKey(keyName);
}
}
else {
deleteKey("mykey");
deleteKey("derived");
}
}
@Test
public void createKeyShouldCreateKey() {