Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static void initializeClients() throws Exception {
isML11OrHigher = version.getMajor() >= 11;

client = newDatabaseClientBuilder().build();
schemasClient = newClientForDatabase("java-functest-schemas");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newClientForDatabase was only used in 2 places, which doesn't seem like justification enough for a helper method since it barely saves on any code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be useful to the users of the client?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newClientForDatabase was in src/test/java. I'm trying to slim down ConnectedRestQA as much as possible so it's easier to comprehend. So if there's a helper method only used in one or two places that doesn't help much at all, I'm removing those. No impact on Java Client users.

schemasClient = newDatabaseClientBuilder().withDatabase("java-functest-schemas").build();
adminModulesClient = newAdminModulesClient();

// Required to ensure that tests using the "/ext/" prefix work reliably. Expand to other directories as needed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void testAuthenticationBasic() throws IOException
String filename = "text-original.txt";

// connect the client
DatabaseClient client = newBasicAuthClient("rest-writer", "x");
DatabaseClient client = newDatabaseClientBuilder().withBasicAuth("rest-writer", "x").build();

// write doc
writeDocumentUsingStringHandle(client, filename, "/write-text-doc-basic/", "Text");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void testRuntimeDBclientWithDifferentAuthType() {
String originalServerAuthentication = getServerAuthentication(getRestServerName());
try {
setAuthentication("basic", getRestServerName());
client = newBasicAuthClient("eval-user", "x");
client = newDatabaseClientBuilder().withBasicAuth("eval-user", "x").build();
String insertJSON = "xdmp:document-insert(\"test2.json\",object-node {\"test\":\"hello\"})";
client.newServerEval().xquery(insertJSON).eval();
String query1 = "fn:count(fn:doc())";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2027,27 +2027,13 @@ public static DatabaseClientBuilder newDatabaseClientBuilder() {
return new DatabaseClientBuilder(props);
}

public static DatabaseClient newBasicAuthClient(String username, String password) {
return newDatabaseClientBuilder()
.withUsername(username)
.withPassword(password)
.withSecurityContextType("basic")
.build();
}

public static DatabaseClient newClientAsUser(String username, String password) {
return newDatabaseClientBuilder()
.withUsername(username)
.withPassword(password)
.build();
}

public static DatabaseClient newClientForDatabase(String database) {
return newDatabaseClientBuilder()
.withDatabase(database)
.build();
}

public static DatabaseClient newAdminModulesClient() {
return newDatabaseClientBuilder()
.withUsername(getAdminUser())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
public class DatabaseClientBuilder {

public final static String PREFIX = "marklogic.client.";
public final static String SECURITY_CONTEXT_TYPE_BASIC = "basic";
public final static String SECURITY_CONTEXT_TYPE_DIGEST = "digest";
public final static String SECURITY_CONTEXT_TYPE_MARKLOGIC_CLOUD = "cloud";
public final static String SECURITY_CONTEXT_TYPE_KERBEROS = "kerberos";
public final static String SECURITY_CONTEXT_TYPE_CERTIFICATE = "certificate";
public final static String SECURITY_CONTEXT_TYPE_SAML = "saml";

private final Map<String, Object> props;

public DatabaseClientBuilder() {
Expand Down Expand Up @@ -113,11 +120,50 @@ public DatabaseClientBuilder withSecurityContext(DatabaseClientFactory.SecurityC
return this;
}

/**
*
* @param type must be one of "basic", "digest", "cloud", "kerberos", "certificate", or "saml"
* @return
*/
public DatabaseClientBuilder withSecurityContextType(String type) {
props.put(PREFIX + "securityContextType", type);
return this;
}

public DatabaseClientBuilder withBasicAuth(String username, String password) {
return withSecurityContextType(SECURITY_CONTEXT_TYPE_BASIC)
.withUsername(username)
.withPassword(password);
}

public DatabaseClientBuilder withDigestAuth(String username, String password) {
return withSecurityContextType(SECURITY_CONTEXT_TYPE_DIGEST)
.withUsername(username)
.withPassword(password);
}

public DatabaseClientBuilder withMarkLogicCloudAuth(String apiKey, String basePath) {
return withSecurityContextType(SECURITY_CONTEXT_TYPE_MARKLOGIC_CLOUD)
.withCloudApiKey(apiKey)
.withBasePath(basePath);
}

public DatabaseClientBuilder withKerberosAuth(String principal) {
return withSecurityContextType(SECURITY_CONTEXT_TYPE_KERBEROS)
.withKerberosPrincipal(principal);
}

public DatabaseClientBuilder withCertificateAuth(String file, String password) {
return withSecurityContextType(SECURITY_CONTEXT_TYPE_CERTIFICATE)
.withCertificateFile(file)
.withCertificatePassword(password);
}

public DatabaseClientBuilder withSAMLAuth(String token) {
return withSecurityContextType(SECURITY_CONTEXT_TYPE_SAML)
.withSAMLToken(token);
}

public DatabaseClientBuilder withConnectionType(DatabaseClient.ConnectionType type) {
props.put(PREFIX + "connectionType", type);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* Contains the implementation for the {@code DatabaseClientFactory.newClient} method that accepts a function as a
* property source. Implementation is here primarily to ease readability and avoid making the factory class any larger
* than it already is.
*
* @since 6.1.0
*/
public class DatabaseClientPropertySource {

Expand Down Expand Up @@ -117,17 +119,17 @@ private DatabaseClientFactory.SecurityContext newSecurityContext() {

private DatabaseClientFactory.SecurityContext newSecurityContext(String type) {
switch (type.toLowerCase()) {
case "basic":
case DatabaseClientBuilder.SECURITY_CONTEXT_TYPE_BASIC:
return newBasicAuthContext();
case "digest":
case DatabaseClientBuilder.SECURITY_CONTEXT_TYPE_DIGEST:
return newDigestAuthContext();
case "cloud":
case DatabaseClientBuilder.SECURITY_CONTEXT_TYPE_MARKLOGIC_CLOUD:
return newCloudAuthContext();
case "kerberos":
case DatabaseClientBuilder.SECURITY_CONTEXT_TYPE_KERBEROS:
return newKerberosAuthContext();
case "certificate":
case DatabaseClientBuilder.SECURITY_CONTEXT_TYPE_CERTIFICATE:
return newCertificateAuthContext();
case "saml":
case DatabaseClientBuilder.SECURITY_CONTEXT_TYPE_SAML:
return newSAMLAuthContext();
default:
throw new IllegalArgumentException("Unrecognized security context type: " + type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ void invalidSecurityContextType() {
@Test
void digest() {
bean = Common.newClientBuilder()
.withUsername("my-user")
.withPassword("my-password")
.withSecurityContextType("digest")
.withDigestAuth("my-user", "my-password")
.buildBean();

DatabaseClientFactory.DigestAuthContext context = (DatabaseClientFactory.DigestAuthContext) bean.getSecurityContext();
Expand All @@ -91,9 +89,7 @@ void digest() {
@Test
void basic() {
bean = Common.newClientBuilder()
.withUsername("my-user")
.withPassword("my-password")
.withSecurityContextType("basic")
.withBasicAuth("my-user", "my-password")
.buildBean();

DatabaseClientFactory.BasicAuthContext context = (DatabaseClientFactory.BasicAuthContext) bean.getSecurityContext();
Expand All @@ -104,9 +100,7 @@ void basic() {
@Test
void cloudWithBasePath() {
bean = Common.newClientBuilder()
.withSecurityContextType("cloud")
.withCloudApiKey("my-key")
.withBasePath("/my/path")
.withMarkLogicCloudAuth("my-key", "/my/path")
.buildBean();

DatabaseClientFactory.MarkLogicCloudAuthContext context =
Expand All @@ -127,8 +121,7 @@ void cloudNoApiKey() {
@Test
void kerberos() {
bean = Common.newClientBuilder()
.withSecurityContextType("kerberos")
.withKerberosPrincipal("someone")
.withKerberosAuth("someone")
.buildBean();

DatabaseClientFactory.KerberosAuthContext context = (DatabaseClientFactory.KerberosAuthContext) bean.getSecurityContext();
Expand All @@ -138,9 +131,7 @@ void kerberos() {
@Test
void certificate() {
DatabaseClientBuilder builder = Common.newClientBuilder()
.withSecurityContextType("CERTificate")
.withCertificateFile("not.found")
.withCertificatePassword("passwd");
.withCertificateAuth("not.found", "passwd");

Exception ex = assertThrows(Exception.class, () -> builder.buildBean());
assertTrue(ex.getMessage().contains("Unable to create CertificateAuthContext"),
Expand All @@ -151,8 +142,7 @@ void certificate() {
@Test
void saml() {
bean = Common.newClientBuilder()
.withSecurityContextType("saml")
.withSAMLToken("my-token")
.withSAMLAuth("my-token")
.buildBean();

DatabaseClientFactory.SAMLAuthContext context = (DatabaseClientFactory.SAMLAuthContext) bean.getSecurityContext();
Expand Down