diff --git a/mvnw.cmd b/mvnw.cmd new file mode 100644 index 0000000..92450f9 --- /dev/null +++ b/mvnw.cmd @@ -0,0 +1,189 @@ +<# : batch portion +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Apache Maven Wrapper startup batch script, version 3.3.4 +@REM +@REM Optional ENV vars +@REM MVNW_REPOURL - repo url base for downloading maven distribution +@REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +@REM MVNW_VERBOSE - true: enable verbose log; others: silence the output +@REM ---------------------------------------------------------------------------- + +@IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0) +@SET __MVNW_CMD__= +@SET __MVNW_ERROR__= +@SET __MVNW_PSMODULEP_SAVE=%PSModulePath% +@SET PSModulePath= +@FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @( + IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B) +) +@SET PSModulePath=%__MVNW_PSMODULEP_SAVE% +@SET __MVNW_PSMODULEP_SAVE= +@SET __MVNW_ARG0_NAME__= +@SET MVNW_USERNAME= +@SET MVNW_PASSWORD= +@IF NOT "%__MVNW_CMD__%"=="" ("%__MVNW_CMD__%" %*) +@echo Cannot start maven from wrapper >&2 && exit /b 1 +@GOTO :EOF +: end batch / begin powershell #> + +$ErrorActionPreference = "Stop" +if ($env:MVNW_VERBOSE -eq "true") { + $VerbosePreference = "Continue" +} + +# calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties +$distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl +if (!$distributionUrl) { + Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" +} + +switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { + "maven-mvnd-*" { + $USE_MVND = $true + $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip" + $MVN_CMD = "mvnd.cmd" + break + } + default { + $USE_MVND = $false + $MVN_CMD = $script -replace '^mvnw','mvn' + break + } +} + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +if ($env:MVNW_REPOURL) { + $MVNW_REPO_PATTERN = if ($USE_MVND -eq $False) { "/org/apache/maven/" } else { "/maven/mvnd/" } + $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace "^.*$MVNW_REPO_PATTERN",'')" +} +$distributionUrlName = $distributionUrl -replace '^.*/','' +$distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' + +$MAVEN_M2_PATH = "$HOME/.m2" +if ($env:MAVEN_USER_HOME) { + $MAVEN_M2_PATH = "$env:MAVEN_USER_HOME" +} + +if (-not (Test-Path -Path $MAVEN_M2_PATH)) { + New-Item -Path $MAVEN_M2_PATH -ItemType Directory | Out-Null +} + +$MAVEN_WRAPPER_DISTS = $null +if ((Get-Item $MAVEN_M2_PATH).Target[0] -eq $null) { + $MAVEN_WRAPPER_DISTS = "$MAVEN_M2_PATH/wrapper/dists" +} else { + $MAVEN_WRAPPER_DISTS = (Get-Item $MAVEN_M2_PATH).Target[0] + "/wrapper/dists" +} + +$MAVEN_HOME_PARENT = "$MAVEN_WRAPPER_DISTS/$distributionUrlNameMain" +$MAVEN_HOME_NAME = ([System.Security.Cryptography.SHA256]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' +$MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" + +if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { + Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME" + Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" + exit $? +} + +if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) { + Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl" +} + +# prepare tmp dir +$TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile +$TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir" +$TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null +trap { + if ($TMP_DOWNLOAD_DIR.Exists) { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } + } +} + +New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null + +# Download and Install Apache Maven +Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +Write-Verbose "Downloading from: $distributionUrl" +Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +$webclient = New-Object System.Net.WebClient +if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) { + $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD) +} +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 +$webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +$distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum +if ($distributionSha256Sum) { + if ($USE_MVND) { + Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." + } + Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash + if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) { + Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property." + } +} + +# unzip and move +Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null + +# Find the actual extracted directory name (handles snapshots where filename != directory name) +$actualDistributionDir = "" + +# First try the expected directory name (for regular distributions) +$expectedPath = Join-Path "$TMP_DOWNLOAD_DIR" "$distributionUrlNameMain" +$expectedMvnPath = Join-Path "$expectedPath" "bin/$MVN_CMD" +if ((Test-Path -Path $expectedPath -PathType Container) -and (Test-Path -Path $expectedMvnPath -PathType Leaf)) { + $actualDistributionDir = $distributionUrlNameMain +} + +# If not found, search for any directory with the Maven executable (for snapshots) +if (!$actualDistributionDir) { + Get-ChildItem -Path "$TMP_DOWNLOAD_DIR" -Directory | ForEach-Object { + $testPath = Join-Path $_.FullName "bin/$MVN_CMD" + if (Test-Path -Path $testPath -PathType Leaf) { + $actualDistributionDir = $_.Name + } + } +} + +if (!$actualDistributionDir) { + Write-Error "Could not find Maven distribution directory in extracted archive" +} + +Write-Verbose "Found extracted Maven distribution directory: $actualDistributionDir" +Rename-Item -Path "$TMP_DOWNLOAD_DIR/$actualDistributionDir" -NewName $MAVEN_HOME_NAME | Out-Null +try { + Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null +} catch { + if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) { + Write-Error "fail to move MAVEN_HOME" + } +} finally { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } +} + +Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" diff --git a/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchAuthMode.java b/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchAuthMode.java new file mode 100644 index 0000000..32c060a --- /dev/null +++ b/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchAuthMode.java @@ -0,0 +1,39 @@ +/* + * Copyright 2024 Kingsrook, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.kingsrook.qbits.quicksearch; + + +/******************************************************************************* + ** How the QBit authenticates to OpenSearch. + ** + ** NONE - no credentials (local development, security plugin disabled). + ** BASIC - HTTP basic authentication (security plugin internal users, or + ** Amazon OpenSearch Service fine-grained access control with an + ** internal-database master user). + ** AWS_SIGV4 - AWS Signature Version 4 request signing with the default AWS + ** credential chain (Amazon OpenSearch Service domains that use + ** IAM). Requires the optional AWS SDK v2 dependencies. + ** + ** Bearer tokens, API keys and client certificates are added through an + ** {@link com.kingsrook.qbits.quicksearch.opensearch.OpenSearchTransportCustomizer}. + *******************************************************************************/ +public enum QuickSearchAuthMode +{ + NONE, + BASIC, + AWS_SIGV4 +} diff --git a/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchQBitConfig.java b/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchQBitConfig.java index c0e2370..253b9fe 100644 --- a/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchQBitConfig.java +++ b/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchQBitConfig.java @@ -13,13 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.kingsrook.qbits.quicksearch; +import java.net.URI; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import java.util.regex.Pattern; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.kingsrook.qqq.backend.core.model.metadata.QInstance; +import com.kingsrook.qqq.backend.core.model.metadata.code.QCodeReference; +import com.kingsrook.qqq.backend.core.model.metadata.permissions.PermissionLevel; +import com.kingsrook.qqq.backend.core.model.metadata.permissions.QPermissionRules; +import com.kingsrook.qqq.backend.core.model.metadata.producers.MetaDataCustomizerInterface; import com.kingsrook.qqq.backend.core.model.metadata.qbits.QBitConfig; +import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData; import com.kingsrook.qqq.backend.core.utils.CollectionUtils; import com.kingsrook.qqq.backend.core.utils.StringUtils; import com.kingsrook.qbits.quicksearch.model.QuickSearchIndex; @@ -28,58 +39,139 @@ import com.kingsrook.qbits.quicksearch.publisher.IndexEventPublisher; /******************************************************************************* - ** Configuration for the Quick Search QBit. + ** Host-facing configuration for the Quick Search QBit. ** - ** Required fields: backendName, opensearchHost, opensearchPort, - ** opensearchIndexName. At least one of searchableEntityClasses or - ** searchableTables must also be provided. + ** Connection: opensearchUrl (preferred) or opensearchHost/opensearchPort/useSsl. + ** Authentication: authMode NONE, BASIC (opensearchUsername/opensearchPassword) + ** or AWS_SIGV4 (awsRegion, awsServiceName, awsAssumeRoleArn). Secret-bearing + ** values may be ${env.X} / ${prop.X} references, resolved only when the + ** transport is built. TLS trust lives in tls. Timeouts, pool size and a + ** transportCustomizer complete the transport settings. ** - ** Optional: opensearchUsername and opensearchPassword (must be set together), - ** tableNamePrefix, useSsl (default false), enableScheduledProcesses (default - ** true), enableRealTimeIndexing (default true), defaultBasepullIntervalMinutes - ** (default 5), bulkBatchSize (default 500), sourceBatchSize (default 1000), - ** searchableTables (config-driven table definitions for tables from external - ** QBit jars that do not have @QuickSearchable annotations). + ** Behaviour: startupMode, enableRealTimeIndexing, enableBasepullProcess, + ** enableMaintenanceProcesses, schedulerName plus basepull/reconcile schedules, + ** permission rules for the admin surface, search bounds, and indexing limits. ** + ** Tables: searchableEntityClasses (annotated entities) and/or searchableTables + ** (config-driven). validate() is list-based and never throws; the producer + ** fails production when it reports errors. *******************************************************************************/ public class QuickSearchQBitConfig implements QBitConfig { - private String backendName; - private String tableNamePrefix; - private String opensearchHost; - private Integer opensearchPort; - private String opensearchIndexName; - private String opensearchUsername; - private String opensearchPassword; - private Boolean useSsl = false; - private Boolean enableScheduledProcesses = true; - private Boolean enableRealTimeIndexing = true; - private Integer defaultBasepullIntervalMinutes = 5; - private Integer bulkBatchSize = 500; - private Integer sourceBatchSize = 1000; - private List> searchableEntityClasses; + public static final String DEFAULT_ADMIN_PERMISSION_BASE_NAME = "quickSearchAdmin"; + + private static final Pattern INDEX_NAME_PATTERN = Pattern.compile("^[a-z0-9][a-z0-9_+.-]*$"); + private static final Set AWS_SERVICE_NAMES = Set.of("es", "aoss"); + + private String backendName; + private String tableNamePrefix; + private String opensearchUrl; + private String opensearchHost; + private Integer opensearchPort; + private Boolean useSsl = false; + private String opensearchIndexName; + private QuickSearchAuthMode authMode; + private String opensearchUsername; + private String opensearchPassword; + private String awsRegion; + private String awsServiceName = "es"; + private String awsAssumeRoleArn; + private Boolean allowPlaintextCredentials = false; + private QuickSearchTlsConfig tls; + private Integer connectTimeoutMillis = 5000; + private Integer responseTimeoutMillis = 60000; + private Integer maxConnections = 30; + private Integer maxBulkRequestBytes = 5 * 1024 * 1024; + private QCodeReference transportCustomizer; + private QuickSearchStartupMode startupMode = QuickSearchStartupMode.FAIL_FAST; + private Boolean enableRealTimeIndexing = true; + private Boolean enableBasepullProcess = true; + private Boolean enableMaintenanceProcesses = true; + private String schedulerName; + private Integer basepullRepeatSeconds = 300; + private String reconcileCronExpression; + private String reconcileCronTimeZoneId; + private Integer basepullOverlapSeconds = 300; + private Integer defaultBasepullIntervalMinutes = 5; + private Integer runHistoryRetentionDays = 30; + private Integer bulkBatchSize = 500; + private Integer sourceBatchSize = 1000; + private Integer maxFieldLength = 10000; + private Integer maxSearchLimit = 100; + private Boolean applyRecordSecurityLocks = true; + private List> searchableEntityClasses; private List searchableTables; - private IndexEventPublisher indexEventPublisher; + private transient IndexEventPublisher indexEventPublisher; + private transient QPermissionRules adminPermissionRules = new QPermissionRules().withLevel(PermissionLevel.HAS_ACCESS_PERMISSION).withPermissionBaseName(DEFAULT_ADMIN_PERMISSION_BASE_NAME); + private transient QPermissionRules tablePermissionRules = new QPermissionRules().withLevel(PermissionLevel.READ_WRITE_PERMISSIONS); + private transient MetaDataCustomizerInterface tableMetaDataCustomizer; + private transient QuickSearchRuntime runtime; - /*************************************************************************** - ** Validate configuration before QBit is produced. - ** - ** Collects all validation errors into the provided errors list. - ***************************************************************************/ + + /******************************************************************************* + ** Collect configuration errors. Never throws; null-safe for a partially + ** built QInstance. + *******************************************************************************/ @Override public void validate(QInstance qInstance, List errors) + { + validateBackend(qInstance, errors); + validateConnection(errors); + validateAuth(errors); + validateTls(errors); + validateIndexName(errors); + validateTables(qInstance, errors); + validateNumbers(errors); + validateScheduling(qInstance, errors); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + private void validateBackend(QInstance qInstance, List errors) { if(!StringUtils.hasContent(backendName)) { errors.add("backendName is required for QuickSearchQBit"); } - else if(qInstance.getBackend(backendName) == null) + else if(qInstance != null && qInstance.getBackends() != null && qInstance.getBackend(backendName) == null) { errors.add("Backend not found: " + backendName); } + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + private void validateConnection(List errors) + { + if(StringUtils.hasContent(opensearchUrl)) + { + try + { + URI uri = new URI(opensearchUrl.trim()); + if(uri.getScheme() == null || !(uri.getScheme().equalsIgnoreCase("http") || uri.getScheme().equalsIgnoreCase("https"))) + { + errors.add("opensearchUrl must start with http:// or https://"); + } + if(!StringUtils.hasContent(uri.getHost())) + { + errors.add("opensearchUrl must include a host"); + } + } + catch(Exception e) + { + errors.add("opensearchUrl is not a valid URL; expected http(s)://host[:port]"); + } + return; + } if(!StringUtils.hasContent(opensearchHost)) { @@ -94,24 +186,118 @@ public class QuickSearchQBitConfig implements QBitConfig { errors.add("opensearchPort must be positive for QuickSearchQBit"); } + } + + + /******************************************************************************* + ** + *******************************************************************************/ + private void validateAuth(List errors) + { + boolean hasUsername = StringUtils.hasContent(opensearchUsername); + boolean hasPassword = StringUtils.hasContent(opensearchPassword); + + QuickSearchAuthMode effectiveAuthMode = getEffectiveAuthMode(); + switch(effectiveAuthMode) + { + case BASIC -> + { + if(hasUsername && !hasPassword) + { + errors.add("opensearchPassword is required when opensearchUsername is set"); + } + else if(!hasUsername && hasPassword) + { + errors.add("opensearchUsername is required when opensearchPassword is set"); + } + else if(!hasUsername) + { + errors.add("opensearchUsername and opensearchPassword are required for authMode BASIC"); + } + + if("http".equals(getEffectiveScheme()) && !Boolean.TRUE.equals(allowPlaintextCredentials)) + { + errors.add("BASIC credentials over plain http are refused; use https or set allowPlaintextCredentials=true for development"); + } + } + case AWS_SIGV4 -> + { + if(hasUsername || hasPassword) + { + errors.add("opensearchUsername and opensearchPassword are not used with authMode AWS_SIGV4"); + } + if(!StringUtils.hasContent(getEffectiveAwsRegion())) + { + errors.add("awsRegion is required for authMode AWS_SIGV4 (or set AWS_REGION in the environment)"); + } + if(!AWS_SERVICE_NAMES.contains(getEffectiveAwsServiceName())) + { + errors.add("awsServiceName must be es or aoss for authMode AWS_SIGV4"); + } + if(!"https".equals(getEffectiveScheme())) + { + errors.add("authMode AWS_SIGV4 requires an https connection"); + } + } + case NONE -> + { + if(hasUsername || hasPassword) + { + errors.add("opensearchUsername and opensearchPassword are set but authMode is NONE"); + } + } + } + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + private void validateTls(List errors) + { + if(tls == null) + { + return; + } + + if(Boolean.TRUE.equals(tls.getInsecureSkipVerify()) && !isLoopbackHost(getEffectiveHost()) && !Boolean.TRUE.equals(tls.getAllowInsecureInProduction())) + { + errors.add("tls.insecureSkipVerify is only allowed for loopback hosts unless tls.allowInsecureInProduction=true"); + } + + if(StringUtils.hasContent(tls.getCaCertificatePath()) && StringUtils.hasContent(tls.getTrustStorePath())) + { + errors.add("Set either tls.caCertificatePath or tls.trustStorePath, not both"); + } + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + private void validateIndexName(List errors) + { if(!StringUtils.hasContent(opensearchIndexName)) { errors.add("opensearchIndexName is required for QuickSearchQBit"); } - - boolean hasUsername = StringUtils.hasContent(opensearchUsername); - boolean hasPassword = StringUtils.hasContent(opensearchPassword); - if(hasUsername && !hasPassword) + else if(!isValidIndexName(opensearchIndexName)) { - errors.add("opensearchPassword is required when opensearchUsername is set"); - } - else if(!hasUsername && hasPassword) - { - errors.add("opensearchUsername is required when opensearchPassword is set"); + errors.add("opensearchIndexName must be lowercase, up to 255 characters, contain only letters, digits, _ - + ., and not start with _ - or +"); } + } - boolean hasEntityClasses = !CollectionUtils.nullSafeIsEmpty(searchableEntityClasses); + + + /******************************************************************************* + ** + *******************************************************************************/ + private void validateTables(QInstance qInstance, List errors) + { + boolean hasEntityClasses = !CollectionUtils.nullSafeIsEmpty(searchableEntityClasses); boolean hasSearchableTables = !CollectionUtils.nullSafeIsEmpty(searchableTables); if(!hasEntityClasses && !hasSearchableTables) @@ -119,53 +305,278 @@ public class QuickSearchQBitConfig implements QBitConfig errors.add("At least one of searchableEntityClasses or searchableTables is required for QuickSearchQBit"); } - if(hasSearchableTables) + if(!hasSearchableTables) { - java.util.Set seenTableNames = new java.util.HashSet<>(); - for(SearchableTableConfig tableConfig : searchableTables) + return; + } + + Set seenTableNames = new HashSet<>(); + for(SearchableTableConfig tableConfig : searchableTables) + { + String tableName = tableConfig.getTableName(); + if(!StringUtils.hasContent(tableName)) { - if(!StringUtils.hasContent(tableConfig.getTableName())) - { - errors.add("tableName is required on each SearchableTableConfig for QuickSearchQBit"); - } - else if(!seenTableNames.add(tableConfig.getTableName())) - { - errors.add("Duplicate tableName in searchableTables: " + tableConfig.getTableName()); - } - - if(CollectionUtils.nullSafeIsEmpty(tableConfig.getFields())) - { - errors.add("fields is required and must be non-empty on SearchableTableConfig" - + (StringUtils.hasContent(tableConfig.getTableName()) ? " [" + tableConfig.getTableName() + "]" : "") - + " for QuickSearchQBit"); - } + errors.add("tableName is required on each SearchableTableConfig for QuickSearchQBit"); + } + else if(!seenTableNames.add(tableName)) + { + errors.add("Duplicate tableName in searchableTables: " + tableName); + } + else if(tableName.contains(":")) + { + errors.add("tableName must not contain ':' (it is part of the document id): " + tableName); } - } - if(bulkBatchSize != null && bulkBatchSize <= 0) - { - errors.add("bulkBatchSize must be positive for QuickSearchQBit"); - } + if(CollectionUtils.nullSafeIsEmpty(tableConfig.getFields())) + { + errors.add("fields is required and must be non-empty on SearchableTableConfig" + + (StringUtils.hasContent(tableName) ? " [" + tableName + "]" : "") + + " for QuickSearchQBit"); + } - if(sourceBatchSize != null && sourceBatchSize <= 0) - { - errors.add("sourceBatchSize must be positive for QuickSearchQBit"); - } - - if(defaultBasepullIntervalMinutes != null && defaultBasepullIntervalMinutes <= 0) - { - errors.add("defaultBasepullIntervalMinutes must be positive for QuickSearchQBit"); + QTableMetaData table = (qInstance == null || tableName == null) ? null : qInstance.getTable(tableName); + if(table != null) + { + validateFieldsExist(table, tableConfig, errors); + } } } - /*************************************************************************** - ** Apply table name prefix if configured. + /******************************************************************************* + ** Check that configured fields exist on a table that is already in the + ** instance. Tables added after this QBit is produced are checked by the + ** validator plugin. + *******************************************************************************/ + public static void validateFieldsExist(QTableMetaData table, SearchableTableConfig tableConfig, List errors) + { + for(SearchableFieldConfig field : CollectionUtils.nonNullList(tableConfig.getFields())) + { + if(field.getFieldName() != null && !table.getFields().containsKey(field.getFieldName())) + { + errors.add("Searchable field [" + field.getFieldName() + "] does not exist on table [" + table.getName() + "]"); + } + } + + String timestampField = tableConfig.getBasepullTimestampField(); + if(timestampField != null && !table.getFields().containsKey(timestampField)) + { + errors.add("basepullTimestampField [" + timestampField + "] does not exist on table [" + table.getName() + "]; set it to null to disable incremental basepull for that table"); + } + } + + + + /******************************************************************************* ** - ** If tableNamePrefix is non-null and non-empty, returns prefix + name. - ** Otherwise returns name unchanged. - ***************************************************************************/ + *******************************************************************************/ + private void validateNumbers(List errors) + { + requirePositive(bulkBatchSize, "bulkBatchSize", errors); + requirePositive(sourceBatchSize, "sourceBatchSize", errors); + requirePositive(defaultBasepullIntervalMinutes, "defaultBasepullIntervalMinutes", errors); + requirePositive(connectTimeoutMillis, "connectTimeoutMillis", errors); + requirePositive(responseTimeoutMillis, "responseTimeoutMillis", errors); + requirePositive(maxConnections, "maxConnections", errors); + requirePositive(maxBulkRequestBytes, "maxBulkRequestBytes", errors); + requirePositive(basepullRepeatSeconds, "basepullRepeatSeconds", errors); + requirePositive(maxSearchLimit, "maxSearchLimit", errors); + requirePositive(maxFieldLength, "maxFieldLength", errors); + requirePositive(runHistoryRetentionDays, "runHistoryRetentionDays", errors); + + if(basepullOverlapSeconds != null && basepullOverlapSeconds < 0) + { + errors.add("basepullOverlapSeconds must not be negative for QuickSearchQBit"); + } + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + private static void requirePositive(Integer value, String name, List errors) + { + if(value != null && value <= 0) + { + errors.add(name + " must be positive for QuickSearchQBit"); + } + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + private void validateScheduling(QInstance qInstance, List errors) + { + if(StringUtils.hasContent(schedulerName) && qInstance != null && !CollectionUtils.nullSafeIsEmpty(qInstance.getSchedulers()) && qInstance.getScheduler(schedulerName) == null) + { + errors.add("Scheduler not found: " + schedulerName); + } + + if(StringUtils.hasContent(reconcileCronExpression) && !StringUtils.hasContent(schedulerName)) + { + errors.add("reconcileCronExpression requires schedulerName"); + } + } + + + + /******************************************************************************* + ** OpenSearch index and alias naming rules. + *******************************************************************************/ + public static boolean isValidIndexName(String name) + { + if(name == null || name.isEmpty() || name.length() > 255 || name.equals(".") || name.equals("..")) + { + return (false); + } + return (INDEX_NAME_PATTERN.matcher(name).matches()); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + private static boolean isLoopbackHost(String host) + { + if(host == null) + { + return (false); + } + String lower = host.toLowerCase(); + return (lower.equals("localhost") || lower.equals("127.0.0.1") || lower.equals("::1") || lower.equals("[::1]") || lower.startsWith("127.")); + } + + + + /******************************************************************************* + ** The auth mode in effect: the configured one, else BASIC when credentials + ** are present, else NONE. + *******************************************************************************/ + @JsonIgnore + public QuickSearchAuthMode getEffectiveAuthMode() + { + if(authMode != null) + { + return (authMode); + } + return (StringUtils.hasContent(opensearchUsername) || StringUtils.hasContent(opensearchPassword) ? QuickSearchAuthMode.BASIC : QuickSearchAuthMode.NONE); + } + + + + /******************************************************************************* + ** http or https, from opensearchUrl or useSsl. + *******************************************************************************/ + @JsonIgnore + public String getEffectiveScheme() + { + URI uri = parseUrlQuietly(); + if(uri != null && uri.getScheme() != null) + { + return (uri.getScheme().toLowerCase()); + } + return (Boolean.TRUE.equals(useSsl) ? "https" : "http"); + } + + + + /******************************************************************************* + ** Host from opensearchUrl or opensearchHost. + *******************************************************************************/ + @JsonIgnore + public String getEffectiveHost() + { + URI uri = parseUrlQuietly(); + if(uri != null && StringUtils.hasContent(uri.getHost())) + { + return (uri.getHost()); + } + return (opensearchHost); + } + + + + /******************************************************************************* + ** Port from opensearchUrl (443 for https, 9200 for http when absent) or + ** opensearchPort. + *******************************************************************************/ + @JsonIgnore + public Integer getEffectivePort() + { + URI uri = parseUrlQuietly(); + if(uri != null && StringUtils.hasContent(uri.getHost())) + { + if(uri.getPort() > 0) + { + return (uri.getPort()); + } + return ("https".equalsIgnoreCase(uri.getScheme()) ? 443 : 9200); + } + return (opensearchPort); + } + + + + /******************************************************************************* + ** awsRegion, else AWS_REGION, else AWS_DEFAULT_REGION from the environment. + *******************************************************************************/ + @JsonIgnore + public String getEffectiveAwsRegion() + { + if(StringUtils.hasContent(awsRegion)) + { + return (awsRegion); + } + String fromEnv = System.getenv("AWS_REGION"); + if(!StringUtils.hasContent(fromEnv)) + { + fromEnv = System.getenv("AWS_DEFAULT_REGION"); + } + return (fromEnv); + } + + + + /******************************************************************************* + ** awsServiceName, defaulting to es. + *******************************************************************************/ + @JsonIgnore + public String getEffectiveAwsServiceName() + { + return (StringUtils.hasContent(awsServiceName) ? awsServiceName.toLowerCase() : "es"); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + private URI parseUrlQuietly() + { + if(!StringUtils.hasContent(opensearchUrl)) + { + return (null); + } + try + { + return (new URI(opensearchUrl.trim())); + } + catch(Exception e) + { + return (null); + } + } + + + + /******************************************************************************* + ** Apply the table-name prefix, if any. + *******************************************************************************/ public String applyPrefix(String name) { if(StringUtils.hasContent(tableNamePrefix)) @@ -177,9 +588,9 @@ public class QuickSearchQBitConfig implements QBitConfig - /*************************************************************************** - ** Return the prefixed table name for the QuickSearchIndex table. - ***************************************************************************/ + /******************************************************************************* + ** Name of the produced quickSearchIndex table. + *******************************************************************************/ public String getQuickSearchIndexTableName() { return applyPrefix(QuickSearchIndex.TABLE_NAME); @@ -187,9 +598,9 @@ public class QuickSearchQBitConfig implements QBitConfig - /*************************************************************************** - ** Return the prefixed table name for the QuickSearchIndexRun table. - ***************************************************************************/ + /******************************************************************************* + ** Name of the produced quickSearchIndexRun table. + *******************************************************************************/ public String getQuickSearchIndexRunTableName() { return applyPrefix(QuickSearchIndexRun.TABLE_NAME); @@ -197,505 +608,96 @@ public class QuickSearchQBitConfig implements QBitConfig - /*************************************************************************** - ** Getter for backendName - ***************************************************************************/ - public String getBackendName() + /******************************************************************************* + ** QBitConfig hook: tables this QBit produces default to backendName. + *******************************************************************************/ + @Override + public String getDefaultBackendNameForTables() { - return (this.backendName); + return (backendName); } - /*************************************************************************** - ** Setter for backendName - ***************************************************************************/ - public void setBackendName(String backendName) + /******************************************************************************* + ** QBitConfig hook: host-supplied customizer for produced tables. + *******************************************************************************/ + @Override + public MetaDataCustomizerInterface getTableMetaDataCustomizer() { - this.backendName = backendName; + return (tableMetaDataCustomizer); } - /*************************************************************************** - ** Fluent setter for backendName - ***************************************************************************/ - public QuickSearchQBitConfig withBackendName(String backendName) + /******************************************************************************* + ** Setter for tableMetaDataCustomizer + *******************************************************************************/ + public void setTableMetaDataCustomizer(MetaDataCustomizerInterface tableMetaDataCustomizer) { - this.backendName = backendName; + this.tableMetaDataCustomizer = tableMetaDataCustomizer; + } + + + + /******************************************************************************* + ** Fluent setter for tableMetaDataCustomizer + *******************************************************************************/ + public QuickSearchQBitConfig withTableMetaDataCustomizer(MetaDataCustomizerInterface tableMetaDataCustomizer) + { + this.tableMetaDataCustomizer = tableMetaDataCustomizer; return (this); } - /*************************************************************************** - ** Getter for tableNamePrefix - ***************************************************************************/ - public String getTableNamePrefix() - { - return (this.tableNamePrefix); - } - - - - /*************************************************************************** - ** Setter for tableNamePrefix - ***************************************************************************/ - public void setTableNamePrefix(String tableNamePrefix) - { - this.tableNamePrefix = tableNamePrefix; - } - - - - /*************************************************************************** - ** Fluent setter for tableNamePrefix - ***************************************************************************/ - public QuickSearchQBitConfig withTableNamePrefix(String tableNamePrefix) - { - this.tableNamePrefix = tableNamePrefix; - return (this); - } - - - - /*************************************************************************** - ** Getter for opensearchHost - ***************************************************************************/ - public String getOpensearchHost() - { - return (this.opensearchHost); - } - - - - /*************************************************************************** - ** Setter for opensearchHost - ***************************************************************************/ - public void setOpensearchHost(String opensearchHost) - { - this.opensearchHost = opensearchHost; - } - - - - /*************************************************************************** - ** Fluent setter for opensearchHost - ***************************************************************************/ - public QuickSearchQBitConfig withOpensearchHost(String opensearchHost) - { - this.opensearchHost = opensearchHost; - return (this); - } - - - - /*************************************************************************** - ** Getter for opensearchPort - ***************************************************************************/ - public Integer getOpensearchPort() - { - return (this.opensearchPort); - } - - - - /*************************************************************************** - ** Setter for opensearchPort - ***************************************************************************/ - public void setOpensearchPort(Integer opensearchPort) - { - this.opensearchPort = opensearchPort; - } - - - - /*************************************************************************** - ** Fluent setter for opensearchPort - ***************************************************************************/ - public QuickSearchQBitConfig withOpensearchPort(Integer opensearchPort) - { - this.opensearchPort = opensearchPort; - return (this); - } - - - - /*************************************************************************** - ** Getter for opensearchIndexName - ***************************************************************************/ - public String getOpensearchIndexName() - { - return (this.opensearchIndexName); - } - - - - /*************************************************************************** - ** Setter for opensearchIndexName - ***************************************************************************/ - public void setOpensearchIndexName(String opensearchIndexName) - { - this.opensearchIndexName = opensearchIndexName; - } - - - - /*************************************************************************** - ** Fluent setter for opensearchIndexName - ***************************************************************************/ - public QuickSearchQBitConfig withOpensearchIndexName(String opensearchIndexName) - { - this.opensearchIndexName = opensearchIndexName; - return (this); - } - - - - /*************************************************************************** - ** Getter for opensearchUsername - ***************************************************************************/ - public String getOpensearchUsername() - { - return (this.opensearchUsername); - } - - - - /*************************************************************************** - ** Setter for opensearchUsername - ***************************************************************************/ - public void setOpensearchUsername(String opensearchUsername) - { - this.opensearchUsername = opensearchUsername; - } - - - - /*************************************************************************** - ** Fluent setter for opensearchUsername - ***************************************************************************/ - public QuickSearchQBitConfig withOpensearchUsername(String opensearchUsername) - { - this.opensearchUsername = opensearchUsername; - return (this); - } - - - - /*************************************************************************** - ** Getter for opensearchPassword - ***************************************************************************/ - public String getOpensearchPassword() - { - return (this.opensearchPassword); - } - - - - /*************************************************************************** - ** Setter for opensearchPassword - ***************************************************************************/ - public void setOpensearchPassword(String opensearchPassword) - { - this.opensearchPassword = opensearchPassword; - } - - - - /*************************************************************************** - ** Fluent setter for opensearchPassword - ***************************************************************************/ - public QuickSearchQBitConfig withOpensearchPassword(String opensearchPassword) - { - this.opensearchPassword = opensearchPassword; - return (this); - } - - - - /*************************************************************************** - ** Getter for useSsl - ***************************************************************************/ - public Boolean getUseSsl() - { - return (this.useSsl); - } - - - - /*************************************************************************** - ** Setter for useSsl - ***************************************************************************/ - public void setUseSsl(Boolean useSsl) - { - this.useSsl = useSsl; - } - - - - /*************************************************************************** - ** Fluent setter for useSsl - ***************************************************************************/ - public QuickSearchQBitConfig withUseSsl(Boolean useSsl) - { - this.useSsl = useSsl; - return (this); - } - - - - /*************************************************************************** - ** Getter for enableScheduledProcesses - ***************************************************************************/ + /******************************************************************************* + ** Deprecated alias: true when both enableBasepullProcess and + ** enableMaintenanceProcesses are true. + ** + ** @deprecated use enableBasepullProcess and enableMaintenanceProcesses + *******************************************************************************/ + @Deprecated public Boolean getEnableScheduledProcesses() { - return (this.enableScheduledProcesses); + return (Boolean.TRUE.equals(enableBasepullProcess) && Boolean.TRUE.equals(enableMaintenanceProcesses)); } - /*************************************************************************** - ** Setter for enableScheduledProcesses - ***************************************************************************/ + /******************************************************************************* + ** Deprecated alias: sets both enableBasepullProcess and + ** enableMaintenanceProcesses. + ** + ** @deprecated use enableBasepullProcess and enableMaintenanceProcesses + *******************************************************************************/ + @Deprecated public void setEnableScheduledProcesses(Boolean enableScheduledProcesses) { - this.enableScheduledProcesses = enableScheduledProcesses; + this.enableBasepullProcess = enableScheduledProcesses; + this.enableMaintenanceProcesses = enableScheduledProcesses; } - /*************************************************************************** - ** Fluent setter for enableScheduledProcesses - ***************************************************************************/ + /******************************************************************************* + ** Deprecated alias: sets both enableBasepullProcess and + ** enableMaintenanceProcesses. + ** + ** @deprecated use enableBasepullProcess and enableMaintenanceProcesses + *******************************************************************************/ + @Deprecated public QuickSearchQBitConfig withEnableScheduledProcesses(Boolean enableScheduledProcesses) { - this.enableScheduledProcesses = enableScheduledProcesses; + setEnableScheduledProcesses(enableScheduledProcesses); return (this); } - /*************************************************************************** - ** Getter for enableRealTimeIndexing - ***************************************************************************/ - public Boolean getEnableRealTimeIndexing() - { - return (this.enableRealTimeIndexing); - } - - - - /*************************************************************************** - ** Setter for enableRealTimeIndexing - ***************************************************************************/ - public void setEnableRealTimeIndexing(Boolean enableRealTimeIndexing) - { - this.enableRealTimeIndexing = enableRealTimeIndexing; - } - - - - /*************************************************************************** - ** Fluent setter for enableRealTimeIndexing - ***************************************************************************/ - public QuickSearchQBitConfig withEnableRealTimeIndexing(Boolean enableRealTimeIndexing) - { - this.enableRealTimeIndexing = enableRealTimeIndexing; - return (this); - } - - - - /*************************************************************************** - ** Getter for defaultBasepullIntervalMinutes - ***************************************************************************/ - public Integer getDefaultBasepullIntervalMinutes() - { - return (this.defaultBasepullIntervalMinutes); - } - - - - /*************************************************************************** - ** Setter for defaultBasepullIntervalMinutes - ***************************************************************************/ - public void setDefaultBasepullIntervalMinutes(Integer defaultBasepullIntervalMinutes) - { - this.defaultBasepullIntervalMinutes = defaultBasepullIntervalMinutes; - } - - - - /*************************************************************************** - ** Fluent setter for defaultBasepullIntervalMinutes - ***************************************************************************/ - public QuickSearchQBitConfig withDefaultBasepullIntervalMinutes(Integer defaultBasepullIntervalMinutes) - { - this.defaultBasepullIntervalMinutes = defaultBasepullIntervalMinutes; - return (this); - } - - - - /*************************************************************************** - ** Getter for bulkBatchSize - ***************************************************************************/ - public Integer getBulkBatchSize() - { - return (this.bulkBatchSize); - } - - - - /*************************************************************************** - ** Setter for bulkBatchSize - ***************************************************************************/ - public void setBulkBatchSize(Integer bulkBatchSize) - { - this.bulkBatchSize = bulkBatchSize; - } - - - - /*************************************************************************** - ** Fluent setter for bulkBatchSize - ***************************************************************************/ - public QuickSearchQBitConfig withBulkBatchSize(Integer bulkBatchSize) - { - this.bulkBatchSize = bulkBatchSize; - return (this); - } - - - - /*************************************************************************** - ** Getter for sourceBatchSize - ***************************************************************************/ - public Integer getSourceBatchSize() - { - return (this.sourceBatchSize); - } - - - - /*************************************************************************** - ** Setter for sourceBatchSize - ***************************************************************************/ - public void setSourceBatchSize(Integer sourceBatchSize) - { - this.sourceBatchSize = sourceBatchSize; - } - - - - /*************************************************************************** - ** Fluent setter for sourceBatchSize - ***************************************************************************/ - public QuickSearchQBitConfig withSourceBatchSize(Integer sourceBatchSize) - { - this.sourceBatchSize = sourceBatchSize; - return (this); - } - - - - /*************************************************************************** - ** Getter for searchableEntityClasses - ***************************************************************************/ - public List> getSearchableEntityClasses() - { - return (this.searchableEntityClasses); - } - - - - /*************************************************************************** - ** Setter for searchableEntityClasses - ***************************************************************************/ - public void setSearchableEntityClasses(List> searchableEntityClasses) - { - this.searchableEntityClasses = searchableEntityClasses; - } - - - - /*************************************************************************** - ** Fluent setter for searchableEntityClasses - ***************************************************************************/ - public QuickSearchQBitConfig withSearchableEntityClasses(List> searchableEntityClasses) - { - this.searchableEntityClasses = searchableEntityClasses; - return (this); - } - - - - /*************************************************************************** - ** Getter for indexEventPublisher - ***************************************************************************/ - public IndexEventPublisher getIndexEventPublisher() - { - return (this.indexEventPublisher); - } - - - - /*************************************************************************** - ** Setter for indexEventPublisher - ***************************************************************************/ - public void setIndexEventPublisher(IndexEventPublisher indexEventPublisher) - { - this.indexEventPublisher = indexEventPublisher; - } - - - - /*************************************************************************** - ** Fluent setter for indexEventPublisher - ***************************************************************************/ - public QuickSearchQBitConfig withIndexEventPublisher(IndexEventPublisher indexEventPublisher) - { - this.indexEventPublisher = indexEventPublisher; - return (this); - } - - - - /*************************************************************************** - ** Getter for searchableTables - ***************************************************************************/ - public List getSearchableTables() - { - return (this.searchableTables); - } - - - - /*************************************************************************** - ** Setter for searchableTables - ***************************************************************************/ - public void setSearchableTables(List searchableTables) - { - this.searchableTables = searchableTables; - } - - - - /*************************************************************************** - ** Fluent setter for searchableTables - ***************************************************************************/ - public QuickSearchQBitConfig withSearchableTables(List searchableTables) - { - this.searchableTables = searchableTables; - return (this); - } - - - - /*************************************************************************** - ** Add a single config-driven searchable table. Lazy-initializes the list. - ***************************************************************************/ + /******************************************************************************* + ** Add one config-driven searchable table. + *******************************************************************************/ public QuickSearchQBitConfig withSearchableTable(SearchableTableConfig tableConfig) { if(this.searchableTables == null) @@ -708,13 +710,1377 @@ public class QuickSearchQBitConfig implements QBitConfig - /*************************************************************************** - ** Convenience shorthand to add a config-driven searchable table with - ** default settings. - ***************************************************************************/ + /******************************************************************************* + ** Add one config-driven searchable table from its name and fields. + *******************************************************************************/ public QuickSearchQBitConfig withSearchableTable(String tableName, List fields) { return withSearchableTable(new SearchableTableConfig(tableName, fields)); } + + + /******************************************************************************* + ** Summary without secrets. + *******************************************************************************/ + @Override + public String toString() + { + return ("QuickSearchQBitConfig{backendName=" + backendName + + ", url=" + getEffectiveScheme() + "://" + getEffectiveHost() + ":" + getEffectivePort() + + ", indexName=" + opensearchIndexName + + ", authMode=" + getEffectiveAuthMode() + + ", username=" + (StringUtils.hasContent(opensearchUsername) ? "[set]" : "[none]") + + ", password=" + (StringUtils.hasContent(opensearchPassword) ? "[redacted]" : "[none]") + + ", startupMode=" + startupMode + + ", schedulerName=" + schedulerName + + ", entityClasses=" + (searchableEntityClasses == null ? 0 : searchableEntityClasses.size()) + + ", searchableTables=" + (searchableTables == null ? 0 : searchableTables.size()) + + "}"); + } + + + /******************************************************************************* + ** Getter for backendName: QQQ backend that holds the operational tables (required) + *******************************************************************************/ + public String getBackendName() + { + return (this.backendName); + } + + + + /******************************************************************************* + ** Setter for backendName + *******************************************************************************/ + public void setBackendName(String backendName) + { + this.backendName = backendName; + } + + + + /******************************************************************************* + ** Fluent setter for backendName + *******************************************************************************/ + public QuickSearchQBitConfig withBackendName(String backendName) + { + this.backendName = backendName; + return (this); + } + + + + + /******************************************************************************* + ** Getter for tableNamePrefix: optional prefix for produced table, process and app names + *******************************************************************************/ + public String getTableNamePrefix() + { + return (this.tableNamePrefix); + } + + + + /******************************************************************************* + ** Setter for tableNamePrefix + *******************************************************************************/ + public void setTableNamePrefix(String tableNamePrefix) + { + this.tableNamePrefix = tableNamePrefix; + } + + + + /******************************************************************************* + ** Fluent setter for tableNamePrefix + *******************************************************************************/ + public QuickSearchQBitConfig withTableNamePrefix(String tableNamePrefix) + { + this.tableNamePrefix = tableNamePrefix; + return (this); + } + + + + + /******************************************************************************* + ** Getter for opensearchUrl: full OpenSearch URL, e.g. https://search.example.com:443 (preferred over host/port/useSsl) + *******************************************************************************/ + public String getOpensearchUrl() + { + return (this.opensearchUrl); + } + + + + /******************************************************************************* + ** Setter for opensearchUrl + *******************************************************************************/ + public void setOpensearchUrl(String opensearchUrl) + { + this.opensearchUrl = opensearchUrl; + } + + + + /******************************************************************************* + ** Fluent setter for opensearchUrl + *******************************************************************************/ + public QuickSearchQBitConfig withOpensearchUrl(String opensearchUrl) + { + this.opensearchUrl = opensearchUrl; + return (this); + } + + + + + /******************************************************************************* + ** Getter for opensearchHost: OpenSearch host (when opensearchUrl is not set) + *******************************************************************************/ + public String getOpensearchHost() + { + return (this.opensearchHost); + } + + + + /******************************************************************************* + ** Setter for opensearchHost + *******************************************************************************/ + public void setOpensearchHost(String opensearchHost) + { + this.opensearchHost = opensearchHost; + } + + + + /******************************************************************************* + ** Fluent setter for opensearchHost + *******************************************************************************/ + public QuickSearchQBitConfig withOpensearchHost(String opensearchHost) + { + this.opensearchHost = opensearchHost; + return (this); + } + + + + + /******************************************************************************* + ** Getter for opensearchPort: OpenSearch port (when opensearchUrl is not set) + *******************************************************************************/ + public Integer getOpensearchPort() + { + return (this.opensearchPort); + } + + + + /******************************************************************************* + ** Setter for opensearchPort + *******************************************************************************/ + public void setOpensearchPort(Integer opensearchPort) + { + this.opensearchPort = opensearchPort; + } + + + + /******************************************************************************* + ** Fluent setter for opensearchPort + *******************************************************************************/ + public QuickSearchQBitConfig withOpensearchPort(Integer opensearchPort) + { + this.opensearchPort = opensearchPort; + return (this); + } + + + + + /******************************************************************************* + ** Getter for useSsl: use https (when opensearchUrl is not set) + *******************************************************************************/ + public Boolean getUseSsl() + { + return (this.useSsl); + } + + + + /******************************************************************************* + ** Setter for useSsl + *******************************************************************************/ + public void setUseSsl(Boolean useSsl) + { + this.useSsl = useSsl; + } + + + + /******************************************************************************* + ** Fluent setter for useSsl + *******************************************************************************/ + public QuickSearchQBitConfig withUseSsl(Boolean useSsl) + { + this.useSsl = useSsl; + return (this); + } + + + + + /******************************************************************************* + ** Getter for opensearchIndexName: name of the OpenSearch index (alias) this QBit owns (required) + *******************************************************************************/ + public String getOpensearchIndexName() + { + return (this.opensearchIndexName); + } + + + + /******************************************************************************* + ** Setter for opensearchIndexName + *******************************************************************************/ + public void setOpensearchIndexName(String opensearchIndexName) + { + this.opensearchIndexName = opensearchIndexName; + } + + + + /******************************************************************************* + ** Fluent setter for opensearchIndexName + *******************************************************************************/ + public QuickSearchQBitConfig withOpensearchIndexName(String opensearchIndexName) + { + this.opensearchIndexName = opensearchIndexName; + return (this); + } + + + + + /******************************************************************************* + ** Getter for authMode: authentication mode; inferred as BASIC when credentials are set, else NONE + *******************************************************************************/ + public QuickSearchAuthMode getAuthMode() + { + return (this.authMode); + } + + + + /******************************************************************************* + ** Setter for authMode + *******************************************************************************/ + public void setAuthMode(QuickSearchAuthMode authMode) + { + this.authMode = authMode; + } + + + + /******************************************************************************* + ** Fluent setter for authMode + *******************************************************************************/ + public QuickSearchQBitConfig withAuthMode(QuickSearchAuthMode authMode) + { + this.authMode = authMode; + return (this); + } + + + + + /******************************************************************************* + ** Getter for opensearchUsername: BASIC auth username; may be a ${env.X} reference + *******************************************************************************/ + public String getOpensearchUsername() + { + return (this.opensearchUsername); + } + + + + /******************************************************************************* + ** Setter for opensearchUsername + *******************************************************************************/ + public void setOpensearchUsername(String opensearchUsername) + { + this.opensearchUsername = opensearchUsername; + } + + + + /******************************************************************************* + ** Fluent setter for opensearchUsername + *******************************************************************************/ + public QuickSearchQBitConfig withOpensearchUsername(String opensearchUsername) + { + this.opensearchUsername = opensearchUsername; + return (this); + } + + + + + /******************************************************************************* + ** Getter for opensearchPassword: BASIC auth password; may be a ${env.X} reference. Never serialized to JSON + *******************************************************************************/ + @JsonIgnore + public String getOpensearchPassword() + { + return (this.opensearchPassword); + } + + + + /******************************************************************************* + ** Setter for opensearchPassword + *******************************************************************************/ + public void setOpensearchPassword(String opensearchPassword) + { + this.opensearchPassword = opensearchPassword; + } + + + + /******************************************************************************* + ** Fluent setter for opensearchPassword + *******************************************************************************/ + public QuickSearchQBitConfig withOpensearchPassword(String opensearchPassword) + { + this.opensearchPassword = opensearchPassword; + return (this); + } + + + + + /******************************************************************************* + ** Getter for awsRegion: AWS_SIGV4 region; defaults to AWS_REGION / AWS_DEFAULT_REGION from the environment + *******************************************************************************/ + public String getAwsRegion() + { + return (this.awsRegion); + } + + + + /******************************************************************************* + ** Setter for awsRegion + *******************************************************************************/ + public void setAwsRegion(String awsRegion) + { + this.awsRegion = awsRegion; + } + + + + /******************************************************************************* + ** Fluent setter for awsRegion + *******************************************************************************/ + public QuickSearchQBitConfig withAwsRegion(String awsRegion) + { + this.awsRegion = awsRegion; + return (this); + } + + + + + /******************************************************************************* + ** Getter for awsServiceName: AWS_SIGV4 signing service: es (managed domains) or aoss (Serverless) + *******************************************************************************/ + public String getAwsServiceName() + { + return (this.awsServiceName); + } + + + + /******************************************************************************* + ** Setter for awsServiceName + *******************************************************************************/ + public void setAwsServiceName(String awsServiceName) + { + this.awsServiceName = awsServiceName; + } + + + + /******************************************************************************* + ** Fluent setter for awsServiceName + *******************************************************************************/ + public QuickSearchQBitConfig withAwsServiceName(String awsServiceName) + { + this.awsServiceName = awsServiceName; + return (this); + } + + + + + /******************************************************************************* + ** Getter for awsAssumeRoleArn: AWS_SIGV4 optional role to assume via STS + *******************************************************************************/ + public String getAwsAssumeRoleArn() + { + return (this.awsAssumeRoleArn); + } + + + + /******************************************************************************* + ** Setter for awsAssumeRoleArn + *******************************************************************************/ + public void setAwsAssumeRoleArn(String awsAssumeRoleArn) + { + this.awsAssumeRoleArn = awsAssumeRoleArn; + } + + + + /******************************************************************************* + ** Fluent setter for awsAssumeRoleArn + *******************************************************************************/ + public QuickSearchQBitConfig withAwsAssumeRoleArn(String awsAssumeRoleArn) + { + this.awsAssumeRoleArn = awsAssumeRoleArn; + return (this); + } + + + + + /******************************************************************************* + ** Getter for allowPlaintextCredentials: permit BASIC credentials over plain http (development only) + *******************************************************************************/ + public Boolean getAllowPlaintextCredentials() + { + return (this.allowPlaintextCredentials); + } + + + + /******************************************************************************* + ** Setter for allowPlaintextCredentials + *******************************************************************************/ + public void setAllowPlaintextCredentials(Boolean allowPlaintextCredentials) + { + this.allowPlaintextCredentials = allowPlaintextCredentials; + } + + + + /******************************************************************************* + ** Fluent setter for allowPlaintextCredentials + *******************************************************************************/ + public QuickSearchQBitConfig withAllowPlaintextCredentials(Boolean allowPlaintextCredentials) + { + this.allowPlaintextCredentials = allowPlaintextCredentials; + return (this); + } + + + + + /******************************************************************************* + ** Getter for tls: TLS trust and client certificate settings + *******************************************************************************/ + public QuickSearchTlsConfig getTls() + { + return (this.tls); + } + + + + /******************************************************************************* + ** Setter for tls + *******************************************************************************/ + public void setTls(QuickSearchTlsConfig tls) + { + this.tls = tls; + } + + + + /******************************************************************************* + ** Fluent setter for tls + *******************************************************************************/ + public QuickSearchQBitConfig withTls(QuickSearchTlsConfig tls) + { + this.tls = tls; + return (this); + } + + + + + /******************************************************************************* + ** Getter for connectTimeoutMillis: transport connect timeout + *******************************************************************************/ + public Integer getConnectTimeoutMillis() + { + return (this.connectTimeoutMillis); + } + + + + /******************************************************************************* + ** Setter for connectTimeoutMillis + *******************************************************************************/ + public void setConnectTimeoutMillis(Integer connectTimeoutMillis) + { + this.connectTimeoutMillis = connectTimeoutMillis; + } + + + + /******************************************************************************* + ** Fluent setter for connectTimeoutMillis + *******************************************************************************/ + public QuickSearchQBitConfig withConnectTimeoutMillis(Integer connectTimeoutMillis) + { + this.connectTimeoutMillis = connectTimeoutMillis; + return (this); + } + + + + + /******************************************************************************* + ** Getter for responseTimeoutMillis: transport response timeout + *******************************************************************************/ + public Integer getResponseTimeoutMillis() + { + return (this.responseTimeoutMillis); + } + + + + /******************************************************************************* + ** Setter for responseTimeoutMillis + *******************************************************************************/ + public void setResponseTimeoutMillis(Integer responseTimeoutMillis) + { + this.responseTimeoutMillis = responseTimeoutMillis; + } + + + + /******************************************************************************* + ** Fluent setter for responseTimeoutMillis + *******************************************************************************/ + public QuickSearchQBitConfig withResponseTimeoutMillis(Integer responseTimeoutMillis) + { + this.responseTimeoutMillis = responseTimeoutMillis; + return (this); + } + + + + + /******************************************************************************* + ** Getter for maxConnections: transport connection pool size + *******************************************************************************/ + public Integer getMaxConnections() + { + return (this.maxConnections); + } + + + + /******************************************************************************* + ** Setter for maxConnections + *******************************************************************************/ + public void setMaxConnections(Integer maxConnections) + { + this.maxConnections = maxConnections; + } + + + + /******************************************************************************* + ** Fluent setter for maxConnections + *******************************************************************************/ + public QuickSearchQBitConfig withMaxConnections(Integer maxConnections) + { + this.maxConnections = maxConnections; + return (this); + } + + + + + /******************************************************************************* + ** Getter for maxBulkRequestBytes: flush a bulk request when its serialized size reaches this many bytes + *******************************************************************************/ + public Integer getMaxBulkRequestBytes() + { + return (this.maxBulkRequestBytes); + } + + + + /******************************************************************************* + ** Setter for maxBulkRequestBytes + *******************************************************************************/ + public void setMaxBulkRequestBytes(Integer maxBulkRequestBytes) + { + this.maxBulkRequestBytes = maxBulkRequestBytes; + } + + + + /******************************************************************************* + ** Fluent setter for maxBulkRequestBytes + *******************************************************************************/ + public QuickSearchQBitConfig withMaxBulkRequestBytes(Integer maxBulkRequestBytes) + { + this.maxBulkRequestBytes = maxBulkRequestBytes; + return (this); + } + + + + + /******************************************************************************* + ** Getter for transportCustomizer: QCodeReference to an OpenSearchTransportCustomizer + *******************************************************************************/ + public QCodeReference getTransportCustomizer() + { + return (this.transportCustomizer); + } + + + + /******************************************************************************* + ** Setter for transportCustomizer + *******************************************************************************/ + public void setTransportCustomizer(QCodeReference transportCustomizer) + { + this.transportCustomizer = transportCustomizer; + } + + + + /******************************************************************************* + ** Fluent setter for transportCustomizer + *******************************************************************************/ + public QuickSearchQBitConfig withTransportCustomizer(QCodeReference transportCustomizer) + { + this.transportCustomizer = transportCustomizer; + return (this); + } + + + + + /******************************************************************************* + ** Getter for startupMode: FAIL_FAST or DEGRADED startup when OpenSearch is unreachable + *******************************************************************************/ + public QuickSearchStartupMode getStartupMode() + { + return (this.startupMode); + } + + + + /******************************************************************************* + ** Setter for startupMode + *******************************************************************************/ + public void setStartupMode(QuickSearchStartupMode startupMode) + { + this.startupMode = startupMode; + } + + + + /******************************************************************************* + ** Fluent setter for startupMode + *******************************************************************************/ + public QuickSearchQBitConfig withStartupMode(QuickSearchStartupMode startupMode) + { + this.startupMode = startupMode; + return (this); + } + + + + + /******************************************************************************* + ** Getter for enableRealTimeIndexing: register the record-change listener that indexes writes as they happen + *******************************************************************************/ + public Boolean getEnableRealTimeIndexing() + { + return (this.enableRealTimeIndexing); + } + + + + /******************************************************************************* + ** Setter for enableRealTimeIndexing + *******************************************************************************/ + public void setEnableRealTimeIndexing(Boolean enableRealTimeIndexing) + { + this.enableRealTimeIndexing = enableRealTimeIndexing; + } + + + + /******************************************************************************* + ** Fluent setter for enableRealTimeIndexing + *******************************************************************************/ + public QuickSearchQBitConfig withEnableRealTimeIndexing(Boolean enableRealTimeIndexing) + { + this.enableRealTimeIndexing = enableRealTimeIndexing; + return (this); + } + + + + + /******************************************************************************* + ** Getter for enableBasepullProcess: register the basepull catch-up process + *******************************************************************************/ + public Boolean getEnableBasepullProcess() + { + return (this.enableBasepullProcess); + } + + + + /******************************************************************************* + ** Setter for enableBasepullProcess + *******************************************************************************/ + public void setEnableBasepullProcess(Boolean enableBasepullProcess) + { + this.enableBasepullProcess = enableBasepullProcess; + } + + + + /******************************************************************************* + ** Fluent setter for enableBasepullProcess + *******************************************************************************/ + public QuickSearchQBitConfig withEnableBasepullProcess(Boolean enableBasepullProcess) + { + this.enableBasepullProcess = enableBasepullProcess; + return (this); + } + + + + + /******************************************************************************* + ** Getter for enableMaintenanceProcesses: register the full reindex and reconcile processes + *******************************************************************************/ + public Boolean getEnableMaintenanceProcesses() + { + return (this.enableMaintenanceProcesses); + } + + + + /******************************************************************************* + ** Setter for enableMaintenanceProcesses + *******************************************************************************/ + public void setEnableMaintenanceProcesses(Boolean enableMaintenanceProcesses) + { + this.enableMaintenanceProcesses = enableMaintenanceProcesses; + } + + + + /******************************************************************************* + ** Fluent setter for enableMaintenanceProcesses + *******************************************************************************/ + public QuickSearchQBitConfig withEnableMaintenanceProcesses(Boolean enableMaintenanceProcesses) + { + this.enableMaintenanceProcesses = enableMaintenanceProcesses; + return (this); + } + + + + + /******************************************************************************* + ** Getter for schedulerName: QQQ scheduler to run basepull and reconcile on; null leaves them unscheduled + *******************************************************************************/ + public String getSchedulerName() + { + return (this.schedulerName); + } + + + + /******************************************************************************* + ** Setter for schedulerName + *******************************************************************************/ + public void setSchedulerName(String schedulerName) + { + this.schedulerName = schedulerName; + } + + + + /******************************************************************************* + ** Fluent setter for schedulerName + *******************************************************************************/ + public QuickSearchQBitConfig withSchedulerName(String schedulerName) + { + this.schedulerName = schedulerName; + return (this); + } + + + + + /******************************************************************************* + ** Getter for basepullRepeatSeconds: basepull schedule interval when schedulerName is set + *******************************************************************************/ + public Integer getBasepullRepeatSeconds() + { + return (this.basepullRepeatSeconds); + } + + + + /******************************************************************************* + ** Setter for basepullRepeatSeconds + *******************************************************************************/ + public void setBasepullRepeatSeconds(Integer basepullRepeatSeconds) + { + this.basepullRepeatSeconds = basepullRepeatSeconds; + } + + + + /******************************************************************************* + ** Fluent setter for basepullRepeatSeconds + *******************************************************************************/ + public QuickSearchQBitConfig withBasepullRepeatSeconds(Integer basepullRepeatSeconds) + { + this.basepullRepeatSeconds = basepullRepeatSeconds; + return (this); + } + + + + + /******************************************************************************* + ** Getter for reconcileCronExpression: optional cron schedule for reconcile (needs a scheduler that supports cron) + *******************************************************************************/ + public String getReconcileCronExpression() + { + return (this.reconcileCronExpression); + } + + + + /******************************************************************************* + ** Setter for reconcileCronExpression + *******************************************************************************/ + public void setReconcileCronExpression(String reconcileCronExpression) + { + this.reconcileCronExpression = reconcileCronExpression; + } + + + + /******************************************************************************* + ** Fluent setter for reconcileCronExpression + *******************************************************************************/ + public QuickSearchQBitConfig withReconcileCronExpression(String reconcileCronExpression) + { + this.reconcileCronExpression = reconcileCronExpression; + return (this); + } + + + + + /******************************************************************************* + ** Getter for reconcileCronTimeZoneId: time zone for reconcileCronExpression + *******************************************************************************/ + public String getReconcileCronTimeZoneId() + { + return (this.reconcileCronTimeZoneId); + } + + + + /******************************************************************************* + ** Setter for reconcileCronTimeZoneId + *******************************************************************************/ + public void setReconcileCronTimeZoneId(String reconcileCronTimeZoneId) + { + this.reconcileCronTimeZoneId = reconcileCronTimeZoneId; + } + + + + /******************************************************************************* + ** Fluent setter for reconcileCronTimeZoneId + *******************************************************************************/ + public QuickSearchQBitConfig withReconcileCronTimeZoneId(String reconcileCronTimeZoneId) + { + this.reconcileCronTimeZoneId = reconcileCronTimeZoneId; + return (this); + } + + + + + /******************************************************************************* + ** Getter for basepullOverlapSeconds: how far before the previous run start basepull re-reads, to absorb clock skew and long transactions + *******************************************************************************/ + public Integer getBasepullOverlapSeconds() + { + return (this.basepullOverlapSeconds); + } + + + + /******************************************************************************* + ** Setter for basepullOverlapSeconds + *******************************************************************************/ + public void setBasepullOverlapSeconds(Integer basepullOverlapSeconds) + { + this.basepullOverlapSeconds = basepullOverlapSeconds; + } + + + + /******************************************************************************* + ** Fluent setter for basepullOverlapSeconds + *******************************************************************************/ + public QuickSearchQBitConfig withBasepullOverlapSeconds(Integer basepullOverlapSeconds) + { + this.basepullOverlapSeconds = basepullOverlapSeconds; + return (this); + } + + + + + /******************************************************************************* + ** Getter for defaultBasepullIntervalMinutes: default per-table basepull interval for config-driven tables + *******************************************************************************/ + public Integer getDefaultBasepullIntervalMinutes() + { + return (this.defaultBasepullIntervalMinutes); + } + + + + /******************************************************************************* + ** Setter for defaultBasepullIntervalMinutes + *******************************************************************************/ + public void setDefaultBasepullIntervalMinutes(Integer defaultBasepullIntervalMinutes) + { + this.defaultBasepullIntervalMinutes = defaultBasepullIntervalMinutes; + } + + + + /******************************************************************************* + ** Fluent setter for defaultBasepullIntervalMinutes + *******************************************************************************/ + public QuickSearchQBitConfig withDefaultBasepullIntervalMinutes(Integer defaultBasepullIntervalMinutes) + { + this.defaultBasepullIntervalMinutes = defaultBasepullIntervalMinutes; + return (this); + } + + + + + /******************************************************************************* + ** Getter for runHistoryRetentionDays: delete quickSearchIndexRun rows older than this many days; null keeps them forever + *******************************************************************************/ + public Integer getRunHistoryRetentionDays() + { + return (this.runHistoryRetentionDays); + } + + + + /******************************************************************************* + ** Setter for runHistoryRetentionDays + *******************************************************************************/ + public void setRunHistoryRetentionDays(Integer runHistoryRetentionDays) + { + this.runHistoryRetentionDays = runHistoryRetentionDays; + } + + + + /******************************************************************************* + ** Fluent setter for runHistoryRetentionDays + *******************************************************************************/ + public QuickSearchQBitConfig withRunHistoryRetentionDays(Integer runHistoryRetentionDays) + { + this.runHistoryRetentionDays = runHistoryRetentionDays; + return (this); + } + + + + + /******************************************************************************* + ** Getter for bulkBatchSize: documents per OpenSearch bulk request + *******************************************************************************/ + public Integer getBulkBatchSize() + { + return (this.bulkBatchSize); + } + + + + /******************************************************************************* + ** Setter for bulkBatchSize + *******************************************************************************/ + public void setBulkBatchSize(Integer bulkBatchSize) + { + this.bulkBatchSize = bulkBatchSize; + } + + + + /******************************************************************************* + ** Fluent setter for bulkBatchSize + *******************************************************************************/ + public QuickSearchQBitConfig withBulkBatchSize(Integer bulkBatchSize) + { + this.bulkBatchSize = bulkBatchSize; + return (this); + } + + + + + /******************************************************************************* + ** Getter for sourceBatchSize: records per source table query page + *******************************************************************************/ + public Integer getSourceBatchSize() + { + return (this.sourceBatchSize); + } + + + + /******************************************************************************* + ** Setter for sourceBatchSize + *******************************************************************************/ + public void setSourceBatchSize(Integer sourceBatchSize) + { + this.sourceBatchSize = sourceBatchSize; + } + + + + /******************************************************************************* + ** Fluent setter for sourceBatchSize + *******************************************************************************/ + public QuickSearchQBitConfig withSourceBatchSize(Integer sourceBatchSize) + { + this.sourceBatchSize = sourceBatchSize; + return (this); + } + + + + + /******************************************************************************* + ** Getter for maxFieldLength: truncate each indexed field value to this many characters; null for no limit + *******************************************************************************/ + public Integer getMaxFieldLength() + { + return (this.maxFieldLength); + } + + + + /******************************************************************************* + ** Setter for maxFieldLength + *******************************************************************************/ + public void setMaxFieldLength(Integer maxFieldLength) + { + this.maxFieldLength = maxFieldLength; + } + + + + /******************************************************************************* + ** Fluent setter for maxFieldLength + *******************************************************************************/ + public QuickSearchQBitConfig withMaxFieldLength(Integer maxFieldLength) + { + this.maxFieldLength = maxFieldLength; + return (this); + } + + + + + /******************************************************************************* + ** Getter for maxSearchLimit: largest page size QuickSearchAction will honour + *******************************************************************************/ + public Integer getMaxSearchLimit() + { + return (this.maxSearchLimit); + } + + + + /******************************************************************************* + ** Setter for maxSearchLimit + *******************************************************************************/ + public void setMaxSearchLimit(Integer maxSearchLimit) + { + this.maxSearchLimit = maxSearchLimit; + } + + + + /******************************************************************************* + ** Fluent setter for maxSearchLimit + *******************************************************************************/ + public QuickSearchQBitConfig withMaxSearchLimit(Integer maxSearchLimit) + { + this.maxSearchLimit = maxSearchLimit; + return (this); + } + + + + + /******************************************************************************* + ** Getter for applyRecordSecurityLocks: re-read search hits through QueryAction so record security locks apply + *******************************************************************************/ + public Boolean getApplyRecordSecurityLocks() + { + return (this.applyRecordSecurityLocks); + } + + + + /******************************************************************************* + ** Setter for applyRecordSecurityLocks + *******************************************************************************/ + public void setApplyRecordSecurityLocks(Boolean applyRecordSecurityLocks) + { + this.applyRecordSecurityLocks = applyRecordSecurityLocks; + } + + + + /******************************************************************************* + ** Fluent setter for applyRecordSecurityLocks + *******************************************************************************/ + public QuickSearchQBitConfig withApplyRecordSecurityLocks(Boolean applyRecordSecurityLocks) + { + this.applyRecordSecurityLocks = applyRecordSecurityLocks; + return (this); + } + + + + + /******************************************************************************* + ** Getter for searchableEntityClasses: entity classes annotated with @QuickSearchable + *******************************************************************************/ + public List> getSearchableEntityClasses() + { + return (this.searchableEntityClasses); + } + + + + /******************************************************************************* + ** Setter for searchableEntityClasses + *******************************************************************************/ + public void setSearchableEntityClasses(List> searchableEntityClasses) + { + this.searchableEntityClasses = searchableEntityClasses; + } + + + + /******************************************************************************* + ** Fluent setter for searchableEntityClasses + *******************************************************************************/ + public QuickSearchQBitConfig withSearchableEntityClasses(List> searchableEntityClasses) + { + this.searchableEntityClasses = searchableEntityClasses; + return (this); + } + + + + + /******************************************************************************* + ** Getter for searchableTables: config-driven searchable tables + *******************************************************************************/ + public List getSearchableTables() + { + return (this.searchableTables); + } + + + + /******************************************************************************* + ** Setter for searchableTables + *******************************************************************************/ + public void setSearchableTables(List searchableTables) + { + this.searchableTables = searchableTables; + } + + + + /******************************************************************************* + ** Fluent setter for searchableTables + *******************************************************************************/ + public QuickSearchQBitConfig withSearchableTables(List searchableTables) + { + this.searchableTables = searchableTables; + return (this); + } + + + + + /******************************************************************************* + ** Getter for indexEventPublisher: host-supplied publisher (defaults to SynchronousIndexEventPublisher) + *******************************************************************************/ + public IndexEventPublisher getIndexEventPublisher() + { + return (this.indexEventPublisher); + } + + + + /******************************************************************************* + ** Setter for indexEventPublisher + *******************************************************************************/ + public void setIndexEventPublisher(IndexEventPublisher indexEventPublisher) + { + this.indexEventPublisher = indexEventPublisher; + } + + + + /******************************************************************************* + ** Fluent setter for indexEventPublisher + *******************************************************************************/ + public QuickSearchQBitConfig withIndexEventPublisher(IndexEventPublisher indexEventPublisher) + { + this.indexEventPublisher = indexEventPublisher; + return (this); + } + + + + + /******************************************************************************* + ** Getter for adminPermissionRules: permission rules for the admin app and the three processes + *******************************************************************************/ + public QPermissionRules getAdminPermissionRules() + { + return (this.adminPermissionRules); + } + + + + /******************************************************************************* + ** Setter for adminPermissionRules + *******************************************************************************/ + public void setAdminPermissionRules(QPermissionRules adminPermissionRules) + { + this.adminPermissionRules = adminPermissionRules; + } + + + + /******************************************************************************* + ** Fluent setter for adminPermissionRules + *******************************************************************************/ + public QuickSearchQBitConfig withAdminPermissionRules(QPermissionRules adminPermissionRules) + { + this.adminPermissionRules = adminPermissionRules; + return (this); + } + + + + + /******************************************************************************* + ** Getter for tablePermissionRules: permission rules for the operational tables + *******************************************************************************/ + public QPermissionRules getTablePermissionRules() + { + return (this.tablePermissionRules); + } + + + + /******************************************************************************* + ** Setter for tablePermissionRules + *******************************************************************************/ + public void setTablePermissionRules(QPermissionRules tablePermissionRules) + { + this.tablePermissionRules = tablePermissionRules; + } + + + + /******************************************************************************* + ** Fluent setter for tablePermissionRules + *******************************************************************************/ + public QuickSearchQBitConfig withTablePermissionRules(QPermissionRules tablePermissionRules) + { + this.tablePermissionRules = tablePermissionRules; + return (this); + } + + + + + /******************************************************************************* + ** Getter for runtime: live state once produced (client, publisher, discovered tables) + *******************************************************************************/ + @JsonIgnore + public QuickSearchRuntime getRuntime() + { + return (this.runtime); + } + + + + /******************************************************************************* + ** Setter for runtime + *******************************************************************************/ + public void setRuntime(QuickSearchRuntime runtime) + { + this.runtime = runtime; + } + + + + /******************************************************************************* + ** Fluent setter for runtime + *******************************************************************************/ + public QuickSearchQBitConfig withRuntime(QuickSearchRuntime runtime) + { + this.runtime = runtime; + return (this); + } + } diff --git a/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchQBitContext.java b/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchQBitContext.java index 66df91f..5b940f4 100644 --- a/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchQBitContext.java +++ b/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchQBitContext.java @@ -13,44 +13,58 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.kingsrook.qbits.quicksearch; import java.util.List; +import com.kingsrook.qqq.backend.core.exceptions.QException; +import com.kingsrook.qqq.backend.core.logging.QLogger; import com.kingsrook.qbits.quicksearch.opensearch.QuickSearchOpenSearchClient; import com.kingsrook.qbits.quicksearch.publisher.IndexEventPublisher; /******************************************************************************* - ** Static holder for Quick Search QBit runtime state. + ** Deprecated static facade over {@link QuickSearchRuntime}. ** - ** Provides access to the active configuration, the OpenSearch client, the - ** index-event publisher, and the list of tables discovered at startup. + ** Until 1.0 the QBit kept its live state in these static fields. The state now + ** lives on the produced QBit's runtime, resolved from QContext. The getters + ** here return an explicitly set static value when one exists (tests and + ** legacy hosts), otherwise they delegate to {@link QuickSearchRuntime#get()}. ** - ** Call clear() in test @AfterEach / @BeforeEach to reset all state. + ** @deprecated resolve state with {@link QuickSearchRuntime#get()}; this + ** facade will be removed in 1.1. *******************************************************************************/ +@Deprecated public class QuickSearchQBitContext { - private static volatile QuickSearchQBitConfig config; - private static volatile List discoveredTables; - private static volatile QuickSearchOpenSearchClient client; - private static volatile IndexEventPublisher publisher; + private static final QLogger LOG = QLogger.getLogger(QuickSearchQBitContext.class); + + private static volatile QuickSearchQBitConfig config; + private static volatile List discoveredTables; + private static volatile QuickSearchOpenSearchClient client; + private static volatile IndexEventPublisher publisher; - /*************************************************************************** - ** Getter for config - ***************************************************************************/ + /******************************************************************************* + ** Get the config: the static override, else the resolved runtime's config. + *******************************************************************************/ public static QuickSearchQBitConfig getConfig() { - return config; + if(config != null) + { + return (config); + } + QuickSearchRuntime runtime = QuickSearchRuntime.get(); + return (runtime == null ? null : runtime.getConfig()); } - /*************************************************************************** - ** Setter for config - ***************************************************************************/ + /******************************************************************************* + ** Set a static config override. + *******************************************************************************/ public static void setConfig(QuickSearchQBitConfig config) { QuickSearchQBitContext.config = config; @@ -58,19 +72,24 @@ public class QuickSearchQBitContext - /*************************************************************************** - ** Getter for discoveredTables - ***************************************************************************/ + /******************************************************************************* + ** Get the discovered tables: the static override, else the runtime's. + *******************************************************************************/ public static List getDiscoveredTables() { - return discoveredTables; + if(discoveredTables != null) + { + return (discoveredTables); + } + QuickSearchRuntime runtime = QuickSearchRuntime.get(); + return (runtime == null ? null : runtime.getDiscoveredTables()); } - /*************************************************************************** - ** Setter for discoveredTables - ***************************************************************************/ + /******************************************************************************* + ** Set a static discovered-tables override. + *******************************************************************************/ public static void setDiscoveredTables(List discoveredTables) { QuickSearchQBitContext.discoveredTables = discoveredTables; @@ -78,44 +97,62 @@ public class QuickSearchQBitContext - /*************************************************************************** - ** Find a table config by table name from the discovered tables list. - ** - ** Returns null if discoveredTables is null or no match is found. - ***************************************************************************/ + /******************************************************************************* + ** Find a table config by name, or null. + *******************************************************************************/ public static QuickSearchableTableConfig getTableConfig(String tableName) { - if(discoveredTables == null) + List tables = getDiscoveredTables(); + if(tables == null || tableName == null) { - return null; + return (null); } - for(QuickSearchableTableConfig tableConfig : discoveredTables) + for(QuickSearchableTableConfig tableConfig : tables) { if(tableName.equals(tableConfig.getTableName())) { - return tableConfig; + return (tableConfig); } } - - return null; + return (null); } - /*************************************************************************** - ** Getter for client - ***************************************************************************/ + /******************************************************************************* + ** Get the client: the static override, else the runtime's (built lazily). + ** Returns null when no runtime is registered or the client cannot be built. + *******************************************************************************/ public static QuickSearchOpenSearchClient getClient() { - return client; + if(client != null) + { + return (client); + } + + QuickSearchRuntime runtime = QuickSearchRuntime.get(); + if(runtime == null) + { + return (null); + } + + try + { + return (runtime.getClient()); + } + catch(QException e) + { + LOG.warn("Could not build OpenSearch client", e); + return (null); + } } - /*************************************************************************** - ** Setter for client - ***************************************************************************/ + /******************************************************************************* + ** Set a static client override. + *******************************************************************************/ public static void setClient(QuickSearchOpenSearchClient client) { QuickSearchQBitContext.client = client; @@ -123,19 +160,38 @@ public class QuickSearchQBitContext - /*************************************************************************** - ** Getter for publisher - ***************************************************************************/ + /******************************************************************************* + ** Get the publisher: the static override, else the runtime's. + *******************************************************************************/ public static IndexEventPublisher getPublisher() { - return publisher; + if(publisher != null) + { + return (publisher); + } + + QuickSearchRuntime runtime = QuickSearchRuntime.get(); + if(runtime == null) + { + return (null); + } + + try + { + return (runtime.getPublisher()); + } + catch(QException e) + { + LOG.warn("Could not build index event publisher", e); + return (null); + } } - /*************************************************************************** - ** Setter for publisher - ***************************************************************************/ + /******************************************************************************* + ** Set a static publisher override. + *******************************************************************************/ public static void setPublisher(IndexEventPublisher publisher) { QuickSearchQBitContext.publisher = publisher; @@ -143,18 +199,15 @@ public class QuickSearchQBitContext - /*************************************************************************** - ** Reset all static state to null. - ** - ** Call in test @BeforeEach or @AfterEach to prevent state leakage - ** between tests. - ***************************************************************************/ + /******************************************************************************* + ** Clear the static overrides (tests). + *******************************************************************************/ public static void clear() { - config = null; + config = null; discoveredTables = null; - client = null; - publisher = null; + client = null; + publisher = null; } } diff --git a/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchRuntime.java b/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchRuntime.java new file mode 100644 index 0000000..94c22da --- /dev/null +++ b/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchRuntime.java @@ -0,0 +1,396 @@ +/* + * Copyright 2024 Kingsrook, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.kingsrook.qbits.quicksearch; + + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import com.kingsrook.qqq.backend.core.actions.tables.QueryAction; +import com.kingsrook.qqq.backend.core.context.QContext; +import com.kingsrook.qqq.backend.core.exceptions.QException; +import com.kingsrook.qqq.backend.core.logging.QLogger; +import com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator; +import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterCriteria; +import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter; +import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryInput; +import com.kingsrook.qqq.backend.core.model.data.QRecord; +import com.kingsrook.qqq.backend.core.model.metadata.QInstance; +import com.kingsrook.qqq.backend.core.model.metadata.qbits.QBitMetaData; +import com.kingsrook.qbits.quicksearch.opensearch.QuickSearchOpenSearchClient; +import com.kingsrook.qbits.quicksearch.publisher.IndexEventPublisher; +import com.kingsrook.qbits.quicksearch.publisher.SynchronousIndexEventPublisher; +import static com.kingsrook.qqq.backend.core.logging.LogUtils.logPair; + + +/******************************************************************************* + ** Live state of one produced Quick Search QBit: its discovered tables, the + ** lazily built OpenSearch client and publisher, and a short-lived cache of + ** which tables are enabled. + ** + ** One runtime exists per produced QBit. It hangs off the QBit's config + ** (QuickSearchQBitConfig.getRuntime()), which the host's QInstance holds in + ** QBitMetaData, so steps, listeners and actions resolve it from QContext with + ** {@link #get()} rather than from JVM-global statics. Two QInstances in one + ** JVM therefore no longer share state. + *******************************************************************************/ +public class QuickSearchRuntime +{ + private static final QLogger LOG = QLogger.getLogger(QuickSearchRuntime.class); + + private static final long ENABLED_CACHE_TTL_MILLIS = 60_000L; + + private final QuickSearchQBitConfig config; + + private List discoveredTables = new ArrayList<>(); + private Map tablesByName = new LinkedHashMap<>(); + + private volatile QuickSearchOpenSearchClient client; + private volatile IndexEventPublisher publisher; + private volatile boolean indexReady = false; + private volatile boolean closed = false; + + private final Map enabledCache = new ConcurrentHashMap<>(); + + + + /******************************************************************************* + ** A cached enabled flag with the time it was read. + *******************************************************************************/ + private record CachedFlag(boolean enabled, long readAtMillis) + { + } + + + + /******************************************************************************* + ** Constructor + *******************************************************************************/ + public QuickSearchRuntime(QuickSearchQBitConfig config) + { + this.config = config; + } + + + + /******************************************************************************* + ** Resolve the runtime of the Quick Search QBit registered in the current + ** QContext's QInstance. Returns null when there is no QContext, no QInstance, + ** no Quick Search QBit, or the QBit has not been produced yet. + *******************************************************************************/ + public static QuickSearchRuntime get() + { + QInstance qInstance = QContext.getQInstance(); + if(qInstance == null || qInstance.getQBits() == null) + { + return (null); + } + + for(QBitMetaData qBitMetaData : qInstance.getQBits().values()) + { + if(qBitMetaData.getConfig() instanceof QuickSearchQBitConfig quickSearchConfig && quickSearchConfig.getRuntime() != null) + { + return (quickSearchConfig.getRuntime()); + } + } + + return (null); + } + + + + /******************************************************************************* + ** Like {@link #get()}, but throws when no runtime can be resolved. + *******************************************************************************/ + public static QuickSearchRuntime getOrThrow() throws QException + { + QuickSearchRuntime runtime = get(); + if(runtime == null) + { + throw (new QException("Quick Search QBit is not registered in the current QInstance (or has not been produced)")); + } + return (runtime); + } + + + + /******************************************************************************* + ** Getter for config + *******************************************************************************/ + public QuickSearchQBitConfig getConfig() + { + return (this.config); + } + + + + /******************************************************************************* + ** Getter for discoveredTables (never null) + *******************************************************************************/ + public List getDiscoveredTables() + { + return (Collections.unmodifiableList(this.discoveredTables)); + } + + + + /******************************************************************************* + ** Replace the discovered tables. + *******************************************************************************/ + public void setDiscoveredTables(List discoveredTables) + { + this.discoveredTables = new ArrayList<>(discoveredTables == null ? List.of() : discoveredTables); + this.tablesByName = new LinkedHashMap<>(); + for(QuickSearchableTableConfig tableConfig : this.discoveredTables) + { + this.tablesByName.put(tableConfig.getTableName(), tableConfig); + } + } + + + + /******************************************************************************* + ** Find the config for one table, or null. + *******************************************************************************/ + public QuickSearchableTableConfig getTableConfig(String tableName) + { + return (tableName == null ? null : tablesByName.get(tableName)); + } + + + + /******************************************************************************* + ** Return the OpenSearch client, building it on first use. Building the + ** client does not contact the cluster; {@link #ensureIndexReady()} does. + *******************************************************************************/ + public QuickSearchOpenSearchClient getClient() throws QException + { + QuickSearchOpenSearchClient existing = client; + if(existing != null) + { + return (existing); + } + + synchronized(this) + { + if(client == null) + { + client = new QuickSearchOpenSearchClient(config); + } + return (client); + } + } + + + + /******************************************************************************* + ** Make sure the index (or alias) exists with the current mapping. Runs the + ** check once per runtime; a failure leaves it unready so the next call + ** retries. + *******************************************************************************/ + public void ensureIndexReady() throws QException + { + if(indexReady) + { + return; + } + + synchronized(this) + { + if(!indexReady) + { + getClient().ensureIndexExists(); + indexReady = true; + } + } + } + + + + /******************************************************************************* + ** Whether {@link #ensureIndexReady()} has succeeded. + *******************************************************************************/ + public boolean isIndexReady() + { + return (indexReady); + } + + + + /******************************************************************************* + ** Return the publisher: the host-supplied one from config, else a + ** SynchronousIndexEventPublisher over this runtime's client. + *******************************************************************************/ + public IndexEventPublisher getPublisher() throws QException + { + IndexEventPublisher existing = publisher; + if(existing != null) + { + return (existing); + } + + synchronized(this) + { + if(publisher == null) + { + if(config.getIndexEventPublisher() != null) + { + publisher = config.getIndexEventPublisher(); + } + else + { + publisher = new SynchronousIndexEventPublisher(getClient(), discoveredTables, config.getBulkBatchSize()); + } + } + return (publisher); + } + } + + + + /******************************************************************************* + ** Whether a table is enabled for indexing and search, per its + ** quickSearchIndex row. A table with no row yet is enabled. Results are + ** cached for one minute; a failed lookup is treated as enabled so indexing + ** does not stop because the operational table is unreachable. + *******************************************************************************/ + public boolean isTableEnabled(String tableName) + { + if(tableName == null || !tablesByName.containsKey(tableName)) + { + return (false); + } + + long now = System.currentTimeMillis(); + CachedFlag cached = enabledCache.get(tableName); + if(cached != null && now - cached.readAtMillis() < ENABLED_CACHE_TTL_MILLIS) + { + return (cached.enabled()); + } + + boolean enabled = readEnabledFlag(tableName); + enabledCache.put(tableName, new CachedFlag(enabled, now)); + return (enabled); + } + + + + /******************************************************************************* + ** Forget cached enabled flags (after an operator toggles a table). + *******************************************************************************/ + public void invalidateEnabledCache() + { + enabledCache.clear(); + } + + + + /******************************************************************************* + ** Read the enabled flag from the quickSearchIndex row. + *******************************************************************************/ + private boolean readEnabledFlag(String tableName) + { + try + { + if(QContext.getQInstance() == null || QContext.getQInstance().getTable(config.getQuickSearchIndexTableName()) == null) + { + return (true); + } + + QueryInput queryInput = new QueryInput(); + queryInput.setTableName(config.getQuickSearchIndexTableName()); + queryInput.setFilter(new QQueryFilter(new QFilterCriteria("tableName", QCriteriaOperator.EQUALS, tableName))); + List rows = new QueryAction().execute(queryInput).getRecords(); + + if(rows == null || rows.isEmpty()) + { + return (true); + } + + Boolean enabled = rows.get(0).getValueBoolean("enabled"); + return (enabled == null || enabled); + } + catch(Exception e) + { + LOG.warn("Could not read enabled flag for table; treating as enabled", e, logPair("tableName", tableName)); + return (true); + } + } + + + + /******************************************************************************* + ** Start the runtime per the configured startup mode: FAIL_FAST throws when + ** the index cannot be prepared; DEGRADED logs and continues. + *******************************************************************************/ + public void start() throws QException + { + try + { + ensureIndexReady(); + } + catch(Exception e) + { + if(config.getStartupMode() == QuickSearchStartupMode.DEGRADED) + { + LOG.warn("OpenSearch is not reachable; Quick Search is running degraded and will retry on first use", e, + logPair("indexName", config.getOpensearchIndexName())); + return; + } + + throw (new QException("Quick Search could not prepare its OpenSearch index [" + config.getOpensearchIndexName() + + "]; set startupMode=DEGRADED to boot without it: " + e.getMessage(), e)); + } + } + + + + /******************************************************************************* + ** Close the publisher and client. Safe to call more than once. + *******************************************************************************/ + public synchronized void close() + { + if(closed) + { + return; + } + closed = true; + + if(publisher != null) + { + try + { + publisher.close(); + } + catch(Exception e) + { + LOG.warn("Error closing Quick Search publisher", e); + } + } + + if(client != null) + { + client.close(); + } + + indexReady = false; + } + +} diff --git a/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchStartupMode.java b/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchStartupMode.java new file mode 100644 index 0000000..cac35ff --- /dev/null +++ b/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchStartupMode.java @@ -0,0 +1,34 @@ +/* + * Copyright 2024 Kingsrook, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.kingsrook.qbits.quicksearch; + + +/******************************************************************************* + ** What happens when OpenSearch cannot be reached while the QBit starts. + ** + ** FAIL_FAST - produce() and the runtime service throw, so the host does not + ** boot with search silently broken. The default. + ** DEGRADED - the failure is logged; the host boots; indexing and search + ** retry the connection on first use and report through the + ** health check. Intended for demo profiles that may run without + ** an OpenSearch cluster. + *******************************************************************************/ +public enum QuickSearchStartupMode +{ + FAIL_FAST, + DEGRADED +} diff --git a/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchTlsConfig.java b/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchTlsConfig.java new file mode 100644 index 0000000..b57030c --- /dev/null +++ b/src/main/java/com/kingsrook/qbits/quicksearch/QuickSearchTlsConfig.java @@ -0,0 +1,373 @@ +/* + * Copyright 2024 Kingsrook, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.kingsrook.qbits.quicksearch; + + +import java.io.Serializable; +import com.fasterxml.jackson.annotation.JsonIgnore; + + +/******************************************************************************* + ** TLS trust and client-certificate settings for the OpenSearch connection. + ** + ** By default the JVM trust store and hostname verification are used. Supply + ** caCertificatePath (a PEM bundle) or trustStorePath to trust a private CA; + ** supply keyStorePath for client-certificate (mTLS) authentication. + ** insecureSkipVerify disables certificate and hostname checks and is refused + ** for non-loopback hosts unless allowInsecureInProduction is also set. + ** + ** Path and password values may be ${env.X} / ${prop.X} references; they are + ** interpreted when the transport is built, never stored resolved. + *******************************************************************************/ +public class QuickSearchTlsConfig implements Serializable +{ + private String caCertificatePath; + private String trustStorePath; + private String trustStorePassword; + private String trustStoreType; + private String keyStorePath; + private String keyStorePassword; + private String keyStoreType; + private Boolean hostnameVerification = true; + private Boolean insecureSkipVerify = false; + private Boolean allowInsecureInProduction = false; + + + + /******************************************************************************* + ** Whether any custom trust or key material is configured. + *******************************************************************************/ + @JsonIgnore + public boolean hasCustomTrustOrKeyMaterial() + { + return (caCertificatePath != null || trustStorePath != null || keyStorePath != null); + } + + + + /******************************************************************************* + ** Getter for caCertificatePath + *******************************************************************************/ + public String getCaCertificatePath() + { + return (this.caCertificatePath); + } + + + + /******************************************************************************* + ** Setter for caCertificatePath + *******************************************************************************/ + public void setCaCertificatePath(String caCertificatePath) + { + this.caCertificatePath = caCertificatePath; + } + + + + /******************************************************************************* + ** Fluent setter for caCertificatePath + *******************************************************************************/ + public QuickSearchTlsConfig withCaCertificatePath(String caCertificatePath) + { + this.caCertificatePath = caCertificatePath; + return (this); + } + + + + /******************************************************************************* + ** Getter for trustStorePath + *******************************************************************************/ + public String getTrustStorePath() + { + return (this.trustStorePath); + } + + + + /******************************************************************************* + ** Setter for trustStorePath + *******************************************************************************/ + public void setTrustStorePath(String trustStorePath) + { + this.trustStorePath = trustStorePath; + } + + + + /******************************************************************************* + ** Fluent setter for trustStorePath + *******************************************************************************/ + public QuickSearchTlsConfig withTrustStorePath(String trustStorePath) + { + this.trustStorePath = trustStorePath; + return (this); + } + + + + /******************************************************************************* + ** Getter for trustStorePassword. Hidden from JSON so the secret (or its + ** reference) never leaves the JVM. + *******************************************************************************/ + @JsonIgnore + public String getTrustStorePassword() + { + return (this.trustStorePassword); + } + + + + /******************************************************************************* + ** Setter for trustStorePassword + *******************************************************************************/ + public void setTrustStorePassword(String trustStorePassword) + { + this.trustStorePassword = trustStorePassword; + } + + + + /******************************************************************************* + ** Fluent setter for trustStorePassword + *******************************************************************************/ + public QuickSearchTlsConfig withTrustStorePassword(String trustStorePassword) + { + this.trustStorePassword = trustStorePassword; + return (this); + } + + + + /******************************************************************************* + ** Getter for trustStoreType (JKS or PKCS12; defaults to the JVM default) + *******************************************************************************/ + public String getTrustStoreType() + { + return (this.trustStoreType); + } + + + + /******************************************************************************* + ** Setter for trustStoreType + *******************************************************************************/ + public void setTrustStoreType(String trustStoreType) + { + this.trustStoreType = trustStoreType; + } + + + + /******************************************************************************* + ** Fluent setter for trustStoreType + *******************************************************************************/ + public QuickSearchTlsConfig withTrustStoreType(String trustStoreType) + { + this.trustStoreType = trustStoreType; + return (this); + } + + + + /******************************************************************************* + ** Getter for keyStorePath + *******************************************************************************/ + public String getKeyStorePath() + { + return (this.keyStorePath); + } + + + + /******************************************************************************* + ** Setter for keyStorePath + *******************************************************************************/ + public void setKeyStorePath(String keyStorePath) + { + this.keyStorePath = keyStorePath; + } + + + + /******************************************************************************* + ** Fluent setter for keyStorePath + *******************************************************************************/ + public QuickSearchTlsConfig withKeyStorePath(String keyStorePath) + { + this.keyStorePath = keyStorePath; + return (this); + } + + + + /******************************************************************************* + ** Getter for keyStorePassword. Hidden from JSON. + *******************************************************************************/ + @JsonIgnore + public String getKeyStorePassword() + { + return (this.keyStorePassword); + } + + + + /******************************************************************************* + ** Setter for keyStorePassword + *******************************************************************************/ + public void setKeyStorePassword(String keyStorePassword) + { + this.keyStorePassword = keyStorePassword; + } + + + + /******************************************************************************* + ** Fluent setter for keyStorePassword + *******************************************************************************/ + public QuickSearchTlsConfig withKeyStorePassword(String keyStorePassword) + { + this.keyStorePassword = keyStorePassword; + return (this); + } + + + + /******************************************************************************* + ** Getter for keyStoreType + *******************************************************************************/ + public String getKeyStoreType() + { + return (this.keyStoreType); + } + + + + /******************************************************************************* + ** Setter for keyStoreType + *******************************************************************************/ + public void setKeyStoreType(String keyStoreType) + { + this.keyStoreType = keyStoreType; + } + + + + /******************************************************************************* + ** Fluent setter for keyStoreType + *******************************************************************************/ + public QuickSearchTlsConfig withKeyStoreType(String keyStoreType) + { + this.keyStoreType = keyStoreType; + return (this); + } + + + + /******************************************************************************* + ** Getter for hostnameVerification + *******************************************************************************/ + public Boolean getHostnameVerification() + { + return (this.hostnameVerification); + } + + + + /******************************************************************************* + ** Setter for hostnameVerification + *******************************************************************************/ + public void setHostnameVerification(Boolean hostnameVerification) + { + this.hostnameVerification = hostnameVerification; + } + + + + /******************************************************************************* + ** Fluent setter for hostnameVerification + *******************************************************************************/ + public QuickSearchTlsConfig withHostnameVerification(Boolean hostnameVerification) + { + this.hostnameVerification = hostnameVerification; + return (this); + } + + + + /******************************************************************************* + ** Getter for insecureSkipVerify + *******************************************************************************/ + public Boolean getInsecureSkipVerify() + { + return (this.insecureSkipVerify); + } + + + + /******************************************************************************* + ** Setter for insecureSkipVerify + *******************************************************************************/ + public void setInsecureSkipVerify(Boolean insecureSkipVerify) + { + this.insecureSkipVerify = insecureSkipVerify; + } + + + + /******************************************************************************* + ** Fluent setter for insecureSkipVerify + *******************************************************************************/ + public QuickSearchTlsConfig withInsecureSkipVerify(Boolean insecureSkipVerify) + { + this.insecureSkipVerify = insecureSkipVerify; + return (this); + } + + + + /******************************************************************************* + ** Getter for allowInsecureInProduction + *******************************************************************************/ + public Boolean getAllowInsecureInProduction() + { + return (this.allowInsecureInProduction); + } + + + + /******************************************************************************* + ** Setter for allowInsecureInProduction + *******************************************************************************/ + public void setAllowInsecureInProduction(Boolean allowInsecureInProduction) + { + this.allowInsecureInProduction = allowInsecureInProduction; + } + + + + /******************************************************************************* + ** Fluent setter for allowInsecureInProduction + *******************************************************************************/ + public QuickSearchTlsConfig withAllowInsecureInProduction(Boolean allowInsecureInProduction) + { + this.allowInsecureInProduction = allowInsecureInProduction; + return (this); + } + +} diff --git a/src/main/java/com/kingsrook/qbits/quicksearch/opensearch/OpenSearchTransportCustomizer.java b/src/main/java/com/kingsrook/qbits/quicksearch/opensearch/OpenSearchTransportCustomizer.java new file mode 100644 index 0000000..7ed8a22 --- /dev/null +++ b/src/main/java/com/kingsrook/qbits/quicksearch/opensearch/OpenSearchTransportCustomizer.java @@ -0,0 +1,56 @@ +/* + * Copyright 2024 Kingsrook, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.kingsrook.qbits.quicksearch.opensearch; + + +import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder; +import org.opensearch.client.transport.httpclient5.ApacheHttpClient5TransportBuilder; + + +/******************************************************************************* + ** Escape hatch for hosts that need something the built-in auth modes do not + ** cover: bearer or JWT tokens, API-key headers, request interceptors, proxy + ** settings, or client certificates loaded from a custom source. + ** + ** Register an implementation on QuickSearchQBitConfig.transportCustomizer as a + ** QCodeReference. Both hooks run after the QBit has applied its own settings + ** (auth, TLS, timeouts) and before the transport is built. Not used for the + ** AWS_SIGV4 transport, which is built by the AWS SDK. + *******************************************************************************/ +public interface OpenSearchTransportCustomizer +{ + + /******************************************************************************* + ** Adjust the transport builder (default headers, request config callback). + *******************************************************************************/ + default ApacheHttpClient5TransportBuilder customizeTransport(ApacheHttpClient5TransportBuilder builder) + { + return (builder); + } + + + + /******************************************************************************* + ** Adjust the Apache HttpAsyncClient builder (interceptors, connection + ** manager, credentials). + *******************************************************************************/ + default HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder builder) + { + return (builder); + } + +} diff --git a/src/test/java/com/kingsrook/qbits/quicksearch/QuickSearchQBitConfigValidationTest.java b/src/test/java/com/kingsrook/qbits/quicksearch/QuickSearchQBitConfigValidationTest.java new file mode 100644 index 0000000..7436ac1 --- /dev/null +++ b/src/test/java/com/kingsrook/qbits/quicksearch/QuickSearchQBitConfigValidationTest.java @@ -0,0 +1,334 @@ +/* + * Copyright 2024 Kingsrook, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.kingsrook.qbits.quicksearch; + + +import java.util.ArrayList; +import java.util.List; +import com.kingsrook.qqq.backend.core.model.metadata.QBackendMetaData; +import com.kingsrook.qqq.backend.core.model.metadata.QInstance; +import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData; +import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType; +import com.kingsrook.qqq.backend.core.model.metadata.scheduleing.simple.SimpleSchedulerMetaData; +import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData; +import com.kingsrook.qqq.backend.core.modules.backend.implementations.memory.MemoryBackendModule; +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; + + +/******************************************************************************* + ** Validation rules added for 1.0: URLs, auth modes, TLS, index names, field + ** existence, scheduling, secrets handling. + *******************************************************************************/ +class QuickSearchQBitConfigValidationTest +{ + + /******************************************************************************* + ** + *******************************************************************************/ + private QInstance instanceWithBackendAndCustomer() + { + QInstance qInstance = new QInstance(); + qInstance.addBackend(new QBackendMetaData().withName("memory").withBackendType(MemoryBackendModule.class)); + qInstance.addTable(new QTableMetaData() + .withName("customer") + .withBackendName("memory") + .withPrimaryKeyField("id") + .withField(new QFieldMetaData("id", QFieldType.INTEGER)) + .withField(new QFieldMetaData("name", QFieldType.STRING)) + .withField(new QFieldMetaData("modifyDate", QFieldType.DATE_TIME))); + return (qInstance); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + private QuickSearchQBitConfig baseConfig() + { + return (new QuickSearchQBitConfig() + .withBackendName("memory") + .withOpensearchHost("localhost") + .withOpensearchPort(9200) + .withOpensearchIndexName("app-search") + .withSearchableTable("customer", List.of(new SearchableFieldConfig("name")))); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + private List validate(QuickSearchQBitConfig config) + { + List errors = new ArrayList<>(); + config.validate(instanceWithBackendAndCustomer(), errors); + return (errors); + } + + + + @Test + void testUrl_replacesHostPortAndScheme() + { + QuickSearchQBitConfig config = baseConfig().withOpensearchHost(null).withOpensearchPort(null).withOpensearchUrl("https://search.example.com"); + assertThat(validate(config)).isEmpty(); + assertThat(config.getEffectiveScheme()).isEqualTo("https"); + assertThat(config.getEffectiveHost()).isEqualTo("search.example.com"); + assertThat(config.getEffectivePort()).isEqualTo(443); + + config.withOpensearchUrl("http://os.internal:9201"); + assertThat(config.getEffectivePort()).isEqualTo(9201); + assertThat(config.getEffectiveScheme()).isEqualTo("http"); + } + + + + @Test + void testUrl_invalid_addsError() + { + assertThat(validate(baseConfig().withOpensearchUrl("ftp://nope"))).anyMatch(e -> e.contains("http:// or https://")); + assertThat(validate(baseConfig().withOpensearchUrl("https://"))).anyMatch(e -> e.contains("host")); + } + + + + @Test + void testAuthMode_inferredFromCredentials() + { + assertThat(baseConfig().getEffectiveAuthMode()).isEqualTo(QuickSearchAuthMode.NONE); + assertThat(baseConfig().withOpensearchUsername("u").withOpensearchPassword("p").getEffectiveAuthMode()).isEqualTo(QuickSearchAuthMode.BASIC); + assertThat(baseConfig().withAuthMode(QuickSearchAuthMode.AWS_SIGV4).getEffectiveAuthMode()).isEqualTo(QuickSearchAuthMode.AWS_SIGV4); + } + + + + @Test + void testBasic_overPlainHttp_isRefusedUnlessAllowed() + { + QuickSearchQBitConfig config = baseConfig().withOpensearchUsername("u").withOpensearchPassword("p"); + assertThat(validate(config)).anyMatch(e -> e.contains("plain http")); + + assertThat(validate(config.withAllowPlaintextCredentials(true))).noneMatch(e -> e.contains("plain http")); + assertThat(validate(baseConfig().withOpensearchUsername("u").withOpensearchPassword("p").withUseSsl(true))).noneMatch(e -> e.contains("plain http")); + } + + + + @Test + void testBasic_explicitWithoutCredentials_addsError() + { + assertThat(validate(baseConfig().withAuthMode(QuickSearchAuthMode.BASIC).withUseSsl(true))).anyMatch(e -> e.contains("required for authMode BASIC")); + } + + + + @Test + void testNone_withCredentials_addsError() + { + assertThat(validate(baseConfig().withAuthMode(QuickSearchAuthMode.NONE).withOpensearchUsername("u").withOpensearchPassword("p"))).anyMatch(e -> e.contains("authMode is NONE")); + } + + + + @Test + void testSigV4_requirementsAndDefaults() + { + QuickSearchQBitConfig config = baseConfig().withAuthMode(QuickSearchAuthMode.AWS_SIGV4).withAwsRegion("us-east-1"); + List errors = validate(config); + assertThat(errors).anyMatch(e -> e.contains("requires an https connection")); + assertThat(config.getEffectiveAwsServiceName()).isEqualTo("es"); + + errors = validate(config.withUseSsl(true).withOpensearchUsername("u").withOpensearchPassword("p")); + assertThat(errors).anyMatch(e -> e.contains("not used with authMode AWS_SIGV4")); + + errors = validate(baseConfig().withAuthMode(QuickSearchAuthMode.AWS_SIGV4).withUseSsl(true).withAwsRegion("us-east-1").withAwsServiceName("bogus")); + assertThat(errors).anyMatch(e -> e.contains("es or aoss")); + + errors = validate(baseConfig().withAuthMode(QuickSearchAuthMode.AWS_SIGV4).withUseSsl(true).withAwsRegion("us-east-1")); + assertThat(errors).isEmpty(); + } + + + + @Test + void testTls_insecureSkipVerify_onlyForLoopbackUnlessOverridden() + { + QuickSearchQBitConfig loopback = baseConfig().withTls(new QuickSearchTlsConfig().withInsecureSkipVerify(true)); + assertThat(validate(loopback)).isEmpty(); + + QuickSearchQBitConfig remote = baseConfig().withOpensearchHost("search.example.com").withTls(new QuickSearchTlsConfig().withInsecureSkipVerify(true)); + assertThat(validate(remote)).anyMatch(e -> e.contains("insecureSkipVerify")); + + remote.getTls().withAllowInsecureInProduction(true); + assertThat(validate(remote)).noneMatch(e -> e.contains("insecureSkipVerify")); + } + + + + @Test + void testTls_caAndTrustStoreTogether_addsError() + { + QuickSearchQBitConfig config = baseConfig().withTls(new QuickSearchTlsConfig().withCaCertificatePath("/ca.pem").withTrustStorePath("/ts.p12")); + assertThat(validate(config)).anyMatch(e -> e.contains("not both")); + } + + + + @Test + void testIndexName_rules() + { + assertThat(QuickSearchQBitConfig.isValidIndexName("app-search_v2.1+x")).isTrue(); + assertThat(QuickSearchQBitConfig.isValidIndexName("App")).isFalse(); + assertThat(QuickSearchQBitConfig.isValidIndexName("_hidden")).isFalse(); + assertThat(QuickSearchQBitConfig.isValidIndexName("has space")).isFalse(); + assertThat(QuickSearchQBitConfig.isValidIndexName("a/b")).isFalse(); + assertThat(QuickSearchQBitConfig.isValidIndexName("..")).isFalse(); + assertThat(QuickSearchQBitConfig.isValidIndexName("a".repeat(256))).isFalse(); + + assertThat(validate(baseConfig().withOpensearchIndexName("Bad Name"))).anyMatch(e -> e.contains("opensearchIndexName must be lowercase")); + } + + + + @Test + void testFieldExistence_checkedAgainstInstanceTables() + { + QuickSearchQBitConfig config = baseConfig().withSearchableTables(null) + .withSearchableTable(new SearchableTableConfig("customer", List.of(new SearchableFieldConfig("name"), new SearchableFieldConfig("nope"))).withBasepullTimestampField("updatedAt")); + List errors = validate(config); + assertThat(errors).anyMatch(e -> e.contains("[nope] does not exist on table [customer]")); + assertThat(errors).anyMatch(e -> e.contains("basepullTimestampField [updatedAt] does not exist")); + + QuickSearchQBitConfig ok = baseConfig().withSearchableTables(null) + .withSearchableTable(new SearchableTableConfig("customer", List.of(new SearchableFieldConfig("name"))).withBasepullTimestampField(null)); + assertThat(validate(ok)).isEmpty(); + } + + + + @Test + void testTableName_withColon_addsError() + { + assertThat(validate(baseConfig().withSearchableTable("weird:name", List.of(new SearchableFieldConfig("x"))))).anyMatch(e -> e.contains("must not contain ':'")); + } + + + + @Test + void testScheduling_schedulerMustExist_andCronNeedsScheduler() + { + QInstance qInstance = instanceWithBackendAndCustomer(); + qInstance.addScheduler(new SimpleSchedulerMetaData().withName("sched")); + + List errors = new ArrayList<>(); + baseConfig().withSchedulerName("missing").validate(qInstance, errors); + assertThat(errors).anyMatch(e -> e.contains("Scheduler not found: missing")); + + errors = new ArrayList<>(); + baseConfig().withSchedulerName("sched").withReconcileCronExpression("0 0 3 * * ?").validate(qInstance, errors); + assertThat(errors).isEmpty(); + + errors = new ArrayList<>(); + baseConfig().withReconcileCronExpression("0 0 3 * * ?").validate(qInstance, errors); + assertThat(errors).anyMatch(e -> e.contains("requires schedulerName")); + + /////////////////////////////////////////////////////////////////////// + // an instance without schedulers cannot be checked; no error raised // + /////////////////////////////////////////////////////////////////////// + assertThat(validate(baseConfig().withSchedulerName("anything"))).isEmpty(); + } + + + + @Test + void testNumbers_mustBePositive() + { + List errors = validate(baseConfig() + .withConnectTimeoutMillis(0) + .withResponseTimeoutMillis(-1) + .withMaxConnections(0) + .withMaxSearchLimit(0) + .withBasepullRepeatSeconds(0) + .withBasepullOverlapSeconds(-5) + .withRunHistoryRetentionDays(0) + .withMaxFieldLength(0)); + assertThat(errors).anyMatch(e -> e.startsWith("connectTimeoutMillis")); + assertThat(errors).anyMatch(e -> e.startsWith("responseTimeoutMillis")); + assertThat(errors).anyMatch(e -> e.startsWith("maxConnections")); + assertThat(errors).anyMatch(e -> e.startsWith("maxSearchLimit")); + assertThat(errors).anyMatch(e -> e.startsWith("basepullRepeatSeconds")); + assertThat(errors).anyMatch(e -> e.startsWith("basepullOverlapSeconds")); + assertThat(errors).anyMatch(e -> e.startsWith("runHistoryRetentionDays")); + assertThat(errors).anyMatch(e -> e.startsWith("maxFieldLength")); + } + + + + @Test + void testValidate_nullInstance_isSafe() + { + List errors = new ArrayList<>(); + baseConfig().validate(null, errors); + assertThat(errors).isEmpty(); + } + + + + @Test + void testDeprecatedScheduledFlag_setsBothNewFlags() + { + @SuppressWarnings("deprecation") + QuickSearchQBitConfig config = baseConfig().withEnableScheduledProcesses(false); + assertThat(config.getEnableBasepullProcess()).isFalse(); + assertThat(config.getEnableMaintenanceProcesses()).isFalse(); + assertThat(config.getEnableScheduledProcesses()).isFalse(); + + config.withEnableBasepullProcess(true); + assertThat(config.getEnableScheduledProcesses()).isFalse(); + } + + + + @Test + void testToString_redactsSecrets() + { + String s = baseConfig().withOpensearchUsername("admin").withOpensearchPassword("hunter2").toString(); + assertThat(s).doesNotContain("hunter2").doesNotContain("admin").contains("[redacted]"); + } + + + + @Test + void testDefaults() + { + QuickSearchQBitConfig config = new QuickSearchQBitConfig(); + assertThat(config.getStartupMode()).isEqualTo(QuickSearchStartupMode.FAIL_FAST); + assertThat(config.getEnableBasepullProcess()).isTrue(); + assertThat(config.getEnableMaintenanceProcesses()).isTrue(); + assertThat(config.getConnectTimeoutMillis()).isEqualTo(5000); + assertThat(config.getResponseTimeoutMillis()).isEqualTo(60000); + assertThat(config.getMaxSearchLimit()).isEqualTo(100); + assertThat(config.getBasepullOverlapSeconds()).isEqualTo(300); + assertThat(config.getAdminPermissionRules().getPermissionBaseName()).isEqualTo(QuickSearchQBitConfig.DEFAULT_ADMIN_PERMISSION_BASE_NAME); + assertThat(config.getApplyRecordSecurityLocks()).isTrue(); + assertThat(config.getDefaultBackendNameForTables()).isNull(); + assertThat(config.withBackendName("memory").getDefaultBackendNameForTables()).isEqualTo("memory"); + } + +} diff --git a/src/test/java/com/kingsrook/qbits/quicksearch/QuickSearchRuntimeTest.java b/src/test/java/com/kingsrook/qbits/quicksearch/QuickSearchRuntimeTest.java new file mode 100644 index 0000000..8beccac --- /dev/null +++ b/src/test/java/com/kingsrook/qbits/quicksearch/QuickSearchRuntimeTest.java @@ -0,0 +1,156 @@ +/* + * Copyright 2024 Kingsrook, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.kingsrook.qbits.quicksearch; + + +import java.util.List; +import com.kingsrook.qqq.backend.core.actions.tables.InsertAction; +import com.kingsrook.qqq.backend.core.actions.tables.UpdateAction; +import com.kingsrook.qqq.backend.core.context.QContext; +import com.kingsrook.qqq.backend.core.exceptions.QException; +import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput; +import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateInput; +import com.kingsrook.qqq.backend.core.model.data.QRecord; +import com.kingsrook.qqq.backend.core.model.metadata.QInstance; +import com.kingsrook.qqq.backend.core.model.metadata.qbits.QBitMetaData; +import com.kingsrook.qbits.quicksearch.model.QuickSearchIndex; +import com.kingsrook.qbits.quicksearch.publisher.IndexEventPublisher; +import com.kingsrook.qbits.quicksearch.publisher.SynchronousIndexEventPublisher; +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.mock; + + +/******************************************************************************* + ** Unit test for QuickSearchRuntime + *******************************************************************************/ +class QuickSearchRuntimeTest extends BaseQuickSearchTest +{ + + /******************************************************************************* + ** Register a QBit with a runtime on the current QInstance. + *******************************************************************************/ + private QuickSearchRuntime registerRuntime(QuickSearchQBitConfig config) + { + QuickSearchRuntime runtime = new QuickSearchRuntime(config); + runtime.setDiscoveredTables(List.of(new QuickSearchableTableConfig().withTableName(TEST_ENTITY_TABLE).withPrimaryKeyField("id").withSearchableFields(List.of("name")))); + config.setRuntime(runtime); + + QInstance qInstance = QContext.getQInstance(); + qInstance.addQBit(new QBitMetaData().withGroupId("com.kingsrook.qbits").withArtifactId("quick-search").withVersion("test").withConfig(config)); + return (runtime); + } + + + + @Test + void testGet_resolvesThroughQInstanceQBits() + { + QuickSearchQBitContext.clear(); + assertThat(QuickSearchRuntime.get()).isNull(); + + QuickSearchRuntime runtime = registerRuntime(new QuickSearchQBitConfig().withBackendName(TEST_BACKEND_NAME).withOpensearchHost("localhost").withOpensearchPort(9200).withOpensearchIndexName("t")); + + assertThat(QuickSearchRuntime.get()).isSameAs(runtime); + assertThat(QuickSearchQBitContext.getConfig()).isSameAs(runtime.getConfig()); + assertThat(QuickSearchQBitContext.getTableConfig(TEST_ENTITY_TABLE)).isNotNull(); + } + + + + @Test + void testGetOrThrow_withoutRuntime_throws() + { + QuickSearchQBitContext.clear(); + assertThatThrownBy(QuickSearchRuntime::getOrThrow).isInstanceOf(QException.class).hasMessageContaining("not registered"); + } + + + + @Test + void testGetClient_isLazyAndDoesNotContactCluster() throws QException + { + QuickSearchRuntime runtime = registerRuntime(new QuickSearchQBitConfig().withBackendName(TEST_BACKEND_NAME).withOpensearchHost("127.0.0.1").withOpensearchPort(1).withOpensearchIndexName("t")); + assertThat(runtime.isIndexReady()).isFalse(); + assertThat(runtime.getClient()).isNotNull().isSameAs(runtime.getClient()); + assertThat(runtime.isIndexReady()).isFalse(); + runtime.close(); + runtime.close(); + } + + + + @Test + void testGetPublisher_prefersHostSuppliedPublisher() throws QException + { + IndexEventPublisher custom = mock(IndexEventPublisher.class); + QuickSearchRuntime runtime = registerRuntime(new QuickSearchQBitConfig().withBackendName(TEST_BACKEND_NAME).withOpensearchHost("127.0.0.1").withOpensearchPort(1).withOpensearchIndexName("t").withIndexEventPublisher(custom)); + assertThat(runtime.getPublisher()).isSameAs(custom); + + QuickSearchRuntime defaultRuntime = new QuickSearchRuntime(new QuickSearchQBitConfig().withBackendName(TEST_BACKEND_NAME).withOpensearchHost("127.0.0.1").withOpensearchPort(1).withOpensearchIndexName("t")); + assertThat(defaultRuntime.getPublisher()).isInstanceOf(SynchronousIndexEventPublisher.class); + defaultRuntime.close(); + } + + + + @Test + void testIsTableEnabled_readsRowAndCaches() throws QException + { + QuickSearchRuntime runtime = registerRuntime(new QuickSearchQBitConfig().withBackendName(TEST_BACKEND_NAME).withOpensearchHost("localhost").withOpensearchPort(9200).withOpensearchIndexName("t")); + + assertThat(runtime.isTableEnabled("notConfigured")).isFalse(); + assertThat(runtime.isTableEnabled(TEST_ENTITY_TABLE)).isTrue(); + + InsertInput insertInput = new InsertInput(); + insertInput.setTableName(QuickSearchIndex.TABLE_NAME); + insertInput.setRecords(List.of(new QRecord().withValue("tableName", TEST_ENTITY_TABLE).withValue("enabled", false))); + Integer id = new InsertAction().execute(insertInput).getRecords().get(0).getValueInteger("id"); + + ////////////////////////////////////////////////////////////// + // cached value survives the row change until invalidated // + ////////////////////////////////////////////////////////////// + assertThat(runtime.isTableEnabled(TEST_ENTITY_TABLE)).isTrue(); + runtime.invalidateEnabledCache(); + assertThat(runtime.isTableEnabled(TEST_ENTITY_TABLE)).isFalse(); + + UpdateInput updateInput = new UpdateInput(); + updateInput.setTableName(QuickSearchIndex.TABLE_NAME); + updateInput.setRecords(List.of(new QRecord().withValue("id", id).withValue("enabled", true))); + new UpdateAction().execute(updateInput); + runtime.invalidateEnabledCache(); + assertThat(runtime.isTableEnabled(TEST_ENTITY_TABLE)).isTrue(); + } + + + + @Test + void testStart_degradedSwallowsConnectionFailure_failFastThrows() + { + QuickSearchRuntime degraded = new QuickSearchRuntime(new QuickSearchQBitConfig().withBackendName(TEST_BACKEND_NAME).withOpensearchHost("127.0.0.1").withOpensearchPort(1).withOpensearchIndexName("t").withStartupMode(QuickSearchStartupMode.DEGRADED).withConnectTimeoutMillis(200).withResponseTimeoutMillis(500)); + assertThatCode(degraded::start).doesNotThrowAnyException(); + assertThat(degraded.isIndexReady()).isFalse(); + degraded.close(); + + QuickSearchRuntime failFast = new QuickSearchRuntime(new QuickSearchQBitConfig().withBackendName(TEST_BACKEND_NAME).withOpensearchHost("127.0.0.1").withOpensearchPort(1).withOpensearchIndexName("t").withConnectTimeoutMillis(200).withResponseTimeoutMillis(500)); + assertThatThrownBy(failFast::start).isInstanceOf(QException.class).hasMessageContaining("startupMode=DEGRADED"); + failFast.close(); + } + +}