| 1 |
package rpc |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"net" |
| 7 |
"regexp" |
| 8 |
"sort" |
| 9 |
|
| 10 |
hadoop "github.com/colinmarc/hdfs/v2/internal/protocol/hadoop_common" |
| 11 |
"github.com/colinmarc/hdfs/v2/internal/sasl" |
| 12 |
"github.com/jcmturner/gokrb5/v8/gssapi" |
| 13 |
"github.com/jcmturner/gokrb5/v8/iana/keyusage" |
| 14 |
"github.com/jcmturner/gokrb5/v8/spnego" |
| 15 |
krbtypes "github.com/jcmturner/gokrb5/v8/types" |
| 16 |
) |
| 17 |
|
| 18 |
const saslRpcCallId = -33 |
| 19 |
|
| 20 |
var ( |
| 21 |
errKerberosNotSupported = errors.New("kerberos authentication not supported by namenode") |
| 22 |
krbSPNHost = regexp.MustCompile(`\A[^/]+/(_HOST)([@/]|\z)`) |
| 23 |
) |
| 24 |
|
| 25 |
func (c *NamenodeConnection) doKerberosHandshake() error { |
| 26 |
// Start negotiation, and get the list of supported mechanisms in reply. |
| 27 |
err := c.writeSaslRequest(&hadoop.RpcSaslProto{ |
| 28 |
State: hadoop.RpcSaslProto_NEGOTIATE.Enum(), |
| 29 |
}) |
| 30 |
if err != nil { |
| 31 |
return err |
| 32 |
} |
| 33 |
|
| 34 |
resp, err := c.readSaslResponse(hadoop.RpcSaslProto_NEGOTIATE) |
| 35 |
if err != nil { |
| 36 |
return err |
| 37 |
} |
| 38 |
|
| 39 |
var krbAuth, tokenAuth *hadoop.RpcSaslProto_SaslAuth |
| 40 |
for _, m := range resp.GetAuths() { |
| 41 |
switch *m.Method { |
| 42 |
case "KERBEROS": |
| 43 |
krbAuth = m |
| 44 |
case "TOKEN": |
| 45 |
tokenAuth = m |
| 46 |
default: |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
if krbAuth == nil { |
| 51 |
return errKerberosNotSupported |
| 52 |
} |
| 53 |
|
| 54 |
// Get a ticket from Kerberos, and send the initial token to the namenode. |
| 55 |
token, sessionKey, err := c.getKerberosTicket() |
| 56 |
if err != nil { |
| 57 |
return err |
| 58 |
} |
| 59 |
|
| 60 |
if tokenAuth != nil { |
| 61 |
challenge, err := sasl.ParseChallenge(tokenAuth.Challenge) |
| 62 |
if err != nil { |
| 63 |
return err |
| 64 |
} |
| 65 |
|
| 66 |
// Some versions of HDP 3.x expect us to pick the highest Qop, and |
| 67 |
// return a malformed response otherwise. |
| 68 |
sort.Sort(challenge.Qop) |
| 69 |
qop := challenge.Qop[0] |
| 70 |
|
| 71 |
switch qop { |
| 72 |
case sasl.QopPrivacy, sasl.QopIntegrity: |
| 73 |
// Switch to SASL RPC handler |
| 74 |
c.transport = &saslTransport{ |
| 75 |
basicTransport: basicTransport{ |
| 76 |
clientID: c.ClientID, |
| 77 |
}, |
| 78 |
sessionKey: sessionKey, |
| 79 |
privacy: qop == sasl.QopPrivacy, |
| 80 |
} |
| 81 |
case sasl.QopAuthentication: |
| 82 |
// No special transport is required. |
| 83 |
default: |
| 84 |
return errors.New("unexpected QOP in challenge") |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
err = c.writeSaslRequest(&hadoop.RpcSaslProto{ |
| 89 |
State: hadoop.RpcSaslProto_INITIATE.Enum(), |
| 90 |
Token: token.MechTokenBytes, |
| 91 |
Auths: []*hadoop.RpcSaslProto_SaslAuth{krbAuth}, |
| 92 |
}) |
| 93 |
if err != nil { |
| 94 |
return err |
| 95 |
} |
| 96 |
|
| 97 |
// In response, we get a server token to verify. |
| 98 |
resp, err = c.readSaslResponse(hadoop.RpcSaslProto_CHALLENGE) |
| 99 |
if err != nil { |
| 100 |
return err |
| 101 |
} |
| 102 |
|
| 103 |
var nnToken gssapi.WrapToken |
| 104 |
err = nnToken.Unmarshal(resp.GetToken(), true) |
| 105 |
if err != nil { |
| 106 |
return err |
| 107 |
} |
| 108 |
|
| 109 |
_, err = nnToken.Verify(sessionKey, keyusage.GSSAPI_ACCEPTOR_SEAL) |
| 110 |
if err != nil { |
| 111 |
return fmt.Errorf("invalid server token: %s", err) |
| 112 |
} |
| 113 |
|
| 114 |
// Sign the payload and send it back to the namenode. |
| 115 |
// TODO: Make sure we can support what is required based on what's in the |
| 116 |
// payload. |
| 117 |
signed, err := gssapi.NewInitiatorWrapToken(nnToken.Payload, sessionKey) |
| 118 |
if err != nil { |
| 119 |
return err |
| 120 |
} |
| 121 |
|
| 122 |
signedBytes, err := signed.Marshal() |
| 123 |
if err != nil { |
| 124 |
return err |
| 125 |
} |
| 126 |
|
| 127 |
err = c.writeSaslRequest(&hadoop.RpcSaslProto{ |
| 128 |
State: hadoop.RpcSaslProto_RESPONSE.Enum(), |
| 129 |
Token: signedBytes, |
| 130 |
}) |
| 131 |
if err != nil { |
| 132 |
return err |
| 133 |
} |
| 134 |
|
| 135 |
// Read the final response. If it's a SUCCESS, then we're done here. |
| 136 |
_, err = c.readSaslResponse(hadoop.RpcSaslProto_SUCCESS) |
| 137 |
return err |
| 138 |
} |
| 139 |
|
| 140 |
func (c *NamenodeConnection) writeSaslRequest(req *hadoop.RpcSaslProto) error { |
| 141 |
rrh := newRPCRequestHeader(saslRpcCallId, c.ClientID) |
| 142 |
packet, err := makeRPCPacket(rrh, req) |
| 143 |
if err != nil { |
| 144 |
return err |
| 145 |
} |
| 146 |
|
| 147 |
_, err = c.conn.Write(packet) |
| 148 |
return err |
| 149 |
} |
| 150 |
|
| 151 |
func (c *NamenodeConnection) readSaslResponse(expectedState hadoop.RpcSaslProto_SaslState) (*hadoop.RpcSaslProto, error) { |
| 152 |
rrh := &hadoop.RpcResponseHeaderProto{} |
| 153 |
resp := &hadoop.RpcSaslProto{} |
| 154 |
err := readRPCPacket(c.conn, rrh, resp) |
| 155 |
if err != nil { |
| 156 |
return nil, err |
| 157 |
} else if int32(rrh.GetCallId()) != saslRpcCallId { |
| 158 |
return nil, errors.New("unexpected sequence number") |
| 159 |
} else if rrh.GetStatus() != hadoop.RpcResponseHeaderProto_SUCCESS { |
| 160 |
return nil, &NamenodeError{ |
| 161 |
method: "sasl", |
| 162 |
message: rrh.GetErrorMsg(), |
| 163 |
code: int(rrh.GetErrorDetail()), |
| 164 |
exception: rrh.GetExceptionClassName(), |
| 165 |
} |
| 166 |
} else if resp.GetState() != expectedState { |
| 167 |
return nil, fmt.Errorf("unexpected SASL state: %s", resp.GetState().String()) |
| 168 |
} |
| 169 |
|
| 170 |
return resp, nil |
| 171 |
} |
| 172 |
|
| 173 |
// getKerberosTicket returns an initial kerberos negotiation token and the |
| 174 |
// paired session key, along with an error if any occured. |
| 175 |
func (c *NamenodeConnection) getKerberosTicket() (spnego.NegTokenInit, krbtypes.EncryptionKey, error) { |
| 176 |
host, _, _ := net.SplitHostPort(c.host.address) |
| 177 |
spn := replaceSPNHostWildcard(c.kerberosServicePrincipleName, host) |
| 178 |
|
| 179 |
ticket, key, err := c.kerberosClient.GetServiceTicket(spn) |
| 180 |
if err != nil { |
| 181 |
return spnego.NegTokenInit{}, key, err |
| 182 |
} |
| 183 |
|
| 184 |
token, err := spnego.NewNegTokenInitKRB5(c.kerberosClient, ticket, key) |
| 185 |
return token, key, err |
| 186 |
} |
| 187 |
|
| 188 |
// replaceSPNHostWildcard substitutes the special string '_HOST' in the given |
| 189 |
// SPN for the given (current) host. |
| 190 |
func replaceSPNHostWildcard(spn, host string) string { |
| 191 |
res := krbSPNHost.FindStringSubmatchIndex(spn) |
| 192 |
if res == nil || res[2] == -1 { |
| 193 |
return spn |
| 194 |
} |
| 195 |
|
| 196 |
return spn[:res[2]] + host + spn[res[3]:] |
| 197 |
} |