关于hadoop-common 中的_HOST替换 涉及反向解析DNS

核心代码在

hadoop-trunk\hadoop-common-project\hadoop-common\src\main\java\org\apache\hadoop\security

hadoop-main--->hadoop-project--->hadoop-project-dist--->hadoop-common 3.4.0-SNAPSHOT

org.apache.hadoop.security.SecurityUtil#getServerPrincipal(java.lang.String, java.lang.String)

IDEA  Ctrl+N找不到某个类的时候,可以试试Ctrl+Shfit+N,项目加载可能会有小问题

@InterfaceAudience.Public
@InterfaceStability.Evolving
public static String getServerPrincipal(String principalConfig,
    String hostname) throws IOException {
  String[] components = principalConfig.split("[/@]")
  if (components == null || components.length != 3
      || !components[1].equals(HOSTNAME_PATTERN)) {
    return principalConfig;
  } else {
    return replacePattern(components, hostname);
  }
}

还有一个重载的方法

@InterfaceAudience.Public
@InterfaceStability.Evolving
public static String getServerPrincipal(String principalConfig,
    InetAddress addr) throws IOException {
  String[] components = getComponents(principalConfig);
  if (components == null || components.length != 3
      || !components[1].equals(HOSTNAME_PATTERN)) {
    return principalConfig;
  } else {
    if (addr == null) {
      throw new IOException("Can't replace " + HOSTNAME_PATTERN
          + " pattern since client address is null");
    }
    return replacePattern(components, domainNameResolver.getHostnameByIP(addr));
  }
}

重点关注 domainNameResolver.getHostnameByIP

org.apache.hadoop.net.DomainNameResolver domainNameResolver = DomainNameResolverFactory.newInstance(conf,
    CommonConfigurationKeys.HADOOP_SECURITY_RESOLVER_IMPL);

有一个配置项 hadoop.security.resolver.impl 默认是 org.apache.hadoop.net.DNSDomainNameResolver

The resolver implementation used to resolve FQDN for Kerberos

反向解析dns的逻辑 根据ip获取域名

@Override
public String getHostnameByIP(InetAddress address) {
  String host = address.getCanonicalHostName();
  if (host != null && host.length() != 0
      && host.charAt(host.length()-1) == '.') {
    host = host.substring(0, host.length()-1);
  }
  // Protect against the Java behaviour of returning the IP address as a string from a cache
  // instead of performing a reverse lookup.

//防止Java行为,即从缓存中返回IP地址的字符串形式, 而不是执行反向DNS查找。
解释
这行注释意味着在处理IP地址时,Java程序可能会直接从缓存中返回IP地址的字符串表示,而不是进行反向DNS查找来获取与该IP地址关联的域名。这可能是因为反向查找可能比较慢,而直接从缓存中读取可以提高性能。然而,在某些情况下,直接使用IP地址可能不满足需求,比如需要知道具体的域名时。因此,这行注释提醒开发者要注意这种行为,并根据具体场景决定是否需要进行反向DNS查找。

此处涉及缓存(即 修改/etc/hosts 需要重启进程的现象)
  if (host != null && host.equals(address.getHostAddress())) {
    LOG.debug("IP address returned for FQDN detected: {}", address.getHostAddress());
    try {
      return DNS.reverseDns(address, null);
    } catch (NamingException lookupFailure) {
      LOG.warn("Failed to perform reverse lookup: {}", address);
    }
  }
  return host;
}
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hirolee88

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值