Skip to content

Don't expose ports to the outside and fix a race condition#1850

Merged
dgageot merged 6 commits intoGoogleContainerTools:masterfrom
dgageot:available-ports
Mar 23, 2019
Merged

Don't expose ports to the outside and fix a race condition#1850
dgageot merged 6 commits intoGoogleContainerTools:masterfrom
dgageot:available-ports

Conversation

@dgageot
Copy link
Copy Markdown
Contributor

@dgageot dgageot commented Mar 21, 2019

Ports used for eventing shouldn't be exposed to the outside world. Also this removed an alert on OSX each time a port needs to be opened on the firewall.

I also took the opportunity to fix a race condition where a port could be said available when it is not.

@codecov-io
Copy link
Copy Markdown

Codecov Report

Merging #1850 into master will increase coverage by 0.2%.
The diff coverage is 83.33%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #1850     +/-   ##
=========================================
+ Coverage   49.18%   49.38%   +0.2%     
=========================================
  Files         166      166             
  Lines        7287     7285      -2     
=========================================
+ Hits         3584     3598     +14     
+ Misses       3357     3340     -17     
- Partials      346      347      +1
Impacted Files Coverage Δ
pkg/skaffold/event/server.go 35.48% <100%> (ø) ⬆️
pkg/skaffold/util/port.go 85.18% <77.77%> (+54.15%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e8ba045...8e3d233. Read the comment docs.

Copy link
Copy Markdown
Contributor

@nkubala nkubala left a comment

Choose a reason for hiding this comment

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

nice find.

// See https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt,
func GetAvailablePort(port int, forwardedPorts *sync.Map) int {
if isPortAvailable(port, forwardedPorts) {
forwardedPorts.Store(port, true)
Copy link
Copy Markdown
Member

@briandealwis briandealwis Mar 21, 2019

Choose a reason for hiding this comment

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

These stores are necessary as there are race conditions: port-forwarding gets an available port but doesn't use it immediately I missed that you moved this into isPortAvailable()


func isPortAvailable(p int, forwardedPorts *sync.Map) bool {
if _, ok := forwardedPorts.Load(p); ok {
alreadyUsed, loaded := forwardedPorts.LoadOrStore(p, true)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems odd for an is* method to mutate state

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe rename the method to something like getPortIfAvailable()?

}

l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe move "127.0.0.1" to a constant.

dgageot added 6 commits March 22, 2019 10:36
Signed-off-by: David Gageot <david@gageot.net>
Signed-off-by: David Gageot <david@gageot.net>
Signed-off-by: David Gageot <david@gageot.net>
Signed-off-by: David Gageot <david@gageot.net>
Signed-off-by: David Gageot <david@gageot.net>
Signed-off-by: David Gageot <david@gageot.net>
@dgageot
Copy link
Copy Markdown
Contributor Author

dgageot commented Mar 22, 2019

@tejal29 @briandealwis should be all good now

Copy link
Copy Markdown
Member

@briandealwis briandealwis left a comment

Choose a reason for hiding this comment

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

LGTM: The tests could do more to check conditions like single-threaded map check, and with an actual open port, but it's better than what's there.

@dgageot dgageot requested a review from tejal29 March 22, 2019 17:42
@dgageot dgageot merged commit 6fd620f into GoogleContainerTools:master Mar 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants