Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 92247b6

Browse files
committed
#180 Using new ConfiguredDatabaseClientFactory
#180 Added SSL support
1 parent 9758450 commit 92247b6

File tree

6 files changed

+239
-59
lines changed

6 files changed

+239
-59
lines changed

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
group=com.marklogic
22
javadocsDir=../gh-pages-marklogic-java/javadocs
3-
version=3.0-alpha1
4-
mlJavaclientUtilVersion=3.0-alpha1
3+
version=3.0-alpha2
4+
mlJavaclientUtilVersion=3.0-alpha2

src/main/java/com/marklogic/appdeployer/AppConfig.java

Lines changed: 143 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
import com.marklogic.client.DatabaseClientFactory;
55
import com.marklogic.client.DatabaseClientFactory.Authentication;
66
import com.marklogic.client.DatabaseClientFactory.SSLHostnameVerifier;
7-
import com.marklogic.client.ext.tokenreplacer.PropertiesSource;
7+
import com.marklogic.client.ext.ConfiguredDatabaseClientFactory;
8+
import com.marklogic.client.ext.DatabaseClientConfig;
9+
import com.marklogic.client.ext.DefaultConfiguredDatabaseClientFactory;
10+
import com.marklogic.client.ext.SecurityContextType;
811
import com.marklogic.client.ext.modulesloader.impl.PropertiesModuleManager;
912
import com.marklogic.client.ext.modulesloader.ssl.SimpleX509TrustManager;
13+
import com.marklogic.client.ext.tokenreplacer.PropertiesSource;
1014

1115
import javax.net.ssl.SSLContext;
1216
import java.io.FileFilter;
@@ -29,7 +33,7 @@
2933
*/
3034
public class AppConfig {
3135

32-
/**
36+
/**
3337
* This is set purely for development purposes so that an app can be created without specifying an app name.
3438
*/
3539
public static final String DEFAULT_APP_NAME = "my-app";
@@ -60,24 +64,35 @@ public class AppConfig {
6064
private String name = DEFAULT_APP_NAME;
6165
private String host = DEFAULT_HOST;
6266

67+
// Used to construct DatabaseClient instances based on inputs defined in this class
68+
private ConfiguredDatabaseClientFactory configuredDatabaseClientFactory = new DefaultConfiguredDatabaseClientFactory();
69+
6370
// Username/password combo for using the client REST API - e.g. to load modules
71+
private SecurityContextType restSecurityContextType = SecurityContextType.DIGEST;
6472
private String restAdminUsername = DEFAULT_USERNAME;
6573
private String restAdminPassword = DEFAULT_PASSWORD;
6674
private SSLContext restSslContext;
6775
private SSLHostnameVerifier restSslHostnameVerifier;
68-
private Authentication restAuthentication = Authentication.DIGEST;
69-
70-
private SSLContext appServicesSslContext;
71-
private SSLHostnameVerifier appServicesSslHostnameVerifier;
72-
private Authentication appServicesAuthentication = Authentication.DIGEST;
73-
76+
private String restCertFile;
77+
private String restCertPassword;
78+
private String restExternalName;
79+
@Deprecated
80+
private Authentication restAuthentication;
7481
private Integer restPort = DEFAULT_PORT;
7582
private Integer testRestPort;
7683

7784
// Username/password combo for using the App Services client REST API - e.g. to load non-REST API modules
85+
private SecurityContextType appServicesSecurityContextType = SecurityContextType.DIGEST;
7886
private String appServicesUsername = DEFAULT_USERNAME;
7987
private String appServicesPassword = DEFAULT_PASSWORD;
8088
private Integer appServicesPort = 8000;
89+
private SSLContext appServicesSslContext;
90+
private SSLHostnameVerifier appServicesSslHostnameVerifier;
91+
private String appServicesCertFile;
92+
private String appServicesCertPassword;
93+
private String appServicesExternalName;
94+
@Deprecated
95+
private Authentication appServicesAuthentication;
8196

8297
// These can all be set to override the default names that are generated off of the "name" attribute.
8398
private String groupName = DEFAULT_GROUP;
@@ -176,19 +191,23 @@ public AppConfig(String defaultModulePath, String defaultSchemasPath) {
176191
}
177192

178193
public void setSimpleSslConfig() {
179-
setRestSslContext(SimpleX509TrustManager.newSSLContext());
180-
setRestSslHostnameVerifier(DatabaseClientFactory.SSLHostnameVerifier.ANY);
194+
setRestSslContext(SimpleX509TrustManager.newSSLContext());
195+
setRestSslHostnameVerifier(DatabaseClientFactory.SSLHostnameVerifier.ANY);
196+
}
197+
198+
public void setAppServicesSimpleSslConfig() {
199+
setAppServicesSslContext(SimpleX509TrustManager.newSSLContext());
200+
setAppServicesSslHostnameVerifier(DatabaseClientFactory.SSLHostnameVerifier.ANY);
181201
}
182202

183203
/**
184-
* Convenience method for constructing a MarkLogic Java API DatabaseClient based on the host, restPort,
185-
* restAdminUsername, restAdminPassword, restAuthentication, restSslContext, and restSslHostnameVerifier properties.
204+
* Convenience method for constructing a MarkLogic Java API DatabaseClient based on the the host and rest*
205+
* properties defined on this class.
186206
*
187207
* @return
188208
*/
189209
public DatabaseClient newDatabaseClient() {
190-
return DatabaseClientFactory.newClient(getHost(), getRestPort(), getRestAdminUsername(), getRestAdminPassword(),
191-
getRestAuthentication(), getRestSslContext(), getRestSslHostnameVerifier());
210+
return configuredDatabaseClientFactory.newDatabaseClient(newRestDatabaseClientConfig(getRestPort()));
192211
}
193212

194213
/**
@@ -197,25 +216,49 @@ public DatabaseClient newDatabaseClient() {
197216
* @return
198217
*/
199218
public DatabaseClient newTestDatabaseClient() {
200-
return DatabaseClientFactory.newClient(getHost(), getTestRestPort(), getRestAdminUsername(),
201-
getRestAdminPassword(), getRestAuthentication(), getRestSslContext(), getRestSslHostnameVerifier());
219+
return configuredDatabaseClientFactory.newDatabaseClient(newRestDatabaseClientConfig(getTestRestPort()));
220+
}
221+
222+
public DatabaseClientConfig newRestDatabaseClientConfig(int port) {
223+
DatabaseClientConfig config = new DatabaseClientConfig(getHost(), port, getRestAdminUsername(), getRestAdminPassword());
224+
config.setAuthentication(getRestAuthentication());
225+
config.setSecurityContextType(restSecurityContextType);
226+
config.setSslHostnameVerifier(getRestSslHostnameVerifier());
227+
config.setSslContext(getRestSslContext());
228+
config.setCertFile(getRestCertFile());
229+
config.setCertPassword(getRestCertPassword());
230+
config.setExternalName(getRestExternalName());
231+
return config;
202232
}
203233

204-
public DatabaseClient newModulesDatabaseClient() {
205-
return DatabaseClientFactory.newClient(getHost(), getAppServicesPort(), getModulesDatabaseName(),
206-
getAppServicesUsername(), getAppServicesPassword(), getAppServicesAuthentication(), getAppServicesSslContext(),
207-
getAppServicesSslHostnameVerifier());
234+
/**
235+
* Constructs a DatabaseClient based on host, the appServices* properties, and the modules database name.
236+
* @return
237+
*/
238+
public DatabaseClient newModulesDatabaseClient() {
239+
return newAppServicesDatabaseClient(getModulesDatabaseName());
208240
}
209241

210242
/**
211-
* Like newDatabaseClient, but connects to schemas database.
243+
* Like newModulesDatabaseClient, but connects to schemas database.
212244
*
213245
* @return
214246
*/
215247
public DatabaseClient newSchemasDatabaseClient() {
216-
return DatabaseClientFactory.newClient(getHost(), getAppServicesPort(), getSchemasDatabaseName(),
217-
getAppServicesUsername(), getAppServicesPassword(), getAppServicesAuthentication(), getAppServicesSslContext(),
218-
getAppServicesSslHostnameVerifier());
248+
return newAppServicesDatabaseClient(getSchemasDatabaseName());
249+
}
250+
251+
public DatabaseClient newAppServicesDatabaseClient(String databaseName) {
252+
DatabaseClientConfig config = new DatabaseClientConfig(getHost(), getAppServicesPort(), getAppServicesUsername(), getAppServicesPassword());
253+
config.setDatabase(databaseName);
254+
config.setAuthentication(getAppServicesAuthentication());
255+
config.setSecurityContextType(appServicesSecurityContextType);
256+
config.setSslHostnameVerifier(getAppServicesSslHostnameVerifier());
257+
config.setSslContext(getAppServicesSslContext());
258+
config.setCertFile(getAppServicesCertFile());
259+
config.setCertPassword(getAppServicesCertPassword());
260+
config.setExternalName(getAppServicesExternalName());
261+
return configuredDatabaseClientFactory.newDatabaseClient(config);
219262
}
220263

221264
/**
@@ -410,10 +453,12 @@ public void setGroupName(String groupName) {
410453
* @return the MarkLogic Java Client {@code Authentication} object that is used for authenticating with a REST API
411454
* server for loading modules
412455
*/
456+
@Deprecated
413457
public Authentication getRestAuthentication() {
414458
return restAuthentication;
415459
}
416460

461+
@Deprecated
417462
public void setRestAuthentication(Authentication authentication) {
418463
this.restAuthentication = authentication;
419464
}
@@ -705,10 +750,12 @@ public void setAppServicesSslHostnameVerifier(SSLHostnameVerifier appServicesSsl
705750
this.appServicesSslHostnameVerifier = appServicesSslHostnameVerifier;
706751
}
707752

753+
@Deprecated
708754
public Authentication getAppServicesAuthentication() {
709755
return appServicesAuthentication;
710756
}
711757

758+
@Deprecated
712759
public void setAppServicesAuthentication(Authentication appServicesAuthentication) {
713760
this.appServicesAuthentication = appServicesAuthentication;
714761
}
@@ -784,4 +831,76 @@ public String getDeleteTestModulesPattern() {
784831
public void setDeleteTestModulesPattern(String deleteTestModulesPattern) {
785832
this.deleteTestModulesPattern = deleteTestModulesPattern;
786833
}
834+
835+
public SecurityContextType getRestSecurityContextType() {
836+
return restSecurityContextType;
837+
}
838+
839+
public void setRestSecurityContextType(SecurityContextType restSecurityContextType) {
840+
this.restSecurityContextType = restSecurityContextType;
841+
}
842+
843+
public SecurityContextType getAppServicesSecurityContextType() {
844+
return appServicesSecurityContextType;
845+
}
846+
847+
public void setAppServicesSecurityContextType(SecurityContextType appServicesSecurityContextType) {
848+
this.appServicesSecurityContextType = appServicesSecurityContextType;
849+
}
850+
851+
public String getRestCertFile() {
852+
return restCertFile;
853+
}
854+
855+
public void setRestCertFile(String restCertFile) {
856+
this.restCertFile = restCertFile;
857+
}
858+
859+
public String getRestCertPassword() {
860+
return restCertPassword;
861+
}
862+
863+
public void setRestCertPassword(String restCertPassword) {
864+
this.restCertPassword = restCertPassword;
865+
}
866+
867+
public String getAppServicesCertFile() {
868+
return appServicesCertFile;
869+
}
870+
871+
public void setAppServicesCertFile(String appServicesCertFile) {
872+
this.appServicesCertFile = appServicesCertFile;
873+
}
874+
875+
public String getAppServicesCertPassword() {
876+
return appServicesCertPassword;
877+
}
878+
879+
public void setAppServicesCertPassword(String appServicesCertPassword) {
880+
this.appServicesCertPassword = appServicesCertPassword;
881+
}
882+
883+
public String getRestExternalName() {
884+
return restExternalName;
885+
}
886+
887+
public void setRestExternalName(String restExternalName) {
888+
this.restExternalName = restExternalName;
889+
}
890+
891+
public String getAppServicesExternalName() {
892+
return appServicesExternalName;
893+
}
894+
895+
public void setAppServicesExternalName(String appServicesExternalName) {
896+
this.appServicesExternalName = appServicesExternalName;
897+
}
898+
899+
public ConfiguredDatabaseClientFactory getConfiguredDatabaseClientFactory() {
900+
return configuredDatabaseClientFactory;
901+
}
902+
903+
public void setConfiguredDatabaseClientFactory(ConfiguredDatabaseClientFactory configuredDatabaseClientFactory) {
904+
this.configuredDatabaseClientFactory = configuredDatabaseClientFactory;
905+
}
787906
}

src/main/java/com/marklogic/appdeployer/DefaultAppConfigFactory.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.marklogic.appdeployer;
22

33
import com.marklogic.client.DatabaseClientFactory;
4+
import com.marklogic.client.ext.SecurityContextType;
45
import com.marklogic.mgmt.util.PropertySource;
56
import com.marklogic.mgmt.util.PropertySourceFactory;
67

@@ -98,6 +99,7 @@ public AppConfig newAppConfig() {
9899
if (prop != null) {
99100
logger.info("App REST authentication: " + prop);
100101
c.setRestAuthentication(DatabaseClientFactory.Authentication.valueOfUncased(prop));
102+
c.setRestSecurityContextType(SecurityContextType.valueOf(prop.toUpperCase()));
101103
}
102104

103105
/**
@@ -138,6 +140,24 @@ public AppConfig newAppConfig() {
138140
c.setRestAdminPassword(mlPassword);
139141
}
140142

143+
prop = getProperty("mlRestCertFile");
144+
if (prop != null) {
145+
logger.info("REST cert file: " + prop);
146+
c.setRestCertFile(prop);
147+
}
148+
149+
prop = getProperty("mlRestCertPassword");
150+
if (prop != null) {
151+
logger.info("REST cert password: " + prop);
152+
c.setRestCertPassword(prop);
153+
}
154+
155+
prop = getProperty("mlRestExternalName");
156+
if (prop != null) {
157+
logger.info("REST external name: " + prop);
158+
c.setRestExternalName(prop);
159+
}
160+
141161
/**
142162
* The username and password for a ML user with the rest-admin role that is used for e.g. loading
143163
* non-REST API modules via the App Services client REST API, which is defined by the appServicesPort.
@@ -165,6 +185,36 @@ public AppConfig newAppConfig() {
165185
c.setAppServicesPassword(mlPassword);
166186
}
167187

188+
prop = getProperty("mlAppServicesAuthentication");
189+
if (prop != null) {
190+
logger.info("App Services authentication: " + prop);
191+
c.setAppServicesSecurityContextType(SecurityContextType.valueOf(prop.toUpperCase()));
192+
c.setAppServicesAuthentication(DatabaseClientFactory.Authentication.valueOfUncased(prop));
193+
}
194+
195+
prop = getProperty("mlAppServicesCertFile");
196+
if (prop != null) {
197+
logger.info("App Services cert file: " + prop);
198+
c.setAppServicesCertFile(prop);
199+
}
200+
201+
prop = getProperty("mlAppServicesCertPassword");
202+
if (prop != null) {
203+
logger.info("App Services cert password: " + prop);
204+
c.setAppServicesCertPassword(prop);
205+
}
206+
207+
prop = getProperty("mlAppServicesExternalName");
208+
if (prop != null) {
209+
logger.info("App Services external name: " + prop);
210+
c.setAppServicesExternalName(prop);
211+
}
212+
213+
if (getProperty("mlAppServicesSimpleSsl") != null) {
214+
logger.info("Using simple SSL context and 'ANY' hostname verifier for authenticating against the App-Services server");
215+
c.setAppServicesSimpleSslConfig();
216+
}
217+
168218
/**
169219
* When a content database is created, this property can be used to control the number of forests per host for
170220
* that database.

src/main/java/com/marklogic/appdeployer/command/modules/DeleteModulesCommand.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ public void execute(CommandContext context) {
3333
logger.info(format("Deleting modules in database '%s' with URIs matching pattern '%s'", dbName, pattern));
3434
}
3535

36-
DatabaseClient client = DatabaseClientFactory.newClient(appConfig.getHost(), appConfig.getAppServicesPort(), dbName,
37-
appConfig.getRestAdminUsername(), appConfig.getRestAdminPassword(), appConfig.getRestAuthentication(),
38-
appConfig.getRestSslContext(), appConfig.getRestSslHostnameVerifier());
36+
DatabaseClient client = appConfig.newAppServicesDatabaseClient(dbName);
3937

4038
String xquery = "for $uri in cts:uri-match('%s') where fn:doc-available($uri) return xdmp:document-delete($uri)";
4139
try {

src/main/java/com/marklogic/mgmt/api/API.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66
import com.fasterxml.jackson.databind.SerializationFeature;
77
import com.marklogic.appdeployer.DefaultAppConfigFactory;
88
import com.marklogic.client.DatabaseClient;
9-
import com.marklogic.client.DatabaseClientFactory;
10-
import com.marklogic.client.DatabaseClientFactory.Authentication;
119
import com.marklogic.client.ext.helper.ClientHelper;
1210
import com.marklogic.client.ext.helper.LoggingObject;
1311
import com.marklogic.mgmt.DefaultManageConfigFactory;
1412
import com.marklogic.mgmt.ManageClient;
1513
import com.marklogic.mgmt.ManageConfig;
16-
import com.marklogic.mgmt.resource.ResourceManager;
1714
import com.marklogic.mgmt.admin.AdminConfig;
1815
import com.marklogic.mgmt.admin.AdminManager;
1916
import com.marklogic.mgmt.api.cluster.Cluster;
@@ -24,6 +21,7 @@
2421
import com.marklogic.mgmt.api.security.*;
2522
import com.marklogic.mgmt.api.server.Server;
2623
import com.marklogic.mgmt.api.task.Task;
24+
import com.marklogic.mgmt.resource.ResourceManager;
2725
import com.marklogic.mgmt.resource.appservers.ServerManager;
2826
import com.marklogic.mgmt.resource.databases.DatabaseManager;
2927
import com.marklogic.mgmt.resource.forests.ForestManager;
@@ -149,19 +147,6 @@ public DatabaseClient newClient() {
149147
return new DefaultAppConfigFactory(new SystemPropertySource()).newAppConfig().newDatabaseClient();
150148
}
151149

152-
/**
153-
* Construct a new DatabaseClient, assuming DIGEST authentication.
154-
*
155-
* @param host
156-
* @param port
157-
* @param user
158-
* @param password
159-
* @return
160-
*/
161-
public DatabaseClient newClient(String host, Integer port, String user, String password) {
162-
return DatabaseClientFactory.newClient(host, port, user, password, Authentication.DIGEST);
163-
}
164-
165150
/**
166151
* Convenience method, seems intuitive that this would apply to the local cluster.
167152
*/

0 commit comments

Comments
 (0)