Skip to content
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

feat: Add option to pass credentials in as http header #23

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
46 changes: 28 additions & 18 deletions trino/trino.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,20 @@ var (
)

const (
preparedStatementHeader = "X-Trino-Prepared-Statement"
preparedStatementName = "_trino_go"
trinoUserHeader = "X-Trino-User"
trinoSourceHeader = "X-Trino-Source"
trinoCatalogHeader = "X-Trino-Catalog"
trinoSchemaHeader = "X-Trino-Schema"
trinoSessionHeader = "X-Trino-Session"
trinoSetCatalogHeader = "X-Trino-Set-Catalog"
trinoSetSchemaHeader = "X-Trino-Set-Schema"
trinoSetPathHeader = "X-Trino-Set-Path"
trinoSetSessionHeader = "X-Trino-Set-Session"
trinoClearSessionHeader = "X-Trino-Clear-Session"
trinoSetRoleHeader = "X-Trino-Set-Role"
preparedStatementHeader = "X-Trino-Prepared-Statement"
preparedStatementName = "_trino_go"
trinoUserHeader = "X-Trino-User"
trinoSourceHeader = "X-Trino-Source"
trinoCatalogHeader = "X-Trino-Catalog"
trinoSchemaHeader = "X-Trino-Schema"
trinoSessionHeader = "X-Trino-Session"
trinoSetCatalogHeader = "X-Trino-Set-Catalog"
trinoSetSchemaHeader = "X-Trino-Set-Schema"
trinoSetPathHeader = "X-Trino-Set-Path"
trinoSetSessionHeader = "X-Trino-Set-Session"
trinoClearSessionHeader = "X-Trino-Clear-Session"
trinoSetRoleHeader = "X-Trino-Set-Role"
trinoExtraCredentialHeader = "X-Trino-Extra-Credential"

KerberosEnabledConfig = "KerberosEnabled"
kerberosKeytabPathConfig = "KerberosKeytabPath"
Expand Down Expand Up @@ -149,6 +150,7 @@ type Config struct {
Catalog string // Catalog (optional)
Schema string // Schema (optional)
SessionProperties map[string]string // Session properties (optional)
ExtraCredentials map[string]string // Extra credentials (optional)
CustomClientName string // Custom client name (optional)
KerberosEnabled string // KerberosEnabled (optional, default is false)
KerberosKeytabPath string // Kerberos Keytab Path (optional)
Expand All @@ -170,6 +172,12 @@ func (c *Config) FormatDSN() (string, error) {
sessionkv = append(sessionkv, k+"="+v)
}
}
var credkv []string
if c.ExtraCredentials != nil {
for k, v := range c.ExtraCredentials {
credkv = append(credkv, k+"="+v)
}
}
source := c.Source
if source == "" {
source = "trino-go-client"
Expand Down Expand Up @@ -199,6 +207,7 @@ func (c *Config) FormatDSN() (string, error) {
"catalog": c.Catalog,
"schema": c.Schema,
"session_properties": strings.Join(sessionkv, ","),
"extra_credentials": strings.Join(credkv, ","),
"custom_client": c.CustomClientName,
} {
if v != "" {
Expand Down Expand Up @@ -297,11 +306,12 @@ func newConn(dsn string) (*Conn, error) {
}

for k, v := range map[string]string{
trinoUserHeader: user,
trinoSourceHeader: query.Get("source"),
trinoCatalogHeader: query.Get("catalog"),
trinoSchemaHeader: query.Get("schema"),
trinoSessionHeader: query.Get("session_properties"),
trinoUserHeader: user,
trinoSourceHeader: query.Get("source"),
trinoCatalogHeader: query.Get("catalog"),
trinoSchemaHeader: query.Get("schema"),
trinoSessionHeader: query.Get("session_properties"),
trinoExtraCredentialHeader: query.Get("extra_credentials"),
} {
if v != "" {
c.httpHeaders.Add(k, v)
Expand Down
16 changes: 16 additions & 0 deletions trino/trino_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ func TestConfigSSLCertPath(t *testing.T) {
}
}

func TestExtraCredentials(t *testing.T) {
c := &Config{
ServerURI: "http://foobar@localhost:8080",
ExtraCredentials: map[string]string{"token": "mYtOkEn", "otherToken": "oThErToKeN"},
}

dsn, err := c.FormatDSN()
if err != nil {
t.Fatal(err)
}
want := "http://foobar@localhost:8080?extra_credentials=token%3DmYtOkEn%2CotherToken%3DoThErToKeN&source=trino-go-client"
if dsn != want {
t.Fatal("unexpected dsn:", dsn)
}
}

func TestConfigWithoutSSLCertPath(t *testing.T) {
c := &Config{
ServerURI: "https://foobar@localhost:8080",
Expand Down