Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 111 additions & 1 deletion src/main/java/io/github/jopenlibs/vault/api/database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.github.jopenlibs.vault.json.Json;
import io.github.jopenlibs.vault.json.JsonObject;
import io.github.jopenlibs.vault.response.DatabaseResponse;
import io.github.jopenlibs.vault.response.DatabaseStaticResponse;
import io.github.jopenlibs.vault.rest.RestResponse;
import java.nio.charset.StandardCharsets;
import java.util.List;
Expand Down Expand Up @@ -125,6 +126,31 @@ public DatabaseResponse createOrUpdateRole(final String roleName,
});
}

public DatabaseStaticResponse createOrUpdateStaticRole(final String roleName,
final DatabaseStaticRoleOptions options) throws VaultException {
return retry(attempt -> {
final String requestJson = staticRoleOptionsToJson(options);

final RestResponse restResponse = getRest()//NOPMD
.url(String.format("%s/v1/%s/static-roles/%s", config.getAddress(), this.mountPath,
roleName))
.token(config.getToken())
.header("X-Vault-Namespace", this.nameSpace)
.header("X-Vault-Request", "true")
.body(requestJson.getBytes(StandardCharsets.UTF_8))
.readTimeoutSeconds(config.getReadTimeout())
.post();

// Validate restResponse
if (restResponse.getStatus() != 204) {
throw new VaultException(
"Vault responded with HTTP status code: " + restResponse.getStatus(),
restResponse.getStatus());
}
return new DatabaseStaticResponse(restResponse, attempt);
});
}

/**
* <p>Operation to retrieve an role using the Database backend. Relies on an authentication
* token being present in the <code>VaultConfig</code> instance.</p>
Expand Down Expand Up @@ -167,8 +193,29 @@ public DatabaseResponse getRole(final String roleName) throws VaultException {
});
}

public DatabaseStaticResponse getStaticRole(final String roleName) throws VaultException {
return retry(attempt -> {
final RestResponse restResponse = getRest()//NOPMD
.url(String.format("%s/v1/%s/static-roles/%s", config.getAddress(), this.mountPath,
roleName))
.token(config.getToken())
.header("X-Vault-Namespace", this.nameSpace)
.header("X-Vault-Request", "true")
.readTimeoutSeconds(config.getReadTimeout())
.get();

// Validate response
if (restResponse.getStatus() != 200 && restResponse.getStatus() != 404) {
throw new VaultException(
"Vault responded with HTTP status code: " + restResponse.getStatus(),
restResponse.getStatus());
}
return new DatabaseStaticResponse(restResponse, attempt);
});
}

/**
* <p>Operation to revike a certificate in the vault using the Database backend.
* <p>Operation to revoke a certificate in the vault using the Database backend.
* Relies on an authentication token being present in the <code>VaultConfig</code>
* instance.</p>
*
Expand Down Expand Up @@ -259,6 +306,27 @@ public DatabaseResponse deleteRole(final String roleName) throws VaultException
});
}

public DatabaseStaticResponse deleteStaticRole(final String roleName) throws VaultException {
return retry(attempt -> {
final RestResponse restResponse = getRest()//NOPMD
.url(String.format("%s/v1/%s/static-roles/%s", config.getAddress(), this.mountPath,
roleName))
.token(config.getToken())
.header("X-Vault-Namespace", this.nameSpace)
.header("X-Vault-Request", "true")
.readTimeoutSeconds(config.getReadTimeout())
.delete();

// Validate response
if (restResponse.getStatus() != 204) {
throw new VaultException(
"Vault responded with HTTP status code: " + restResponse.getStatus(),
restResponse.getStatus());
}
return new DatabaseStaticResponse(restResponse, attempt);
});
}

/**
* <p>Operation to generate a new set of credentials using the Database backend.
*
Expand Down Expand Up @@ -308,6 +376,32 @@ public DatabaseResponse creds(final String roleName) throws VaultException {
});
}

public DatabaseStaticResponse staticCreds(final String roleName) throws VaultException {
return retry(attempt -> {
final RestResponse restResponse = getRest()//NOPMD
.url(String.format("%s/v1/%s/static-creds/%s", config.getAddress(), this.mountPath,
roleName))
.token(config.getToken())
.header("X-Vault-Namespace", this.nameSpace)
.header("X-Vault-Request", "true")
.readTimeoutSeconds(config.getReadTimeout())
.get();

// Validate response
if (restResponse.getStatus() != 200 && restResponse.getStatus() != 404) {
String body =
restResponse.getBody() != null ? new String(restResponse.getBody(),
StandardCharsets.UTF_8)
: "(no body)";
throw new VaultException(
"Vault responded with HTTP status code: " + restResponse.getStatus()
+ " " + body, restResponse.getStatus());
}

return new DatabaseStaticResponse(restResponse, attempt);
});
}

private String roleOptionsToJson(final DatabaseRoleOptions options) {
final JsonObject jsonObject = Json.object();

Expand All @@ -328,6 +422,22 @@ private String roleOptionsToJson(final DatabaseRoleOptions options) {
return jsonObject.toString();
}

private String staticRoleOptionsToJson(final DatabaseStaticRoleOptions options) {
final JsonObject jsonObject = Json.object();

if (options != null) {
addJsonFieldIfNotNull(jsonObject, "db_name", options.getDbName());
addJsonFieldIfNotNull(jsonObject, "username", options.getUsername());
addJsonFieldIfNotNull(jsonObject, "credential_type", options.getCredentialType());
addJsonFieldIfNotNull(jsonObject, "rotation_period", options.getRotationPeriod());
addJsonFieldIfNotNull(jsonObject, "skip_import_rotation", options.getSkipImportRotation());
addJsonFieldIfNotNull(jsonObject, "rotation_statements",
joinList(options.getRotationStatements()));
}

return jsonObject.toString();
}

private String joinList(List<String> data) {
String result = "";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.github.jopenlibs.vault.api.database;

public class DatabaseStaticCredential extends DatabaseCredential {
private String lastVaultRotation;
private String ttl;
private String rotationPeriod;

/**
* @return Returns the last vault rotation time associated with the retrieved Credential
*/
public String getLastVaultRotation() {
return lastVaultRotation;
}

/**
* @return Returns the TTL associated with the retrieved Credential
*/
public String getTtl() {
return ttl;
}

/**
* @return Returns the rotation period associated with the retrieved Credential
*/
public String getRotationPeriod() {
return rotationPeriod;
}

public DatabaseStaticCredential username(String username) {
super.username(username);
return this;
}

public DatabaseStaticCredential password(String password) {
super.password(password);
return this;
}

public DatabaseStaticCredential rotationPeriod(String period) {
this.rotationPeriod = period;
return this;
}

public DatabaseStaticCredential ttl(String ttl) {
this.ttl = ttl;
return this;
}

public DatabaseStaticCredential lastVaultRotation(String rotation) {
this.lastVaultRotation = rotation;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package io.github.jopenlibs.vault.api.database;

import java.util.ArrayList;
import java.util.List;

public class DatabaseStaticRoleOptions {

private String name;
private String dbName;
private List<String> rotationStatements = new ArrayList<>();
private String username;
private String credentialType;
private String lastVaultRotation;
private String rotationPeriod;
private Boolean skipImportRotation;

public String getName() {
return name;
}

public String getDbName() {
return dbName;
}

public String getCredentialType() {
return credentialType;
}

public String getLastVaultRotation() {
return lastVaultRotation;
}

public String getRotationPeriod() {
return rotationPeriod;
}

public Boolean getSkipImportRotation() {
return skipImportRotation;
}

public String getUsername() {
return username;
}

public List<String> getRotationStatements() {
return rotationStatements;
}

/**
* @param name {@code String} – Specifies the name of the role to create. This is specified as
* part of the URL.
* @return This object, with name populated, ready for other builder methods or immediate use.
*/
public DatabaseStaticRoleOptions name(final String name) {
this.name = name;
return this;
}

/**
* @param dbName {@code String} - The name of the database connection to use for this role.
* @return This object, with dbName populated, ready for other builder methods or immediate use.
*/
public DatabaseStaticRoleOptions dbName(final String dbName) {
this.dbName = dbName;
return this;
}

/**
* @param
* @return This object, with username populated, ready for other builder methods or immediate
* use.
*/
public DatabaseStaticRoleOptions username(final String username) {
this.username = username;
return this;
}

/**
* @param
* @return This object, with skipImportRotation populated, ready for other builder methods or immediate
* use.
*/
public DatabaseStaticRoleOptions skipImportRotation(final Boolean skipImportRotation) {
this.skipImportRotation = skipImportRotation;
return this;
}

/**
* @param
* @return This object, with rotationPeriod populated, ready for other builder methods or immediate
* use.
*/
public DatabaseStaticRoleOptions rotationPeriod(final String rotationPeriod) {
this.rotationPeriod = rotationPeriod;
return this;
}

/**
* @param
* @return This object, with lastVaultRotation populated, ready for other builder methods or immediate
* use.
*/
public DatabaseStaticRoleOptions lastVaultRotation(final String lastVaultRotation) {
this.lastVaultRotation = lastVaultRotation;
return this;
}

/**
* @param
* @return This object, with credentialType populated, ready for other builder methods or immediate
* use.
*/
public DatabaseStaticRoleOptions credentialType(final String credentialType) {
this.credentialType = credentialType;
return this;
}

/**
* @param creationStatements {@code List<String>} – Specifies the database statements executed
* to rotate the password for a user. See the plugin's API page for more information on support and
* formatting for this parameter.
* @return This object, with rotationStatements populated, ready for other builder methods or
* immediate use.
*/
public DatabaseStaticRoleOptions rotationStatements(final List<String> rotationStatements) {
this.rotationStatements = rotationStatements;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ private DatabaseCredential buildCredentialFromData(final Map<String, String> dat
}

private JsonArray safeGetJsonArray(JsonObject source, String key) {
if (source == null || source.get(key) == null || source.get(key) == null || !source.get(key)
.isArray()) {
if (source == null || source.get(key) == null || !source.get(key).isArray()) {
return new JsonArray();
}

Expand Down
Loading
Loading