Commit graph

85 commits

Author SHA1 Message Date
Cory Snider
ef1545ed4a libnetwork: leave global logger alone in tests
Swapping out the global logger on the fly is causing tests to flake out
by logging to a test's log output after the test function has returned.
Refactor Resolver to use a dependency-injected logger and the resolver
unit tests to inject a private logger instance into the Resolver under
test.

Signed-off-by: Cory Snider <csnider@mirantis.com>
(cherry picked from commit d4f3858a40)
Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-05-23 11:31:28 -04:00
Cory Snider
0869b089e4
libnetwork: just forward the external DNS response
Our resolver is just a forwarder for external DNS so it should act like
it. Unless it's a server failure or refusal, take the response at face
value and forward it along to the client. RFC 8020 is only applicable to
caching recursive name servers and our resolver is neither caching nor
recursive.

Signed-off-by: Cory Snider <csnider@mirantis.com>
(cherry picked from commit 41356227f2)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-05-18 23:10:21 +02:00
Cory Snider
f8cfd3a61f libnetwork: devirtualize Resolver type
https://github.com/golang/go/wiki/CodeReviewComments#interfaces

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-16 19:05:59 -05:00
Cory Snider
faaa4fdf18 libnetwork: forward unknown PTR queries externally
PTR queries with domain names unknown to us are not necessarily invalid.
Act like a well-behaved middlebox and fall back to forwarding
externally, same as we do with the other query types.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-16 19:05:59 -05:00
Cory Snider
25b51cad3d libnetwork: replace ad-hoc semaphore implementation
...for limiting concurrent external DNS requests with
"golang.org/x/sync/semaphore".Weighted. Replace the ad-hoc rate limiter
for when the concurrency limit is hit (which contains a data-race bug)
with "golang.org/x/time/rate".Sometimes.

Immediately retrying with the next server if the concurrency limit has
been hit just further compounds the problem. Wait on the semaphore and
refuse the query if it could not be acquired in a reasonable amount of
time.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-16 19:05:59 -05:00
Cory Snider
a1f7c644be libnetwork: use dns.Client for forwarded requests
It handles figuring out the UDP receive buffer size and setting IO
timeouts, which simplifies our code. It is also more robust to receiving
UDP replies to earlier queries which timed out.

Log failures to perform a client exchange at level error so they are
more visible to operators and administrators.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-16 18:50:23 -05:00
Cory Snider
e6258e6590 libnetwork: reply SERVFAIL if DNS forwarding fails
Fixes moby/moby issue 44575

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-16 18:44:56 -05:00
Cory Snider
9cf8c4f689 libnetwork: extract DNS client exchange to method
forwardExtDNS() will now continue with the next external DNS sever if
co.ReadMsg() returns (nil, nil). Previously it would abort resolving the
query and not reply to the container client. The implementation of
ReadMsg() in the currently- vendored version of miekg/dns cannot return
(nil, nil) so the difference is immaterial in practice.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-16 18:44:56 -05:00
Cory Snider
854ec3ffb3 libnetwork: extract dialExtDNS to method
Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-16 18:44:56 -05:00
Cory Snider
51cdd7ceac libnetwork: truncate DNS msgs using library method
(*dns.Msg).Truncate() is more intelligent and standards-compliant about
truncating DNS response messages than our hand-rolled version. Fix a
silly fencepost error the max TCP message size: the limit is
dns.MaxMsgSize (65535), full stop.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-16 18:44:56 -05:00
Cory Snider
860e83e52f libnetwork: get rid of truncation red herring
The TC flag in a DNS message indicates that the sender had to
truncate it to fit within the length limit of the transmission channel.
It does NOT indicate that part of the message was lost before reaching
the recipient. Older versions of github.com/miekg/dns conflated the two
cases by returning ErrTruncated from ReadMsg() if the message was parsed
without error but had the TC flag set. The version of miekg/dns
currently vendored no longer returns an error when a well-formed DNS
message is received which has its TC flag set, but there was some
confusion on how to update libnetwork to deal with this behaviour
change. Truncated DNS replies are no longer different from any other
reply message: they are normal replies which do not need any special-
case handling to proxy back to the client.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-16 18:44:56 -05:00
Cory Snider
8a35fb0d1c libnetwork: refactor ServeDNS for readability
Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-16 18:44:55 -05:00
Cory Snider
0bd30e90bb libnetwork: reply SERVFAIL on resolve error
...instead of silently dropping the DNS query.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-16 18:44:54 -05:00
Cory Snider
92aa6e6282 libnetwork: extract fn for external DNS forwarding
Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-16 18:43:15 -05:00
Cory Snider
78792eae68 libnetwork: add regression test for issue 44575
Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-16 18:41:13 -05:00
er0k
81f9f90e47
Do not log connection info before the connection exists
If the resolver encounters an error before it attempts to forward the
request to external DNS, do not try to log information about the
external connection, because at this point `extConn` is `nil`. This
makes sure `dockerd` won't panic and crash from a nil pointer
dereference when it sees an invalid DNS query.

fixes #44979

Signed-off-by: er0k <er0k@er0k.net>
(cherry picked from commit 6c2637be11)
Signed-off-by: Bjorn Neergaard <bneergaard@mirantis.com>
2023-02-16 13:21:45 -07:00
Cory Snider
dea3f2b417 Migrate away from things deprecated in Go 1.20
"math/rand".Seed
  - Migrate to using local RNG instances.

"archive/tar".TypeRegA
  - The deprecated constant tar.TypeRegA is the same value as
    tar.TypeReg and so is not needed at all.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-15 12:30:32 -05:00
Cory Snider
d6cc02d301 libnetwork: drop (resolver).resolverKey field
...as it is now unused.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-11 12:14:32 -05:00
Yamazaki Masashi
0787ea8b26 libnetwork: improve logs for DNS failures
Signed-off-by: Yamazaki Masashi <masi19bw@gmail.com>

libnetwork: fix function call

Signed-off-by: Yamazaki Masashi <masi19bw@gmail.com>
2022-12-22 23:25:21 +09:00
Sebastiaan van Stijn
cd381aea56
libnetwork: fix empty-lines (revive)
libnetwork/etchosts/etchosts_test.go:167:54: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/osl/route_linux.go:185:74: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/osl/sandbox_linux_test.go:323:36: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/bitseq/sequence.go:412:48: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/datastore/datastore_test.go:67:46: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/datastore/mock_store.go:34:60: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/iptables/firewalld.go:202:44: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/iptables/firewalld_test.go:76:36: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/iptables/iptables.go:256:67: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/iptables/iptables.go:303:128: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/networkdb/cluster.go:183:72: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/ipams/null/null_test.go:44:38: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/macvlan/macvlan_store.go:45:52: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/ipam/allocator_test.go:1058:39: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/bridge/port_mapping.go:88:111: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/bridge/link.go:26:90: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/bridge/setup_ipv6_test.go:17:34: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/bridge/setup_ip_tables.go:392:4: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/bridge/bridge.go:804:50: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/ov_serf.go:183:29: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/ov_utils.go:81:64: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/overlay/peerdb.go:172:67: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/peerdb.go:209:67: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/peerdb.go:344:89: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/peerdb.go:436:63: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/overlay.go:183:36: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/encryption.go:69:28: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/overlay/ov_network.go:563:81: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/default_gateway.go:32:43: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/errors_test.go:9:40: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/service_common.go:184:64: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/endpoint.go:161:55: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/store.go:320:33: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/store_linux_test.go:11:38: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/sandbox.go:571:36: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/service_common.go:317:246: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/endpoint.go:550:17: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/sandbox_dns_unix.go:213:106: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/controller.go:676:85: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/agent.go:876:60: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/resolver.go:324:69: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/network.go:1153:92: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/network.go:1955:67: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/network.go:2235:9: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/libnetwork_internal_test.go:336:26: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/resolver_test.go:76:35: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/libnetwork_test.go:303:38: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/libnetwork_test.go:985:46: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/ipam/allocator_test.go:1263:37: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/errors_test.go:9:40: empty-lines: extra empty line at the end of a block (revive)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-26 19:21:58 +02:00
Sebastiaan van Stijn
4f08346686
fix formatting of "nolint" tags for go1.19
The correct formatting for machine-readable comments is;

    //<some alphanumeric identifier>:<options>[,<option>...][ // comment]

Which basically means:

- MUST NOT have a space before `<identifier>` (e.g. `nolint`)
- Identified MUST be alphanumeric
- MUST be followed by a colon
- MUST be followed by at least one `<option>`
- Optionally additional `<options>` (comma-separated)
- Optionally followed by a comment

Any other format will not be considered a machine-readable comment by `gofmt`,
and thus formatted as a regular comment. Note that this also means that a
`//nolint` (without anything after it) is considered invalid, same for `//#nosec`
(starts with a `#`).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-07-13 22:31:53 +02:00
Sebastiaan van Stijn
79d6e935ad
libnetwork: some minor refactoring / cleanup
- don't pass the query's quetion.name separately, as we're already
  passing the query itself.
- remove a "fallthrough" in favor of combining the cases in the switch

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-10-15 15:26:15 +02:00
Sebastiaan van Stijn
d86a331fa4
libnetwork: improve consistency in log messages
- Make sure all log messages have the `[resolver]` prefix
- Use `logrus.WithError()` consistently
- Improve information included in some logs

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-10-15 12:51:02 +02:00
Sebastiaan van Stijn
9a09448540
libnetwork: ServeDNS(): don't panic on unsupported query types
This was added in b3c883bb2f, but resulted
in a panic if the embedded DNS had to handle an unsupported query-type,
such as ANY.

This patch adds a debug log for this case (to better describe how it's
handled.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-10-14 20:17:39 +02:00
Brian Goff
116f200737
Fix gosec complaints in libnetwork
These were purposefully ignored before but this goes ahead and "fixes"
most of them.
Note that none of the things gosec flagged are problematic, just
quieting the linter here.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-06-25 18:02:03 +02:00
Sebastiaan van Stijn
117bca149f
libnetwork/resolver: fix minor linting issues
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-06-09 19:37:10 +02:00
Brian Goff
b3c883bb2f Skip libnetwork integration tests on Windows
Most of these tests are making use of the bridge network and do not work
on Windows.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-06-02 16:53:29 +00:00
Brian Goff
4b981436fe Fixup libnetwork lint errors
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-06-01 23:48:32 +00:00
Brian Goff
a0a473125b Fix libnetwork imports
After moving libnetwork to this repo, we need to update all the import
paths for libnetwork to point to docker/docker/libnetwork instead of
docker/libnetwork.
This change implements that.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-06-01 21:51:23 +00:00
Sebastiaan van Stijn
efe0ab37a1 Resolver: fix error handling if we didn't receive a response
Commit 2a480d515e updated the DNS library
and updated the error handling.

Due to changes in the library, we now had to check the response itself
to check if the response was truncated (Truncated DNS replies should
be sent to the client so that the client can retry over TCP).

However, 1e02aae252 added an incorrect
`nil` check to fix a panic, which ignored situations where
an error was returned, but no response (for example, if we failed
to connect to the DNS server).

In that situation, the error would be ignored, and further down we
would consider the connection to have been succesfull, but the DNS
server not returning a result.

After a "successful" lookup (but no results), we break the loop,
and don't attempt lookups in other DNS servers.

Versions before 1e02aae252 would produce:

    Name To resolve: bbc.co.uk.
    [resolver] query bbc.co.uk. (A) from 172.21.0.2:36181, forwarding to udp:192.168.5.1
    [resolver] read from DNS server failed, read udp 172.21.0.2:36181->192.168.5.1:53: i/o timeout
    [resolver] query bbc.co.uk. (A) from 172.21.0.2:38582, forwarding to udp:8.8.8.8
    [resolver] received A record "151.101.0.81" for "bbc.co.uk." from udp:8.8.8.8
    [resolver] received A record "151.101.192.81" for "bbc.co.uk." from udp:8.8.8.8
    [resolver] received A record "151.101.64.81" for "bbc.co.uk." from udp:8.8.8.8
    [resolver] received A record "151.101.128.81" for "bbc.co.uk." from udp:8.8.8.8

Versions after that commit would ignore the error, and stop further lookups:

    Name To resolve: bbc.co.uk.
    [resolver] query bbc.co.uk. (A) from 172.21.0.2:59870, forwarding to udp:192.168.5.1
    [resolver] external DNS udp:192.168.5.1 returned empty response for "bbc.co.uk."

This patch updates the logic to handle the error to log the error (and continue with the next DNS):

 - if an error is returned, and no response was received
 - if an error is returned, but it was not related to a truncated response

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Tibor Vass <tibor@docker.com>
2020-05-21 17:50:39 +00:00
Sam Whited
1e02aae252 Fixes a panic in the DNS resolver
Under certain conditions it appears that the DNS response and returned
error can be nil. When this happens, checking resp.Truncated results in
a nil panic so we must first check that the response is not nil before
checking if a truncated response was received.

See moby/moby#40715

Signed-off-by: Sam Whited <sam@samwhited.com>
2020-03-18 13:59:59 -04:00
Sam Whited
2a480d515e Bump the DNS library and revendor
Signed-off-by: Sam Whited <sam@samwhited.com>
2020-02-25 15:37:30 -05:00
Arko Dasgupta
313d2b8a74 Make DNS records and queries case-insensitive
RFC434 states that DNS Servers should be case insensitive
    This commit makes sure that all DNS queries will be translated
    to lower ASCII characters and all svcRecords will be saved in
    lower case to abide by the RFC

    Relates to https://github.com/moby/moby/issues/21169

Signed-off-by: Arko Dasgupta <arko.dasgupta@docker.com>
2019-06-19 11:23:31 -07:00
Sebastiaan van Stijn
6dd3f45248 Handle NXDOMAIN, REFUSED and log errors
- NXDOMAIN is an authoritive answer, so when receiving an NXDOMAIN, we're done.
  From RFC 1035: Name Error - Meaningful only for responses from an authoritative
  name server, this code signifies that the domain name referenced in the query
  does not exist.
  FROM RFC 8020: When an iterative caching DNS resolver receives an NXDOMAIN
  response, it SHOULD store it in its cache and then all names and resource
  record sets (RRsets) at or below that node SHOULD be considered unreachable.
  Subsequent queries for such names SHOULD elicit an NXDOMAIN response.
- REFUSED can be a transitional status: (https://www.ietf.org/rfc/rfc1035.txt)
  The name server refuses to perform the specified operation for
  policy reasons.  For example, a name server may not wish to provide the
  information to the particular requester, or a name server may not wish to
  perform a particular operation (e.g., zone)

Other errors are now logged as debug-message, which can be useful for
troubleshooting.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-10-12 00:55:36 +02:00
Sebastiaan van Stijn
a72bff0da3 Remove if/else and redundant brackets in resolver
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-10-12 00:55:28 +02:00
Thiago Alves Silva
d642cfdeb6 Increase max concurrent requests for DNS from 100 to 1000
This addresses/alleviates https://github.com/docker/libnetwork/issues/2214

The new proposed limit should remediate the issue for most users.

Signed-off-by: Thiago Alves Silva <thiago.alves@aurea.com>
2018-09-11 09:08:58 -03:00
Josh Soref
a06f1b2c4e Spelling fixes
* addresses
* assigned
* at least
* attachments
* auxiliary
* available
* cleanup
* communicate
* communications
* configuration
* connection
* connectivity
* destination
* encountered
* endpoint
* example
* existing
* expansion
* expected
* external
* forwarded
* gateway
* implementations
* implemented
* initialize
* internally
* loses
* message
* network
* occurred
* operational
* origin
* overlapping
* reaper
* redirector
* release
* representation
* resolver
* retrieve
* returns
* sanbdox
* sequence
* succesful
* synchronizing
* update
* validates

Signed-off-by: Josh Soref <jsoref@gmail.com>
2018-07-12 12:54:44 -07:00
Chris Telfer
147912afad Merge pull request #2132 from cziebuhr/2093-iface_order2
Improve interface order
2018-05-30 12:26:38 -04:00
Deep Debroy
20faf0adf0 Retry other external DNS servers on ServFail
Signed-off-by: Deep Debroy <ddebroy@docker.com>
2018-03-23 10:22:04 -07:00
Christoph Ziebuhr
6362d28969 Make go-tools happy
Signed-off-by: Christoph Ziebuhr <chris@codefrickler.de>
2018-03-21 10:31:56 +01:00
Deep Debroy
6a4c8d0ac9 Handle DNS querries of type MX
Signed-off-by: Deep Debroy <ddebroy@docker.com>
2017-12-20 14:32:47 -08:00
Madhu Venugopal
1662fc9709 Merge pull request #1856 from dmcgowan/update-logrus
Update logrus to v1.0.1
2017-08-08 14:01:10 -07:00
Derek McGowan
710e0664c4 Update logrus to v1.0.1
Fix case sensitivity issue
Update docker and runc vendors

Signed-off-by: Derek McGowan <derek@mcgstyle.net>
2017-08-07 11:20:47 -07:00
Sebastiaan van Stijn
7bd4fc1de4 Improve debugging for resolver
This patch improves debugging for the resolver;

- prefix debug messages with `[resolver]` for easier finding in the daemon logs
- use `A` / `AAAA` for query-types in the logs instead of their numeric code
- add debug messages if the external DNS did not return a result
- print sucessful results (t.b.d.)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-08-02 17:57:15 +02:00
Flavio Crisciani
af5e370627 Add gosimple check
Add the gosimple tool check in the Makefile
Fix all the issues identified

Signed-off-by: Flavio Crisciani <flavio.crisciani@docker.com>
2017-07-06 09:42:38 -07:00
Santhosh Manohar
6f507f53e3 Fix the data model inconsistency that breaks daemon upgrade to 1.14-dev
Signed-off-by: Santhosh Manohar <santhosh@docker.com>
2017-01-19 14:25:26 -08:00
Alessandro Boch
6dc6fb703b Merge pull request #1595 from sanimej/host
Add support in embedded DNS server for host loopback resolver
2017-01-05 12:18:48 -08:00
Santhosh Manohar
bf832ec2a7 Add embedded DNS server support for host loopback resolver
Signed-off-by: Santhosh Manohar <santhosh@docker.com>
2016-12-22 14:34:13 -08:00
Santhosh Manohar
879d94edbd Defer PTR queries to external servers based on A/AAAA response
Signed-off-by: Santhosh Manohar <santhosh@docker.com>
2016-12-20 14:45:13 -08:00
Santhosh Manohar
46b59b7964 Fix incorrect debug message
Signed-off-by: Santhosh Manohar <santhosh@docker.com>
2016-11-29 10:59:29 -08:00