forked from denisenkom/go-mssqldb
-
Notifications
You must be signed in to change notification settings - Fork 65
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
add Kerberos auth #35
Merged
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
80db7fd
add kerberos auth
85c33fc
added kerberos auth
shwetamoharil 4986010
Merge branch 'main' into kerberos_auth
7ffe6ce
Krb auth (#1)
chandanjainn bb2bb99
build tag for go 1.13
d842b56
Remove krb dep (#2)
shwetamoharil ef2e8b2
add Kerberos and KerberosConfig structs
d92f2a8
make krb cache and keyta mutually excusive
fe3924f
Merge branch 'main' into kerberos_auth
cde874d
add krb5 provider
73d65f1
add krb conn str examples
770b7da
update krb con str example
47f4981
update krb params info
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package mssql | ||
|
||
import "github.com/microsoft/go-mssqldb/msdsn" | ||
|
||
func getAuthN(p msdsn.Config) (auth auth, authOk bool) { | ||
if p.Kerberos != nil && p.Kerberos.Config != nil { | ||
auth, authOk = getKRB5Auth(p.User, p.ServerSPN, p.Kerberos.Config, p.Kerberos.Keytab, p.Kerberos.Cache) | ||
} else { | ||
auth, authOk = getAuth(p.User, p.Password, p.ServerSPN, p.Workstation) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package mssql | ||
|
||
import "github.com/microsoft/go-mssqldb/msdsn" | ||
|
||
func getAuthN(p msdsn.Config) (auth auth, authOk bool) { | ||
auth, authOk = getAuth(p.User, p.Password, p.ServerSPN, p.Workstation) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package mssql | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
chandanjainn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
"github.com/jcmturner/gokrb5/v8/client" | ||
"github.com/jcmturner/gokrb5/v8/config" | ||
"github.com/jcmturner/gokrb5/v8/credentials" | ||
"github.com/jcmturner/gokrb5/v8/keytab" | ||
"github.com/jcmturner/gokrb5/v8/spnego" | ||
) | ||
|
||
type krb5Auth struct { | ||
username string | ||
realm string | ||
serverSPN string | ||
port uint64 | ||
krb5Config *config.Config | ||
krbKeytab *keytab.Keytab | ||
krbCache *credentials.CCache | ||
krb5Client *client.Client | ||
state krb5ClientState | ||
} | ||
|
||
func getKRB5Auth(user, serverSPN string, krb5Conf *config.Config, keytabContent *keytab.Keytab, cacheContent *credentials.CCache) (auth, bool) { | ||
var port uint64 | ||
var realm, serviceStr string | ||
var err error | ||
|
||
params1 := strings.Split(serverSPN, ":") | ||
if len(params1) != 2 { | ||
return nil, false | ||
} | ||
|
||
params2 := strings.Split(params1[1], "@") | ||
switch len(params2) { | ||
case 1: | ||
port, err = strconv.ParseUint(params1[1], 10, 16) | ||
if err != nil { | ||
return nil, false | ||
} | ||
case 2: | ||
port, err = strconv.ParseUint(params2[0], 10, 16) | ||
if err != nil { | ||
return nil, false | ||
} | ||
default: | ||
return nil, false | ||
} | ||
|
||
params3 := strings.Split(serverSPN, "@") | ||
switch len(params3) { | ||
case 1: | ||
serviceStr = params3[0] | ||
params3 = strings.Split(params1[0], "/") | ||
params3 = strings.Split(params3[1], ".") | ||
realm = params3[1] + "." + params3[2] | ||
case 2: | ||
realm = params3[1] | ||
serviceStr = params3[0] | ||
default: | ||
return nil, false | ||
} | ||
|
||
return &krb5Auth{ | ||
username: user, | ||
serverSPN: serviceStr, | ||
port: port, | ||
realm: realm, | ||
krb5Config: krb5Conf, | ||
krbKeytab: keytabContent, | ||
krbCache: cacheContent, | ||
}, true | ||
} | ||
|
||
func (auth *krb5Auth) InitialBytes() ([]byte, error) { | ||
var cl *client.Client | ||
var err error | ||
// Init keytab from conf | ||
if auth.krbKeytab != nil { | ||
// Init krb5 client and login | ||
cl = client.NewWithKeytab(auth.username, auth.realm, auth.krbKeytab, auth.krb5Config, client.DisablePAFXFAST(true)) | ||
} else { | ||
cl, err = client.NewFromCCache(auth.krbCache, auth.krb5Config) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
} | ||
auth.krb5Client = cl | ||
auth.state = initiatorStart | ||
tkt, sessionKey, err := cl.GetServiceTicket(auth.serverSPN) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
negTok, err := spnego.NewNegTokenInitKRB5(auth.krb5Client, tkt, sessionKey) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
outToken, err := negTok.Marshal() | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
auth.state = initiatorWaitForMutal | ||
return outToken, nil | ||
} | ||
|
||
func (auth *krb5Auth) Free() { | ||
auth.krb5Client.Destroy() | ||
} | ||
|
||
func (auth *krb5Auth) NextBytes(token []byte) ([]byte, error) { | ||
var spnegoToken spnego.SPNEGOToken | ||
if err := spnegoToken.Unmarshal(token); err != nil { | ||
err := fmt.Errorf("unmarshal APRep token failed: %w", err) | ||
return []byte{}, err | ||
} | ||
auth.state = initiatorReady | ||
return []byte{}, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JDBC driver docs have "realm" as optional
https://docs.microsoft.com/sql/connect/jdbc/setting-the-connection-properties?view=sql-server-ver16
(Version 9.4+) The realm for Kerberos authentication. Set this value to override the Kerberos authentication realm the driver autodetects from the server's realm.
Do you know what it means by "autodetects from the server's realm"? Is that a feature in JDBC we are missing here? Or is it something handled in the krb module itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will check up on the JDBC part and revert. I hope, I have answered rest of your questions?