Skip to content

Commit 06e65fe

Browse files
authored
Merge branch 'main' into codecommit-delete-event
2 parents 0a79053 + 2ff7818 commit 06e65fe

File tree

6 files changed

+121
-3
lines changed

6 files changed

+121
-3
lines changed

.github/workflows/tests.yml

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ on:
55

66
jobs:
77
test:
8-
name: run tests with code coverage
8+
name: run tests
99
runs-on: ubuntu-latest
1010
strategy:
1111
matrix:
1212
go:
13+
- "1.21"
1314
- "1.20"
1415
- "1.19"
1516
- "1.18"
@@ -24,12 +25,43 @@ jobs:
2425
uses: actions/setup-go@v3
2526
with:
2627
go-version: ${{ matrix.go }}
27-
28+
2829
- run: go version
2930

3031
- name: install lambda runtime interface emulator
3132
run: curl -L -o /usr/local/bin/aws-lambda-rie https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie-x86_64
32-
- run: chmod +x /usr/local/bin/aws-lambda-rie
33+
- run: chmod +x /usr/local/bin/aws-lambda-rie
34+
35+
- name: Check out code into the Go module directory
36+
uses: actions/checkout@v3
37+
38+
- name: Edit the go.mod file to allow tests to run for versions of go before 1.16
39+
run: >
40+
if [[ ${{ matrix.go }} < "1.16" ]]; then
41+
sed -i.bak 's/^.*retract.*$//' go.mod
42+
else
43+
echo "no edit required"
44+
fi
45+
46+
- name: go test
47+
run: go test -v -race ./...
48+
49+
coverage:
50+
name: run tests with coverage
51+
runs-on: ubuntu-latest
52+
strategy:
53+
matrix:
54+
go:
55+
- "1.21"
56+
steps:
57+
- name: Set up Go ${{ matrix.go }}
58+
uses: actions/setup-go@v3
59+
with:
60+
go-version: ${{ matrix.go }}
61+
62+
- name: install lambda runtime interface emulator
63+
run: curl -L -o /usr/local/bin/aws-lambda-rie https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie-x86_64
64+
- run: chmod +x /usr/local/bin/aws-lambda-rie
3365

3466
- name: Check out code into the Go module directory
3567
uses: actions/checkout@v3
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Sample Function
2+
3+
The following is a sample Lambda function that handles a SecretsManager secret rotation event.
4+
5+
```go
6+
package main
7+
8+
import (
9+
"fmt"
10+
"context"
11+
12+
"github.com/aws/aws-lambda-go/lambda"
13+
"github.com/aws/aws-lambda-go/events"
14+
)
15+
16+
func handler(ctx context.Context, event SecretsManagerSecretRotationEvent) error {
17+
fmt.Printf("rotating secret %s with token %s\n",
18+
event.SecretID, event.ClientRequestToken)
19+
20+
switch event.Step {
21+
case "createSecret":
22+
// create
23+
case "setSecret":
24+
// set
25+
case "finishSecret":
26+
// finish
27+
case "testSecret":
28+
// test
29+
}
30+
31+
return nil
32+
}
33+
34+
35+
func main() {
36+
lambda.Start(handler)
37+
}
38+
```

events/secretsmanager.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package events
2+
3+
// SecretsManagerSecretRotationEvent is the event passed to a Lambda function to handle
4+
// automatic secret rotation.
5+
//
6+
// https://docs.aws.amazon.com/secretsmanager/latest/userguide/rotating-secrets.html#rotate-secrets_how
7+
type SecretsManagerSecretRotationEvent struct {
8+
Step string `json:"Step"`
9+
SecretID string `json:"SecretId"`
10+
ClientRequestToken string `json:"ClientRequestToken"`
11+
}

events/secretsmanager_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package events
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/aws/aws-lambda-go/events/test"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestSecretsManagerSecretRotationEventMarshaling(t *testing.T) {
12+
13+
// 1. read JSON from file
14+
inputJSON := test.ReadJSONFromFile(t, "./testdata/secretsmanager-secret-rotation-event.json")
15+
16+
// 2. de-serialize into Go object
17+
var inputEvent SecretsManagerSecretRotationEvent
18+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
19+
t.Errorf("could not unmarshal event. details: %v", err)
20+
}
21+
22+
// 3. serialize to JSON
23+
outputJSON, err := json.Marshal(inputEvent)
24+
if err != nil {
25+
t.Errorf("could not marshal event. details: %v", err)
26+
}
27+
28+
// 4. check result
29+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
30+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"Step": "createSecret",
3+
"SecretId": "arn:aws:secretsmanager:us-east-1:111122223333:secret:id-ABCD1E",
4+
"ClientRequestToken": "1ab23456-cde7-8912-34fg-h56i78j9k12l"
5+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ require (
99
github.com/pmezard/go-difflib v1.0.0 // indirect
1010
gopkg.in/yaml.v3 v3.0.1 // indirect
1111
)
12+
13+
retract v1.39.0

0 commit comments

Comments
 (0)