-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vamsi Manohar <[email protected]>
- Loading branch information
Showing
7 changed files
with
156 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
datasources/src/main/java/org/opensearch/sql/datasources/glue/GlueDatasourceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package org.opensearch.sql.datasources.glue; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.commons.validator.routines.DomainValidator; | ||
import org.opensearch.sql.common.setting.Settings; | ||
import org.opensearch.sql.datasource.model.DataSource; | ||
import org.opensearch.sql.datasource.model.DataSourceMetadata; | ||
import org.opensearch.sql.datasource.model.DataSourceType; | ||
import org.opensearch.sql.storage.DataSourceFactory; | ||
|
||
|
||
@RequiredArgsConstructor | ||
public class GlueDatasourceFactory implements DataSourceFactory { | ||
|
||
private final Settings pluginSettings; | ||
|
||
// Glue configuration properties | ||
public static final String GLUE_AUTH_TYPE = "glue.auth.type"; | ||
public static final String GLUE_ACCOUNT_ID = "glue.auth.account_id"; | ||
public static final String GLUE_ROLE_ARN = "glue.auth.role_arn"; | ||
public static final String GLUE_REGION = "glue.auth.region"; | ||
|
||
// Flint integration jar configuration properties | ||
public static final String FLINT_INTEGRATION = "spark.datasource.flint.integration"; | ||
public static final String FLINT_HOST = "spark.datasource.flint.host"; | ||
public static final String FLINT_PORT = "spark.datasource.flint.port"; | ||
public static final String FLINT_SCHEME = "spark.datasource.flint.scheme"; | ||
public static final String FLINT_AUTH = "spark.datasource.flint.auth"; | ||
public static final String FLINT_REGION = "spark.datasource.flint.region"; | ||
|
||
private static final Integer MAX_LENGTH_FOR_CONFIG_PROPERTY = 1000; | ||
@Override | ||
public DataSourceType getDataSourceType() { | ||
return DataSourceType.GLUE; | ||
} | ||
|
||
@Override | ||
public DataSource createDataSource(DataSourceMetadata metadata) throws URISyntaxException { | ||
validateGlueDataSourceConfiguration(metadata.getProperties()); | ||
return new DataSource(metadata.getName(), metadata.getConnector(), | ||
(dataSourceSchemaName, tableName) -> { | ||
throw new UnsupportedOperationException("Glue storage engine is not supported."); | ||
}); | ||
} | ||
|
||
private void validateGlueDataSourceConfiguration(Map<String, String> dataSourceMetadataConfig) | ||
throws URISyntaxException { | ||
validateMissingFields(dataSourceMetadataConfig, | ||
Set.of(GLUE_AUTH_TYPE, GLUE_ROLE_ARN, GLUE_REGION, GLUE_ACCOUNT_ID, | ||
FLINT_INTEGRATION, FLINT_HOST, FLINT_PORT, FLINT_REGION, FLINT_SCHEME, FLINT_AUTH)); | ||
validateFlintHost(dataSourceMetadataConfig); | ||
} | ||
|
||
|
||
private void validateMissingFields(Map<String, String> config, Set<String> fields) { | ||
Set<String> missingFields = new HashSet<>(); | ||
Set<String> invalidLengthFields = new HashSet<>(); | ||
for (String field : fields) { | ||
if (!config.containsKey(field)) { | ||
missingFields.add(field); | ||
} else if (config.get(field).length() > MAX_LENGTH_FOR_CONFIG_PROPERTY) { | ||
invalidLengthFields.add(field); | ||
} | ||
} | ||
StringBuilder errorStringBuilder = new StringBuilder(); | ||
if (missingFields.size() > 0) { | ||
errorStringBuilder.append( | ||
String.format( | ||
"Missing %s fields in the Prometheus connector properties.", missingFields)); | ||
} | ||
|
||
if (invalidLengthFields.size() > 0) { | ||
errorStringBuilder.append( | ||
String.format("Fields %s exceeds more than 1000 characters.", invalidLengthFields)); | ||
} | ||
if (errorStringBuilder.length() > 0) { | ||
throw new IllegalArgumentException(errorStringBuilder.toString()); | ||
} | ||
} | ||
|
||
private void validateFlintHost(Map<String, String> config) throws URISyntaxException { | ||
URI uri = new URI(config.get(FLINT_HOST)); | ||
String host = uri.getHost(); | ||
if (host == null | ||
|| (!(DomainValidator.getInstance().isValid(host) | ||
|| DomainValidator.getInstance().isValidLocalTld(host)))) { | ||
throw new IllegalArgumentException( | ||
String.format("Invalid hostname in the uri: %s", config.get(FLINT_HOST))); | ||
} else { | ||
Pattern allowHostsPattern = | ||
Pattern.compile(pluginSettings.getSettingValue(Settings.Key.DATASOURCES_URI_ALLOWHOSTS)); | ||
Matcher matcher = allowHostsPattern.matcher(host); | ||
if (!matcher.matches()) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"Disallowed hostname in the uri. Validate with %s config", | ||
Settings.Key.DATASOURCES_URI_ALLOWHOSTS.getKeyValue())); | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...sources/src/main/java/org/opensearch/sql/datasources/utils/DataSourceValidationUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.opensearch.sql.datasources.utils; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import lombok.experimental.UtilityClass; | ||
import org.apache.commons.validator.routines.DomainValidator; | ||
|
||
@UtilityClass | ||
public class DataSourceValidationUtils { | ||
|
||
public static void validateHost() { | ||
|
||
} | ||
|
||
public static void validateMissingFields(String uriString) throws URISyntaxException { | ||
URI uri = new URI(uriString); | ||
String host = uri.getHost(); | ||
if (host == null | ||
|| (!(DomainValidator.getInstance().isValid(host) | ||
|| DomainValidator.getInstance().isValidLocalTld(host)))) { | ||
throw new IllegalArgumentException( | ||
String.format("Invalid hostname in the uri: %s", config.get(FLINT_HOST))); | ||
} else { | ||
Pattern allowHostsPattern = | ||
Pattern.compile(pluginSettings.getSettingValue(Settings.Key.DATASOURCES_URI_ALLOWHOSTS)); | ||
Matcher matcher = allowHostsPattern.matcher(host); | ||
if (!matcher.matches()) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"Disallowed hostname in the uri. Validate with %s config", | ||
Settings.Key.DATASOURCES_URI_ALLOWHOSTS.getKeyValue())); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters