Skip to content

Commit d66af91

Browse files
authored
add revised opensearch query builder package (#249)
## What Add revised openSearch query builder package `osquery`. Keep the old package for backwards compatibility. Notable changes: - mapping entirely handled by passed filter field mapping (no setting for appending `.keyword` to the filter field name anymore) - improved error handling: fail on invalid input rather than executing an ambiguous query - removed several specific filters - removed all nested query functionality Also added tests which test against a real openSearch instance. ## Why The old version was inconvenient to use and error handling was not ideal. ## References VTI-255
2 parents bb8ca4f + 5a3f542 commit d66af91

23 files changed

Lines changed: 2769 additions & 52 deletions

File tree

.github/actions/go-setup/action.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ description: 'Setup go and cache and go download mods'
44
runs:
55
using: "composite"
66
steps:
7-
- uses: actions/checkout@v4
87
- uses: actions/setup-go@v4
98
with:
109
cache: true

.github/workflows/codecov.yml

Lines changed: 0 additions & 24 deletions
This file was deleted.

.github/workflows/tests.yml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,29 @@ jobs:
1313
steps:
1414
- uses: actions/checkout@v5
1515
- uses: ./.github/actions/go-setup
16-
- run: |
17-
go test -v ./...
16+
- run: make test-codecov
17+
- name: Upload unit test coverage to Codecov
18+
uses: codecov/codecov-action@v5
19+
with:
20+
files: cov-unit-tests.out
21+
flags: unit-tests
22+
name: Unit Tests
23+
token: ${{ secrets.CODECOV_TOKEN }}
24+
fail_ci_if_error: true
25+
26+
opensearch-test:
27+
name: Execute Opensearch tests
28+
runs-on: ubuntu-latest
29+
steps:
30+
- uses: actions/checkout@v5
31+
- uses: ./.github/actions/go-setup
32+
- name: Execute OpenSearch tests
33+
run: make test-opensearch
34+
- name: Upload OpenSearch test coverage to Codecov
35+
uses: codecov/codecov-action@v5
36+
with:
37+
files: cov-os-tests.out
38+
flags: opensearch-tests
39+
name: OpenSearch Tests
40+
token: ${{ secrets.CODECOV_TOKEN }}
41+
fail_ci_if_error: true

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1+
# common IDEs
12
.idea
3+
.vscode
4+
5+
# coverage reports
6+
*.out

Makefile

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ REGISTRY := docker-gps.greenbone.net
33
# Define submodules
44
PKG_DIR := pkg
55

6+
.PHONY: all
67
all: build test
78

89
.PHONY: help
@@ -27,6 +28,7 @@ OS="$(shell go env var GOOS | xargs)"
2728
ALL_GO_DIRS := $(shell find $(PKG_DIR) -name '*.go' -exec dirname {} \; | sort -u)
2829

2930
# Clean up
31+
.PHONY: clean
3032
clean:
3133
go clean -i ./...
3234

@@ -64,19 +66,40 @@ build: build-common ## build go library packages
6466
go build -trimpath ./...
6567

6668
.PHONY: test
67-
test: ## run all tests
68-
go test -test.short ./...
69+
test: ## run short tests
70+
go test -short ./...
6971

7072
.PHONY: test-codecov
7173
test-codecov:
72-
go test -cover -coverprofile=coverage.txt ./...
73-
74-
.PHONY: all build test clean
74+
go test -cover -coverprofile=cov-unit-tests.out ./...
75+
76+
.PHONY: start-opensearch-test-service
77+
start-opensearch-test-service:
78+
docker compose -f ./pkg/openSearch/ostesting/compose.yml -p opensearch-test up -d --wait --wait-timeout=120
79+
80+
.PHONY: stop-opensearch-test-service
81+
stop-opensearch-test-service:
82+
docker compose -f ./pkg/openSearch/ostesting/compose.yml -p opensearch-test down -v
83+
84+
.PHONY: run-opensearch-tests
85+
run-opensearch-tests:
86+
TEST_OPENSEARCH=1 go test ./pkg/openSearch/osquery -coverprofile=cov-os-tests.out
87+
88+
# test-opensearch runs whole opensearch execution with docker setup and cleanup
89+
# we want to stop-opensearch-test-service regardless of the result of run-opensearch-tests
90+
# and return the result of run-opensearch-tests
91+
.PHONY: test-opensearch
92+
test-opensearch:
93+
$(MAKE) start-opensearch-test-service
94+
$(MAKE) run-opensearch-tests; \
95+
status=$$?; \
96+
$(MAKE) stop-opensearch-test-service; \
97+
exit $$status
7598

7699
.PHONY: generate_docs
77100
generate_docs: check_tools
78101
gomarkdoc -e --output '{{.Dir}}/README.md' \
79-
--exclude-dirs .,./pkg/configReader/helper,./pkg/dbcrypt/config,./pkg/openSearch/openSearchClient/config,./pkg/swagger/ginSwagger.go \
102+
--exclude-dirs .,./pkg/configReader/helper,./pkg/dbcrypt/config,./pkg/openSearch/openSearchClient/config \
80103
./...
81104

82105
check_tools:

pkg/openSearch/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ subpackages:
88
* [esextensions](esextension/README.md) - extensions for the esquery library
99
* [openSearchClient](openSearchClient/README.md) - a client for OpenSearch designed to allow easy mocking/
1010
* [openSearchQuery](openSearchQuery/README.md) - query builders for OpenSearch
11+
* [osquery](osquery/README.md) - query builders for OpenSearch (simplified version)
12+
* [ostesting](ostesting/README.md) - conveniently test against a real openSearch instance
1113

1214
# License
1315

1416
Copyright (C) 2022-2023 [Greenbone AG][Greenbone AG]
1517

1618
Licensed under the [GNU General Public License v3.0 or later](../../LICENSE).
17-
18-

pkg/openSearch/openSearchClient/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ func NewIndexFunction(openSearchProjectClient *opensearchapi.Client) *IndexFunct
567567

568568

569569
<a name="IndexFunction.AliasExists"></a>
570-
### func \(\*IndexFunction\) [AliasExists](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L219>)
570+
### func \(\*IndexFunction\) [AliasExists](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L238>)
571571

572572
```go
573573
func (i *IndexFunction) AliasExists(aliasName string) (bool, error)
@@ -594,7 +594,7 @@ func (i *IndexFunction) CreateOrPutAlias(aliasName string, indexNames ...string)
594594

595595

596596
<a name="IndexFunction.DeleteAliasFromIndex"></a>
597-
### func \(\*IndexFunction\) [DeleteAliasFromIndex](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L168>)
597+
### func \(\*IndexFunction\) [DeleteAliasFromIndex](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L187>)
598598

599599
```go
600600
func (i *IndexFunction) DeleteAliasFromIndex(indexName string, aliasName string) error
@@ -612,7 +612,7 @@ func (i *IndexFunction) DeleteIndex(indexName string) error
612612

613613

614614
<a name="IndexFunction.ForceMerge"></a>
615-
### func \(\*IndexFunction\) [ForceMerge](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L395>)
615+
### func \(\*IndexFunction\) [ForceMerge](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L414>)
616616

617617
```go
618618
func (i *IndexFunction) ForceMerge(index string, maximumNumberOfSegments int) error
@@ -621,7 +621,7 @@ func (i *IndexFunction) ForceMerge(index string, maximumNumberOfSegments int) er
621621

622622

623623
<a name="IndexFunction.GetIndexSettings"></a>
624-
### func \(\*IndexFunction\) [GetIndexSettings](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L342>)
624+
### func \(\*IndexFunction\) [GetIndexSettings](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L361>)
625625

626626
```go
627627
func (i *IndexFunction) GetIndexSettings(index string) (map[string]interface{}, error)
@@ -639,7 +639,7 @@ func (i *IndexFunction) GetIndexes(pattern string) ([]string, error)
639639

640640

641641
<a name="IndexFunction.GetIndexesForAlias"></a>
642-
### func \(\*IndexFunction\) [GetIndexesForAlias](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L248>)
642+
### func \(\*IndexFunction\) [GetIndexesForAlias](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L267>)
643643

644644
```go
645645
func (i *IndexFunction) GetIndexesForAlias(aliasName string) ([]string, error)
@@ -657,7 +657,7 @@ func (i *IndexFunction) IndexExists(indexName string) (bool, error)
657657

658658

659659
<a name="IndexFunction.IndexHasAlias"></a>
660-
### func \(\*IndexFunction\) [IndexHasAlias](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L194>)
660+
### func \(\*IndexFunction\) [IndexHasAlias](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L213>)
661661

662662
```go
663663
func (i *IndexFunction) IndexHasAlias(indexNames []string, aliasNames []string) (bool, error)
@@ -666,7 +666,7 @@ func (i *IndexFunction) IndexHasAlias(indexNames []string, aliasNames []string)
666666

667667

668668
<a name="IndexFunction.RefreshIndex"></a>
669-
### func \(\*IndexFunction\) [RefreshIndex](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L318>)
669+
### func \(\*IndexFunction\) [RefreshIndex](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L337>)
670670

671671
```go
672672
func (i *IndexFunction) RefreshIndex(index string) error
@@ -675,7 +675,7 @@ func (i *IndexFunction) RefreshIndex(index string) error
675675

676676

677677
<a name="IndexFunction.RemoveIndexesFromAlias"></a>
678-
### func \(\*IndexFunction\) [RemoveIndexesFromAlias](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L267>)
678+
### func \(\*IndexFunction\) [RemoveIndexesFromAlias](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L286>)
679679

680680
```go
681681
func (i *IndexFunction) RemoveIndexesFromAlias(indexesToRemove []string, aliasName string) error
@@ -684,7 +684,7 @@ func (i *IndexFunction) RemoveIndexesFromAlias(indexesToRemove []string, aliasNa
684684

685685

686686
<a name="IndexFunction.SetIndexSettings"></a>
687-
### func \(\*IndexFunction\) [SetIndexSettings](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L370>)
687+
### func \(\*IndexFunction\) [SetIndexSettings](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/openSearch/openSearchClient/indexFunctions.go#L389>)
688688

689689
```go
690690
func (i *IndexFunction) SetIndexSettings(index string, settingsBody io.Reader) error

pkg/openSearch/openSearchClient/opensearchProjectClient.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func NewOpenSearchProjectClient(ctx context.Context, config config.OpensearchCli
3636
Client: opensearch.Config{
3737
Transport: &http.Transport{
3838
TLSClientConfig: &tls.Config{
39-
InsecureSkipVerify: true, // nolint:gosec
39+
InsecureSkipVerify: true, //nolint:gosec
4040
},
4141
},
4242
Addresses: []string{

pkg/openSearch/openSearchQuery/boolQueryBuilder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func createMappedField(dtoField filter.RequestField, fieldMapping map[string]str
224224
entityName, ok := fieldMapping[dtoField.Name]
225225
if !ok {
226226
return filter.RequestField{}, filter.NewInvalidFilterFieldError(
227-
"Mapping for filter field '%s' is currently not implemented.", dtoField.Name)
227+
"mapping for filter field '%s' is currently not implemented", dtoField.Name)
228228
}
229229

230230
return filter.RequestField{

pkg/openSearch/openSearchQuery/compareHandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func HandleCompareOperatorNotBeginsWith(fieldName string, fieldKeys []string, fi
137137
})...,
138138
)
139139
} else { // for single values
140-
return esquery.Prefix(fieldName+".keyword", fieldValue.(string))
140+
return esquery.Bool().MustNot(esquery.Prefix(fieldName+".keyword", fieldValue.(string)))
141141
}
142142
}
143143

0 commit comments

Comments
 (0)