Skip to content

Commit

Permalink
Make use_hosts_file a tristate option
Browse files Browse the repository at this point in the history
The Auto option is meant to allow downstream users to intercept the
config and replace it with what they deem to be suitable.
  • Loading branch information
hch12907 committed Mar 21, 2024
1 parent f3732bd commit 2881ca8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
9 changes: 4 additions & 5 deletions crates/resolver/src/async_resolver.rs
Expand Up @@ -19,7 +19,7 @@ use proto::xfer::{DnsRequestOptions, RetryDnsHandle};
use tracing::{debug, trace};

use crate::caching_client::CachingClient;
use crate::config::{ResolverConfig, ResolverOpts};
use crate::config::{ResolveHosts, ResolverConfig, ResolverOpts};
use crate::dns_lru::{self, DnsLru};
use crate::error::*;
use crate::lookup::{self, Lookup, LookupEither, LookupFuture};
Expand Down Expand Up @@ -223,10 +223,9 @@ impl<P: ConnectionProvider> AsyncResolver<P> {
either = LookupEither::Retry(client);
}

let hosts = if options.use_hosts_file {
Some(Arc::new(Hosts::new()))
} else {
None
let hosts = match options.use_hosts_file {
ResolveHosts::Always | ResolveHosts::Auto => Some(Arc::new(Hosts::new())),
ResolveHosts::Never => None,
};

trace!("handle passed back");
Expand Down
26 changes: 23 additions & 3 deletions crates/resolver/src/config.rs
Expand Up @@ -906,6 +906,26 @@ impl Default for ServerOrderingStrategy {
}
}

/// Whether the system hosts file should be respected by the resolver.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde-config", derive(Serialize, Deserialize))]
pub enum ResolveHosts {
/// Always attempt to look up IP addresses from the system hosts file.
/// If the hostname cannot be found, query the DNS.
Always,
/// The DNS will always be queried.
Never,
/// Use local resolver configurations only when this resolver is not used in
/// a DNS forwarder. This is the default.
Auto,
}

impl Default for ResolveHosts {
fn default() -> Self {
Self::Auto
}
}

/// Configuration for the Resolver
#[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(
Expand Down Expand Up @@ -937,8 +957,8 @@ pub struct ResolverOpts {
pub ip_strategy: LookupIpStrategy,
/// Cache size is in number of records (some records can be large)
pub cache_size: usize,
/// Check /ect/hosts file before dns requery (only works for unix like OS)
pub use_hosts_file: bool,
/// Check /etc/hosts file before dns requery (only works for unix like OS)
pub use_hosts_file: ResolveHosts,
/// Optional minimum TTL for positive responses.
///
/// If this is set, any positive responses with a TTL lower than this value will have a TTL of
Expand Down Expand Up @@ -999,7 +1019,7 @@ impl Default for ResolverOpts {
validate: false,
ip_strategy: LookupIpStrategy::default(),
cache_size: 32,
use_hosts_file: true,
use_hosts_file: ResolveHosts::default(),
positive_min_ttl: None,
negative_min_ttl: None,
positive_max_ttl: None,
Expand Down
8 changes: 7 additions & 1 deletion crates/server/src/store/forwarder/authority.rs
Expand Up @@ -7,7 +7,7 @@

use std::io;

use hickory_resolver::name_server::TokioConnectionProvider;
use hickory_resolver::{config::ResolveHosts, name_server::TokioConnectionProvider};
use tracing::{debug, info};

use crate::{
Expand Down Expand Up @@ -75,6 +75,12 @@ impl ForwardAuthority {
options.preserve_intermediates = true;
}

// Require people to explicitly request for /etc/hosts usage in forwarder
// configs
if options.use_hosts_file == ResolveHosts::Auto {
options.use_hosts_file = ResolveHosts::Never;
}

let config = ResolverConfig::from_parts(None, vec![], name_servers);

let resolver = TokioAsyncResolver::new(config, options, TokioConnectionProvider::default());
Expand Down

0 comments on commit 2881ca8

Please sign in to comment.