-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update log level and add integration test to validate ec2 permissions
- Loading branch information
Showing
5 changed files
with
193 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
package ec2api_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/config" | ||
"github.com/aws/amazon-vpc-resource-controller-k8s/test/framework" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestEc2api(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "EC2API Suite") | ||
} | ||
|
||
var frameWork *framework.Framework | ||
var nodeListLen int | ||
var _ = BeforeSuite(func() { | ||
By("creating a framework") | ||
frameWork = framework.New(framework.GlobalOptions) | ||
By("verify node count before test") | ||
nodeList, err := frameWork.NodeManager.GetNodesWithOS(config.OSLinux) | ||
Expect(err).ToNot(HaveOccurred()) | ||
nodeListLen = len(nodeList.Items) | ||
Expect(nodeListLen).To(BeNumerically(">", 1)) | ||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
nodeList, err := frameWork.NodeManager.GetNodesWithOS(config.OSLinux) | ||
Expect(err).ToNot(HaveOccurred()) | ||
By("verifying node count after test is unchanged") | ||
Expect(len(nodeList.Items)).To(Equal(nodeListLen)) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
package ec2api_test | ||
|
||
import ( | ||
"strings" | ||
"time" | ||
|
||
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/config" | ||
"github.com/aws/amazon-vpc-resource-controller-k8s/test/framework/utils" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/credentials/stscreds" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
// requires AmazonEKSVPCResourceController policy to be attached to the EKS cluster role | ||
var _ = Describe("[LOCAL] Test IAM permissions for EC2 API calls", func() { | ||
var instanceID string | ||
var subnetID string | ||
var instanceType string | ||
var nwInterfaceID string | ||
var err error | ||
BeforeEach(func() { | ||
By("getting instance details") | ||
nodeList, err := frameWork.NodeManager.GetNodesWithOS(config.OSLinux) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(nodeList.Items).ToNot(BeEmpty()) | ||
instanceID = frameWork.NodeManager.GetInstanceID(&nodeList.Items[0]) | ||
ec2Instance, err := frameWork.EC2Manager.GetInstanceDetails(instanceID) | ||
Expect(err).ToNot(HaveOccurred()) | ||
subnetID = *ec2Instance.SubnetId | ||
instanceType = *ec2Instance.InstanceType | ||
|
||
}) | ||
AfterEach(func() { | ||
By("deleting test interface") | ||
err = frameWork.EC2Manager.DeleteNetworkInterface(nwInterfaceID) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
Describe("Test DeleteNetworkInterface permission", func() { | ||
Context("when instance is terminated", func() { | ||
It("it should only delete ENIs provisioned by the controller or vpc-cni", func() { | ||
By("creating test ENI without eks:eni:owner tag and attach to EC2 instance") | ||
nwInterfaceID, err = frameWork.EC2Manager.CreateAndAttachNetworkInterface(subnetID, instanceID, instanceType) | ||
Expect(err).ToNot(HaveOccurred()) | ||
By("terminating the instance and sleeping") | ||
err = frameWork.EC2Manager.TerminateInstances(instanceID) | ||
Expect(err).ToNot(HaveOccurred()) | ||
// allow time for instance to be deleted and ENI to be available, new node to be ready | ||
time.Sleep(utils.ResourceCreationTimeout) | ||
By("verifying ENI is not deleted by controller") | ||
err = frameWork.EC2Manager.DescribeNetworkInterface(nwInterfaceID) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
}) | ||
}) | ||
Describe("Test CreateNetworkInterfacePermission permission", func() { | ||
Context("when assuming EKS cluster role", func() { | ||
It("it should not grant CreateNetworkInterfacePermission on ENIs without tag eks:eni:owner=eks-vpc-resource-controller", func() { | ||
By("assuming EKS cluster role") | ||
sess := session.Must(session.NewSession()) | ||
creds := stscreds.NewCredentials(sess, frameWork.Options.ClusterRoleArn) | ||
ec2Client := ec2.New(sess, &aws.Config{Credentials: creds}) | ||
By("creating network interface") | ||
nwInterfaceOp, err := ec2Client.CreateNetworkInterface(&ec2.CreateNetworkInterfaceInput{ | ||
SubnetId: aws.String(subnetID), | ||
Description: aws.String("VPC-Resource-Controller integration test ENI"), | ||
}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
nwInterfaceID = *nwInterfaceOp.NetworkInterface.NetworkInterfaceId | ||
By("creating network interface permission") | ||
arnSplit := strings.Split(frameWork.Options.ClusterRoleArn, ":") | ||
accountId := arnSplit[len(arnSplit)-2] | ||
_, err = ec2Client.CreateNetworkInterfacePermission(&ec2.CreateNetworkInterfacePermissionInput{ | ||
AwsAccountId: aws.String(accountId), | ||
NetworkInterfaceId: aws.String(nwInterfaceID), | ||
Permission: aws.String(ec2.InterfacePermissionTypeInstanceAttach), | ||
}) | ||
By("validating error occurred") | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) | ||
}) | ||
}) |