Skip to content

Commit

Permalink
chore: user guide for aws sdk v2 signer
Browse files Browse the repository at this point in the history
Signed-off-by: Máté Lang <[email protected]>
  • Loading branch information
Máté Lang committed Jun 20, 2022
1 parent 8c64902 commit 1af5ede
Showing 1 changed file with 84 additions and 4 deletions.
88 changes: 84 additions & 4 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,28 @@ func main() {

## How to use IAMs as authentication method

Before starting, we strongly recommend reading the full AWS documentation regarding using IAM credentials to sign requests to OpenSearch APIs.
Before starting, we strongly recommend reading the full AWS documentation regarding using IAM credentials to sign
requests to OpenSearch APIs.
See [Identity and Access Management in Amazon OpenSearch Service.](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/ac.html)

> Even if you configure a completely open resource-based access policy, all requests to the OpenSearch Service configuration API must be signed. If your policies specify IAM users or roles, requests to the OpenSearch APIs also must be signed using AWS Signature Version 4.
> Even if you configure a completely open resource-based access policy, all requests to the OpenSearch Service
> configuration API must be signed. If your policies specify IAM users or roles, requests to the OpenSearch APIs also
> must
> be signed using AWS Signature Version 4.
>
See [Managed Domains signing-service requests.](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/ac.html#managedomains-signing-service-requests)

Here is some sample Go code that shows how to sign each OpenSearch request and automatically search for AWS credentials from the ~/.aws folder or environment variables:
Depending on the version of AWS SDK used, import the v1 or v2 request signer from `signer/aws` or `signer/awsv2`
respectively.
Both signers are equivalent in their functionality, they provide AWS Signature Version 4 (SigV4).

To read more about SigV4
see [Signature Version 4 signing process](https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html)

Here are some Go samples that show how to sign each OpenSearch request and automatically search for AWS credentials from
the ~/.aws folder or environment variables:

#### AWS SDK V1

```go
package main
Expand Down Expand Up @@ -173,7 +188,7 @@ func main() {
// Create an opensearch client and use the request-signer
client, err := opensearch.NewClient(opensearch.Config{
Addresses: []string{endpoint},
Signer: signer,
Signer: signer,
})
if err != nil {
log.Fatal("client creation err", err)
Expand Down Expand Up @@ -201,3 +216,68 @@ func main() {
log.Println("PING OK")
}
```

#### AWS SDK V2

```go
package main

import (
"context"
"io"
"log"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/opensearch-project/opensearch-go/v2"
"github.com/opensearch-project/opensearch-go/v2/opensearchapi"
requestsigner "github.com/opensearch-project/opensearch-go/v2/signer/awsv2"
)

const endpoint = "" // e.g. https://opensearch-domain.region.com

func main() {
ctx := context.Background()

cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
log.Fatal(err) // Do not log.fatal in a production ready app.
}

// Create an AWS request Signer and load AWS configuration using default config folder or env vars.
// See https://docs.aws.amazon.com/opensearch-service/latest/developerguide/request-signing.html#request-signing-go
signer, err := requestsigner.NewSigner(cfg)
if err != nil {
log.Fatal(err) // Do not log.fatal in a production ready app.
}

// Create an opensearch client and use the request-signer
client, err := opensearch.NewClient(opensearch.Config{
Addresses: []string{endpoint},
Signer: signer,
})
if err != nil {
log.Fatal("client creation err", err)
}

ping := opensearchapi.PingRequest{}

resp, err := ping.Do(ctx, client)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()

if resp.IsError() {
log.Println("ping response status ", resp.Status())

respBody, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal("response body read err", err)
}

log.Fatal("ping resp body", respBody)
}

log.Println("PING OK")
}
```

0 comments on commit 1af5ede

Please sign in to comment.