Skip to content

Commit

Permalink
Merge pull request wildfly-extras#42 from hsvabek/my_elytron
Browse files Browse the repository at this point in the history
refactor: CredentialRef contains CredentialRefBuilder
  • Loading branch information
Hynek Svabek authored Nov 14, 2016
2 parents e79451c + a60ac5c commit 616bc36
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,46 +27,65 @@

import org.wildfly.extras.creaper.core.online.operations.Values;

public final class CredentialRefBuilder {
public final class CredentialRef {
private String alias;
private String type;
private String store;
private String clearText;

public CredentialRefBuilder alias(String alias) {
public CredentialRef(CredentialRefBuilder builder) {
this.alias = builder.alias;
this.type = builder.type;
this.store = builder.store;
this.clearText = builder.clearText;
}

public String getAlias() {
return alias;
}

public void setAlias(String alias) {
this.alias = alias;
return this;
}

public CredentialRefBuilder type(String type) {
public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
return this;
}

public CredentialRefBuilder store(String store) {
public String getStore() {
return store;
}

public void setStore(String store) {
this.store = store;
return this;
}

public CredentialRefBuilder clearText(String clearText) {
public String getClearText() {
return clearText;
}

public void setClearText(String clearText) {
this.clearText = clearText;
return this;
}

public Values build() {
public Values toValues() {
final Map<String, String> credentialReference = new HashMap<String, String>();

if (isNotBlank(alias)) {
credentialReference.put("alias", alias);
credentialReference.put("alias", getAlias());
}
if (isNotBlank(type)) {
credentialReference.put("type", type);
credentialReference.put("type", getType());
}
if (isNotBlank(store)) {
credentialReference.put("store", store);
credentialReference.put("store", getStore());
}
if (isNotBlank(clearText)) {
credentialReference.put("clear-text", clearText);
credentialReference.put("clear-text", getClearText());
}

return Values.fromMap(credentialReference);
Expand All @@ -75,4 +94,35 @@ public Values build() {
private static boolean isNotBlank(String str) {
return (str != null && str.length() > 0);
}

public static final class CredentialRefBuilder {
private String alias;
private String type;
private String store;
private String clearText;

public CredentialRefBuilder alias(String alias) {
this.alias = alias;
return this;
}

public CredentialRefBuilder type(String type) {
this.type = type;
return this;
}

public CredentialRefBuilder store(String store) {
this.store = store;
return this;
}

public CredentialRefBuilder clearText(String clearText) {
this.clearText = clearText;
return this;
}

public CredentialRef build() {
return new CredentialRef(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.wildfly.extras.creaper.commands.elytron.tls;

import org.wildfly.extras.creaper.commands.elytron.CredentialRef;
import org.wildfly.extras.creaper.core.online.OnlineCommand;
import org.wildfly.extras.creaper.core.online.OnlineCommandContext;
import org.wildfly.extras.creaper.core.online.operations.Address;
Expand All @@ -17,7 +18,7 @@ public final class AddKeyManager implements OnlineCommand {
private final String name;
private final String algorithm;
private final String keyStore;
private final Values credentialReference;
private final CredentialRef credentialReference;
private final String provider;
private final String providerLoader;
private final boolean replaceExisting;
Expand Down Expand Up @@ -46,7 +47,7 @@ public void apply(OnlineCommandContext ctx) throws Exception {
.and("name", name)
.and("algorithm", algorithm)
.and("key-store", keyStore)
.andObject("credential-reference", credentialReference)
.andObject("credential-reference", credentialReference.toValues())
.andOptional("provider", provider)
.andOptional("provider-loader", providerLoader));
}
Expand All @@ -56,7 +57,7 @@ public static final class Builder {
private final String name;
private String algorithm;
private String keyStore;
private Values credentialReference;
private CredentialRef credentialReference;
private String provider;
private String providerLoader;
private boolean replaceExisting;
Expand Down Expand Up @@ -88,7 +89,7 @@ public Builder providerLoader(String providerLoader) {
return this;
}

public Builder credentialReference(Values credentialReference) {
public Builder credentialReference(CredentialRef credentialReference) {
this.credentialReference = credentialReference;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.wildfly.extras.creaper.commands.elytron.tls;

import org.wildfly.extras.creaper.commands.elytron.CredentialRef;
import org.wildfly.extras.creaper.core.online.OnlineCommand;
import org.wildfly.extras.creaper.core.online.OnlineCommandContext;
import org.wildfly.extras.creaper.core.online.operations.Address;
Expand All @@ -18,7 +19,7 @@ public final class AddKeyStore implements OnlineCommand {
private final String type;
private final String provider;
private final String providerLoader;
private final Values credentialReference;
private final CredentialRef credentialReference;
private final String aliasFilter;
private final String path;
private final String relativeTo;
Expand Down Expand Up @@ -52,7 +53,7 @@ public void apply(OnlineCommandContext ctx) throws Exception {
ops.add(keyStoreAddress, Values.empty()
.and("name", name)
.and("type", type)
.andObject("credential-reference", credentialReference)
.andObject("credential-reference", credentialReference.toValues())
.andOptional("provider", provider)
.andOptional("provider-loader", providerLoader)
.andOptional("alias-filter", aliasFilter)
Expand All @@ -67,7 +68,7 @@ public static final class Builder {
private String type;
private String provider;
private String providerLoader;
private Values credentialReference;
private CredentialRef credentialReference;
private String aliasFilter;
private String path;
private String relativeTo;
Expand Down Expand Up @@ -96,7 +97,7 @@ public Builder providerLoader(String providerLoader) {
return this;
}

public Builder credentialReference(Values credentialReference) {
public Builder credentialReference(CredentialRef credentialReference) {
this.credentialReference = credentialReference;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.extras.creaper.commands.elytron.AbstractElytronOnlineTest;
import org.wildfly.extras.creaper.commands.elytron.CredentialRefBuilder;
import org.wildfly.extras.creaper.commands.elytron.CredentialRef;
import org.wildfly.extras.creaper.commands.elytron.tls.AddKeyStore;
import org.wildfly.extras.creaper.core.CommandFailedException;
import org.wildfly.extras.creaper.core.online.operations.Address;
import org.wildfly.extras.creaper.core.online.operations.Values;


@RunWith(Arquillian.class)
Expand Down Expand Up @@ -121,13 +120,11 @@ private void addDefaultKeyStore() throws CommandFailedException {
}

private void addKeyStore(String keyStoreName) throws CommandFailedException {
Values credRef = new CredentialRefBuilder()
.clearText("test-Password")
.build();

AddKeyStore addKeyStore = new AddKeyStore.Builder(keyStoreName)
.type(TEST_KEY_STORE_TYPE)
.credentialReference(credRef)
.credentialReference(new CredentialRef.CredentialRefBuilder()
.clearText("test-Password")
.build())
.build();
client.apply(addKeyStore);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.wildfly.extras.creaper.commands.elytron.AbstractElytronOnlineTest;
import org.wildfly.extras.creaper.commands.elytron.CredentialRefBuilder;
import org.wildfly.extras.creaper.commands.elytron.CredentialRef;
import org.wildfly.extras.creaper.core.online.OnlineManagementClient;
import org.wildfly.extras.creaper.core.online.operations.Address;
import org.wildfly.extras.creaper.core.online.operations.Operations;
Expand Down Expand Up @@ -40,28 +40,28 @@ public static void addDependentResources() throws Exception {
try (OnlineManagementClient client = createManagementClient()) {
AddKeyStore addKeyStore = new AddKeyStore.Builder(TEST_KEY_STORE_NAME)
.type(TEST_KEY_STORE_TYPE)
.credentialReference(new CredentialRefBuilder()
.credentialReference(new CredentialRef.CredentialRefBuilder()
.clearText(TEST_KEY_STORE_PASSWORD)
.build())
.build();
AddKeyStore addKeyStore2 = new AddKeyStore.Builder(TEST_KEY_STORE_NAME2)
.type(TEST_KEY_STORE_TYPE)
.credentialReference(new CredentialRefBuilder()
.credentialReference(new CredentialRef.CredentialRefBuilder()
.clearText(TEST_KEY_STORE_PASSWORD)
.build())
.build();

AddKeyManager addKeyManager = new AddKeyManager.Builder(TEST_KEY_MNGR_NAME)
.algorithm(TEST_KEY_MANAGER_ALGORITHM)
.keyStore(TEST_KEY_STORE_NAME)
.credentialReference(new CredentialRefBuilder()
.credentialReference(new CredentialRef.CredentialRefBuilder()
.clearText(TEST_KEY_PASSWORD)
.build())
.build();
AddKeyManager addKeyManager2 = new AddKeyManager.Builder(TEST_KEY_MNGR_NAME2)
.algorithm(TEST_KEY_MANAGER_ALGORITHM)
.keyStore(TEST_KEY_STORE_NAME2)
.credentialReference(new CredentialRefBuilder()
.credentialReference(new CredentialRef.CredentialRefBuilder()
.clearText(TEST_KEY_PASSWORD)
.build())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.extras.creaper.commands.elytron.AbstractElytronOnlineTest;
import org.wildfly.extras.creaper.commands.elytron.CredentialRefBuilder;
import org.wildfly.extras.creaper.commands.elytron.CredentialRef;
import org.wildfly.extras.creaper.core.CommandFailedException;
import org.wildfly.extras.creaper.core.online.OnlineManagementClient;
import org.wildfly.extras.creaper.core.online.operations.Address;
Expand Down Expand Up @@ -40,7 +40,7 @@ public static void addKeyStores() throws Exception {
try (OnlineManagementClient client = createManagementClient()) {
AddKeyStore addKeyStore = new AddKeyStore.Builder(KEY_STORE_NAME)
.type(KEY_STORE_TYPE)
.credentialReference(new CredentialRefBuilder()
.credentialReference(new CredentialRef.CredentialRefBuilder()
.clearText(TEST_KEY_STORE_PASSWORD)
.build())
.build();
Expand Down
Loading

0 comments on commit 616bc36

Please sign in to comment.