Skip to content

Commit b8e3eaa

Browse files
authored
chore(deps): update jwx library to v3 (#1287)
1 parent 034370c commit b8e3eaa

File tree

17 files changed

+108
-51
lines changed

17 files changed

+108
-51
lines changed

database/integration_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
"github.com/adhocore/gronx"
1414
"github.com/google/go-cmp/cmp"
15-
"github.com/lestrrat-go/jwx/v2/jwk"
15+
"github.com/lestrrat-go/jwx/v3/jwk"
1616

1717
api "github.com/go-vela/server/api/types"
1818
"github.com/go-vela/server/api/types/settings"
@@ -991,9 +991,14 @@ func testJWKs(t *testing.T, db Interface, resources *Resources) {
991991

992992
jkPub, _ := jk.(jwk.RSAPublicKey)
993993

994+
kid, ok := jkPub.KeyID()
995+
if !ok {
996+
t.Errorf("unable to get key ID for jwk")
997+
}
998+
994999
err := db.CreateJWK(context.TODO(), jkPub)
9951000
if err != nil {
996-
t.Errorf("unable to create jwk %s: %v", jkPub.KeyID(), err)
1001+
t.Errorf("unable to create jwk %s: %v", kid, err)
9971002
}
9981003
}
9991004
methods["CreateJWK"] = true
@@ -1014,9 +1019,14 @@ func testJWKs(t *testing.T, db Interface, resources *Resources) {
10141019

10151020
jkPub, _ := jk.(jwk.RSAPublicKey)
10161021

1017-
got, err := db.GetActiveJWK(context.TODO(), jkPub.KeyID())
1022+
kid, ok := jkPub.KeyID()
1023+
if !ok {
1024+
t.Errorf("unable to get key ID for jwk")
1025+
}
1026+
1027+
got, err := db.GetActiveJWK(context.TODO(), kid)
10181028
if err != nil {
1019-
t.Errorf("unable to get jwk %s: %v", jkPub.KeyID(), err)
1029+
t.Errorf("unable to get jwk %s: %v", kid, err)
10201030
}
10211031

10221032
if !cmp.Equal(jkPub, got, testutils.JwkKeyOpts) {
@@ -1036,7 +1046,12 @@ func testJWKs(t *testing.T, db Interface, resources *Resources) {
10361046

10371047
jkPub, _ := jk.(jwk.RSAPublicKey)
10381048

1039-
_, err := db.GetActiveJWK(context.TODO(), jkPub.KeyID())
1049+
kid, ok := jkPub.KeyID()
1050+
if !ok {
1051+
t.Errorf("unable to get key ID for jwk")
1052+
}
1053+
1054+
_, err := db.GetActiveJWK(context.TODO(), kid)
10401055
if err == nil {
10411056
t.Errorf("GetActiveJWK() should return err after rotation")
10421057
}

database/jwk/create.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ package jwk
55
import (
66
"context"
77
"database/sql"
8+
"fmt"
89

9-
"github.com/lestrrat-go/jwx/v2/jwk"
10+
"github.com/lestrrat-go/jwx/v3/jwk"
1011
"github.com/sirupsen/logrus"
1112

1213
"github.com/go-vela/server/constants"
@@ -15,9 +16,14 @@ import (
1516

1617
// CreateJWK creates a new JWK in the database.
1718
func (e *engine) CreateJWK(ctx context.Context, j jwk.RSAPublicKey) error {
19+
logKeyID, ok := j.KeyID()
20+
if !ok {
21+
return fmt.Errorf("unable to create JWK: no key provided")
22+
}
23+
1824
e.logger.WithFields(logrus.Fields{
19-
"jwk": j.KeyID(),
20-
}).Tracef("creating key %s", j.KeyID())
25+
"jwk": logKeyID,
26+
}).Tracef("creating key %s", logKeyID)
2127

2228
key := types.JWKFromAPI(j)
2329
key.Active = sql.NullBool{Bool: true, Valid: true}

database/jwk/create_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@ func TestJWK_Engine_CreateJWK(t *testing.T) {
2323
_postgres, _mock := testPostgres(t)
2424
defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }()
2525

26+
kid, ok := _jwk.KeyID()
27+
if !ok {
28+
t.Errorf("unable to get key ID for jwk")
29+
}
30+
2631
// ensure the mock expects the query
2732
_mock.ExpectExec(`INSERT INTO "jwks"
2833
("id","active","key")
2934
VALUES ($1,$2,$3)`).
30-
WithArgs(_jwk.KeyID(), true, _jwkBytes).
35+
WithArgs(kid, true, _jwkBytes).
3136
WillReturnResult(sqlmock.NewResult(1, 1))
3237

3338
_sqlite := testSqlite(t)

database/jwk/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package jwk
55
import (
66
"context"
77

8-
"github.com/lestrrat-go/jwx/v2/jwk"
8+
"github.com/lestrrat-go/jwx/v3/jwk"
99

1010
"github.com/go-vela/server/constants"
1111
"github.com/go-vela/server/database/types"

database/jwk/get_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
"github.com/DATA-DOG/go-sqlmock"
1111
"github.com/google/go-cmp/cmp"
12-
"github.com/lestrrat-go/jwx/v2/jwk"
12+
"github.com/lestrrat-go/jwx/v3/jwk"
1313

1414
"github.com/go-vela/server/database/testutils"
1515
)
@@ -25,13 +25,18 @@ func TestJWK_Engine_GetJWK(t *testing.T) {
2525
_postgres, _mock := testPostgres(t)
2626
defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }()
2727

28+
kid, ok := _jwk.KeyID()
29+
if !ok {
30+
t.Errorf("unable to get key ID for jwk")
31+
}
32+
2833
// create expected result in mock
2934
_rows := sqlmock.NewRows(
3035
[]string{"id", "active", "key"},
31-
).AddRow(_jwk.KeyID(), true, _jwkBytes)
36+
).AddRow(kid, true, _jwkBytes)
3237

3338
// ensure the mock expects the query
34-
_mock.ExpectQuery(`SELECT * FROM "jwks" WHERE id = $1 AND active = $2 LIMIT $3`).WithArgs(_jwk.KeyID(), true, 1).WillReturnRows(_rows)
39+
_mock.ExpectQuery(`SELECT * FROM "jwks" WHERE id = $1 AND active = $2 LIMIT $3`).WithArgs(kid, true, 1).WillReturnRows(_rows)
3540

3641
_sqlite := testSqlite(t)
3742
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()
@@ -65,7 +70,7 @@ func TestJWK_Engine_GetJWK(t *testing.T) {
6570
// run tests
6671
for _, test := range tests {
6772
t.Run(test.name, func(t *testing.T) {
68-
got, err := test.database.GetActiveJWK(context.TODO(), _jwk.KeyID())
73+
got, err := test.database.GetActiveJWK(context.TODO(), kid)
6974

7075
if test.failure {
7176
if err == nil {

database/jwk/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package jwk
55
import (
66
"context"
77

8-
"github.com/lestrrat-go/jwx/v2/jwk"
8+
"github.com/lestrrat-go/jwx/v3/jwk"
99
)
1010

1111
// JWKInterface represents the Vela interface for JWK

database/jwk/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package jwk
55
import (
66
"context"
77

8-
"github.com/lestrrat-go/jwx/v2/jwk"
8+
"github.com/lestrrat-go/jwx/v3/jwk"
99

1010
"github.com/go-vela/server/constants"
1111
"github.com/go-vela/server/database/types"

database/jwk/list_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"testing"
1010

1111
"github.com/DATA-DOG/go-sqlmock"
12-
"github.com/lestrrat-go/jwx/v2/jwk"
12+
"github.com/lestrrat-go/jwx/v3/jwk"
1313

1414
"github.com/go-vela/server/database/testutils"
1515
)
@@ -31,11 +31,21 @@ func TestJWK_Engine_ListJWKs(t *testing.T) {
3131
_postgres, _mock := testPostgres(t)
3232
defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }()
3333

34+
kidOne, ok := _jwkOne.KeyID()
35+
if !ok {
36+
t.Errorf("unable to get key ID for jwk")
37+
}
38+
39+
kidTwo, ok := _jwkTwo.KeyID()
40+
if !ok {
41+
t.Errorf("unable to get key ID for jwk")
42+
}
43+
3444
// create expected result in mock
3545
_rows := sqlmock.NewRows(
3646
[]string{"id", "active", "key"}).
37-
AddRow(_jwkOne.KeyID(), true, _jwkOneBytes).
38-
AddRow(_jwkTwo.KeyID(), true, _jwkTwoBytes)
47+
AddRow(kidOne, true, _jwkOneBytes).
48+
AddRow(kidTwo, true, _jwkTwoBytes)
3949

4050
// ensure the mock expects the query
4151
_mock.ExpectQuery(`SELECT * FROM "jwks"`).WillReturnRows(_rows)

database/jwk/rotate_test.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,31 @@ func TestJWK_Engine_RotateKeys(t *testing.T) {
2929
_postgres, _mock := testPostgres(t)
3030
defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }()
3131

32+
kidOne, ok := _jwkOne.KeyID()
33+
if !ok {
34+
t.Errorf("unable to get key ID for jwk")
35+
}
36+
37+
kidTwo, ok := _jwkTwo.KeyID()
38+
if !ok {
39+
t.Errorf("unable to get key ID for jwk")
40+
}
41+
3242
// create expected result in mock
3343
_rows := sqlmock.NewRows(
3444
[]string{"id", "active", "key"},
35-
).AddRow(_jwkOne.KeyID(), true, _jwkOneBytes)
45+
).AddRow(kidOne, true, _jwkOneBytes)
3646

3747
// ensure the mock expects the query
38-
_mock.ExpectQuery(`SELECT * FROM "jwks" WHERE id = $1 AND active = $2 LIMIT $3`).WithArgs(_jwkOne.KeyID(), true, 1).WillReturnRows(_rows)
48+
_mock.ExpectQuery(`SELECT * FROM "jwks" WHERE id = $1 AND active = $2 LIMIT $3`).WithArgs(kidOne, true, 1).WillReturnRows(_rows)
3949

4050
// create expected result in mock
4151
_rows = sqlmock.NewRows(
4252
[]string{"id", "active", "key"},
43-
).AddRow(_jwkTwo.KeyID(), true, _jwkTwoBytes)
53+
).AddRow(kidTwo, true, _jwkTwoBytes)
4454

4555
// ensure the mock expects the query
46-
_mock.ExpectQuery(`SELECT * FROM "jwks" WHERE id = $1 AND active = $2 LIMIT $3`).WithArgs(_jwkTwo.KeyID(), true, 1).WillReturnRows(_rows)
56+
_mock.ExpectQuery(`SELECT * FROM "jwks" WHERE id = $1 AND active = $2 LIMIT $3`).WithArgs(kidTwo, true, 1).WillReturnRows(_rows)
4757

4858
_mock.ExpectExec(`DELETE FROM "jwks" WHERE active = $1`).
4959
WithArgs(false).
@@ -87,12 +97,12 @@ func TestJWK_Engine_RotateKeys(t *testing.T) {
8797
// run tests
8898
for _, test := range tests {
8999
t.Run(test.name, func(t *testing.T) {
90-
_, err := test.database.GetActiveJWK(context.TODO(), _jwkOne.KeyID())
100+
_, err := test.database.GetActiveJWK(context.TODO(), kidOne)
91101
if err != nil {
92102
t.Errorf("GetActiveJWK for %s returned err: %v", test.name, err)
93103
}
94104

95-
_, err = test.database.GetActiveJWK(context.TODO(), _jwkTwo.KeyID())
105+
_, err = test.database.GetActiveJWK(context.TODO(), kidTwo)
96106
if err != nil {
97107
t.Errorf("GetActiveJWK for %s returned err: %v", test.name, err)
98108
}
@@ -111,12 +121,12 @@ func TestJWK_Engine_RotateKeys(t *testing.T) {
111121
t.Errorf("RotateKeys for %s returned err: %v", test.name, err)
112122
}
113123

114-
_, err = test.database.GetActiveJWK(context.TODO(), _jwkOne.KeyID())
124+
_, err = test.database.GetActiveJWK(context.TODO(), kidOne)
115125
if err == nil {
116126
t.Errorf("GetActiveJWK for %s should have returned err", test.name)
117127
}
118128

119-
_, err = test.database.GetActiveJWK(context.TODO(), _jwkTwo.KeyID())
129+
_, err = test.database.GetActiveJWK(context.TODO(), kidTwo)
120130
if err == nil {
121131
t.Errorf("GetActiveJWK for %s should have returned err", test.name)
122132
}

database/testutils/api_resources.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"crypto/rsa"
88

99
"github.com/google/uuid"
10-
"github.com/lestrrat-go/jwx/v2/jwk"
10+
"github.com/lestrrat-go/jwx/v3/jwk"
1111

1212
api "github.com/go-vela/server/api/types"
1313
"github.com/go-vela/server/api/types/actions"
@@ -302,7 +302,7 @@ func JWK() jwk.RSAPublicKey {
302302
return nil
303303
}
304304

305-
pubJwk, err := jwk.FromRaw(privateRSAKey.PublicKey)
305+
pubJwk, err := jwk.Import(privateRSAKey.PublicKey)
306306
if err != nil {
307307
return nil
308308
}

0 commit comments

Comments
 (0)