This repository has been archived by the owner on Sep 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 466
kube-aws: validate that the key pair exists #362
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
/assets | ||
*~ | ||
/_gopath-kube-aws | ||
/_gopath-vendor | ||
/pkg/config/templates.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,40 @@ | ||
package cluster | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/coreos/coreos-kubernetes/multi-node/aws/pkg/config" | ||
) | ||
|
||
type dummyEC2Service map[string]struct { | ||
const minimalConfigYaml = ` | ||
externalDNSName: test-external-dns-name | ||
keyName: test-key-name | ||
region: us-west-1 | ||
availabilityZone: us-west-1c | ||
clusterName: test-cluster-name | ||
kmsKeyArn: "arn:aws:kms:us-west-1:xxxxxxxxx:key/xxxxxxxxxxxxxxxxxxx" | ||
` | ||
|
||
type VPC struct { | ||
cidr string | ||
subnetCidrs []string | ||
} | ||
|
||
type dummyEC2Service struct { | ||
VPCs map[string]VPC | ||
KeyPairs map[string]bool | ||
} | ||
|
||
func (svc dummyEC2Service) DescribeVpcs(input *ec2.DescribeVpcsInput) (*ec2.DescribeVpcsOutput, error) { | ||
output := ec2.DescribeVpcsOutput{} | ||
for _, vpcId := range input.VpcIds { | ||
if vpc, ok := svc[*vpcId]; ok { | ||
for _, vpcID := range input.VpcIds { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This capitalization makes golint happy, but it's inconsistent with the amazon's naming. I'd prefer to not propagate their deviancy, but I can change this back if we'd rather be consistent. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. golint is our source of truth in these matters. |
||
if vpc, ok := svc.VPCs[*vpcID]; ok { | ||
output.Vpcs = append(output.Vpcs, &ec2.Vpc{ | ||
VpcId: vpcId, | ||
VpcId: vpcID, | ||
CidrBlock: aws.String(vpc.cidr), | ||
}) | ||
} | ||
|
@@ -39,8 +55,8 @@ func (svc dummyEC2Service) DescribeSubnets(input *ec2.DescribeSubnetsInput) (*ec | |
} | ||
} | ||
|
||
for _, vpcId := range vpcIds { | ||
if vpc, ok := svc[vpcId]; ok { | ||
for _, vpcID := range vpcIds { | ||
if vpc, ok := svc.VPCs[vpcID]; ok { | ||
for _, subnetCidr := range vpc.subnetCidrs { | ||
output.Subnets = append( | ||
output.Subnets, | ||
|
@@ -53,15 +69,23 @@ func (svc dummyEC2Service) DescribeSubnets(input *ec2.DescribeSubnetsInput) (*ec | |
return &output, nil | ||
} | ||
|
||
func (svc dummyEC2Service) DescribeKeyPairs(input *ec2.DescribeKeyPairsInput) (*ec2.DescribeKeyPairsOutput, error) { | ||
output := &ec2.DescribeKeyPairsOutput{} | ||
|
||
for _, keyName := range input.KeyNames { | ||
if _, ok := svc.KeyPairs[*keyName]; ok { | ||
output.KeyPairs = append(output.KeyPairs, &ec2.KeyPairInfo{ | ||
KeyName: keyName, | ||
}) | ||
} else { | ||
return nil, awserr.New("InvalidKeyPair.NotFound", "", errors.New("")) | ||
} | ||
} | ||
|
||
return output, nil | ||
} | ||
|
||
func TestExistingVPCValidation(t *testing.T) { | ||
minimalConfigYaml := ` | ||
externalDNSName: test-external-dns-name | ||
keyName: test-key-name | ||
region: us-west-1 | ||
availabilityZone: us-west-1c | ||
clusterName: test-cluster-name | ||
kmsKeyArn: "arn:aws:kms:us-west-1:xxxxxxxxx:key/xxxxxxxxxxxxxxxxxxx" | ||
` | ||
|
||
goodExistingVPCConfigs := []string{ | ||
``, //Tests default create VPC mode, which bypasses existing VPC validation | ||
|
@@ -108,20 +132,22 @@ routeTableId: rtb-xxxxxx | |
} | ||
|
||
ec2Service := dummyEC2Service{ | ||
"vpc-xxx1": { | ||
cidr: "10.5.0.0/16", | ||
subnetCidrs: []string{ | ||
"10.5.1.0/24", | ||
"10.5.2.0/24", | ||
"10.5.10.100/29", | ||
VPCs: map[string]VPC{ | ||
"vpc-xxx1": { | ||
cidr: "10.5.0.0/16", | ||
subnetCidrs: []string{ | ||
"10.5.1.0/24", | ||
"10.5.2.0/24", | ||
"10.5.10.100/29", | ||
}, | ||
}, | ||
}, | ||
"vpc-xxx2": { | ||
cidr: "192.168.1.0/24", | ||
subnetCidrs: []string{ | ||
"192.168.1.100/28", | ||
"192.168.1.150/28", | ||
"192.168.1.200/28", | ||
"vpc-xxx2": { | ||
cidr: "192.168.1.0/24", | ||
subnetCidrs: []string{ | ||
"192.168.1.100/28", | ||
"192.168.1.150/28", | ||
"192.168.1.200/28", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
@@ -152,5 +178,28 @@ routeTableId: rtb-xxxxxx | |
t.Errorf("Incorrect config tested valid, expected error:\n%s", networkConfig) | ||
} | ||
} | ||
} | ||
|
||
func TestValidateKeyPair(t *testing.T) { | ||
|
||
clusterConfig, err := config.ClusterFromBytes([]byte(minimalConfigYaml)) | ||
if err != nil { | ||
t.Errorf("could not get valid cluster config: %v", err) | ||
} | ||
|
||
c := &Cluster{Cluster: *clusterConfig} | ||
|
||
ec2Svc := dummyEC2Service{} | ||
ec2Svc.KeyPairs = map[string]bool{ | ||
c.KeyName: true, | ||
} | ||
|
||
if err := c.validateKeyPair(ec2Svc); err != nil { | ||
t.Errorf("returned an error for valid key") | ||
} | ||
|
||
c.KeyName = "invalidKeyName" | ||
if err := c.validateKeyPair(ec2Svc); err == nil { | ||
t.Errorf("failed to catch invalid key \"%s\"", c.KeyName) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was just leading to output like "Error creating cluster: Error: Key does not exist". I think the "Validating stack template..." output added in the validate command makes the extra messaging unnecessary.