11package com .marklogic .hub .central ;
22
33import com .marklogic .client .DatabaseClient ;
4+ import com .marklogic .client .DatabaseClientFactory ;
45import com .marklogic .client .ext .ConfiguredDatabaseClientFactory ;
56import com .marklogic .client .ext .DatabaseClientConfig ;
67import com .marklogic .client .ext .DefaultConfiguredDatabaseClientFactory ;
78import com .marklogic .client .ext .SecurityContextType ;
89import com .marklogic .client .ext .helper .LoggingObject ;
10+ import com .marklogic .client .ext .modulesloader .ssl .SimpleX509TrustManager ;
911import com .marklogic .hub .impl .HubConfigImpl ;
1012import com .marklogic .mgmt .util .PropertySource ;
1113import org .springframework .beans .factory .InitializingBean ;
1416import org .springframework .core .env .Environment ;
1517import org .springframework .stereotype .Component ;
1618
19+ import javax .net .ssl .SSLContext ;
1720import java .util .Properties ;
1821
1922/**
@@ -55,6 +58,38 @@ public HubConfigImpl newHubConfig(String username, String password) {
5558 return hubConfig ;
5659 }
5760
61+ /**
62+ * This constructs a HubConfigImpl by starting with the default property values in HubConfigImpl, and then applying
63+ * properties based on the Spring environment of this application plus the username/password provided by the user
64+ * logging in.
65+ *
66+ * @param cloudApiKey
67+ * @return
68+ */
69+ public HubConfigImpl newHubConfig (String cloudApiKey ) {
70+ HubConfigImpl hubConfig = new HubConfigImpl ();
71+ DatabaseClient client = getStagingDbClient (cloudApiKey );
72+ Properties primaryProperties = hubConfig .getHubPropertiesFromDb (client );
73+ primaryProperties .setProperty ("mlCloudApiKey" , cloudApiKey );
74+ primaryProperties .setProperty ("mlStagingBasePath" , CloudParameters .STAGING_BASE_PATH );
75+ primaryProperties .setProperty ("mlFinalBasePath" , CloudParameters .FINAL_BASE_PATH );
76+ primaryProperties .setProperty ("mlJobBasePath" , CloudParameters .JOB_BASE_PATH );
77+
78+ primaryProperties .setProperty ("mlManageBasePath" , CloudParameters .MANAGE_BASE_PATH );
79+ primaryProperties .setProperty ("mlAppServicesBasePath" , CloudParameters .APP_SERVICES_BASE_PATH );
80+ primaryProperties .setProperty ("mlAdminBasePath" , CloudParameters .ADMIN_BASE_PATH );
81+
82+ primaryProperties .setProperty ("mlAdminSimpleSsl" , "true" );
83+ primaryProperties .setProperty ("mlManageSimpleSsl" , "true" );
84+ primaryProperties .setProperty ("mlAppServicesSimpleSsl" , "true" );
85+
86+ primaryProperties .setProperty ("mlAuthentication" , CloudParameters .AUTHENTICATION_TYPE );
87+ primaryProperties .setProperty ("mlSslHostnameVerifier" , "ANY" );
88+ primaryProperties .setProperty ("mlManageAuthentication" , "cloud" );
89+ hubConfig .applyProperties (buildPropertySource (primaryProperties ));
90+ return hubConfig ;
91+ }
92+
5893 /**
5994 * Construct a PropertySource based on the properties in the Spring Boot environment plus the given username and
6095 * password, which are supplied when a user authenticates.
@@ -70,7 +105,10 @@ protected PropertySource buildPropertySource(String username, String password) {
70105 protected PropertySource buildPropertySource (String username , String password , Properties primaryProperties ) {
71106 primaryProperties .setProperty ("mlUsername" , username );
72107 primaryProperties .setProperty ("mlPassword" , password );
108+ return buildPropertySource (primaryProperties );
109+ }
73110
111+ protected PropertySource buildPropertySource (Properties primaryProperties ) {
74112 return propertyName -> {
75113 String value = primaryProperties .getProperty (propertyName );
76114 if (!propertyName .equals ("mlUsername" ) && !propertyName .equals ("mlPassword" ) && environment .getProperty (propertyName ) != null ) {
@@ -81,20 +119,53 @@ protected PropertySource buildPropertySource(String username, String password, P
81119 }
82120
83121 private DatabaseClient getStagingDbClient (String username , String password ) {
84- ConfiguredDatabaseClientFactory configuredDatabaseClientFactory = new DefaultConfiguredDatabaseClientFactory ();
85- DatabaseClientConfig config = new DatabaseClientConfig ("localhost" , 8010 , username , password );
86- config .setSecurityContextType (SecurityContextType .valueOf ("DIGEST" ));
122+ DatabaseClientConfig config = new DatabaseClientConfig ();
123+ config .setUsername (username );
124+ config .setPassword (password );
125+ config .setCloudApiKey ("" );
126+ return getStagingDbClient (config );
127+ }
87128
88- if ( environment . getProperty ( "mlHost" ) != null ) {
89- config . setHost ( environment . getProperty ( "mlHost" ) );
90- }
129+ private DatabaseClient getStagingDbClient ( String cloudApiKey ) {
130+ DatabaseClientConfig config = new DatabaseClientConfig ( );
131+ config . setCloudApiKey ( cloudApiKey );
91132
92- if (environment .getProperty ("mlStagingPort" ) != null ) {
93- config .setPort (Integer .parseInt (environment .getProperty ("mlStagingPort" )));
94- }
133+ SSLContext stagingSslContext = SimpleX509TrustManager .newSSLContext ();
134+ DatabaseClientFactory .SSLHostnameVerifier stagingSslHostnameVerifier = DatabaseClientFactory .SSLHostnameVerifier .ANY ;
135+ SimpleX509TrustManager stagingTrustManager = new SimpleX509TrustManager ();
136+
137+ config .setSslHostnameVerifier (stagingSslHostnameVerifier );
138+ config .setSslContext (stagingSslContext );
139+ config .setCertFile (null );
140+ config .setCertPassword (null );
141+ config .setExternalName (null );
142+ config .setTrustManager (stagingTrustManager );
143+ config .setUsername ("" );
144+ config .setPassword ("" );
95145
96- if (environment .getProperty ("mlStagingAuth" ) != null ) {
97- config .setSecurityContextType (SecurityContextType .valueOf (environment .getProperty ("mlStagingAuth" ).toUpperCase ()));
146+ return getStagingDbClient (config );
147+ }
148+
149+ private DatabaseClient getStagingDbClient (DatabaseClientConfig config ) {
150+ ConfiguredDatabaseClientFactory configuredDatabaseClientFactory = new DefaultConfiguredDatabaseClientFactory ();
151+ if (CloudParameters .AUTHENTICATION_TYPE .equals ("cloud" )) {
152+ config .setHost (CloudParameters .ML_HOST );
153+ config .setPort (CloudParameters .ML_REVERSE_PROXY_PORT );
154+ config .setBasePath (CloudParameters .STAGING_BASE_PATH );
155+ config .setSecurityContextType (SecurityContextType .CLOUD );
156+ } else {
157+ config .setHost (environment .getProperty ("mlHost" ) != null ?
158+ environment .getProperty ("mlHost" ) :
159+ "localhost"
160+ );
161+ config .setPort (environment .getProperty ("mlStagingPort" ) != null ?
162+ Integer .parseInt (environment .getProperty ("mlStagingPort" )) :
163+ 8010
164+ );
165+ config .setSecurityContextType (environment .getProperty ("mlStagingAuth" ) != null ?
166+ SecurityContextType .valueOf (environment .getProperty ("mlStagingAuth" ).toUpperCase ()) :
167+ SecurityContextType .valueOf ("DIGEST" )
168+ );
98169 }
99170 // Need to work on SSL
100171 return configuredDatabaseClientFactory .newDatabaseClient (config );
0 commit comments