Skip to content

Commit e7d9983

Browse files
Jan Waśwendigo
authored andcommitted
Add integration test for JWT auth
1 parent f1b3d95 commit e7d9983

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/trinodb/trino-go-client
33
go 1.21
44

55
require (
6+
github.com/golang-jwt/jwt/v4 v4.5.0
67
github.com/ory/dockertest/v3 v3.10.0
78
github.com/stretchr/testify v1.9.0
89
gopkg.in/jcmturner/gokrb5.v6 v6.1.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfC
2727
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
2828
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
2929
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
30+
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
31+
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
3032
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
3133
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3234
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=

trino/integration_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"testing"
3737
"time"
3838

39+
"github.com/golang-jwt/jwt/v4"
3940
dt "github.com/ory/dockertest/v3"
4041
)
4142

@@ -875,6 +876,63 @@ func TestIntegrationQueryContextCancellation(t *testing.T) {
875876
}
876877
}
877878

879+
func TestIntegrationAccessToken(t *testing.T) {
880+
if tlsServer == "" {
881+
t.Skip("Skipping access token test when using a custom integration server.")
882+
}
883+
884+
accessToken, err := generateToken()
885+
if err != nil {
886+
t.Fatal(err)
887+
}
888+
889+
dsn := tlsServer + "?accessToken=" + accessToken
890+
891+
db := integrationOpen(t, dsn)
892+
893+
defer db.Close()
894+
rows, err := db.Query("SHOW CATALOGS")
895+
if err != nil {
896+
t.Fatal(err)
897+
}
898+
defer rows.Close()
899+
count := 0
900+
for rows.Next() {
901+
count++
902+
}
903+
if count < 1 {
904+
t.Fatal("not enough rows returned:", count)
905+
}
906+
}
907+
908+
func generateToken() (string, error) {
909+
privateKeyPEM, err := os.ReadFile("etc/secrets/private_key.pem")
910+
if err != nil {
911+
return "", fmt.Errorf("error reading private key file: %w", err)
912+
}
913+
914+
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(privateKeyPEM)
915+
if err != nil {
916+
return "", fmt.Errorf("error parsing private key: %w", err)
917+
}
918+
919+
// Subject must be 'test'
920+
claims := jwt.RegisteredClaims{
921+
ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * 365 * time.Hour)),
922+
Issuer: "gotrino",
923+
Subject: "test",
924+
}
925+
926+
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
927+
signedToken, err := token.SignedString(privateKey)
928+
929+
if err != nil {
930+
return "", fmt.Errorf("error generating token: %w", err)
931+
}
932+
933+
return signedToken, nil
934+
}
935+
878936
func contextSleep(ctx context.Context, d time.Duration) error {
879937
timer := time.NewTimer(100 * time.Millisecond)
880938
select {

0 commit comments

Comments
 (0)