2022-03-22 11:03:15 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
|
|
|
|
"github.com/edgelesssys/constellation/cli/cloud/cloudtypes"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateSecurityGroup(t *testing.T) {
|
|
|
|
testInput := SecurityGroupInput{
|
|
|
|
Inbound: cloudtypes.Firewall{
|
|
|
|
{
|
|
|
|
Description: "perm1",
|
|
|
|
Protocol: "TCP",
|
|
|
|
IPRange: "192.0.2.0/24",
|
2022-04-26 11:09:03 -04:00
|
|
|
FromPort: 22,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "perm2",
|
|
|
|
Protocol: "UDP",
|
|
|
|
IPRange: "192.0.2.0/24",
|
2022-04-26 11:09:03 -04:00
|
|
|
FromPort: 4433,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: cloudtypes.Firewall{
|
|
|
|
{
|
|
|
|
Description: "perm3",
|
|
|
|
Protocol: "TCP",
|
|
|
|
IPRange: "192.0.2.0/24",
|
2022-04-26 11:09:03 -04:00
|
|
|
FromPort: 4040,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
var noErr error
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
api stubAPI
|
|
|
|
securityGroup string
|
|
|
|
input SecurityGroupInput
|
|
|
|
wantErr bool
|
|
|
|
wantSecurityGroup string
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"create security group": {
|
2022-04-26 10:54:05 -04:00
|
|
|
api: stubAPI{securityGroup: types.SecurityGroup{GroupId: aws.String("sg-test")}},
|
|
|
|
input: testInput,
|
|
|
|
wantSecurityGroup: "sg-test",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"create security group without permissions": {
|
2022-04-26 10:54:05 -04:00
|
|
|
api: stubAPI{securityGroup: types.SecurityGroup{GroupId: aws.String("sg-test")}},
|
|
|
|
input: SecurityGroupInput{},
|
|
|
|
wantSecurityGroup: "sg-test",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"client already has security group": {
|
|
|
|
api: stubAPI{},
|
|
|
|
securityGroup: "sg-test",
|
|
|
|
input: testInput,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"create returns nil security group ID": {
|
2022-04-26 10:54:05 -04:00
|
|
|
api: stubAPI{securityGroup: types.SecurityGroup{GroupId: nil}},
|
|
|
|
input: testInput,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"create API error": {
|
2022-04-26 10:54:05 -04:00
|
|
|
api: stubAPI{createSecurityGroupErr: someErr},
|
|
|
|
input: testInput,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"create DryRun API error": {
|
2022-04-26 10:54:05 -04:00
|
|
|
api: stubAPI{createSecurityGroupDryRunErr: &someErr},
|
|
|
|
input: testInput,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"create DryRun missing expected error": {
|
2022-04-26 10:54:05 -04:00
|
|
|
api: stubAPI{createSecurityGroupDryRunErr: &noErr},
|
|
|
|
input: testInput,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"authorize error": {
|
|
|
|
api: stubAPI{
|
|
|
|
securityGroup: types.SecurityGroup{GroupId: aws.String("sg-test")},
|
|
|
|
authorizeSecurityGroupIngressErr: someErr,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
input: testInput,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
client, err := newClient(tc.api)
|
|
|
|
require.NoError(err)
|
|
|
|
client.securityGroup = tc.securityGroup
|
|
|
|
|
|
|
|
err = client.CreateSecurityGroup(context.Background(), tc.input)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantSecurityGroup, client.securityGroup)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteSecurityGroup(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
var noErr error
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
api stubAPI
|
|
|
|
securityGroup string
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"delete security group": {
|
|
|
|
api: stubAPI{},
|
|
|
|
securityGroup: "sg-test",
|
|
|
|
},
|
|
|
|
"client without security group": {
|
|
|
|
api: stubAPI{},
|
|
|
|
},
|
|
|
|
"delete API error": {
|
|
|
|
api: stubAPI{deleteSecurityGroupErr: someErr},
|
|
|
|
securityGroup: "sg-test",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"delete DryRun API error": {
|
|
|
|
api: stubAPI{deleteSecurityGroupDryRunErr: &someErr},
|
|
|
|
securityGroup: "sg-test",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"delete DryRun missing expected error": {
|
|
|
|
api: stubAPI{deleteSecurityGroupDryRunErr: &noErr},
|
|
|
|
securityGroup: "sg-test",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
client, err := newClient(tc.api)
|
|
|
|
require.NoError(err)
|
|
|
|
client.securityGroup = tc.securityGroup
|
|
|
|
|
|
|
|
err = client.DeleteSecurityGroup(context.Background())
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Empty(client.securityGroup)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizeSecurityGroup(t *testing.T) {
|
|
|
|
testInput := SecurityGroupInput{
|
|
|
|
Inbound: cloudtypes.Firewall{
|
|
|
|
{
|
|
|
|
Description: "perm1",
|
|
|
|
Protocol: "TCP",
|
|
|
|
IPRange: " 192.0.2.0/24",
|
2022-04-26 11:09:03 -04:00
|
|
|
FromPort: 22,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "perm2",
|
|
|
|
Protocol: "UDP",
|
|
|
|
IPRange: "192.0.2.0/24",
|
2022-04-26 11:09:03 -04:00
|
|
|
FromPort: 4433,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: cloudtypes.Firewall{
|
|
|
|
{
|
|
|
|
Description: "perm3",
|
|
|
|
Protocol: "TCP",
|
|
|
|
IPRange: "192.0.2.0/24",
|
2022-04-26 11:09:03 -04:00
|
|
|
FromPort: 4040,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
var noErr error
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
api stubAPI
|
|
|
|
securityGroup string
|
|
|
|
input SecurityGroupInput
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"authorize": {
|
|
|
|
api: stubAPI{},
|
|
|
|
securityGroup: "sg-test",
|
|
|
|
input: testInput,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"client without security group": {
|
2022-04-26 10:54:05 -04:00
|
|
|
api: stubAPI{},
|
|
|
|
input: testInput,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"authorizeIngress API error": {
|
|
|
|
api: stubAPI{authorizeSecurityGroupIngressErr: someErr},
|
|
|
|
securityGroup: "sg-test",
|
|
|
|
input: testInput,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"authorizeIngress DryRun API error": {
|
|
|
|
api: stubAPI{authorizeSecurityGroupIngressDryRunErr: &someErr},
|
|
|
|
securityGroup: "sg-test",
|
|
|
|
input: testInput,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"authorizeIngress DryRun missing expected error": {
|
|
|
|
api: stubAPI{authorizeSecurityGroupIngressDryRunErr: &noErr},
|
|
|
|
securityGroup: "sg-test",
|
|
|
|
input: testInput,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"authorizeEgress API error": {
|
|
|
|
api: stubAPI{authorizeSecurityGroupEgressErr: someErr},
|
|
|
|
securityGroup: "sg-test",
|
|
|
|
input: testInput,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"authorizeEgress DryRun API error": {
|
|
|
|
api: stubAPI{authorizeSecurityGroupEgressDryRunErr: &someErr},
|
|
|
|
securityGroup: "sg-test",
|
|
|
|
input: testInput,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"authorizeEgress DryRun missing expected error": {
|
|
|
|
api: stubAPI{authorizeSecurityGroupEgressDryRunErr: &noErr},
|
|
|
|
securityGroup: "sg-test",
|
|
|
|
input: testInput,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
client, err := newClient(tc.api)
|
|
|
|
require.NoError(err)
|
|
|
|
client.securityGroup = tc.securityGroup
|
|
|
|
|
|
|
|
err = client.authorizeSecurityGroup(context.Background(), tc.input)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|