Skip to content

Commit 39be283

Browse files
authored
Merge pull request #90 from silinternational/develop
Production release - update AWS SDK to v2
2 parents 8f13f9b + a59d36a commit 39be283

13 files changed

+292
-258
lines changed

actions-services.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ services:
22
test:
33
build: .
44
environment:
5-
AWS_ENDPOINT: dynamo:8000
6-
AWS_DISABLE_SSL: "true"
5+
AWS_ENDPOINT: http://dynamo:8000
76
API_KEY_TABLE: ApiKey
87
WEBAUTHN_TABLE: WebAuthn
98
LAMBDA_ROLE: placeholder
10-
AWS_REGION: $AWS_REGION
9+
AWS_REGION: localhost
1110
GITHUB_REF_NAME: $GITHUB_REF_NAME
1211
STG_AWS_ACCESS_KEY_ID: $STG_AWS_ACCESS_KEY_ID
1312
STG_AWS_SECRET_ACCESS_KEY: $STG_AWS_SECRET_ACCESS_KEY

apikey.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ import (
1717
const ApiKeyTablePK = "value"
1818

1919
type ApiKey struct {
20-
Key string `json:"value"`
21-
Secret string `json:"-"`
22-
HashedSecret string `json:"hashedApiSecret"`
23-
Email string `json:"email"`
24-
CreatedAt int `json:"createdAt"`
25-
ActivatedAt int `json:"activatedAt"`
26-
Store *Storage `json:"-"`
20+
Key string `dynamodbav:"value" json:"value"`
21+
Secret string `dynamodbav:"-" json:"-"`
22+
HashedSecret string `dynamodbav:"hashedApiSecret" json:"hashedApiSecret"`
23+
Email string `dynamodbav:"email" json:"email"`
24+
CreatedAt int `dynamodbav:"createdAt" json:"createdAt"`
25+
ActivatedAt int `dynamodbav:"activatedAt" json:"activatedAt"`
26+
Store *Storage `dynamodbav:"-" json:"-"`
2727
}
2828

2929
func (k *ApiKey) Load() error {

config.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package mfa
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"log"
78

8-
"github.com/aws/aws-sdk-go/aws"
9+
"github.com/aws/aws-sdk-go-v2/aws"
10+
"github.com/aws/aws-sdk-go-v2/config"
911
)
1012

11-
var (
12-
storage *Storage
13-
envConfig EnvConfig
14-
)
13+
var envConfig EnvConfig
1514

1615
// EnvConfig holds environment specific configurations and is populated on init
1716
type EnvConfig struct {
@@ -20,23 +19,20 @@ type EnvConfig struct {
2019

2120
AwsEndpoint string `default:"" split_words:"true"`
2221
AwsDefaultRegion string `default:"" split_words:"true"`
23-
AwsDisableSSL bool `default:"false" split_words:"true"`
2422

25-
AWSConfig *aws.Config `json:"-"`
23+
AWSConfig aws.Config `json:"-"`
2624
}
2725

2826
func (e *EnvConfig) InitAWS() {
29-
e.AWSConfig = &aws.Config{
30-
DisableSSL: aws.Bool(e.AwsDisableSSL),
31-
}
32-
33-
if e.AwsEndpoint != "" {
34-
e.AWSConfig.Endpoint = aws.String(e.AwsEndpoint)
35-
}
36-
37-
if e.AwsDefaultRegion != "" {
38-
e.AWSConfig.Region = aws.String(e.AwsDefaultRegion)
27+
cfg, err := config.LoadDefaultConfig(
28+
context.Background(),
29+
config.WithRegion(e.AwsDefaultRegion),
30+
config.WithBaseEndpoint(e.AwsEndpoint),
31+
)
32+
if err != nil {
33+
panic("InitAWS failed at LoadDefaultConfig: " + err.Error())
3934
}
35+
e.AWSConfig = cfg
4036
}
4137

4238
func (e *EnvConfig) String() string {

docker-compose.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ services:
2626
ports:
2727
- 8080
2828
environment:
29-
AWS_ENDPOINT: dynamo:8000
30-
AWS_DEFAULT_REGION: us-east-1
29+
AWS_ENDPOINT: http://dynamo:8000
30+
AWS_DEFAULT_REGION: localhost
3131
AWS_ACCESS_KEY_ID: abc123
3232
AWS_SECRET_ACCESS_KEY: abc123
33-
AWS_DISABLE_SSL: "true"
3433
API_KEY_TABLE: ApiKey
3534
WEBAUTHN_TABLE: WebAuthn
3635
depends_on:

fixtures_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package mfa
22

33
import (
4-
"github.com/aws/aws-sdk-go/aws"
5-
"github.com/aws/aws-sdk-go/service/dynamodb"
4+
"context"
5+
6+
"github.com/aws/aws-sdk-go-v2/aws"
7+
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
68
"github.com/go-webauthn/webauthn/webauthn"
79
)
810

@@ -16,7 +18,7 @@ type baseTestConfig struct {
1618
func getDBConfig(ms *MfaSuite) baseTestConfig {
1719
awsConfig := testAwsConfig()
1820
envCfg := testEnvConfig(awsConfig)
19-
localStorage, err := NewStorage(&awsConfig)
21+
localStorage, err := NewStorage(awsConfig)
2022
ms.NoError(err, "failed creating local storage for test")
2123

2224
web, err := webauthn.New(&webauthn.Config{
@@ -100,11 +102,10 @@ func getTestWebauthnUsers(ms *MfaSuite, config baseTestConfig) []DynamoUser {
100102
TableName: aws.String(config.EnvConfig.WebauthnTable),
101103
}
102104

103-
results, err := config.Storage.client.Scan(params)
105+
ctx := context.Background()
106+
results, err := config.Storage.client.Scan(ctx, params)
104107
ms.NoError(err, "failed to scan storage for new user entries")
105-
106-
resultsStr := formatDynamoResults(results)
107-
ms.Contains(resultsStr, "Count: 3", "initial data wasn't saved properly")
108+
ms.Equal(int32(3), results.Count, "Count:3", "initial data wasn't saved properly")
108109

109110
return []DynamoUser{testUser0, testUser1, testUser2}
110111
}

go.mod

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ go 1.23
44

55
require (
66
github.com/aws/aws-lambda-go v1.47.0
7-
github.com/aws/aws-sdk-go v1.55.6
7+
github.com/aws/aws-sdk-go-v2 v1.33.0
8+
github.com/aws/aws-sdk-go-v2/config v1.29.1
9+
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.15.28
10+
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.39.5
811
github.com/fxamacker/cbor/v2 v2.7.0
912
github.com/go-webauthn/webauthn v0.11.2
1013
github.com/gorilla/mux v1.8.1
@@ -16,6 +19,19 @@ require (
1619
)
1720

1821
require (
22+
github.com/aws/aws-sdk-go-v2/credentials v1.17.54 // indirect
23+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.24 // indirect
24+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.28 // indirect
25+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.28 // indirect
26+
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
27+
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.24.15 // indirect
28+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1 // indirect
29+
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.10.9 // indirect
30+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.9 // indirect
31+
github.com/aws/aws-sdk-go-v2/service/sso v1.24.11 // indirect
32+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.10 // indirect
33+
github.com/aws/aws-sdk-go-v2/service/sts v1.33.9 // indirect
34+
github.com/aws/smithy-go v1.22.1 // indirect
1935
github.com/davecgh/go-spew v1.1.1 // indirect
2036
github.com/go-webauthn/x v0.1.16 // indirect
2137
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect

go.sum

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,39 @@
11
github.com/aws/aws-lambda-go v1.47.0 h1:0H8s0vumYx/YKs4sE7YM0ktwL2eWse+kfopsRI1sXVI=
22
github.com/aws/aws-lambda-go v1.47.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A=
3-
github.com/aws/aws-sdk-go v1.55.6 h1:cSg4pvZ3m8dgYcgqB97MrcdjUmZ1BeMYKUxMMB89IPk=
4-
github.com/aws/aws-sdk-go v1.55.6/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
3+
github.com/aws/aws-sdk-go-v2 v1.33.0 h1:Evgm4DI9imD81V0WwD+TN4DCwjUMdc94TrduMLbgZJs=
4+
github.com/aws/aws-sdk-go-v2 v1.33.0/go.mod h1:P5WJBrYqqbWVaOxgH0X/FYYD47/nooaPOZPlQdmiN2U=
5+
github.com/aws/aws-sdk-go-v2/config v1.29.1 h1:JZhGawAyZ/EuJeBtbQYnaoftczcb2drR2Iq36Wgz4sQ=
6+
github.com/aws/aws-sdk-go-v2/config v1.29.1/go.mod h1:7bR2YD5euaxBhzt2y/oDkt3uNRb6tjFp98GlTFueRwk=
7+
github.com/aws/aws-sdk-go-v2/credentials v1.17.54 h1:4UmqeOqJPvdvASZWrKlhzpRahAulBfyTJQUaYy4+hEI=
8+
github.com/aws/aws-sdk-go-v2/credentials v1.17.54/go.mod h1:RTdfo0P0hbbTxIhmQrOsC/PquBZGabEPnCaxxKRPSnI=
9+
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.15.28 h1:Q5xJGlNgUJ7nnL4klwoaEimWJo3N6B6S8y/fMGG165I=
10+
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.15.28/go.mod h1:wIjOAtUwNtKiZXq7wD1aZvrjcr2AJwE7pmUUWXyz5Es=
11+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.24 h1:5grmdTdMsovn9kPZPI23Hhvp0ZyNm5cRO+IZFIYiAfw=
12+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.24/go.mod h1:zqi7TVKTswH3Ozq28PkmBmgzG1tona7mo9G2IJg4Cis=
13+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.28 h1:igORFSiH3bfq4lxKFkTSYDhJEUCYo6C8VKiWJjYwQuQ=
14+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.28/go.mod h1:3So8EA/aAYm36L7XIvCVwLa0s5N0P7o2b1oqnx/2R4g=
15+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.28 h1:1mOW9zAUMhTSrMDssEHS/ajx8JcAj/IcftzcmNlmVLI=
16+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.28/go.mod h1:kGlXVIWDfvt2Ox5zEaNglmq0hXPHgQFNMix33Tw22jA=
17+
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 h1:VaRN3TlFdd6KxX1x3ILT5ynH6HvKgqdiXoTxAF4HQcQ=
18+
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1/go.mod h1:FbtygfRFze9usAadmnGJNc8KsP346kEe+y2/oyhGAGc=
19+
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.39.5 h1:RLbuYls/4gmY3AIHVyCLZgRjclRlSbUEUXLeva6C81Y=
20+
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.39.5/go.mod h1:2xlKGs8OTgN92fRVfP4EgFgQGhYwVI7LQ2PLQ0tIFAQ=
21+
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.24.15 h1:c6fGxhbI9ffZquEkJQATpam3vchGuEEQXgWwxQAy3o4=
22+
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.24.15/go.mod h1:SnMeleniez26QKaqTeco4TSxBU3WzRpGu6HELM6OyQ8=
23+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1 h1:iXtILhvDxB6kPvEXgsDhGaZCSC6LQET5ZHSdJozeI0Y=
24+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1/go.mod h1:9nu0fVANtYiAePIBh2/pFUSwtJ402hLnp854CNoDOeE=
25+
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.10.9 h1:ramlTFqWSsOt4Y/skpd30D8oI0kfKf5wd1Yu9C5HhPw=
26+
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.10.9/go.mod h1:+B//vxKaB6Z/HfJfRV4ikLz0M7nIcKheHKm96FuaRrs=
27+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.9 h1:TQmKDyETFGiXVhZfQ/I0cCFziqqX58pi4tKJGYGFSz0=
28+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.9/go.mod h1:HVLPK2iHQBUx7HfZeOQSEu3v2ubZaAY2YPbAm5/WUyY=
29+
github.com/aws/aws-sdk-go-v2/service/sso v1.24.11 h1:kuIyu4fTT38Kj7YCC7ouNbVZSSpqkZ+LzIfhCr6Dg+I=
30+
github.com/aws/aws-sdk-go-v2/service/sso v1.24.11/go.mod h1:Ro744S4fKiCCuZECXgOi760TiYylUM8ZBf6OGiZzJtY=
31+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.10 h1:l+dgv/64iVlQ3WsBbnn+JSbkj01jIi+SM0wYsj3y/hY=
32+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.10/go.mod h1:Fzsj6lZEb8AkTE5S68OhcbBqeWPsR8RnGuKPr8Todl8=
33+
github.com/aws/aws-sdk-go-v2/service/sts v1.33.9 h1:BRVDbewN6VZcwr+FBOszDKvYeXY1kJ+GGMCcpghlw0U=
34+
github.com/aws/aws-sdk-go-v2/service/sts v1.33.9/go.mod h1:f6vjfZER1M17Fokn0IzssOTMT2N8ZSq+7jnNF0tArvw=
35+
github.com/aws/smithy-go v1.22.1 h1:/HPHZQ0g7f4eUeK6HKglFz8uwVfZKgoI25rb/J+dnro=
36+
github.com/aws/smithy-go v1.22.1/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg=
537
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
638
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
739
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

storage.go

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
package mfa
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67

7-
"github.com/aws/aws-sdk-go/aws"
8-
"github.com/aws/aws-sdk-go/aws/session"
9-
"github.com/aws/aws-sdk-go/service/dynamodb"
10-
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
8+
"github.com/aws/aws-sdk-go-v2/aws"
9+
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
10+
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
11+
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
1112
)
1213

1314
// Storage provides wrapper methods for interacting with DynamoDB
1415
type Storage struct {
15-
awsSession *session.Session
16-
client *dynamodb.DynamoDB
16+
client *dynamodb.Client
1717
}
1818

19-
func NewStorage(config *aws.Config) (*Storage, error) {
19+
// NewStorage creates a new Storage service, which includes a new DynamoDB Client
20+
func NewStorage(config aws.Config) (*Storage, error) {
2021
s := Storage{}
21-
22-
var err error
23-
s.awsSession, err = session.NewSession(config)
24-
if err != nil {
25-
return &Storage{}, err
26-
}
27-
28-
s.client = dynamodb.New(s.awsSession)
22+
s.client = dynamodb.NewFromConfig(config, func(o *dynamodb.Options) {
23+
o.EndpointOptions.DisableHTTPS = config.BaseEndpoint != nil
24+
})
2925
if s.client == nil {
3026
return nil, fmt.Errorf("failed to create new dynamo client")
3127
}
@@ -35,7 +31,7 @@ func NewStorage(config *aws.Config) (*Storage, error) {
3531

3632
// Store puts item at key.
3733
func (s *Storage) Store(table string, item interface{}) error {
38-
av, err := dynamodbattribute.MarshalMap(item)
34+
av, err := attributevalue.MarshalMap(item)
3935
if err != nil {
4036
return err
4137
}
@@ -45,7 +41,8 @@ func (s *Storage) Store(table string, item interface{}) error {
4541
TableName: aws.String(table),
4642
}
4743

48-
_, err = s.client.PutItem(input)
44+
ctx := context.Background()
45+
_, err = s.client.PutItem(ctx, input)
4946
return err
5047
}
5148

@@ -62,21 +59,20 @@ func (s *Storage) Load(table, attrName, attrVal string, item interface{}) error
6259
}
6360

6461
input := &dynamodb.GetItemInput{
65-
Key: map[string]*dynamodb.AttributeValue{
66-
attrName: {
67-
S: aws.String(attrVal),
68-
},
62+
Key: map[string]types.AttributeValue{
63+
attrName: &types.AttributeValueMemberS{Value: attrVal},
6964
},
7065
TableName: aws.String(table),
7166
ConsistentRead: aws.Bool(false),
7267
}
7368

74-
result, err := s.client.GetItem(input)
69+
ctx := context.Background()
70+
result, err := s.client.GetItem(ctx, input)
7571
if err != nil {
7672
return err
7773
}
7874

79-
return dynamodbattribute.UnmarshalMap(result.Item, item)
75+
return attributevalue.UnmarshalMap(result.Item, item)
8076
}
8177

8278
// Delete deletes key.
@@ -89,14 +85,13 @@ func (s *Storage) Delete(table, attrName, attrVal string) error {
8985
}
9086

9187
input := &dynamodb.DeleteItemInput{
92-
Key: map[string]*dynamodb.AttributeValue{
93-
attrName: {
94-
S: aws.String(attrVal),
95-
},
88+
Key: map[string]types.AttributeValue{
89+
attrName: &types.AttributeValueMemberS{Value: attrVal},
9690
},
9791
TableName: aws.String(table),
9892
}
9993

100-
_, err := s.client.DeleteItem(input)
94+
ctx := context.Background()
95+
_, err := s.client.DeleteItem(ctx, input)
10196
return err
10297
}

storage_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
package mfa
22

33
import (
4+
"context"
45
"os"
56
"testing"
67

7-
"github.com/aws/aws-sdk-go/aws"
8-
"github.com/aws/aws-sdk-go/aws/session"
8+
"github.com/aws/aws-sdk-go-v2/config"
99
)
1010

1111
const TestTableName = "WebAuthn"
12-
const DisableSSL = true
1312

1413
func (ms *MfaSuite) TestStorage_StoreLoad() {
1514
type fields struct {
1615
Table string
17-
AwsSession *session.Session
1816
AwsEndpoint string
1917
AwsRegion string
20-
AwsDisableSSL bool
2118
PrimaryKeyAttribute string
2219
}
2320
type args struct {
@@ -36,7 +33,6 @@ func (ms *MfaSuite) TestStorage_StoreLoad() {
3633
Table: TestTableName,
3734
AwsEndpoint: os.Getenv("AWS_ENDPOINT"),
3835
AwsRegion: os.Getenv("AWS_DEFAULT_REGION"),
39-
AwsDisableSSL: DisableSSL,
4036
PrimaryKeyAttribute: "uuid",
4137
},
4238
args: args{
@@ -52,19 +48,22 @@ func (ms *MfaSuite) TestStorage_StoreLoad() {
5248
}
5349
for _, tt := range tests {
5450
ms.T().Run(tt.name, func(t *testing.T) {
55-
s, err := NewStorage(&aws.Config{
56-
Endpoint: aws.String(tt.fields.AwsEndpoint),
57-
Region: aws.String(tt.fields.AwsRegion),
58-
DisableSSL: aws.Bool(tt.fields.AwsDisableSSL),
59-
})
51+
cfg, err := config.LoadDefaultConfig(
52+
context.Background(),
53+
config.WithRegion(tt.fields.AwsRegion),
54+
config.WithBaseEndpoint(tt.fields.AwsEndpoint),
55+
)
56+
ms.NoError(err)
57+
58+
s, err := NewStorage(cfg)
6059
ms.NoError(err)
6160
err = s.Store(tt.fields.Table, tt.args.item)
6261
if tt.wantErr {
6362
ms.Error(err, "didn't get an expected error Store()")
6463
return
6564
}
66-
6765
ms.NoError(err, "unexpected error with Store()")
66+
6867
var user DynamoUser
6968
ms.NoError(s.Load(tt.fields.Table, "uuid", tt.args.key, &user), "error with s.Load()")
7069

0 commit comments

Comments
 (0)