Skip to content
This repository was archived by the owner on Mar 30, 2018. It is now read-only.

Commit a511daa

Browse files
jeroirazsrderson
authored andcommitted
Fix issue 2120 (#2213)
* Changed mutex to RWMutex, use mutex to solve sync to db * Changed to write lock in populateAttribute * Changed to reader lock in getCertificateSets * Removed mutex from populateAttribute
1 parent 89d5704 commit a511daa

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

membersrvc/ca/aca.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ func (aca *ACA) fetchAndPopulateAttributes(id, affiliation string) error {
340340
func (aca *ACA) findAttribute(owner *AttributeOwner, attributeName string) (*AttributePair, error) {
341341
var count int
342342

343+
mutex.RLock()
344+
defer mutex.RUnlock()
345+
343346
err := aca.db.QueryRow("SELECT count(row) AS cant FROM Attributes WHERE id=? AND affiliation =? AND attributeName =?",
344347
owner.GetID(), owner.GetAffiliation(), attributeName).Scan(&count)
345348
if err != nil {

membersrvc/ca/ca.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type AffiliationGroup struct {
7474
}
7575

7676
var (
77-
mutex = &sync.Mutex{}
77+
mutex = &sync.RWMutex{}
7878
caOrganization string
7979
caCountry string
8080
rootPath string
@@ -367,9 +367,6 @@ func (ca *CA) createCertificate(id string, pub interface{}, usage x509.KeyUsage,
367367
}
368368

369369
func (ca *CA) createCertificateFromSpec(spec *CertificateSpec, timestamp int64, kdfKey []byte, persist bool) ([]byte, error) {
370-
mutex.Lock()
371-
defer mutex.Unlock()
372-
373370
Trace.Println("Creating certificate for " + spec.GetID() + ".")
374371

375372
raw, err := ca.newCertificateFromSpec(spec)
@@ -386,6 +383,9 @@ func (ca *CA) createCertificateFromSpec(spec *CertificateSpec, timestamp int64,
386383
}
387384

388385
func (ca *CA) persistCertificate(id string, timestamp int64, usage x509.KeyUsage, certRaw []byte, kdfKey []byte) error {
386+
mutex.Lock()
387+
defer mutex.Unlock()
388+
389389
hash := primitives.NewHash()
390390
hash.Write(certRaw)
391391
var err error
@@ -451,6 +451,9 @@ func (ca *CA) newCertificateFromSpec(spec *CertificateSpec) ([]byte, error) {
451451
func (ca *CA) readCertificateByKeyUsage(id string, usage x509.KeyUsage) ([]byte, error) {
452452
Trace.Printf("Reading certificate for %s and usage %v", id, usage)
453453

454+
mutex.RLock()
455+
defer mutex.RUnlock()
456+
454457
var raw []byte
455458
err := ca.db.QueryRow("SELECT cert FROM Certificates WHERE id=? AND usage=?", id, usage).Scan(&raw)
456459

@@ -464,6 +467,9 @@ func (ca *CA) readCertificateByKeyUsage(id string, usage x509.KeyUsage) ([]byte,
464467
func (ca *CA) readCertificateByTimestamp(id string, ts int64) ([]byte, error) {
465468
Trace.Println("Reading certificate for " + id + ".")
466469

470+
mutex.RLock()
471+
defer mutex.RUnlock()
472+
467473
var raw []byte
468474
err := ca.db.QueryRow("SELECT cert FROM Certificates WHERE id=? AND timestamp=?", id, ts).Scan(&raw)
469475

@@ -473,6 +479,9 @@ func (ca *CA) readCertificateByTimestamp(id string, ts int64) ([]byte, error) {
473479
func (ca *CA) readCertificates(id string, opt ...int64) (*sql.Rows, error) {
474480
Trace.Println("Reading certificatess for " + id + ".")
475481

482+
mutex.RLock()
483+
defer mutex.RUnlock()
484+
476485
if len(opt) > 0 && opt[0] != 0 {
477486
return ca.db.Query("SELECT cert, kdfkey FROM Certificates WHERE id=? AND timestamp=? ORDER BY usage", id, opt[0])
478487
}
@@ -483,12 +492,18 @@ func (ca *CA) readCertificates(id string, opt ...int64) (*sql.Rows, error) {
483492
func (ca *CA) readCertificateSets(id string, start, end int64) (*sql.Rows, error) {
484493
Trace.Println("Reading certificate sets for " + id + ".")
485494

495+
mutex.RLock()
496+
defer mutex.RUnlock()
497+
486498
return ca.db.Query("SELECT cert, kdfKey, timestamp FROM Certificates WHERE id=? AND timestamp BETWEEN ? AND ? ORDER BY timestamp", id, start, end)
487499
}
488500

489501
func (ca *CA) readCertificateByHash(hash []byte) ([]byte, error) {
490502
Trace.Println("Reading certificate for hash " + string(hash) + ".")
491503

504+
mutex.RLock()
505+
defer mutex.RUnlock()
506+
492507
var raw []byte
493508
row := ca.db.QueryRow("SELECT cert FROM Certificates WHERE hash=?", hash)
494509
err := row.Scan(&raw)
@@ -499,6 +514,9 @@ func (ca *CA) readCertificateByHash(hash []byte) ([]byte, error) {
499514
func (ca *CA) isValidAffiliation(affiliation string) (bool, error) {
500515
Trace.Println("Validating affiliation: " + affiliation)
501516

517+
mutex.RLock()
518+
defer mutex.RUnlock()
519+
502520
var count int
503521
var err error
504522
err = ca.db.QueryRow("SELECT count(row) FROM AffiliationGroups WHERE name=?", affiliation).Scan(&count)
@@ -662,6 +680,9 @@ func (ca *CA) registerAffiliationGroup(name string, parentName string) error {
662680
func (ca *CA) deleteUser(id string) error {
663681
Trace.Println("Deleting user " + id + ".")
664682

683+
mutex.Lock()
684+
defer mutex.Unlock()
685+
665686
var row int
666687
err := ca.db.QueryRow("SELECT row FROM Users WHERE id=?", id).Scan(&row)
667688
if err == nil {
@@ -684,6 +705,9 @@ func (ca *CA) deleteUser(id string) error {
684705
func (ca *CA) readUser(id string) *sql.Row {
685706
Trace.Println("Reading token for " + id + ".")
686707

708+
mutex.RLock()
709+
defer mutex.RUnlock()
710+
687711
return ca.db.QueryRow("SELECT role, token, state, key, enrollmentId FROM Users WHERE id=?", id)
688712
}
689713

@@ -700,6 +724,9 @@ func (ca *CA) readUsers(role int) (*sql.Rows, error) {
700724
func (ca *CA) readRole(id string) int {
701725
Trace.Println("Reading role for " + id + ".")
702726

727+
mutex.RLock()
728+
defer mutex.RUnlock()
729+
703730
var role int
704731
ca.db.QueryRow("SELECT role FROM Users WHERE id=?", id).Scan(&role)
705732

@@ -771,6 +798,9 @@ func (ca *CA) parseEnrollID(enrollID string) (id string, role string, affiliatio
771798
// and with metadata associated with 'newMemberMetadataStr'
772799
// Return nil if allowed, or an error if not allowed
773800
func (ca *CA) canRegister(registrar string, newMemberRole string, newMemberMetadataStr string) error {
801+
mutex.RLock()
802+
defer mutex.RUnlock()
803+
774804
// Read the user metadata associated with 'registrar'
775805
var registrarMetadataStr string
776806
err := ca.db.QueryRow("SELECT metadata FROM Users WHERE id=?", registrar).Scan(&registrarMetadataStr)

membersrvc/ca/ecap.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func (ecap *ECAP) CreateCertificatePair(ctx context.Context, in *pb.ECertCreateR
106106

107107
id := in.Id.Id
108108
err := ecap.eca.readUser(id).Scan(&role, &tok, &state, &prev, &enrollID)
109+
109110
if err != nil {
110111
errMsg := "Identity lookup error: " + err.Error()
111112
Trace.Println(errMsg)
@@ -127,7 +128,10 @@ func (ecap *ECAP) CreateCertificatePair(ctx context.Context, in *pb.ECertCreateR
127128
// initial request, create encryption challenge
128129
tok = []byte(randomString(12))
129130

131+
mutex.Lock()
130132
_, err = ecap.eca.db.Exec("UPDATE Users SET token=?, state=?, key=? WHERE id=?", tok, 1, in.Enc.Key, id)
133+
mutex.Unlock()
134+
131135
if err != nil {
132136
Error.Println(err)
133137
return nil, err
@@ -190,14 +194,20 @@ func (ecap *ECAP) CreateCertificatePair(ctx context.Context, in *pb.ECertCreateR
190194
spec = NewDefaultCertificateSpecWithCommonName(id, enrollID, ekey.(*ecdsa.PublicKey), x509.KeyUsageDataEncipherment, pkix.Extension{Id: ECertSubjectRole, Critical: true, Value: []byte(strconv.Itoa(ecap.eca.readRole(id)))})
191195
eraw, err := ecap.eca.createCertificateFromSpec(spec, ts, nil, true)
192196
if err != nil {
197+
mutex.Lock()
193198
ecap.eca.db.Exec("DELETE FROM Certificates Where id=?", id)
199+
mutex.Unlock()
194200
Error.Println(err)
195201
return nil, err
196202
}
197203

204+
mutex.Lock()
198205
_, err = ecap.eca.db.Exec("UPDATE Users SET state=? WHERE id=?", 2, id)
206+
mutex.Unlock()
199207
if err != nil {
208+
mutex.Lock()
200209
ecap.eca.db.Exec("DELETE FROM Certificates Where id=?", id)
210+
mutex.Unlock()
201211
Error.Println(err)
202212
return nil, err
203213
}

membersrvc/ca/tca.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ func (tca *TCA) startTCAA(srv *grpc.Server) {
240240
}
241241

242242
func (tca *TCA) getCertificateSets(enrollmentID string) ([]*TCertSet, error) {
243+
mutex.RLock()
244+
defer mutex.RUnlock()
245+
243246
var sets = []*TCertSet{}
244247
var err error
245248

@@ -269,6 +272,9 @@ func (tca *TCA) getCertificateSets(enrollmentID string) ([]*TCertSet, error) {
269272
}
270273

271274
func (tca *TCA) persistCertificateSet(enrollmentID string, timestamp int64, nonce []byte, kdfKey []byte) error {
275+
mutex.Lock()
276+
defer mutex.Unlock()
277+
272278
var err error
273279

274280
if _, err = tca.db.Exec("INSERT INTO TCertificateSets (enrollmentID, timestamp, nonce, kdfkey) VALUES (?, ?, ?, ?)", enrollmentID, timestamp, nonce, kdfKey); err != nil {

0 commit comments

Comments
 (0)