Skip to content

Commit 489fc90

Browse files
committed
Add unified security guide and eliminate raw JSON injection patterns
Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
1 parent 44c4b0e commit 489fc90

10 files changed

Lines changed: 475 additions & 9 deletions

USER_GUIDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ All `OPENSEARCH_GO_*` environment variables are evaluated once at client initial
510510

511511
## Guides by Topic
512512

513+
- [**Security**](guides/security.md) - TLS, credentials, input validation, index patterns, error disclosure
513514
- [Index Lifecycle](guides/index_lifecycle.md)
514515
- [Document Lifecycle](guides/document_lifecycle.md)
515516
- [Search](guides/search.md)

guides/advanced_index_actions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Advanced Index Actions
22

3+
> **Note:** Examples in this guide use raw JSON strings for request bodies because the `opensearchapi` package accepts `io.Reader`. When building bodies from user-supplied values, always use `opensearchutil.NewJSONReader` with a Go struct or map instead of string interpolation. See [Security](security.md#request-body-construction) for details.
4+
35
In this guide, we will look at some advanced index actions that are not covered in the [Index Lifecycle](index_lifecycle.md) guide.
46

57
## Setup

guides/data_streams.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Data Streams API
22

3+
> **Note:** Examples in this guide use raw JSON strings for request bodies because the `opensearchapi` package accepts `io.Reader`. When building bodies from user-supplied values, always use `opensearchutil.NewJSONReader` with a Go struct or map instead of string interpolation. See [Security](security.md#request-body-construction) for details.
4+
35
## Setup
46

57
First, create a client instance with the following code:

guides/document_lifecycle.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Document Lifecycle
22

3+
> **Note:** Examples in this guide use raw JSON strings for request bodies because the `opensearchapi` package accepts `io.Reader`. When building bodies from user-supplied values, always use `opensearchutil.NewJSONReader` with a Go struct or map instead of string interpolation. See [Security](security.md#request-body-construction) for details.
4+
35
This guide covers OpenSearch Golang Client API actions for Document Lifecycle. You'll learn how to create, read, update, and delete documents in your OpenSearch cluster. Whether you're new to OpenSearch or an experienced user, this guide provides the information you need to manage your document lifecycle effectively.
46

57
## Setup

guides/error_handling.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Error Handling and Partial Failures
22

3+
> **Note:** Examples in this guide use raw JSON strings for request bodies because the `opensearchapi` package accepts `io.Reader`. When building bodies from user-supplied values, always use `opensearchutil.NewJSONReader` with a Go struct or map instead of string interpolation. See [Security](security.md#request-body-construction) for details.
4+
35
## Overview
46

57
OpenSearch is a distributed system in which operations may partially succeed. Understanding how to detect and handle partial failures is essential for building reliable applications.

guides/index_lifecycle.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Index Lifecycle
22

3+
> **Note:** Examples in this guide use raw JSON strings for request bodies because the `opensearchapi` package accepts `io.Reader`. When building bodies from user-supplied values, always use `opensearchutil.NewJSONReader` with a Go struct or map instead of string interpolation. See [Security](security.md#request-body-construction) for details.
4+
35
This guide covers OpenSearch Golang Client API actions for Index Lifecycle. You'll learn how to create, get, update settings, update mapping, and delete indices in your OpenSearch cluster. We will also leverage index templates to create default settings and mappings for indices of certain patterns.
46

57
## Setup

guides/index_template.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Index Template
22

3+
> **Note:** Examples in this guide use raw JSON strings for request bodies because the `opensearchapi` package accepts `io.Reader`. When building bodies from user-supplied values, always use `opensearchutil.NewJSONReader` with a Go struct or map instead of string interpolation. See [Security](security.md#request-body-construction) for details.
4+
35
Index templates are a convenient way to define settings, mappings, and aliases for one or more indices when they are created. In this guide, you'll learn how to create an index template and apply it to an index.
46

57
## Setup

guides/search.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Search
22

3+
> **Note:** Examples in this guide use `opensearchutil.NewJSONReader` for request bodies that contain dynamic values. For static query strings, raw JSON is acceptable. When building bodies from user-supplied values, always use structured serialization. See [Security](security.md#request-body-construction) for details.
4+
35
OpenSearch provides a powerful search API that allows you to search for documents in an index. The search API supports a number of parameters that allow you to customize the search operation. In this guide, we will explore the search API and its parameters.
46

57
# Setup
@@ -21,6 +23,7 @@ import (
2123
"github.com/opensearch-project/opensearch-go/v4"
2224
"github.com/opensearch-project/opensearch-go/v4/opensearchapi"
2325
"github.com/opensearch-project/opensearch-go/v4/opensearchtransport"
26+
"github.com/opensearch-project/opensearch-go/v4/opensearchutil"
2427
)
2528

2629
func main() {
@@ -78,7 +81,11 @@ For search-heavy applications, you can configure the client to automatically rou
7881
opensearchapi.IndexReq{
7982
Index: exampleIndex,
8083
DocumentID: strconv.Itoa(i),
81-
Body: strings.NewReader(fmt.Sprintf(`{"title": "The Dark Knight %d", "director": "Christopher Nolan", "year": %d}`, i, 2008+i)),
84+
Body: opensearchutil.NewJSONReader(map[string]any{
85+
"title": fmt.Sprintf("The Dark Knight %d", i),
86+
"director": "Christopher Nolan",
87+
"year": 2008 + i,
88+
}),
8289
},
8390
)
8491
if err != nil {
@@ -227,7 +234,12 @@ The scroll example above has one weakness: if the index is updated while you are
227234
searchResp, err = client.Search(
228235
ctx,
229236
&opensearchapi.SearchReq{
230-
Body: strings.NewReader(fmt.Sprintf(`{ "pit": { "id": "%s", "keep_alive": "1m" } }`, pitCreateResp.PitID)),
237+
Body: opensearchutil.NewJSONReader(map[string]any{
238+
"pit": map[string]any{
239+
"id": pitCreateResp.PitID,
240+
"keep_alive": "1m",
241+
},
242+
}),
231243
Params: opensearchapi.SearchParams{
232244
Size: opensearchapi.ToPointer(5),
233245
Sort: []string{"year:desc"},
@@ -246,7 +258,13 @@ The scroll example above has one weakness: if the index is updated while you are
246258
searchResp, err = client.Search(
247259
ctx,
248260
&opensearchapi.SearchReq{
249-
Body: strings.NewReader(fmt.Sprintf(`{ "pit": { "id": "%s", "keep_alive": "1m" }, "search_after": [ "1994" ] }`, pitCreateResp.PitID)),
261+
Body: opensearchutil.NewJSONReader(map[string]any{
262+
"pit": map[string]any{
263+
"id": pitCreateResp.PitID,
264+
"keep_alive": "1m",
265+
},
266+
"search_after": []string{"1994"},
267+
}),
250268
Params: opensearchapi.SearchParams{
251269
Size: opensearchapi.ToPointer(5),
252270
Sort: []string{"year:desc"},

0 commit comments

Comments
 (0)