Skip to content
This repository has been archived by the owner on Jul 25, 2022. It is now read-only.

Commit

Permalink
Public IP ipv4 check
Browse files Browse the repository at this point in the history
aws IPv6 support
use net.ParseIP identify ipv4 or ipv6
modify isIP method
rename isIP() to isIPv4()
  • Loading branch information
tedteng committed Aug 31, 2020
1 parent c62e0be commit 4804ec0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
24 changes: 7 additions & 17 deletions pkg/cmd/miscellaneous.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"

"github.com/gardener/gardener/pkg/apis/core"
Expand Down Expand Up @@ -327,22 +327,8 @@ func capture() func() (string, error) {
}
}

func isIP(word string) bool {
parts := strings.Split(word, ".")
if len(parts) < 4 {
return false
}
for _, x := range parts {
if i, err := strconv.Atoi(x); err == nil {
if i < 0 || i > 255 {
return false
}
} else {
return false
}

}
return true
func isIPv4(host string) bool {
return net.ParseIP(host) != nil && net.ParseIP(host).To4() != nil
}

func getPublicIP() string {
Expand All @@ -356,5 +342,9 @@ func getPublicIP() string {
defer resp.Body.Close()
ip, err := ioutil.ReadAll(resp.Body)
checkError(err)
if !isIPv4(string(ip)) {
fmt.Println("Not valid ipv4 address")
os.Exit(1)
}
return string(ip)
}
16 changes: 12 additions & 4 deletions pkg/cmd/ssh_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd
import (
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -56,7 +57,7 @@ type AwsInstanceAttribute struct {
func sshToAWSNode(nodeName, path, user, pathSSKeypair string, sshPublicKey []byte, myPublicIP string) {
a := &AwsInstanceAttribute{}
a.SSHPublicKey = sshPublicKey
a.MyPublicIP = myPublicIP + "/32"
a.MyPublicIP = myPublicIP

fmt.Println("")

Expand Down Expand Up @@ -171,7 +172,7 @@ func (a *AwsInstanceAttribute) createBastionHostSecurityGroup() {
return
}

// create security group and ssh rule
// create security group for bastion host
arguments := fmt.Sprintf("aws ec2 create-security-group --group-name %s --description ssh-access --vpc-id %s", a.BastionSecurityGroupName, a.VpcID)
captured := capture()
operate("aws", arguments)
Expand All @@ -180,10 +181,17 @@ func (a *AwsInstanceAttribute) createBastionHostSecurityGroup() {
a.BastionSecurityGroupID = strings.Trim((capturedOutput), "\n")
arguments = fmt.Sprintf("aws ec2 create-tags --resources %s --tags Key=component,Value=gardenctl", a.BastionSecurityGroupID)
operate("aws", arguments)
arguments = fmt.Sprintf("aws ec2 authorize-security-group-ingress --group-id %s --protocol tcp --port 22 --cidr %s", a.BastionSecurityGroupID, a.MyPublicIP)

if net.IP.To4([]byte(a.MyPublicIP)) != nil {
arguments = fmt.Sprintf("aws ec2 authorize-security-group-ingress --group-id %s --protocol tcp --port 22 --cidr %s/32", a.BastionSecurityGroupID, a.MyPublicIP)
} else if net.IP.To16([]byte(a.MyPublicIP)) != nil {
arguments = fmt.Sprintf("aws ec2 authorize-security-group-ingress --group-id %s --ip-permissions IpProtocol=tcp,FromPort=22,ToPort=22,Ipv6Ranges=[{CidrIpv6=%s/64}]", a.BastionSecurityGroupID, a.MyPublicIP)
} else {
fmt.Printf("IP not valid:" + a.MyPublicIP)
os.Exit(0)
}
operate("aws", arguments)
fmt.Println("Bastion host security group set up.")

}

func (a *AwsInstanceAttribute) createNodeHostSecurityGroup() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/ssh_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (g *GCPInstanceAttribute) createBastionHostInstance() {
checkError(err)
ip := ""
for _, value := range words {
if isIP(value) && !strings.HasPrefix(value, "10.") {
if isIPv4(value) && !strings.HasPrefix(value, "10.") {
ip = value
break
}
Expand Down

0 comments on commit 4804ec0

Please sign in to comment.