Repository: opensearch-project/opensearch-go
Pull request #102 (2022-04-18 16:47:06)

Support service configuration
### Description
Create new method to accept AWS service name
as input.
 
### Check List
- [x] Commits are signed per the DCO using --signoff 

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check [here](https://github.com/opensearch-project/OpenSearch/blob/main/CONTRIBUTING.md#developer-certificate-of-origin).



Patch:
diff --git a/.github/workflows/dco.yml b/.github/workflows/dco.yml
deleted file mode 100644
index 53ed530..0000000
--- a/.github/workflows/dco.yml
+++ /dev/null
@@ -1,18 +0,0 @@
-name: Developer Certificate of Origin Check
-
-on: [pull_request]
-
-jobs:
-  check:
-    runs-on: ubuntu-latest
-
-    steps:
-      - name: Get PR Commits
-        id: 'get-pr-commits'
-        uses: tim-actions/get-pr-commits@v1.1.0
-        with:
-          token: ${{ secrets.GITHUB_TOKEN }}
-      - name: DCO Check
-        uses: tim-actions/dco@v1.1.0
-        with:
-          commits: ${{ steps.get-pr-commits.outputs.commits }}
\ No newline at end of file
diff --git a/.github/workflows/build.yml b/.github/workflows/lint.yml
similarity index 96%
rename from .github/workflows/build.yml
rename to .github/workflows/lint.yml
index 245a8d8..bb0f02b 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/lint.yml
@@ -1,4 +1,4 @@
-name: Build
+name: Lint check
 
 on:
   push:
diff --git a/signer/aws/aws.go b/signer/aws/aws.go
index 31999f8..42abc33 100644
--- a/signer/aws/aws.go
+++ b/signer/aws/aws.go
@@ -11,9 +11,11 @@ package aws
 
 import (
 	"bytes"
+	"errors"
 	"fmt"
 	"io"
 	"net/http"
+	"strings"
 	"time"
 
 	"github.com/aws/aws-sdk-go/aws/session"
@@ -29,15 +31,23 @@ type Signer struct {
 	service string
 }
 
-// NewSigner returns an instance of Signer
+// NewSigner returns an instance of Signer for AWS OpenSearchService
 func NewSigner(opts session.Options) (*Signer, error) {
+	return NewSignerWithService(opts, OpenSearchService)
+}
+
+// NewSignerWithService returns an instance of Signer for given service
+func NewSignerWithService(opts session.Options, service string) (*Signer, error) {
+	if len(strings.TrimSpace(service)) < 1 {
+		return nil, errors.New("service cannot be empty")
+	}
 	awsSession, err := session.NewSessionWithOptions(opts)
 	if err != nil {
 		return nil, fmt.Errorf("failed to get session from given option %v due to %s", opts, err)
 	}
 	return &Signer{
 		session: *awsSession,
-		service: OpenSearchService,
+		service: service,
 	}, nil
 }
 


Test patch:
diff --git a/signer/aws/aws_test.go b/signer/aws/aws_test.go
index cb75e79..3c049af 100644
--- a/signer/aws/aws_test.go
+++ b/signer/aws/aws_test.go
@@ -90,4 +90,41 @@ func TestV4Signer(t *testing.T) {
 		assert.NotEmpty(t, q.Get("Authorization"))
 		assert.NotEmpty(t, q.Get("X-Amz-Date"))
 	})
+
+	t.Run("sign request success with body for other AWS Services", func(t *testing.T) {
+		req, err := http.NewRequest(
+			http.MethodPost, "https://localhost:9200",
+			bytes.NewBuffer([]byte(`some data`)))
+		assert.NoError(t, err)
+		region := os.Getenv("AWS_REGION")
+		os.Setenv("AWS_REGION", "us-west-2")
+		defer func() {
+			os.Setenv("AWS_REGION", region)
+		}()
+		sessionOptions := session.Options{
+			Config: aws.Config{
+				Region:      aws.String("us-west-2"),
+				Credentials: credentials.NewStaticCredentials("AKID", "SECRET_KEY", "TOKEN"),
+			},
+		}
+		signer, err := NewSignerWithService(sessionOptions, "ec")
+		assert.NoError(t, err)
+		err = signer.SignRequest(req)
+		assert.NoError(t, err)
+		q := req.Header
+		assert.NotEmpty(t, q.Get("Authorization"))
+		assert.NotEmpty(t, q.Get("X-Amz-Date"))
+	})
+
+	t.Run("sign request failed due to invalid service", func(t *testing.T) {
+		sessionOptions := session.Options{
+			Config: aws.Config{
+				Region:      aws.String("us-west-2"),
+				Credentials: credentials.NewStaticCredentials("AKID", "SECRET_KEY", "TOKEN"),
+			},
+		}
+		_, err := NewSignerWithService(sessionOptions, "")
+		assert.EqualError(t, err, "service cannot be empty")
+
+	})
 }