Skip to content

Commit

Permalink
Logic corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
Artemkaaas committed Nov 18, 2024
1 parent 7268e3c commit 98379d5
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 17 deletions.
1 change: 0 additions & 1 deletion x/pki/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions x/pki/keeper/all_certificates_by_subject_key_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions x/pki/keeper/grpc_query_all_certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions x/pki/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion x/pki/keeper/msg_server_add_noc_x_509_ica_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/pki/keeper/msg_server_add_noc_x_509_root_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/pki/keeper/msg_server_add_x_509_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/pki/keeper/msg_server_approve_add_x_509_root_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions x/pki/keeper/msg_server_assign_vid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion x/pki/keeper/msg_server_remove_x_509_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion x/pki/keeper/msg_server_revoke_noc_x_509_ica_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion x/pki/keeper/msg_server_revoke_noc_x_509_root_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion x/pki/keeper/msg_server_revoke_x_509_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 98379d5

Please sign in to comment.