|
30 | 30 | import com.couchbase.client.core.env.SecurityConfig; |
31 | 31 | import com.couchbase.client.core.env.TimeoutConfig; |
32 | 32 | import com.couchbase.client.core.msg.kv.DurabilityLevel; |
| 33 | +import com.couchbase.client.java.ClusterOptions; |
33 | 34 | import com.couchbase.client.java.env.ClusterEnvironment; |
34 | 35 | import com.couchbase.client.java.json.JsonArray; |
35 | 36 | import com.couchbase.client.java.json.JsonObject; |
@@ -101,85 +102,65 @@ ClusterEnvironment.Builder convertClusterConfig(ClusterConnectionCreateRequest r |
101 | 102 | if (request.hasClusterConfig()) { |
102 | 103 | var cc = request.getClusterConfig(); |
103 | 104 |
|
104 | | - if (cc.getUseCustomSerializer()) { |
105 | | - clusterEnvironment.jsonSerializer(new CustomSerializer()); |
106 | | - } |
107 | | - |
108 | 105 | // [if:3.3.0] |
109 | 106 | if (request.getClusterConfig().hasTransactionsConfig()) { |
110 | 107 | applyTransactionsConfig(request, getCluster, clusterEnvironment); |
111 | 108 | } |
112 | 109 | // [end] |
113 | 110 |
|
114 | | - SecurityConfig.Builder secBuilder = null; |
115 | | - if (cc.getUseTls()) { |
116 | | - secBuilder = SecurityConfig.builder(); |
117 | | - secBuilder.enableTls(true); |
118 | | - } |
| 111 | + applyClusterConfig(clusterEnvironment, cc, onClusterConnectionClose); |
119 | 112 |
|
120 | | - if (cc.hasCertPath()) { |
121 | | - if (secBuilder == null) secBuilder = SecurityConfig.builder(); |
122 | | - logger.info("Using certificate from file {}", cc.getCertPath()); |
123 | | - secBuilder.trustCertificate(Path.of(cc.getCertPath())); |
124 | | - } |
| 113 | + } |
| 114 | + |
| 115 | + return clusterEnvironment; |
| 116 | + } |
| 117 | + |
| 118 | + private static void applyClusterConfig(ClusterEnvironment.Builder clusterEnvironment, |
| 119 | + ClusterConfig cc, |
| 120 | + ArrayList<Runnable> onClusterConnectionClose) { |
| 121 | + IoConfig.Builder ioConfig = null; |
| 122 | + TimeoutConfig.Builder timeoutConfig = null; |
| 123 | + |
| 124 | + if (cc.getUseCustomSerializer()) { |
| 125 | + clusterEnvironment.jsonSerializer(new CustomSerializer()); |
| 126 | + } |
| 127 | + |
| 128 | + SecurityConfig.Builder secBuilder = null; |
| 129 | + if (cc.getUseTls()) { |
| 130 | + secBuilder = SecurityConfig.builder(); |
| 131 | + secBuilder.enableTls(true); |
| 132 | + } |
| 133 | + |
| 134 | + if (cc.hasCertPath()) { |
| 135 | + if (secBuilder == null) secBuilder = SecurityConfig.builder(); |
| 136 | + logger.info("Using certificate from file {}", cc.getCertPath()); |
| 137 | + secBuilder.trustCertificate(Path.of(cc.getCertPath())); |
| 138 | + } |
125 | 139 |
|
126 | | - if (cc.hasCert()) { |
| 140 | + if (cc.hasCert()) { |
127 | 141 | if (secBuilder == null) secBuilder = SecurityConfig.builder(); |
128 | 142 | try { |
129 | | - CertificateFactory cFactory = CertificateFactory.getInstance("X.509"); |
130 | | - var file = new ByteArrayInputStream(cc.getCert().getBytes(StandardCharsets.UTF_8)); |
131 | | - logger.info("Using certificate {}", cc.getCert()); |
132 | | - X509Certificate cert = (X509Certificate) cFactory.generateCertificate(file); |
| 143 | + CertificateFactory cFactory = CertificateFactory.getInstance("X.509"); |
| 144 | + var file = new ByteArrayInputStream(cc.getCert().getBytes(StandardCharsets.UTF_8)); |
| 145 | + logger.info("Using certificate {}", cc.getCert()); |
| 146 | + X509Certificate cert = (X509Certificate) cFactory.generateCertificate(file); |
133 | 147 |
|
134 | | - secBuilder.trustCertificates(List.of(cert)); |
| 148 | + secBuilder.trustCertificates(List.of(cert)); |
135 | 149 | } |
136 | 150 | catch (CertificateException err) { |
137 | | - throw new RuntimeException(err); |
| 151 | + throw new RuntimeException(err); |
138 | 152 | } |
139 | | - } |
| 153 | + } |
140 | 154 |
|
141 | | - if (cc.hasInsecure() && cc.getInsecure()) { |
| 155 | + if (cc.hasInsecure() && cc.getInsecure()) { |
142 | 156 | if (secBuilder == null) secBuilder = SecurityConfig.builder(); |
143 | 157 | // Cannot use enableCertificateVerification as it was added later |
144 | 158 | secBuilder.trustManagerFactory(InsecureTrustManagerFactory.INSTANCE); |
145 | | - } |
146 | | - |
147 | | - if (secBuilder != null) { |
148 | | - clusterEnvironment.securityConfig(secBuilder); |
149 | | - } |
150 | | - |
151 | | - applyClusterConfig(clusterEnvironment, cc); |
152 | | - |
153 | | - if (cc.hasObservabilityConfig()) { |
154 | | - applyObservabilityConfig(clusterEnvironment, cc, onClusterConnectionClose); |
155 | | - } |
156 | | - |
157 | | - if (cc.hasPreferredServerGroup()) { |
158 | | - // [if:3.7.4] |
159 | | - clusterEnvironment.preferredServerGroup(cc.getPreferredServerGroup()); |
160 | | - // [end] |
161 | | - } |
162 | | - |
163 | | - if (cc.hasAppTelemetryEndpoint()) { |
164 | | - // [if:3.8.0] |
165 | | - clusterEnvironment.appTelemetryEndpoint(cc.getAppTelemetryEndpoint()); |
166 | | - // [end] |
167 | | - } |
168 | | - |
169 | | - if (cc.hasEnableAppTelemetry()) { |
170 | | - // [if:3.8.0] |
171 | | - clusterEnvironment.disableAppTelemetry(!cc.getEnableAppTelemetry()); |
172 | | - // [end] |
173 | | - } |
174 | 159 | } |
175 | 160 |
|
176 | | - return clusterEnvironment; |
177 | | - } |
178 | | - |
179 | | - private static void applyClusterConfig(ClusterEnvironment.Builder clusterEnvironment, ClusterConfig cc) { |
180 | | - IoConfig.Builder ioConfig = null; |
181 | | - TimeoutConfig.Builder timeoutConfig = null; |
182 | | - |
| 161 | + if (secBuilder != null) { |
| 162 | + clusterEnvironment.securityConfig(secBuilder); |
| 163 | + } |
183 | 164 | if (cc.hasKvConnectTimeoutSecs()) { |
184 | 165 | if (timeoutConfig == null) timeoutConfig = TimeoutConfig.builder(); |
185 | 166 | timeoutConfig.connectTimeout(Duration.ofSeconds(cc.getKvConnectTimeoutSecs())); |
@@ -292,6 +273,28 @@ private static void applyClusterConfig(ClusterEnvironment.Builder clusterEnviron |
292 | 273 | if (timeoutConfig != null) { |
293 | 274 | clusterEnvironment.timeoutConfig(timeoutConfig); |
294 | 275 | } |
| 276 | + |
| 277 | + if (cc.hasObservabilityConfig()) { |
| 278 | + applyObservabilityConfig(clusterEnvironment, cc, onClusterConnectionClose); |
| 279 | + } |
| 280 | + |
| 281 | + if (cc.hasPreferredServerGroup()) { |
| 282 | + // [if:3.7.4] |
| 283 | + clusterEnvironment.preferredServerGroup(cc.getPreferredServerGroup()); |
| 284 | + // [end] |
| 285 | + } |
| 286 | + |
| 287 | + if (cc.hasAppTelemetryEndpoint()) { |
| 288 | + // [if:3.8.0] |
| 289 | + clusterEnvironment.appTelemetryEndpoint(cc.getAppTelemetryEndpoint()); |
| 290 | + // [end] |
| 291 | + } |
| 292 | + |
| 293 | + if (cc.hasEnableAppTelemetry()) { |
| 294 | + // [if:3.8.0] |
| 295 | + clusterEnvironment.disableAppTelemetry(!cc.getEnableAppTelemetry()); |
| 296 | + // [end] |
| 297 | + } |
295 | 298 | } |
296 | 299 |
|
297 | 300 | // [if:3.5.1] |
|
0 commit comments