Skip to content

Commit

Permalink
OCM-6449 | test: Change proxy prepare workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
yingzhanredhat committed May 8, 2024
1 parent ab80c46 commit 6095245
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 64 deletions.
22 changes: 19 additions & 3 deletions pkg/aws/aws_client/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/openshift-online/ocm-common/pkg/log"
)

Expand All @@ -21,13 +22,28 @@ func (client *AWSClient) CopyImage(sourceImageID string, sourceRegion string, na
return *output.ImageId, nil
}

func (client *AWSClient) DescribeImage(imageID string) (*ec2.DescribeImagesOutput, error) {
func (client *AWSClient) DescribeImage(imageIDs []string, filters ...map[string][]string) (*ec2.DescribeImagesOutput, error) {
FilterInput := []types.Filter{}
for _, filter := range filters {
for k, v := range filter {
awsFilter := types.Filter{
Name: &k,
Values: v,
}
FilterInput = append(FilterInput, awsFilter)
}
}

describeImageInput := &ec2.DescribeImagesInput{
ImageIds: []string{imageID},
Filters: FilterInput,
}

if len(imageIDs) != 0 {
describeImageInput.ImageIds = imageIDs
}
output, err := client.EC2().DescribeImages(context.TODO(), describeImageInput)
if err != nil {
log.LogError("Describe image %s meet error: %s", imageID, err)
log.LogError("Describe image %s meet error: %s", imageIDs, err)
return nil, err
}

Expand Down
6 changes: 2 additions & 4 deletions pkg/aws/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ const (
BastionName = "ocm-bastion"
)

var ProxyImageMap = map[string]string{
"us-west-2": "ami-03b82d95dbe67072d",
"ap-northeast-1": "ami-0517f6ca1da98f337",
}
var PublicImageName = "al2023-ami-2023.4.20240416.0-kernel-6.1-x86_64"

var BastionImageMap = map[string]string{
"us-east-1": "ami-01c647eace872fc02",
"us-east-2": "ami-00a9282ce3b5ddfb1",
Expand Down
92 changes: 35 additions & 57 deletions pkg/test/vpc_client/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,22 @@ import (
// LaunchProxyInstance will launch a proxy instance on the indicated zone.
// If set imageID to empty, it will find the proxy image in the ProxyImageMap map
// LaunchProxyInstance will return proxyInstance detail, privateIPAddress,CAcontent and error
func (vpc *VPC) LaunchProxyInstance(imageID string, zone string, keypairName string, privateKeyPath string) (in types.Instance, privateIP string, proxyServerCA string, err error) {
var inst types.Instance
if imageID == "" {
var ok bool
imageID, ok = CON.ProxyImageMap[vpc.Region]
if !ok {
log.LogInfo("Cannot find proxy image of region %s in map ProxyImageMap, will copy from existing region", vpc.Region)
var err error
imageID, err = vpc.CopyImageToProxy(CON.ProxyName)
if err != nil {
log.LogError("Error to copy image ID %s: %s", imageID, err)
return inst, "", "", err
}
//Wait 30 minutes for image to active
result, err := vpc.WaitImageToActive(imageID, 30)
if err != nil || !result {
log.LogError("Error wait image %s to active %s", imageID, err)
return inst, "", "", err
}
}
func (vpc *VPC) LaunchProxyInstance(zone string, keypairName string, privateKeyPath string) (inst types.Instance, privateIP string, proxyServerCA string, err error) {
filters := []map[string][]string{
{
"name": {
CON.PublicImageName,
},
},
}

output, err := vpc.AWSClient.DescribeImage([]string{}, filters...)
if err != nil || output == nil {
log.LogError("Describe image failed: %s", err)
return inst, "", "", err
}
imageID := *output.Images[0].ImageId
log.LogInfo("Get the image ID : %s", imageID)

pubSubnet, err := vpc.PreparePublicSubnet(zone)
if err != nil {
Expand Down Expand Up @@ -91,55 +86,38 @@ func (vpc *VPC) LaunchProxyInstance(imageID string, zone string, keypairName str
log.LogInfo("Prepare EIP successfully for the proxy preparation. Launch with IP: %s", publicIP)

time.Sleep(2 * time.Minute)
cmd1 := "http_proxy=127.0.0.1:8080 curl http://mitm.it/cert/pem -s > mitm-ca.pem"
cmd2 := "cat mitm-ca.pem"
hostname := fmt.Sprintf("%s:22", publicIP)
_, err = Exec_CMD(CON.AWSInstanceUser, sshKey, hostname, cmd1)
err = setupMITMProxyServer(sshKey, hostname)
if err != nil {
log.LogError("login instance to run cmd %s failed %s", cmd1, err)
log.LogError("Setup MITM proxy server failed %s", err)
return inst, "", "", err
}
caContent, err := Exec_CMD(CON.AWSInstanceUser, sshKey, hostname, cmd2)

cmd := "cat mitm-ca.pem"
caContent, err := Exec_CMD(CON.AWSInstanceUser, sshKey, hostname, cmd)
if err != nil {
log.LogError("login instance to run cmd %s failed %s", cmd2, err)
log.LogError("login instance to run cmd %s:%s", cmd, err)
return inst, "", "", err
}
return instOut.Instances[0], *instOut.Instances[0].PrivateIpAddress, caContent, err
}

func (vpc *VPC) CopyImageToProxy(name string) (destinationImageID string, err error) {
sourceRegion := "us-west-2"
sourceImageID, ok := CON.ProxyImageMap[sourceRegion]
if !ok {
log.LogError("Can't find image from region %s :%s", sourceRegion, err)
return "", err
}
destinationImageID, err = vpc.AWSClient.CopyImage(sourceImageID, sourceRegion, name)
if err != nil {
log.LogError("Copy image %s meet error %s", sourceImageID, err)
return "", err
func setupMITMProxyServer(sshKey string, hostname string) (err error) {
setupProxyCMDs := []string{
"sudo yum install -y wget",
"wget https://snapshots.mitmproxy.org/7.0.2/mitmproxy-7.0.2-linux.tar.gz",
"mkdir mitm",
"tar zxvf mitmproxy-7.0.2-linux.tar.gz -C mitm",
"nohup ./mitm/mitmdump --showhost --ssl-insecure > mitm.log 2>&1 &",
"sleep 5",
"http_proxy=127.0.0.1:8080 curl http://mitm.it/cert/pem -s > ~/mitm-ca.pem",
}
return destinationImageID, nil
}

func (vpc *VPC) WaitImageToActive(imageID string, timeout time.Duration) (imageAvailable bool, err error) {
log.LogInfo("Waiting for image %s status to active. Timeout after %v mins", imageID, timeout)
startTime := time.Now()
imageAvailable = false
for time.Now().Before(startTime.Add(timeout * time.Minute)) {
output, err := vpc.AWSClient.DescribeImage(imageID)
for _, cmd := range setupProxyCMDs {
_, err = Exec_CMD(CON.AWSInstanceUser, sshKey, hostname, cmd)
if err != nil {
log.LogError("Error happened when describe image status: %s", imageID)
return imageAvailable, err
return err
}
if string(output.Images[0].State) == "available" {
imageAvailable = true
return imageAvailable, nil
}

time.Sleep(time.Minute)
log.LogDebug("Run the cmd successfully: %s", cmd)
}
err = fmt.Errorf("timeout for waiting image active")
return imageAvailable, err

return
}

0 comments on commit 6095245

Please sign in to comment.