|
15 | 15 |
|
16 | 16 | import io.airlift.configuration.Config; |
17 | 17 | import io.airlift.configuration.ConfigDescription; |
| 18 | +import io.airlift.configuration.ConfigSecuritySensitive; |
| 19 | +import io.airlift.units.Duration; |
| 20 | +import io.airlift.units.MinDuration; |
| 21 | +import jakarta.validation.constraints.AssertTrue; |
18 | 22 | import jakarta.validation.constraints.NotEmpty; |
19 | 23 | import jakarta.validation.constraints.NotNull; |
20 | 24 |
|
21 | 25 | import java.net.URI; |
| 26 | +import java.util.Optional; |
| 27 | +import java.util.regex.Matcher; |
| 28 | +import java.util.regex.Pattern; |
| 29 | + |
| 30 | +import static com.google.common.base.Preconditions.checkArgument; |
| 31 | +import static io.trino.plugin.iceberg.catalog.nessie.IcebergNessieCatalogConfig.Security.BEARER; |
| 32 | +import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 33 | +import static java.util.function.Predicate.isEqual; |
| 34 | +import static org.projectnessie.client.NessieConfigConstants.DEFAULT_CONNECT_TIMEOUT_MILLIS; |
| 35 | +import static org.projectnessie.client.NessieConfigConstants.DEFAULT_READ_TIMEOUT_MILLIS; |
22 | 36 |
|
23 | 37 | public class IcebergNessieCatalogConfig |
24 | 38 | { |
| 39 | + public enum Security |
| 40 | + { |
| 41 | + BEARER, |
| 42 | + } |
| 43 | + |
| 44 | + public enum ClientApiVersion |
| 45 | + { |
| 46 | + V1, |
| 47 | + V2, |
| 48 | + } |
| 49 | + |
25 | 50 | private String defaultReferenceName = "main"; |
26 | 51 | private String defaultWarehouseDir; |
27 | 52 | private URI serverUri; |
| 53 | + private Duration readTimeout = new Duration(DEFAULT_READ_TIMEOUT_MILLIS, MILLISECONDS); |
| 54 | + private Duration connectionTimeout = new Duration(DEFAULT_CONNECT_TIMEOUT_MILLIS, MILLISECONDS); |
| 55 | + private boolean enableCompression = true; |
| 56 | + private Security security; |
| 57 | + private Optional<String> bearerToken = Optional.empty(); |
| 58 | + private Optional<ClientApiVersion> clientAPIVersion = Optional.empty(); |
| 59 | + private static final Pattern VERSION_PATTERN = Pattern.compile("/v(\\d+)$"); |
28 | 60 |
|
29 | 61 | @NotNull |
30 | 62 | public String getDefaultReferenceName() |
@@ -67,4 +99,113 @@ public IcebergNessieCatalogConfig setDefaultWarehouseDir(String defaultWarehouse |
67 | 99 | this.defaultWarehouseDir = defaultWarehouseDir; |
68 | 100 | return this; |
69 | 101 | } |
| 102 | + |
| 103 | + @MinDuration("1ms") |
| 104 | + public Duration getReadTimeout() |
| 105 | + { |
| 106 | + return readTimeout; |
| 107 | + } |
| 108 | + |
| 109 | + @Config("iceberg.nessie-catalog.read-timeout") |
| 110 | + @ConfigDescription("The read timeout for the client.") |
| 111 | + public IcebergNessieCatalogConfig setReadTimeout(Duration readTimeout) |
| 112 | + { |
| 113 | + this.readTimeout = readTimeout; |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + @MinDuration("1ms") |
| 118 | + public Duration getConnectionTimeout() |
| 119 | + { |
| 120 | + return connectionTimeout; |
| 121 | + } |
| 122 | + |
| 123 | + @Config("iceberg.nessie-catalog.connection-timeout") |
| 124 | + @ConfigDescription("The connection timeout for the client.") |
| 125 | + public IcebergNessieCatalogConfig setConnectionTimeout(Duration connectionTimeout) |
| 126 | + { |
| 127 | + this.connectionTimeout = connectionTimeout; |
| 128 | + return this; |
| 129 | + } |
| 130 | + |
| 131 | + public boolean isCompressionEnabled() |
| 132 | + { |
| 133 | + return enableCompression; |
| 134 | + } |
| 135 | + |
| 136 | + @Config("iceberg.nessie-catalog.enable-compression") |
| 137 | + @ConfigDescription("Configure whether compression should be enabled or not.") |
| 138 | + public IcebergNessieCatalogConfig setCompressionEnabled(boolean enableCompression) |
| 139 | + { |
| 140 | + this.enableCompression = enableCompression; |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + public Optional<Security> getSecurity() |
| 145 | + { |
| 146 | + return Optional.ofNullable(security); |
| 147 | + } |
| 148 | + |
| 149 | + @Config("iceberg.nessie-catalog.authentication.type") |
| 150 | + @ConfigDescription("The authentication type to use") |
| 151 | + public IcebergNessieCatalogConfig setSecurity(Security security) |
| 152 | + { |
| 153 | + this.security = security; |
| 154 | + return this; |
| 155 | + } |
| 156 | + |
| 157 | + public Optional<String> getBearerToken() |
| 158 | + { |
| 159 | + return bearerToken; |
| 160 | + } |
| 161 | + |
| 162 | + @Config("iceberg.nessie-catalog.authentication.token") |
| 163 | + @ConfigDescription("The token to use with BEARER authentication") |
| 164 | + @ConfigSecuritySensitive |
| 165 | + public IcebergNessieCatalogConfig setBearerToken(String token) |
| 166 | + { |
| 167 | + this.bearerToken = Optional.ofNullable(token); |
| 168 | + return this; |
| 169 | + } |
| 170 | + |
| 171 | + @AssertTrue(message = "'iceberg.nessie-catalog.authentication.token' must be configured only with 'iceberg.nessie-catalog.authentication.type' BEARER") |
| 172 | + public boolean isTokenConfiguredWithoutType() |
| 173 | + { |
| 174 | + return getSecurity().filter(isEqual(BEARER)).isPresent() || getBearerToken().isEmpty(); |
| 175 | + } |
| 176 | + |
| 177 | + @AssertTrue(message = "'iceberg.nessie-catalog.authentication.token' must be configured with 'iceberg.nessie-catalog.authentication.type' BEARER") |
| 178 | + public boolean isMissingTokenForBearerAuth() |
| 179 | + { |
| 180 | + return getSecurity().filter(isEqual(BEARER)).isEmpty() || getBearerToken().isPresent(); |
| 181 | + } |
| 182 | + |
| 183 | + public Optional<ClientApiVersion> getClientAPIVersion() |
| 184 | + { |
| 185 | + return clientAPIVersion; |
| 186 | + } |
| 187 | + |
| 188 | + @Config("iceberg.nessie-catalog.client-api-version") |
| 189 | + @ConfigDescription("Client API version to use") |
| 190 | + public IcebergNessieCatalogConfig setClientAPIVersion(ClientApiVersion version) |
| 191 | + { |
| 192 | + this.clientAPIVersion = Optional.ofNullable(version); |
| 193 | + return this; |
| 194 | + } |
| 195 | + |
| 196 | + protected IcebergNessieCatalogConfig.ClientApiVersion inferVersionFromURI() |
| 197 | + { |
| 198 | + checkArgument(serverUri != null, "URI is not specified in the catalog properties"); |
| 199 | + // match for uri ending with /v1, /v2 etc |
| 200 | + Matcher matcher = VERSION_PATTERN.matcher(serverUri.toString()); |
| 201 | + if (!matcher.find()) { |
| 202 | + throw new IllegalArgumentException("URI doesn't end with the version: %s. Please configure `client-api-version` in the catalog properties explicitly.".formatted(serverUri)); |
| 203 | + } |
| 204 | + |
| 205 | + return switch (matcher.group(1)) { |
| 206 | + case "1" -> ClientApiVersion.V1; |
| 207 | + case "2" -> ClientApiVersion.V2; |
| 208 | + default -> throw new IllegalArgumentException("Unknown API version in the URI: " + matcher.group(1)); |
| 209 | + }; |
| 210 | + } |
70 | 211 | } |
0 commit comments