From 98379d5e2d37c842a26537259de93d2dc06cb3db Mon Sep 17 00:00:00 2001 From: "artem.ivanov" Date: Mon, 18 Nov 2024 10:33:33 +0300 Subject: [PATCH] Logic corrections --- x/pki/genesis_test.go | 1 - x/pki/keeper/all_certificates_by_subject_key_id.go | 8 ++++---- x/pki/keeper/grpc_query_all_certificates.go | 4 ++-- x/pki/keeper/migrations.go | 2 -- x/pki/keeper/msg_server_add_noc_x_509_ica_cert.go | 2 +- x/pki/keeper/msg_server_add_noc_x_509_root_cert.go | 2 +- x/pki/keeper/msg_server_add_x_509_cert.go | 2 +- x/pki/keeper/msg_server_approve_add_x_509_root_cert.go | 2 +- x/pki/keeper/msg_server_assign_vid.go | 9 +++++++++ x/pki/keeper/msg_server_remove_x_509_cert.go | 3 ++- x/pki/keeper/msg_server_revoke_noc_x_509_ica_cert.go | 3 ++- x/pki/keeper/msg_server_revoke_noc_x_509_root_cert.go | 3 ++- x/pki/keeper/msg_server_revoke_x_509_cert.go | 3 ++- 13 files changed, 27 insertions(+), 17 deletions(-) diff --git a/x/pki/genesis_test.go b/x/pki/genesis_test.go index 1695215ca..ca297e9d9 100644 --- a/x/pki/genesis_test.go +++ b/x/pki/genesis_test.go @@ -264,6 +264,5 @@ func TestGenesis(t *testing.T) { require.ElementsMatch(t, genesisState.RevokedNocIcaCertificatesList, got.RevokedNocIcaCertificatesList) require.ElementsMatch(t, genesisState.AllCertificatesBySubjectList, got.AllCertificatesBySubjectList) require.ElementsMatch(t, genesisState.AllCertificatesBySubjectKeyIdList, got.AllCertificatesBySubjectKeyIdList) - require.ElementsMatch(t, genesisState.AllCertificatesBySubjectKeyIdList, got.AllCertificatesBySubjectKeyIdList) // this line is used by starport scaffolding # genesis/test/assert } diff --git a/x/pki/keeper/all_certificates_by_subject_key_id.go b/x/pki/keeper/all_certificates_by_subject_key_id.go index 757aa688a..84c19a2fe 100644 --- a/x/pki/keeper/all_certificates_by_subject_key_id.go +++ b/x/pki/keeper/all_certificates_by_subject_key_id.go @@ -19,18 +19,18 @@ func (k Keeper) SetAllCertificatesBySubjectKeyID(ctx sdk.Context, allCertificate // Add an All certificate to the list of All certificates with the subjectKeyId map. func (k Keeper) AddAllCertificateBySubjectKeyID(ctx sdk.Context, certificate types.Certificate) { - k.addAllCertificates(ctx, certificate.SubjectKeyId, []*types.Certificate{&certificate}) + k.addAllCertificatesBySubjectKeyID(ctx, certificate.SubjectKeyId, []*types.Certificate{&certificate}) } // Add an All certificates list to All certificates with the subjectKeyId map. func (k Keeper) AddAllCertificatesBySubjectKeyID(ctx sdk.Context, allCertificate types.AllCertificates) { - k.addAllCertificates(ctx, allCertificate.SubjectKeyId, allCertificate.Certs) + k.addAllCertificatesBySubjectKeyID(ctx, allCertificate.SubjectKeyId, allCertificate.Certs) } -func (k Keeper) addAllCertificates(ctx sdk.Context, subjectKeyID string, certs []*types.Certificate) { +func (k Keeper) addAllCertificatesBySubjectKeyID(ctx sdk.Context, subjectKeyID string, certs []*types.Certificate) { store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.AllCertificatesBySubjectKeyIDKeyPrefix)) - AllCertificatesBytes := store.Get(types.AllCertificatesBySubjectKey( + AllCertificatesBytes := store.Get(types.AllCertificatesBySubjectKeyIDKey( subjectKeyID, )) var AllCertificates types.AllCertificatesBySubjectKeyId diff --git a/x/pki/keeper/grpc_query_all_certificates.go b/x/pki/keeper/grpc_query_all_certificates.go index f78bf4f49..f3625a42f 100644 --- a/x/pki/keeper/grpc_query_all_certificates.go +++ b/x/pki/keeper/grpc_query_all_certificates.go @@ -41,9 +41,9 @@ func (k Keeper) CertificatesAll(c context.Context, req *types.QueryAllCertificat pageRes = &query.PageResponse{Total: 1} } else { store := ctx.KVStore(k.storeKey) - approvedCertificatesStore := prefix.NewStore(store, pkitypes.KeyPrefix(types.ApprovedCertificatesKeyPrefix)) + allCertificatesStore := prefix.NewStore(store, pkitypes.KeyPrefix(types.AllCertificatesBySubjectKeyIDKeyPrefix)) - pageRes, err = query.Paginate(approvedCertificatesStore, req.Pagination, func(key []byte, value []byte) error { + pageRes, err = query.Paginate(allCertificatesStore, req.Pagination, func(key []byte, value []byte) error { var certificates types.AllCertificates if err := k.cdc.Unmarshal(value, &certificates); err != nil { return err diff --git a/x/pki/keeper/migrations.go b/x/pki/keeper/migrations.go index acf9f9c8d..46977bf77 100644 --- a/x/pki/keeper/migrations.go +++ b/x/pki/keeper/migrations.go @@ -33,8 +33,6 @@ func (m Migrator) Migrate3to4(ctx sdk.Context) error { approvedCertificates := m.keeper.GetAllApprovedCertificates(ctx) for _, cert := range approvedCertificates { m.keeper.AddAllCertificates(ctx, cert.Subject, cert.SubjectKeyId, cert.SchemaVersion, cert.Certs) - } - for _, cert := range approvedCertificates { m.keeper.AddAllCertificatesBySubjectKeyID(ctx, pkitypes.AllCertificates(cert)) } approvedCertificatesBySubject := m.keeper.GetAllApprovedCertificatesBySubject(ctx) diff --git a/x/pki/keeper/msg_server_add_noc_x_509_ica_cert.go b/x/pki/keeper/msg_server_add_noc_x_509_ica_cert.go index 6b2951fbe..c4fcf3dc7 100644 --- a/x/pki/keeper/msg_server_add_noc_x_509_ica_cert.go +++ b/x/pki/keeper/msg_server_add_noc_x_509_ica_cert.go @@ -109,7 +109,7 @@ func (k msgServer) AddNocX509IcaCert(goCtx context.Context, msg *types.MsgAddNoc // append to global list of certificates indexed by subject k.AddAllCertificateBySubject(ctx, certificate.Subject, certificate.SubjectKeyId) - // add to global list of certificates indexed by subject + // add to global list of certificates indexed by skid k.AddAllCertificateBySubjectKeyID(ctx, certificate) // Add to the list of all NOC certificates diff --git a/x/pki/keeper/msg_server_add_noc_x_509_root_cert.go b/x/pki/keeper/msg_server_add_noc_x_509_root_cert.go index 20f58ede7..0f22ab2f0 100644 --- a/x/pki/keeper/msg_server_add_noc_x_509_root_cert.go +++ b/x/pki/keeper/msg_server_add_noc_x_509_root_cert.go @@ -90,7 +90,7 @@ func (k msgServer) AddNocX509RootCert(goCtx context.Context, msg *types.MsgAddNo // append to global list of certificates indexed by subject k.AddAllCertificateBySubject(ctx, certificate.Subject, certificate.SubjectKeyId) - // add to global list of certificates indexed by subject + // add to global list of certificates indexed by skid k.AddAllCertificateBySubjectKeyID(ctx, certificate) // Add to the list of all NOC certificates diff --git a/x/pki/keeper/msg_server_add_x_509_cert.go b/x/pki/keeper/msg_server_add_x_509_cert.go index fc35f648e..816f446d1 100644 --- a/x/pki/keeper/msg_server_add_x_509_cert.go +++ b/x/pki/keeper/msg_server_add_x_509_cert.go @@ -113,7 +113,7 @@ func (k msgServer) AddX509Cert(goCtx context.Context, msg *types.MsgAddX509Cert) // append to global list of certificates indexed by subject k.AddAllCertificateBySubject(ctx, certificate.Subject, certificate.SubjectKeyId) - // add to global list of certificates indexed by subject + // add to global list of certificates indexed by skid k.AddAllCertificateBySubjectKeyID(ctx, certificate) // append new certificate to list of certificates with the same Subject/SubjectKeyID combination and store updated list diff --git a/x/pki/keeper/msg_server_approve_add_x_509_root_cert.go b/x/pki/keeper/msg_server_approve_add_x_509_root_cert.go index 189228b4f..757891504 100644 --- a/x/pki/keeper/msg_server_approve_add_x_509_root_cert.go +++ b/x/pki/keeper/msg_server_approve_add_x_509_root_cert.go @@ -85,7 +85,7 @@ func (k msgServer) ApproveAddX509RootCert(goCtx context.Context, msg *types.MsgA // append to global list of certificates indexed by subject k.AddAllCertificateBySubject(ctx, rootCertificate.Subject, rootCertificate.SubjectKeyId) - // add to global list of certificates indexed by subject + // add to global list of certificates indexed by skid k.AddAllCertificateBySubjectKeyID(ctx, rootCertificate) // add approved certificate to stored list of certificates with the same Subject/SubjectKeyID combination diff --git a/x/pki/keeper/msg_server_assign_vid.go b/x/pki/keeper/msg_server_assign_vid.go index 32e8ad2fa..e6dd607bf 100644 --- a/x/pki/keeper/msg_server_assign_vid.go +++ b/x/pki/keeper/msg_server_assign_vid.go @@ -68,6 +68,13 @@ func (k msgServer) AssignVid(goCtx context.Context, msg *types.MsgAssignVid) (*t return nil, pkitypes.NewErrNotEmptyVid("Vendor ID (VID) already present in certificates") } + // assign VID to certificates in global list indexed by subject key id + allCertificatesBySubjectKeyID, found := k.GetAllCertificatesBySubjectKeyID(ctx, msg.SubjectKeyId) + if !found { + return nil, pkitypes.NewErrCertificateDoesNotExist(msg.Subject, msg.SubjectKeyId) + } + k.assignVid(&allCertificatesBySubjectKeyID.Certs, msg.Vid) + // assign VID to certificates in approved list approvedCertificates, found := k.GetApprovedCertificates(ctx, msg.Subject, msg.SubjectKeyId) if !found { @@ -84,6 +91,8 @@ func (k msgServer) AssignVid(goCtx context.Context, msg *types.MsgAssignVid) (*t // update global certificates list k.SetAllCertificates(ctx, certificates) + // update global certificates list indexed by subject key id + k.SetAllCertificatesBySubjectKeyID(ctx, allCertificatesBySubjectKeyID) // update approved certificates list k.SetApprovedCertificates(ctx, approvedCertificates) // update certificates list indexed by subject key id diff --git a/x/pki/keeper/msg_server_remove_x_509_cert.go b/x/pki/keeper/msg_server_remove_x_509_cert.go index 1dd6c0c9f..7f8269477 100644 --- a/x/pki/keeper/msg_server_remove_x_509_cert.go +++ b/x/pki/keeper/msg_server_remove_x_509_cert.go @@ -69,8 +69,9 @@ func (k msgServer) RemoveX509Cert(goCtx context.Context, msg *types.MsgRemoveX50 } else { // remove from global certificates map k.RemoveAllCertificates(ctx, certID.Subject, certID.SubjectKeyId) - // remove from global subject -> subject key ID map + // remove from global subject -> subject map k.RemoveAllCertificateBySubject(ctx, certID.Subject, certID.SubjectKeyId) + // remove from global subject -> subject key ID map k.RemoveAllCertificatesBySubjectKeyID(ctx, certID.Subject, certID.SubjectKeyId) // remove from approved certificates map k.RemoveApprovedCertificates(ctx, certID.Subject, certID.SubjectKeyId) diff --git a/x/pki/keeper/msg_server_revoke_noc_x_509_ica_cert.go b/x/pki/keeper/msg_server_revoke_noc_x_509_ica_cert.go index a588f4fe9..add76ed31 100644 --- a/x/pki/keeper/msg_server_revoke_noc_x_509_ica_cert.go +++ b/x/pki/keeper/msg_server_revoke_noc_x_509_ica_cert.go @@ -112,8 +112,9 @@ func (k msgServer) _revokeNocIcaCertificates(ctx sdk.Context, certificates types }) // remove cert from global certs list k.RemoveAllCertificates(ctx, certificates.Subject, certificates.SubjectKeyId) - // remove cert from global certs list -> subject key ID map + // remove cert from global certs list -> subject map k.RemoveAllCertificateBySubject(ctx, certificates.Subject, certificates.SubjectKeyId) + // remove cert from global certs list -> subject key ID map k.RemoveAllCertificatesBySubjectKeyID(ctx, certificates.Subject, certificates.SubjectKeyId) // remove cert from NOC certs list k.RemoveNocCertificates(ctx, certificates.Subject, certificates.SubjectKeyId) diff --git a/x/pki/keeper/msg_server_revoke_noc_x_509_root_cert.go b/x/pki/keeper/msg_server_revoke_noc_x_509_root_cert.go index d40ef0620..043f4d65b 100644 --- a/x/pki/keeper/msg_server_revoke_noc_x_509_root_cert.go +++ b/x/pki/keeper/msg_server_revoke_noc_x_509_root_cert.go @@ -118,8 +118,9 @@ func (k msgServer) _revokeNocRootCertificates(ctx sdk.Context, certificates type // remove cert from global certs list k.RemoveAllCertificates(ctx, certificates.Subject, certificates.SubjectKeyId) - // remove cert from global certs list -> subject key ID map + // remove cert from global certs list -> subject map k.RemoveAllCertificateBySubject(ctx, certificates.Subject, certificates.SubjectKeyId) + // remove cert from global certs list -> subject key ID map k.RemoveAllCertificatesBySubjectKeyID(ctx, certificates.Subject, certificates.SubjectKeyId) // remove cert from NOC certs list k.RemoveNocCertificates(ctx, certificates.Subject, certificates.SubjectKeyId) diff --git a/x/pki/keeper/msg_server_revoke_x_509_cert.go b/x/pki/keeper/msg_server_revoke_x_509_cert.go index 29a7b53db..614b3f3ea 100644 --- a/x/pki/keeper/msg_server_revoke_x_509_cert.go +++ b/x/pki/keeper/msg_server_revoke_x_509_cert.go @@ -65,8 +65,9 @@ func (k msgServer) _revokeX509Certificates(ctx sdk.Context, certID types.Certifi // Remove certificate from global list k.RemoveAllCertificates(ctx, certID.Subject, certID.SubjectKeyId) - // Remove certificate from global list -> subject key ID map + // Remove certificate from global list -> subject map k.RemoveAllCertificateBySubject(ctx, certID.Subject, certID.SubjectKeyId) + // Remove certificate from global list -> subject key ID map k.RemoveAllCertificatesBySubjectKeyID(ctx, certID.Subject, certID.SubjectKeyId) // Remove certificate from approved list k.RemoveApprovedCertificates(ctx, certID.Subject, certID.SubjectKeyId)