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

Ipv4 check #276

Merged
merged 1 commit into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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