Skip to content

Commit

Permalink
Ask for DB password when creating OCI Vault
Browse files Browse the repository at this point in the history
  • Loading branch information
jhorvath committed Oct 31, 2023
1 parent fb08355 commit e18f55e
Showing 1 changed file with 59 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@
"Cancel=Cancel",
"SecretExists=Secrets with name {0} already exists",
"NoProfile=There is not any OCI profile in the config",
"NoCompartment=There are no compartments in the Tenancy"
"NoCompartment=There are no compartments in the Tenancy",
"Password=Enter password for Database user {0}"
})
public class AddDbConnectionToVault implements ActionListener {

Expand Down Expand Up @@ -209,7 +210,7 @@ public boolean onlyOneChoice() {
}
}

static class CompartmentStep implements Step<TenancyItem, CompartmentItem> {
class CompartmentStep implements Step<TenancyItem, CompartmentItem> {

private Map<String, OCIItem> compartments = null;
private CompartmentItem selected;
Expand Down Expand Up @@ -261,7 +262,7 @@ public boolean onlyOneChoice() {
}
}

static class VaultStep implements Step<CompartmentItem, VaultItem> {
class VaultStep implements Step<CompartmentItem, VaultItem> {

private Map<String, VaultItem> vaults = null;
private VaultItem selected;
Expand Down Expand Up @@ -307,7 +308,7 @@ public boolean onlyOneChoice() {
}
}

static class KeyStep implements Step<VaultItem, Pair<VaultItem, KeyItem>> {
class KeyStep implements Step<VaultItem, Pair<VaultItem, KeyItem>> {

private Map<String, KeyItem> keys = null;
private KeyItem selected;
Expand Down Expand Up @@ -363,7 +364,7 @@ public Pair<VaultItem, KeyItem> getValue() {

}

static class DatasourceNameStep implements Step<Pair<VaultItem, KeyItem>, Result> {
class DatasourceNameStep implements Step<Pair<VaultItem, KeyItem>, Result> {

private Result result = new Result();

Expand Down Expand Up @@ -401,7 +402,7 @@ public boolean onlyOneChoice() {

}

static class OverwriteStep implements Step<Result, Result> {
class OverwriteStep implements Step<Result, Result> {

private Result result;
private Set<String> dsNames;
Expand All @@ -428,7 +429,7 @@ public NotifyDescriptor createInput() {

@Override
public Step getNext() {
return null;
return new PasswordStep().prepare(result);
}

@Override
Expand All @@ -451,11 +452,51 @@ public boolean onlyOneChoice() {
}

}

class PasswordStep implements Step<Result, Result> {

private Result item;
private boolean ask;

@Override
public Step<Result, Result> prepare(Result item) {
item.password = context.getPassword();
ask = item.password == null || item.password.isEmpty();
this.item = item;
return this;
}

@Override
public NotifyDescriptor createInput() {
return new NotifyDescriptor.InputLine("DEFAULT", Bundle.Password(context.getUser())); //NOI18N
}

@Override
public boolean onlyOneChoice() {
return !ask;
}

@Override
public Step getNext() {
return null;
}

@Override
public void setValue(String password) {
item.password = password;
}

@Override
public Result getValue() {
return item;
}
}

static class Result {
VaultItem vault;
KeyItem key;
String datasourceName;
String password;
private boolean update;
}

Expand Down Expand Up @@ -523,25 +564,23 @@ Object getResult() {

@Override
public void actionPerformed(ActionEvent e) {

Multistep multistep = new Multistep(new TenancyStep());

NotifyDescriptor.ComposedInput ci = new NotifyDescriptor.ComposedInput(Bundle.AddADB(), 3, multistep.createInput());
if (DialogDescriptor.OK_OPTION == DialogDisplayer.getDefault().notify(ci)) {
if (multistep.getResult() != null) {
Result v = (Result) multistep.getResult();
addDbConnectionToVault(v.vault, v.key, context, v.datasourceName);
addDbConnectionToVault((Result) multistep.getResult());
}
}

}

private static void addDbConnectionToVault(VaultItem vault, KeyItem key, DatabaseConnection connection, String datasourceName) {
private void addDbConnectionToVault(Result item) {
VaultsClient client = VaultsClient.builder().build(getDefault().getActiveProfile().getConfigProvider());

ListSecretsRequest listSecretsRequest = ListSecretsRequest.builder()
.compartmentId(vault.getCompartmentId())
.vaultId(vault.getKey().getValue())
.compartmentId(item.vault.getCompartmentId())
.vaultId(item.vault.getKey().getValue())
.limit(88)
.build();

Expand All @@ -552,16 +591,16 @@ private static void addDbConnectionToVault(VaultItem vault, KeyItem key, Databas

Map<String, String> values = new HashMap<String, String>() {
{
put("Username", connection.getUser()); //NOI18N
put("Password", connection.getPassword()); //NOI18N
put("OCID", (String) connection.getConnectionProperties().get("OCID")); //NOI18N
put("Username", context.getUser()); //NOI18N
put("Password", item.password); //NOI18N
put("OCID", (String) context.getConnectionProperties().get("OCID")); //NOI18N
put("wallet_Password", UUID.randomUUID().toString()); //NOI18N
}
};

try {
for (Entry<String, String> entry : values.entrySet()) {
String secretName = "DATASOURCES_" + datasourceName + "_" + entry.getKey().toUpperCase(); //NOI18N
String secretName = "DATASOURCES_" + item.datasourceName + "_" + entry.getKey().toUpperCase(); //NOI18N
String base64Content = Base64.getEncoder().encodeToString(entry.getValue().getBytes(StandardCharsets.UTF_8));

SecretContentDetails contentDetails = Base64SecretContentDetails.builder()
Expand All @@ -582,9 +621,9 @@ private static void addDbConnectionToVault(VaultItem vault, KeyItem key, Databas
.secretContent(contentDetails)
.secretRules(new ArrayList<>(Arrays.asList(SecretReuseRule.builder()
.isEnforcedOnDeletedSecretVersions(false).build())))
.compartmentId(vault.getCompartmentId())
.vaultId(vault.getKey().getValue())
.keyId(key.getKey().getValue())
.compartmentId(item.vault.getCompartmentId())
.vaultId(item.vault.getKey().getValue())
.keyId(item.key.getKey().getValue())
.build();
CreateSecretRequest request = CreateSecretRequest
.builder()
Expand Down

0 comments on commit e18f55e

Please sign in to comment.