Skip to content

Commit 9898fbd

Browse files
authored
fix: add pagination to list public key mappings response (#1889)
### Proposed Changes Missed adding pagination response to the list public key mappings response. Also includes additional tests for limiting the result set and also testing of filters by kas id and public key id. ### Checklist - [x] I have added or updated unit tests - [x] I have added or updated integration tests (if appropriate) - [x] I have added or updated documentation ### Testing Instructions
1 parent a86132b commit 9898fbd

File tree

8 files changed

+474
-348
lines changed

8 files changed

+474
-348
lines changed

docs/grpc/index.html

Lines changed: 30 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/openapi/policy/kasregistry/key_access_server_registry.swagger.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protocol/go/policy/kasregistry/key_access_server_registry.pb.go

Lines changed: 331 additions & 317 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

service/integration/kas_registry_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,17 @@ func (s *KasRegistrySuite) Test_List_Public_Keys_ByKasID() {
12151215
s.NotEqual(totalKeys, filteredTotalKeys)
12161216
}
12171217

1218+
func (s *KasRegistrySuite) Test_List_Public_Keys_WithLimit_1() {
1219+
r, err := s.db.PolicyClient.ListPublicKeys(s.ctx, &kasregistry.ListPublicKeysRequest{
1220+
Pagination: &policy.PageRequest{
1221+
Limit: 1,
1222+
},
1223+
})
1224+
s.Require().NoError(err)
1225+
s.NotNil(r)
1226+
s.Len(r.GetKeys(), 1)
1227+
}
1228+
12181229
func (s *KasRegistrySuite) Test_List_Public_Keys_WithNonExistentKasID() {
12191230
r, err := s.db.PolicyClient.ListPublicKeys(s.ctx, &kasregistry.ListPublicKeysRequest{KasId: nonExistentKasRegistryID})
12201231
s.Require().NoError(err)
@@ -1249,6 +1260,42 @@ func (s *KasRegistrySuite) Test_List_Public_Key_Mappings() {
12491260
}
12501261
}
12511262

1263+
func (s *KasRegistrySuite) Test_List_Public_Key_Mappings_WithLimit_1() {
1264+
r, err := s.db.PolicyClient.ListPublicKeyMappings(s.ctx, &kasregistry.ListPublicKeyMappingRequest{
1265+
Pagination: &policy.PageRequest{
1266+
Limit: 1,
1267+
},
1268+
})
1269+
s.Require().NoError(err)
1270+
s.NotNil(r)
1271+
s.Len(r.GetPublicKeyMappings(), 1)
1272+
}
1273+
1274+
func (s *KasRegistrySuite) Test_List_Public_Key_Mappings_By_KAS_ID() {
1275+
kasID := s.f.GetKasRegistryKey("key_access_server_1").ID
1276+
r, err := s.db.PolicyClient.ListPublicKeyMappings(s.ctx, &kasregistry.ListPublicKeyMappingRequest{KasId: kasID})
1277+
s.Require().NoError(err)
1278+
s.NotNil(r)
1279+
s.Len(r.GetPublicKeyMappings(), 1)
1280+
1281+
for _, m := range r.GetPublicKeyMappings() {
1282+
s.Equal(kasID, m.GetKasId())
1283+
}
1284+
}
1285+
1286+
func (s *KasRegistrySuite) Test_List_Public_Key_Mapping_By_PublicKey_ID() {
1287+
id := s.f.GetPublicKey("key_1").ID
1288+
r, err := s.db.PolicyClient.ListPublicKeyMappings(s.ctx, &kasregistry.ListPublicKeyMappingRequest{PublicKeyId: id})
1289+
s.Require().NoError(err)
1290+
s.NotNil(r)
1291+
1292+
for _, m := range r.GetPublicKeyMappings() {
1293+
s.True(slices.ContainsFunc(m.GetPublicKeys(), func(key *kasregistry.ListPublicKeyMappingResponse_PublicKey) bool {
1294+
return key.GetKey().GetId() == id
1295+
}))
1296+
}
1297+
}
1298+
12521299
func (s *KasRegistrySuite) Test_Deactivate_Public_Key() {
12531300
id := s.f.GetPublicKey("key_4").ID
12541301
r, err := s.db.PolicyClient.DeactivatePublicKey(s.ctx, &kasregistry.DeactivatePublicKeyRequest{Id: id})

service/policy/db/key_access_server_registry.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,9 @@ func (c PolicyDBClient) ListPublicKeyMappings(ctx context.Context, r *kasregistr
414414
KasID: r.GetKasId(),
415415
// KasUri: r.GetKasUri(),
416416
// KasName: r.GetKasName(),
417-
Offset: offset,
418-
Limit: limit,
417+
PublicKeyID: r.GetPublicKeyId(),
418+
Offset: offset,
419+
Limit: limit,
419420
}
420421

421422
listRows, err := c.Queries.listPublicKeyMappings(ctx, params)
@@ -426,15 +427,27 @@ func (c PolicyDBClient) ListPublicKeyMappings(ctx context.Context, r *kasregistr
426427
mappings := make([]*kasregistry.ListPublicKeyMappingResponse_PublicKeyMapping, len(listRows))
427428
for i, mapping := range listRows {
428429
pkm := new(kasregistry.ListPublicKeyMappingResponse_PublicKeyMapping)
429-
err := protojson.Unmarshal(mapping, pkm)
430+
err := protojson.Unmarshal(mapping.KasInfo, pkm)
430431
if err != nil {
431432
return nil, db.WrapIfKnownInvalidQueryErr(err)
432433
}
433434
mappings[i] = pkm
434435
}
435436

437+
var total int32
438+
var nextOffset int32
439+
if len(listRows) > 0 {
440+
total = int32(listRows[0].Total)
441+
nextOffset = getNextOffset(offset, limit, total)
442+
}
443+
436444
return &kasregistry.ListPublicKeyMappingResponse{
437445
PublicKeyMappings: mappings,
446+
Pagination: &policy.PageResponse{
447+
CurrentOffset: params.Offset,
448+
Total: total,
449+
NextOffset: nextOffset,
450+
},
438451
}, nil
439452
}
440453

service/policy/db/query.sql

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,12 @@ LIMIT @limit_
984984
OFFSET @offset_;
985985

986986
-- name: listPublicKeyMappings :many
987-
WITH base_keys AS (
987+
WITH counted AS (
988+
SELECT COUNT(pk.id) AS total FROM public_keys AS pk
989+
WHERE (NULLIF(@kas_id, '') IS NULL OR pk.key_access_server_id = @kas_id::uuid)
990+
AND ( NULLIF(@public_key_id, '') IS NULL OR pk.id = @public_key_id::uuid )
991+
),
992+
base_keys AS (
988993
SELECT
989994
pk.id,
990995
pk.is_active,
@@ -1076,9 +1081,11 @@ SELECT jsonb_build_object(
10761081
)
10771082
)
10781083
)
1079-
) as kas_info
1084+
) as kas_info,
1085+
counted.total
10801086
FROM base_keys bk
1081-
GROUP BY bk.kas_id, bk.kas_name, bk.kas_uri
1087+
CROSS JOIN counted
1088+
GROUP BY bk.kas_id, bk.kas_name, bk.kas_uri, counted.total
10821089
LIMIT @limit_
10831090
OFFSET @offset_;
10841091

service/policy/db/query.sql.go

Lines changed: 30 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

service/policy/kasregistry/key_access_server_registry.proto

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ message ListPublicKeysRequest {
146146
// string kas_name = 2;
147147
// // Optional
148148
// string kas_uri = 3;
149+
149150
// Optional
150151
policy.PageRequest pagination = 10;
151152
}
@@ -164,6 +165,10 @@ message ListPublicKeyMappingRequest {
164165
// string kas_name = 2;
165166
// // Optional
166167
// string kas_uri = 3;
168+
169+
// Optional Public Key ID
170+
string public_key_id = 4;
171+
167172
// Optional
168173
policy.PageRequest pagination = 10;
169174
}
@@ -188,6 +193,8 @@ message ListPublicKeyMappingResponse {
188193
}
189194

190195
repeated PublicKeyMapping public_key_mappings = 1;
196+
197+
policy.PageResponse pagination = 10;
191198
}
192199

193200
message UpdatePublicKeyRequest {

0 commit comments

Comments
 (0)