Skip to content

reverse resolve the namenode hostname for kerberos spn #334

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions internal/rpc/kerberos.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"regexp"
"sort"
"strings"

hadoop "github.com/colinmarc/hdfs/v2/internal/protocol/hadoop_common"
"github.com/colinmarc/hdfs/v2/internal/sasl"
Expand Down Expand Up @@ -170,10 +171,53 @@ func (c *NamenodeConnection) readSaslResponse(expectedState hadoop.RpcSaslProto_
return resp, nil
}

func isValidHostname(host string, addr string) bool {
addrs, err := net.LookupHost(host)
if err != nil {
return false
}
if len(addrs) == 0 {
return false
}
for _, a := range addrs {
if a != addr {
return false
}
}
return true
}

func reverseLookup(host string, restrict bool) string {
addrs, err := net.LookupHost(host)
if err != nil {
return ""
}
for _, addr := range addrs {
names, err := net.LookupAddr(addr)
if err != nil {
continue
}
for _, name := range names {
if restrict {
if !isValidHostname(name, addr) {
continue
}
}
return strings.TrimSuffix(name, ".")
}
}
return ""
}

// getKerberosTicket returns an initial kerberos negotiation token and the
// paired session key, along with an error if any occured.
func (c *NamenodeConnection) getKerberosTicket() (spnego.NegTokenInit, krbtypes.EncryptionKey, error) {
host, _, _ := net.SplitHostPort(c.host.address)
// Hadoop uses the reverse-resolved hostname for the SPN, so we do the same.
// https://github.com/apache/hadoop/blob/7a7db7f0dc4107f44b281eb834fdffc9fd9b08b3/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSUtil.java#L445
if revHost := reverseLookup(host, true); revHost != "" {
host = revHost
}
spn := replaceSPNHostWildcard(c.kerberosServicePrincipleName, host)

ticket, key, err := c.kerberosClient.GetServiceTicket(spn)
Expand Down