22
33import io .github .jopenlibs .vault .Vault ;
44import io .github .jopenlibs .vault .VaultException ;
5+ import io .github .jopenlibs .vault .api .database .DatabaseRoleOptions ;
6+ import io .github .jopenlibs .vault .response .DatabaseResponse ;
57import io .github .jopenlibs .vault .response .VaultResponse ;
8+ import io .github .jopenlibs .vault .v1_11_4 .util .DbContainer ;
69import io .github .jopenlibs .vault .v1_11_4 .util .VaultContainer ;
710import java .io .IOException ;
11+ import java .util .ArrayList ;
12+ import java .util .Arrays ;
13+ import java .util .List ;
814import org .junit .Before ;
915import org .junit .BeforeClass ;
1016import org .junit .ClassRule ;
1117import org .junit .Test ;
1218
1319import static junit .framework .TestCase .assertEquals ;
20+ import static junit .framework .TestCase .assertTrue ;
1421
1522/**
16- * <p>Integration tests for the basic (i.e. "sys") Vault API operations.</p>
23+ * <p>Integration tests for the leases (i.e. "sys/leases ") Vault API operations.</p>
1724 *
18- * <p>Unfortunately, it's not really possible to fully test these endpoints with the current integration testing
19- * strategy. The "generic" backend, used by the dev server, does not issue leases at all. You CAN obtain leases
20- * by using the PKI backend, but those aren't renewable:</p>
21- *
22- * <p>https://github.com/hashicorp/vault/issues/877</p>
23- *
24- * <p>Therefore, these revocation tests are basically just testing that the Vault server returns a 204 status
25- * code. Which isn't much of a test, since Vault routinely returns 204 even if you pass a non-existent lease ID.</p>
26- *
27- * <p>In the future, we may be shifting to an integration testing approach that uses a "real" Vault server
28- * instance, running in a Docker container (see: https://github.com/BetterCloud/vault-java-driver/pull/25). At
29- * that time, these tests should be re-visited and better implemented.</p>
30- *
31- * TODO: Revisit, now that we're using testcontainers
25+ * According to the Vault documentation, it is possible to use a dynamic secret like database to
26+ * test these methods
3227 */
3328public class LeasesTests {
3429
30+ @ ClassRule
31+ public static final DbContainer dbContainer = new DbContainer ();
32+
3533 @ ClassRule
3634 public static final VaultContainer container = new VaultContainer ();
3735
@@ -40,29 +38,69 @@ public class LeasesTests {
4038 @ BeforeClass
4139 public static void setupClass () throws IOException , InterruptedException {
4240 container .initAndUnsealVault ();
41+ container .setupBackendDatabase (DbContainer .hostname );
4342 }
4443
4544 @ Before
4645 public void setup () throws VaultException {
4746 vault = container .getRootVault ();
4847 }
4948
49+ public DatabaseResponse generateCredentials () throws VaultException {
50+ List <String > creationStatements = new ArrayList <>();
51+ creationStatements .add (
52+ "CREATE USER \" {{name}}\" WITH PASSWORD '{{password}}';"
53+ + "GRANT SELECT ON ALL TABLES IN SCHEMA public TO \" {{name}}\" ;" );
54+
55+ DatabaseResponse databaseResponse = vault .database ().createOrUpdateRole ("new-role" ,
56+ new DatabaseRoleOptions ().dbName ("postgres" )
57+ .creationStatements (creationStatements ));
58+ assertEquals (204 , databaseResponse .getRestResponse ().getStatus ());
59+
60+ DatabaseResponse credsResponse = vault .database ().creds ("new-role" );
61+ assertEquals (200 , credsResponse .getRestResponse ().getStatus ());
62+
63+ assertTrue (credsResponse .getCredential ().getUsername ().contains ("new-role" ));
64+
65+ return credsResponse ;
66+ }
67+
5068 @ Test
5169 public void testRevoke () throws VaultException {
52- final VaultResponse response = vault .leases ().revoke ("sys/revoke-prefix/dummy" );
70+ DatabaseResponse credsResponse = this .generateCredentials ();
71+
72+ final VaultResponse response = vault .leases ().revoke (credsResponse .getLeaseId ());
5373 assertEquals (204 , response .getRestResponse ().getStatus ());
5474 }
5575
5676 @ Test
5777 public void testRevokePrefix () throws VaultException {
58- final VaultResponse response = vault .leases ().revokePrefix ("sys/revoke-prefix/dummy" );
78+ DatabaseResponse credsResponse = this .generateCredentials ();
79+
80+ String prefix = Arrays .stream (credsResponse .getLeaseId ().split ("([^/]+)$" ))
81+ .map (str -> str .substring (0 , str .length () - 1 )).findFirst ().get ();
82+
83+ final VaultResponse response = vault .leases ().revokePrefix (prefix );
5984 assertEquals (204 , response .getRestResponse ().getStatus ());
6085 }
6186
6287 @ Test
6388 public void testRevokeForce () throws VaultException {
64- final VaultResponse response = vault .leases ().revokeForce ("sys/revoke-prefix/dummy" );
89+ DatabaseResponse credsResponse = this .generateCredentials ();
90+
91+ String prefix = Arrays .stream (credsResponse .getLeaseId ().split ("([^/]+)$" ))
92+ .map (str -> str .substring (0 , str .length () - 1 )).findFirst ().get ();
93+
94+ final VaultResponse response = vault .leases ().revokeForce (prefix );
6595 assertEquals (204 , response .getRestResponse ().getStatus ());
6696 }
6797
98+ @ Test
99+ public void testRenew () throws VaultException {
100+ DatabaseResponse credsResponse = this .generateCredentials ();
101+
102+ final VaultResponse response = vault .leases ().renew (credsResponse .getLeaseId (),
103+ credsResponse .getLeaseDuration ());
104+ assertEquals (200 , response .getRestResponse ().getStatus ());
105+ }
68106}
0 commit comments