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

dig lookup: Allow to pass port for DNS lookup #8966

Merged
merged 1 commit into from
Oct 7, 2024

Conversation

JaegerMaKn
Copy link
Contributor

SUMMARY

Add new option port to the dig lookup module to specify which port is to be used for DNS query.
This is necessary for setups in which a (custom/internal) DNS server listens on a non-default port.

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

dig

ADDITIONAL INFORMATION

dnspython accepts a port as part of the nameserver and not, like the other optioins that are currently present in the dig module, to be passed to the query function.

Currently, the nameservers are passed as strings which leads dnspython to create Nameserver objects out of them using the port that is currently set in the Resolver instance. That creation of Nameserver objects is done right when the nameservers property is set.
If a port is to be set by us, the port attribute of the Resolver needs to be set before the nameservers are passed to the Resolver so when the nameservers are passed, that new port is used to create the Nameserver objects.
Therefore, the assignment of the nameservers property of the Resolver is moved after the argument processing so the port attribute is (if it's given) definitely set before the nameservers property.

@ansibullbot
Copy link
Collaborator

cc @jpmens
click here for bot help

@ansibullbot ansibullbot added feature This issue/PR relates to a feature request lookup lookup plugin needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI new_contributor Help guide this first time contributor plugins plugin (any type) and removed needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI labels Oct 2, 2024
@felixfontein felixfontein added check-before-release PR will be looked at again shortly before release and merged if possible. backport-9 Automatically create a backport for the stable-9 branch labels Oct 2, 2024
Copy link
Collaborator

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution!

@@ -0,0 +1,4 @@
---
minor_changes:
- dig lookup module - add ``port`` option to specify DNS server port (https://github.com/ansible-collections/community.general/pull/8966).
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
- dig lookup module - add ``port`` option to specify DNS server port (https://github.com/ansible-collections/community.general/pull/8966).
- dig lookup plugin - add ``port`` option to specify DNS server port (https://github.com/ansible-collections/community.general/pull/8966).

plugins/lookup/dig.py Show resolved Hide resolved
@@ -358,7 +363,6 @@ def run(self, terms, variables=None, **kwargs):
nameservers.append(nsaddr)
except Exception as e:
raise AnsibleError("dns lookup NS: %s" % to_native(e))
myres.nameservers = nameservers
Copy link
Collaborator

Choose a reason for hiding this comment

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

The old code only keeps the nameservers from the last entry in @, while with your change all nameservers provided by the user are used.

I would say this is a bugfix. (Or is myres.nameservers = ... doing some magic so that if you do it twice, the list of nameservers is extended?)

Do you mind moving the nameserver modification to a separate PR? Bugfixes are backported to more stable branches than new features (which adding port is).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't even notice there was a bug and my change fixed it. Thanks for pointing that out. I'll open another PR for just that bugfix.

Comment on lines 390 to 391
elif opt == 'port':
port = int(arg)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This processing is only for legacy argument parsing.

Suggested change
elif opt == 'port':
port = int(arg)

@JaegerMaKn
Copy link
Contributor Author

Thanks for your review! I've applied the suggested changes

@JaegerMaKn
Copy link
Contributor Author

Created separate PR with just the multi-nameservers-bugfix as #8970

@felixfontein
Copy link
Collaborator

This will likely need a rebase to resolve the conflict once #8970 has been merged.

@felixfontein
Copy link
Collaborator

This now needs a rebase.

@ansibullbot ansibullbot added needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR and removed new_contributor Help guide this first time contributor labels Oct 5, 2024
dnspython accepts a port as part of the nameserver.

Currently, the nameservers are passed as strings which
leads dnspython to create Nameserver objects out of them
using the port that is currently set in the Resolver instance.
That creation of Nameserver objects is done right when the
`nameservers` property is set.
If a port is to be set by us, the `port` attribute of the
Resolver needs to be set before the nameservers are passed
to the Resolver so when the nameservers are passed, that new
port is used to create the Nameserver objects.
Therefore, the assignment of the `nameservers` property of the
Resolver is moved after the argument processing so the `port`
attribute is (if it's given in the lookup-call) definitely set
before the `nameservers` property.
@JaegerMaKn
Copy link
Contributor Author

Rebased

@ansibullbot ansibullbot removed needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Oct 7, 2024
@felixfontein felixfontein merged commit 5e6b8e5 into ansible-collections:main Oct 7, 2024
141 checks passed
Copy link

patchback bot commented Oct 7, 2024

Backport to stable-9: 💚 backport PR created

✅ Backport PR branch: patchback/backports/stable-9/5e6b8e53274095c7a62b8b4dadf2a9c18ac2e562/pr-8966

Backported as #9004

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

@felixfontein felixfontein removed the check-before-release PR will be looked at again shortly before release and merged if possible. label Oct 7, 2024
patchback bot pushed a commit that referenced this pull request Oct 7, 2024
dnspython accepts a port as part of the nameserver.

Currently, the nameservers are passed as strings which
leads dnspython to create Nameserver objects out of them
using the port that is currently set in the Resolver instance.
That creation of Nameserver objects is done right when the
`nameservers` property is set.
If a port is to be set by us, the `port` attribute of the
Resolver needs to be set before the nameservers are passed
to the Resolver so when the nameservers are passed, that new
port is used to create the Nameserver objects.
Therefore, the assignment of the `nameservers` property of the
Resolver is moved after the argument processing so the `port`
attribute is (if it's given in the lookup-call) definitely set
before the `nameservers` property.

(cherry picked from commit 5e6b8e5)
@felixfontein
Copy link
Collaborator

@JaegerMaKn thanks for your contribution!

felixfontein pushed a commit that referenced this pull request Oct 7, 2024
… for DNS lookup (#9004)

dig lookup: Allow to pass port for DNS lookup (#8966)

dnspython accepts a port as part of the nameserver.

Currently, the nameservers are passed as strings which
leads dnspython to create Nameserver objects out of them
using the port that is currently set in the Resolver instance.
That creation of Nameserver objects is done right when the
`nameservers` property is set.
If a port is to be set by us, the `port` attribute of the
Resolver needs to be set before the nameservers are passed
to the Resolver so when the nameservers are passed, that new
port is used to create the Nameserver objects.
Therefore, the assignment of the `nameservers` property of the
Resolver is moved after the argument processing so the `port`
attribute is (if it's given in the lookup-call) definitely set
before the `nameservers` property.

(cherry picked from commit 5e6b8e5)

Co-authored-by: JaegerMaKn <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-9 Automatically create a backport for the stable-9 branch feature This issue/PR relates to a feature request lookup lookup plugin plugins plugin (any type)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants