-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7873 from lordofthejars/issue-7865
Add support for Vault TOTP secrets
- Loading branch information
Showing
26 changed files
with
794 additions
and
5 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
extensions/vault/runtime/src/main/java/io/quarkus/vault/VaultTOTPSecretEngine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package io.quarkus.vault; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import io.quarkus.vault.secrets.totp.CreateKeyParameters; | ||
import io.quarkus.vault.secrets.totp.KeyConfiguration; | ||
import io.quarkus.vault.secrets.totp.KeyDefinition; | ||
|
||
/** | ||
* This service provides access to the TOTP secret engine. | ||
* | ||
* @see <a href="https://www.vaultproject.io/api/secret/totp/index.html">TOTP Secrets Engine </a> | ||
*/ | ||
public interface VaultTOTPSecretEngine { | ||
|
||
/** | ||
* Creates or updates a key definition. | ||
* | ||
* @param name of the key. | ||
* @param createKeyParameters required to create or update a key. | ||
* @return Barcode and/or URL of the created OTP key. | ||
*/ | ||
Optional<KeyDefinition> createKey(String name, CreateKeyParameters createKeyParameters); | ||
|
||
/** | ||
* Queries the key definition. | ||
* | ||
* @param name of the key. | ||
* @return The key configuration. | ||
*/ | ||
KeyConfiguration readKey(String name); | ||
|
||
/** | ||
* Returns a list of available keys. Only the key names are returned, not any values. | ||
* | ||
* @return List of available keys. | ||
*/ | ||
List<String> listKeys(); | ||
|
||
/** | ||
* Deletes the key definition. | ||
* | ||
* @param name of the key. | ||
*/ | ||
void deleteKey(String name); | ||
|
||
/** | ||
* Generates a new time-based one-time use password based on the named key. | ||
* | ||
* @param name of the key. | ||
* @return The Code. | ||
*/ | ||
String generateCode(String name); | ||
|
||
/** | ||
* Validates a time-based one-time use password generated from the named key. | ||
* | ||
* @param name of the key. | ||
* @param code to validate. | ||
* @return True if valid, false otherwise. | ||
*/ | ||
boolean validateCode(String name, String code); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
extensions/vault/runtime/src/main/java/io/quarkus/vault/runtime/VaultTOTPManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package io.quarkus.vault.runtime; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import io.quarkus.vault.VaultTOTPSecretEngine; | ||
import io.quarkus.vault.runtime.client.VaultClient; | ||
import io.quarkus.vault.runtime.client.VaultClientException; | ||
import io.quarkus.vault.runtime.client.dto.totp.VaultTOTPCreateKeyBody; | ||
import io.quarkus.vault.runtime.client.dto.totp.VaultTOTPCreateKeyResult; | ||
import io.quarkus.vault.runtime.client.dto.totp.VaultTOTPReadKeyResult; | ||
import io.quarkus.vault.secrets.totp.CreateKeyParameters; | ||
import io.quarkus.vault.secrets.totp.KeyConfiguration; | ||
import io.quarkus.vault.secrets.totp.KeyDefinition; | ||
|
||
public class VaultTOTPManager implements VaultTOTPSecretEngine { | ||
|
||
private VaultAuthManager vaultAuthManager; | ||
private VaultClient vaultClient; | ||
|
||
public VaultTOTPManager(VaultAuthManager vaultAuthManager, VaultClient vaultClient) { | ||
this.vaultAuthManager = vaultAuthManager; | ||
this.vaultClient = vaultClient; | ||
} | ||
|
||
@Override | ||
public Optional<KeyDefinition> createKey(String name, CreateKeyParameters createKeyParameters) { | ||
VaultTOTPCreateKeyBody body = new VaultTOTPCreateKeyBody(); | ||
|
||
body.accountName = createKeyParameters.getAccountName(); | ||
body.algorithm = createKeyParameters.getAlgorithm(); | ||
body.digits = createKeyParameters.getDigits(); | ||
body.exported = createKeyParameters.getExported(); | ||
body.generate = createKeyParameters.getGenerate(); | ||
body.issuer = createKeyParameters.getIssuer(); | ||
body.key = createKeyParameters.getKey(); | ||
body.keySize = createKeyParameters.getKeySize(); | ||
body.period = createKeyParameters.getPeriod(); | ||
body.qrSize = createKeyParameters.getQrSize(); | ||
body.skew = createKeyParameters.getSkew(); | ||
body.url = createKeyParameters.getUrl(); | ||
|
||
final VaultTOTPCreateKeyResult result = this.vaultClient | ||
.createTOTPKey(getToken(), name, body); | ||
|
||
return result == null ? Optional.empty() : Optional.of(new KeyDefinition(result.data.barcode, result.data.url)); | ||
} | ||
|
||
@Override | ||
public KeyConfiguration readKey(String name) { | ||
final VaultTOTPReadKeyResult result = this.vaultClient.readTOTPKey(getToken(), name); | ||
return new KeyConfiguration(result.data.accountName, | ||
result.data.algorithm, result.data.digits, | ||
result.data.issuer, result.data.period); | ||
} | ||
|
||
@Override | ||
public List<String> listKeys() { | ||
try { | ||
return this.vaultClient.listTOTPKeys(getToken()).data.keys; | ||
} catch (VaultClientException e) { | ||
if (e.getStatus() == 404) { | ||
return Collections.emptyList(); | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
@Override | ||
public void deleteKey(String name) { | ||
this.vaultClient.deleteTOTPKey(getToken(), name); | ||
} | ||
|
||
@Override | ||
public String generateCode(String name) { | ||
return this.vaultClient.generateTOTPCode(getToken(), name).data.code; | ||
} | ||
|
||
@Override | ||
public boolean validateCode(String name, String code) { | ||
return this.vaultClient.validateTOTPCode(getToken(), name, code).data.valid; | ||
} | ||
|
||
private String getToken() { | ||
return vaultAuthManager.getClientToken(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.