Files
Krzysztof Skrzętnicki d8fe3a6d8f Extract Kerberos functionality from SQL Server engine (#51352)
* Extract Kerberos functionality from SQL Server engine

* fix typo, add missing trace.Wrap()
2025-02-12 11:11:16 +00:00

71 lines
2.0 KiB
Go

/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package sqlserver
import (
"github.com/gravitational/trace"
"github.com/jcmturner/gokrb5/v8/client"
"github.com/jcmturner/gokrb5/v8/spnego"
"github.com/gravitational/teleport/lib/srv/db/common"
)
// getAuth returns Kerberos authenticator used by SQL Server driver.
//
// TODO(r0mant): Unit-test this. In-memory Kerberos server?
func (c *connector) getAuth(sessionCtx *common.Session, kbClient *client.Client) (*krbAuth, error) {
// Obtain service ticket for the database's Service Principal Name.
ticket, encryptionKey, err := kbClient.GetServiceTicket(sessionCtx.Database.GetAD().SPN)
if err != nil {
return nil, trace.Wrap(err)
}
// Create init negotiation token.
initToken, err := spnego.NewNegTokenInitKRB5(kbClient, ticket, encryptionKey)
if err != nil {
return nil, trace.Wrap(err)
}
// Marshal init negotiation token.
initTokenBytes, err := initToken.Marshal()
if err != nil {
return nil, trace.Wrap(err)
}
return &krbAuth{
initToken: initTokenBytes,
}, nil
}
// krbAuth implements SQL Server driver's "auth" interface used during login
// to provide Kerberos authentication.
type krbAuth struct {
initToken []byte
}
func (a *krbAuth) InitialBytes() ([]byte, error) {
return a.initToken, nil
}
func (a *krbAuth) NextBytes(bytes []byte) ([]byte, error) {
return nil, nil
}
func (a *krbAuth) Free() {}