Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PUP-8107) Support specifying the source-address for agent #6371

Closed
wants to merge 1 commit into from

Conversation

jerearista
Copy link
Contributor

@jerearista jerearista commented Nov 16, 2017

In an environment with multiple IP addresses on a system, there are cases where the default system-selected source IP address may not be appropriate for reaching the Puppet server due to access-lists, policy, etc. This adds the --sourceaddress <ADDRESS> option to the CLI and puppet.conf allowing the user to supply the desired source IP address or hostname for outbound connections.

@shermdog
Copy link
Contributor

@jerearista if you want this in 4.10.x you'll need to re-target the PR to that branch so that it can be merged up from there.

@shermdog
Copy link
Contributor

shermdog commented Nov 17, 2017

Also, please squash the commits :)

@puppetcla
Copy link

CLA signed by all contributors.

@jerearista jerearista changed the base branch from master to 4.10.x November 17, 2017 15:11
@jerearista jerearista force-pushed the source-ip branch 3 times, most recently from c579288 to f5adb3a Compare November 17, 2017 15:45
@jerearista
Copy link
Contributor Author

Thanks, @shermdog.

I rebased to 4.10.x and squashed commits.

The Travis failure is due to Net::HTTP local_host method not existing before Ruby 2.0.

@@ -38,6 +38,11 @@ def create_connection(site)
http.read_timeout = Puppet[:http_read_timeout]
http.open_timeout = Puppet[:http_connect_timeout]

if Puppet[:sourceaddress]
Puppet.debug("Using source IP #{Puppet[:sourceaddress]}")
http.local_host = Puppet[:sourceaddress]
Copy link
Contributor

Choose a reason for hiding this comment

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

Puppet still needs to support MRI 1.9.3 because it is embedded in JRuby 1.7. I would add a check here to raise an exception when running on < 2.0, something like

if RUBY_VERSION =~ /1\./
  raise ...
else
  http.local_host = ...
end

Or just check if http.responds_to?(:local_host=)

@jerearista
Copy link
Contributor Author

jerearista commented Nov 17, 2017 via email

if Puppet[:sourceaddress] && http.respond_to?(:local_host)
Puppet.debug("Using source IP #{Puppet[:sourceaddress]}")
http.local_host = Puppet[:sourceaddress]
end
Copy link
Contributor

Choose a reason for hiding this comment

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

After thinking some more about this, I think we want to raise if it doesn't respond to the method, otherwise, we might wonder why it's not working as intended.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added an exception and testcase.

@@ -38,6 +38,13 @@ def create_connection(site)
http.read_timeout = Puppet[:http_read_timeout]
http.open_timeout = Puppet[:http_connect_timeout]

if Puppet[:sourceaddress]
msg = 'Setting source address is unsupported by this version of Net::HTTP.'
raise Net::HTTPError.new(msg, 400) unless http.respond_to?(:local_host)
Copy link
Contributor

Choose a reason for hiding this comment

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

How about raising an ArgumentError instead of HTTP 400?

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 made. Yes, that's a much better choice for the user instead of just indicating that the library doesn't support the option.

In an environment with multiple IP addresses on a system, there are
cases where the default system-selected source IP address may not be
appropriate for reaching the Puppet server due to access-lists, policy,
etc. This adds the `--sourceaddress <ADDRESS>` option to the CLI
allowing the user to supply the desired source IP address or hostname
for outbound connections.

Add check if Net::HTTP supports local_host (Ruby 2.x) and raise an
ArgumentError when unsupported.
@joshcooper
Copy link
Contributor

Thanks @jerearista! I made one additional change, and will merge it in PR #6393.

@joshcooper joshcooper closed this Nov 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants