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

Ipv4 check #276

merged 1 commit into from
Sep 1, 2020

Conversation

tedteng
Copy link
Contributor

@tedteng tedteng commented Aug 26, 2020

What this PR does / why we need it:

Which issue(s) this PR fixes:
Fixes #275

Special notes for your reviewer:

Release note:

Support valid Public IP for IPv4 and IPv6 in aws bastion security group when `ssh`

@tedteng tedteng requested a review from a team as a code owner August 26, 2020 02:21
@gardener-robot-ci-2 gardener-robot-ci-2 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Aug 26, 2020
@gardener-robot-ci-3 gardener-robot-ci-3 added needs/ok-to-test Needs approval for testing (check PR in detail before setting this label because PR is run on CI/CD) and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Aug 26, 2020
@neo-liang-sap
Copy link
Contributor

please hold this PR until #274 , currently under review from @petersutter for urlencoding

@gardener-robot-ci-2 gardener-robot-ci-2 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Aug 26, 2020
@gardener-robot-ci-3 gardener-robot-ci-3 removed the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Aug 26, 2020
@tedteng
Copy link
Contributor Author

tedteng commented Aug 26, 2020

please hold this PR until #274 , currently under review from @petersutter for urlencoding

sure, definitely this PR merge will wait for #274

@tedteng
Copy link
Contributor Author

tedteng commented Aug 27, 2020

find one issue before rebase, will waiting for the fix #273 (comment)
image

@gardener-robot-ci-2 gardener-robot-ci-2 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Aug 28, 2020
@gardener-robot-ci-1 gardener-robot-ci-1 removed the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Aug 28, 2020
@gardener-robot-ci-2 gardener-robot-ci-2 added reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Aug 28, 2020
@tedteng
Copy link
Contributor Author

tedteng commented Aug 28, 2020

/lgtm

@gardener-robot gardener-robot added the reviewed/lgtm Has approval for merging label Aug 28, 2020
@tedteng
Copy link
Contributor Author

tedteng commented Aug 28, 2020

/remove reviewed/lgtm

@gardener-robot gardener-robot removed the reviewed/lgtm Has approval for merging label Aug 28, 2020
Comment on lines 184 to 192
if isIP(a.MyPublicIP) {
arguments = fmt.Sprintf("aws ec2 authorize-security-group-ingress --group-id %s --protocol tcp --port 22 --cidr %s/32", a.BastionSecurityGroupID, a.MyPublicIP)
} else {
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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isIP is a custom implemented function to check for ipv4 addresses. Replace the custom implementation by using net.ParseIP and return netIP != nil && netIP.To4() != nil to check for ipv4
see also https://github.com/kubernetes/kubernetes/blob/dc025534afb0c3106b3aab166f88b971102135d1/vendor/k8s.io/utils/net/net.go#L116-L125 as example for the ipv6 check

In the end the code should look something like:
if isIPv4String(a.MyPublicIP) {
...
} else if IsIPv6String(a.MyPublicIP) {
...
} else {
// error
}

@gardener-robot-ci-2 gardener-robot-ci-2 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Aug 28, 2020
@gardener-robot-ci-1 gardener-robot-ci-1 removed the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Aug 28, 2020
@@ -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)

ip := net.ParseIP(a.MyPublicIP)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the string is not a valid textual representation of an IP address, ParseIP returns nil. Hence you also need to check for nil and that's why I suggested initially to move that code into utils/miscellaneous and dump/replace the isIP function that is already there

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update isIP method which return bool to check whether IP is valid for ipv4 or ipv6.
also change to logic in here, add exit after print the "IP not valid:" + a.MyPublicIP.

@gardener-robot-ci-2 gardener-robot-ci-2 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Aug 31, 2020
@gardener-robot-ci-1 gardener-robot-ci-1 removed the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Aug 31, 2020
Comment on lines 330 to 332
func isIP(host string) bool {
return net.ParseIP(host) != nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if the function was named isIP, the old implementation would only return true in case it was an ipv4 address.
Now it will return true if it's an ipv6 and ipv4 address..

Copy link
Contributor Author

@tedteng tedteng Aug 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should I rename isIP to like isIPv4 something like that then check and return only valid ipv4 address
?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I guess this would be better. It's only used in one place currently createBastionHostInstance (gcp). Not sure if this will work with ipv6 addresses..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, so far GCP createBastionHostInstance not support ipv6 yet I think, but I will check it later when I work on support GCP host in the user role later.

@gardener-robot-ci-2 gardener-robot-ci-2 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Aug 31, 2020
@gardener-robot-ci-3 gardener-robot-ci-3 removed the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Aug 31, 2020
Copy link
Contributor

@petersutter petersutter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm, thanks
however I haven't tested it

@gardener-robot-ci-2 gardener-robot-ci-2 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Aug 31, 2020
@@ -356,5 +342,9 @@ func getPublicIP() string {
defer resp.Body.Close()
ip, err := ioutil.ReadAll(resp.Body)
checkError(err)
if !isIP(string(ip)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if !isIP(string(ip)) {
if !isIPv4(string(ip)) {

shouldn't this be isIPv4?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes thanks.

@gardener-robot-ci-1 gardener-robot-ci-1 removed the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Aug 31, 2020
Copy link
Contributor

@neo-liang-sap neo-liang-sap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. this commit should not exist in this PR
    image
  2. these three commits are dummy content, should not exist in this PR
    image

aws IPv6 support
use net.ParseIP identify ipv4 or ipv6
modify isIP method
rename isIP() to isIPv4()
@gardener-robot-ci-1 gardener-robot-ci-1 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Aug 31, 2020
@gardener-robot-ci-2 gardener-robot-ci-2 removed the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Aug 31, 2020
Copy link
Contributor

@neo-liang-sap neo-liang-sap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
however i haven't tested, confirmed with @tedteng he's fully confidence with it

@gardener-robot gardener-robot added the reviewed/lgtm Has approval for merging label Sep 1, 2020
@neo-liang-sap neo-liang-sap merged commit 91e3070 into gardener-attic:master Sep 1, 2020
@tedteng tedteng deleted the ipv4_check branch September 15, 2020 01:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs/ok-to-test Needs approval for testing (check PR in detail before setting this label because PR is run on CI/CD) reviewed/lgtm Has approval for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ipv4 check after get public IP
7 participants