Skip to content

Commit

Permalink
Accept additional values for encrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvdeshmukh committed Jul 6, 2023
1 parent 78ad891 commit 3cc2463
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
24 changes: 14 additions & 10 deletions msdsn/conn_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
EncryptionOff = 0
EncryptionRequired = 1
EncryptionDisabled = 3
EncryptionStrict = 4
)

const (
Expand Down Expand Up @@ -130,21 +131,24 @@ func parseTLS(params map[string]string, host string) (Encryption, *tls.Config, e
var encryption Encryption = EncryptionOff
encrypt, ok := params["encrypt"]
if ok {
if strings.EqualFold(encrypt, "DISABLE") {
encrypt = strings.ToLower(encrypt)
switch encrypt {
case "mandatory", "yes", "1", "t", "true":
encryption = EncryptionRequired
case "disable":
encryption = EncryptionDisabled
} else {
e, err := strconv.ParseBool(encrypt)
if err != nil {
f := "invalid encrypt '%s': %s"
return encryption, nil, fmt.Errorf(f, encrypt, err.Error())
}
if e {
encryption = EncryptionRequired
}
case "strict":
encryption = EncryptionStrict
case "optional", "no", "0", "f", "false":
encryption = EncryptionOff
default:
f := "invalid encrypt '%s'"
return encryption, nil, fmt.Errorf(f, encrypt)
}
} else {
trustServerCert = true
}

trust, ok := params["trustservercertificate"]
if ok {
var err error
Expand Down
3 changes: 3 additions & 0 deletions msdsn/conn_str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func TestValidConnectionString(t *testing.T) {
{"encrypt=disable", func(p Config) bool { return p.Encryption == EncryptionDisabled }},
{"encrypt=disable;tlsmin=1.1", func(p Config) bool { return p.Encryption == EncryptionDisabled && p.TLSConfig == nil }},
{"encrypt=true", func(p Config) bool { return p.Encryption == EncryptionRequired && p.TLSConfig.MinVersion == 0 }},
{"encrypt=mandatory", func(p Config) bool { return p.Encryption == EncryptionRequired && p.TLSConfig.MinVersion == 0 }},
{"encrypt=true;tlsmin=1.0", func(p Config) bool {
return p.Encryption == EncryptionRequired && p.TLSConfig.MinVersion == tls.VersionTLS10
}},
Expand All @@ -78,6 +79,8 @@ func TestValidConnectionString(t *testing.T) {
return p.Encryption == EncryptionRequired && p.TLSConfig.MinVersion == 0
}},
{"encrypt=false", func(p Config) bool { return p.Encryption == EncryptionOff }},
{"encrypt=optional", func(p Config) bool { return p.Encryption == EncryptionOff }},
{"encrypt=strict", func(p Config) bool { return p.Encryption == EncryptionStrict }},
{"connection timeout=3;dial timeout=4;keepalive=5", func(p Config) bool {
return p.ConnTimeout == 3*time.Second && p.DialTimeout == 4*time.Second && p.KeepAlive == 5*time.Second
}},
Expand Down

0 comments on commit 3cc2463

Please sign in to comment.