Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Commit 375f4ce

Browse files
Merge pull request #3218 from hashicorp/backport/kevin/lambda-tests/briefly-alive-pigeon
This pull request was automerged via backport-assistant
2 parents 8d453b5 + fa41b5a commit 375f4ce

File tree

3 files changed

+73
-34
lines changed

3 files changed

+73
-34
lines changed

Diff for: .changelog/3193.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
plugin/aws-lambda: Add platform config validation.
3+
```

Diff for: builtin/aws/lambda/platform.go

+38-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/aws/aws-sdk-go/service/elbv2"
2020
"github.com/aws/aws-sdk-go/service/iam"
2121
"github.com/aws/aws-sdk-go/service/lambda"
22+
validation "github.com/go-ozzo/ozzo-validation/v4"
2223
"github.com/hashicorp/go-hclog"
2324
"github.com/hashicorp/waypoint/builtin/aws/ecr"
2425
"github.com/hashicorp/waypoint/builtin/aws/utils"
@@ -38,10 +39,44 @@ type Platform struct {
3839
// ConfigSet is called after a configuration has been decoded
3940
// we can use this to validate the config
4041
func (p *Platform) ConfigSet(config interface{}) error {
41-
_, ok := config.(*Config)
42+
c, ok := config.(*Config)
4243
if !ok {
4344
// this should never happen
44-
return fmt.Errorf("Invalid configuration, expected *lambda.Config, got %s", reflect.TypeOf(config))
45+
return fmt.Errorf("Invalid configuration, expected *lambda.Config, got %q", reflect.TypeOf(config))
46+
}
47+
48+
// validate architecture
49+
if c.Architecture != "" {
50+
architectures := make([]interface{}, len(lambda.Architecture_Values()))
51+
52+
for i, ca := range lambda.Architecture_Values() {
53+
architectures[i] = ca
54+
}
55+
56+
var validArchitectures []string
57+
for _, arch := range lambda.Architecture_Values() {
58+
validArchitectures = append(validArchitectures, fmt.Sprintf("%q", arch))
59+
}
60+
61+
if err := utils.Error(validation.ValidateStruct(c,
62+
validation.Field(&c.Architecture,
63+
validation.In(architectures...).Error(fmt.Sprintf("Unsupported function architecture %q. Must be one of [%s], or left blank", c.Architecture, strings.Join(validArchitectures, ", "))),
64+
),
65+
)); err != nil {
66+
return err
67+
}
68+
}
69+
70+
// validate timeout - max is 900 (15 minutes)
71+
if c.Timeout != 0 {
72+
if err := utils.Error(validation.ValidateStruct(c,
73+
validation.Field(&c.Timeout,
74+
validation.Min(0).Error("Timeout must not be negative"),
75+
validation.Max(900).Error("Timeout must be less than or equal to 15 minutes"),
76+
),
77+
)); err != nil {
78+
return err
79+
}
4580
}
4681

4782
return nil
@@ -822,7 +857,7 @@ type Config struct {
822857
Memory int `hcl:"memory,optional"`
823858

824859
// The number of seconds to wait for a function to complete it's work.
825-
// Defaults to 256
860+
// Defaults to 60
826861
Timeout int `hcl:"timeout,optional"`
827862

828863
// The instruction set architecture that the function supports.

Diff for: builtin/aws/lambda/platform_test.go

+32-31
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
package lambda
22

33
import (
4-
"crypto/rand"
5-
"crypto/rsa"
6-
"crypto/x509"
7-
"encoding/base64"
84
"testing"
95

10-
"github.com/stretchr/testify/assert"
116
"github.com/stretchr/testify/require"
12-
"golang.org/x/crypto/ssh"
137
)
148

15-
// Just to validate that the key armoring works properly
16-
func TestKeys(t *testing.T) {
17-
hostkey, err := rsa.GenerateKey(rand.Reader, 4096)
18-
require.NoError(t, err)
19-
20-
hoststr := base64.StdEncoding.EncodeToString(x509.MarshalPKCS1PrivateKey(hostkey))
21-
22-
hostbytes, err := base64.StdEncoding.DecodeString(hoststr)
23-
require.NoError(t, err)
24-
25-
hkey, err := x509.ParsePKCS1PrivateKey(hostbytes)
26-
require.NoError(t, err)
27-
28-
assert.True(t, hostkey.Equal(hkey))
29-
30-
userstr := base64.StdEncoding.EncodeToString(x509.MarshalPKCS1PublicKey(&hostkey.PublicKey))
31-
32-
userbytes, err := base64.StdEncoding.DecodeString(userstr)
33-
require.NoError(t, err)
34-
35-
userKey, err := x509.ParsePKCS1PublicKey(userbytes)
36-
require.NoError(t, err)
37-
38-
_, err = ssh.NewPublicKey(userKey)
39-
require.NoError(t, err)
9+
func TestPlatformConfig(t *testing.T) {
10+
t.Run("empty is fine", func(t *testing.T) {
11+
var p Platform
12+
cfg := &Config{}
13+
require.NoError(t, p.ConfigSet(cfg))
14+
})
15+
16+
t.Run("disallows unsupported architecture", func(t *testing.T) {
17+
var p Platform
18+
cfg := &Config{
19+
Architecture: "foobar",
20+
}
21+
22+
require.EqualError(t, p.ConfigSet(cfg), "rpc error: code = InvalidArgument desc = Architecture: Unsupported function architecture \"foobar\". Must be one of [\"x86_64\", \"arm64\"], or left blank.")
23+
})
24+
25+
t.Run("disallows invalid timeout", func(t *testing.T) {
26+
var p Platform
27+
{
28+
cfg := &Config{
29+
Timeout: 901,
30+
}
31+
require.EqualError(t, p.ConfigSet(cfg), "rpc error: code = InvalidArgument desc = Timeout: Timeout must be less than or equal to 15 minutes.")
32+
}
33+
34+
{
35+
cfg := &Config{
36+
Timeout: -1,
37+
}
38+
require.EqualError(t, p.ConfigSet(cfg), "rpc error: code = InvalidArgument desc = Timeout: Timeout must not be negative.")
39+
}
40+
})
4041
}

0 commit comments

Comments
 (0)