|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 Tencent Cloud. |
| 3 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 | + * this software and associated documentation files (the vectordb-sdk-java), to |
| 5 | + * deal in the Software without restriction, including without limitation the |
| 6 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | + * copies of the Software, and to permit persons to whom the Software is furnished |
| 8 | + * to do so, subject to the following conditions: |
| 9 | + * |
| 10 | + * The above copyright notice and this permission notice shall be included in all |
| 11 | + * copies or substantial portions of the Software. |
| 12 | + * |
| 13 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 14 | + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 15 | + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 16 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 17 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 18 | + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.tencent.tcvectordb.model.param.database; |
| 22 | + |
| 23 | +/** |
| 24 | + * TLS configuration for HTTPClient and RPCClient. |
| 25 | + * <p> |
| 26 | + * This class provides configuration options for TLS/SSL connections to VectorDB server. |
| 27 | + * </p> |
| 28 | + * |
| 29 | + * <h3>Usage Examples:</h3> |
| 30 | + * |
| 31 | + * <p><b>1. Using CA certificate file path:</b></p> |
| 32 | + * <pre>{@code |
| 33 | + * TLSConfig tlsConfig = TLSConfig.newBuilder() |
| 34 | + * .withCACert("/path/to/ca-cert.pem") |
| 35 | + * .build(); |
| 36 | + * }</pre> |
| 37 | + * |
| 38 | + * <p><b>2. Using CA certificate content directly:</b></p> |
| 39 | + * <pre>{@code |
| 40 | + * String caCertContent = "-----BEGIN CERTIFICATE-----\n...certificate content...\n-----END CERTIFICATE-----"; |
| 41 | + * TLSConfig tlsConfig = TLSConfig.newBuilder() |
| 42 | + * .withCACert(caCertContent) |
| 43 | + * .build(); |
| 44 | + * }</pre> |
| 45 | + * |
| 46 | + * <p><b>3. Skip certificate verification (for testing only):</b></p> |
| 47 | + * <pre>{@code |
| 48 | + * TLSConfig tlsConfig = TLSConfig.newBuilder() |
| 49 | + * .withInsecureSkipVerify(true) |
| 50 | + * .build(); |
| 51 | + * }</pre> |
| 52 | + */ |
| 53 | +public class TLSConfig { |
| 54 | + |
| 55 | + /** |
| 56 | + * Default server name for SNI when connecting to IP addresses. |
| 57 | + */ |
| 58 | + public static final String DEFAULT_SERVER_NAME = "vdb.tencentcloud.com"; |
| 59 | + |
| 60 | + /** |
| 61 | + * CA certificate content or file path for HTTPS connections. |
| 62 | + * <p> |
| 63 | + * If the value is a valid file path, the certificate will be read from the file. |
| 64 | + * Otherwise, it will be treated as the certificate content directly (PEM format). |
| 65 | + * </p> |
| 66 | + */ |
| 67 | + private final String caCert; |
| 68 | + |
| 69 | + /** |
| 70 | + * Whether to skip TLS certificate verification. |
| 71 | + * <p> |
| 72 | + * WARNING: This should only be used for testing environments. |
| 73 | + * Never use this in production as it makes the connection vulnerable to MITM attacks. |
| 74 | + * </p> |
| 75 | + */ |
| 76 | + private final boolean insecureSkipVerify; |
| 77 | + |
| 78 | + /** |
| 79 | + * Server name for SNI (Server Name Indication). |
| 80 | + * <p> |
| 81 | + * This is automatically set to {@link #DEFAULT_SERVER_NAME} when connecting to an IP address. |
| 82 | + * </p> |
| 83 | + */ |
| 84 | + private final String serverName; |
| 85 | + |
| 86 | + private TLSConfig(Builder builder) { |
| 87 | + this.caCert = builder.caCert; |
| 88 | + this.insecureSkipVerify = builder.insecureSkipVerify; |
| 89 | + this.serverName = builder.serverName; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Gets the CA certificate content or file path. |
| 94 | + * |
| 95 | + * @return CA certificate content or file path, or null if not set |
| 96 | + */ |
| 97 | + public String getCACert() { |
| 98 | + return caCert; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Returns whether to skip TLS certificate verification. |
| 103 | + * |
| 104 | + * @return true if certificate verification should be skipped, false otherwise |
| 105 | + */ |
| 106 | + public boolean isInsecureSkipVerify() { |
| 107 | + return insecureSkipVerify; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Gets the server name for SNI. |
| 112 | + * |
| 113 | + * @return server name for SNI |
| 114 | + */ |
| 115 | + public String getServerName() { |
| 116 | + return serverName; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Creates a new builder for TLSConfig. |
| 121 | + * |
| 122 | + * @return a new Builder instance |
| 123 | + */ |
| 124 | + public static Builder newBuilder() { |
| 125 | + return new Builder(); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Builder class for TLSConfig. |
| 130 | + */ |
| 131 | + public static class Builder { |
| 132 | + private String caCert; |
| 133 | + private boolean insecureSkipVerify = false; |
| 134 | + private String serverName = DEFAULT_SERVER_NAME; |
| 135 | + |
| 136 | + private Builder() { |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Sets the CA certificate content or file path. |
| 141 | + * <p> |
| 142 | + * The certificate can be provided in two ways: |
| 143 | + * <ul> |
| 144 | + * <li>As a file path pointing to a PEM-encoded certificate file</li> |
| 145 | + * <li>As the certificate content directly in PEM format</li> |
| 146 | + * </ul> |
| 147 | + * </p> |
| 148 | + * |
| 149 | + * @param caCert CA certificate content or file path |
| 150 | + * @return this builder |
| 151 | + */ |
| 152 | + public Builder withCACert(String caCert) { |
| 153 | + this.caCert = caCert; |
| 154 | + return this; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Sets whether to skip TLS certificate verification. |
| 159 | + * <p> |
| 160 | + * WARNING: Only use this for testing environments. Never use in production. |
| 161 | + * </p> |
| 162 | + * |
| 163 | + * @param insecureSkipVerify true to skip certificate verification |
| 164 | + * @return this builder |
| 165 | + */ |
| 166 | + public Builder withInsecureSkipVerify(boolean insecureSkipVerify) { |
| 167 | + this.insecureSkipVerify = insecureSkipVerify; |
| 168 | + return this; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Sets the server name for SNI. |
| 173 | + * <p> |
| 174 | + * By default, this is set to {@link TLSConfig#DEFAULT_SERVER_NAME}. |
| 175 | + * </p> |
| 176 | + * |
| 177 | + * @param serverName server name for SNI |
| 178 | + * @return this builder |
| 179 | + */ |
| 180 | + public Builder withServerName(String serverName) { |
| 181 | + if (serverName != null && !serverName.isEmpty()) { |
| 182 | + this.serverName = serverName; |
| 183 | + } |
| 184 | + return this; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Builds the TLSConfig instance. |
| 189 | + * |
| 190 | + * @return a new TLSConfig instance |
| 191 | + */ |
| 192 | + public TLSConfig build() { |
| 193 | + return new TLSConfig(this); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public String toString() { |
| 199 | + return "TLSConfig{" + |
| 200 | + "caCert='" + (caCert != null ? "[SET]" : "[NOT SET]") + '\'' + |
| 201 | + ", insecureSkipVerify=" + insecureSkipVerify + |
| 202 | + ", serverName='" + serverName + '\'' + |
| 203 | + '}'; |
| 204 | + } |
| 205 | +} |
0 commit comments