@@ -35,6 +35,8 @@ import (
35
35
"sync"
36
36
"time"
37
37
38
+ gp "google/protobuf"
39
+
38
40
"github.com/hyperledger/fabric/core/crypto/primitives"
39
41
"github.com/hyperledger/fabric/flogging"
40
42
pb "github.com/hyperledger/fabric/membersrvc/protos"
@@ -578,11 +580,11 @@ func (ca *CA) validateAndGenerateEnrollID(id, affiliation string, role pb.Role)
578
580
579
581
// registerUser registers a new member with the CA
580
582
//
581
- func (ca * CA ) registerUser (id , affiliation string , role pb.Role , registrar , memberMetadata string , opt ... string ) (string , error ) {
583
+ func (ca * CA ) registerUser (id , affiliation string , role pb.Role , attrs [] * pb. Attribute , aca * ACA , registrar , memberMetadata string , opt ... string ) (string , error ) {
582
584
memberMetadata = removeQuotes (memberMetadata )
583
585
roleStr , _ := MemberRoleToString (role )
584
- caLogger .Debugf ("Received request to register user with id: %s, affiliation: %s, role: %s, registrar: %s, memberMetadata: %s\n " ,
585
- id , affiliation , roleStr , registrar , memberMetadata )
586
+ caLogger .Debugf ("Received request to register user with id: %s, affiliation: %s, role: %s, attrs: %+v, registrar: %s, memberMetadata: %s\n " ,
587
+ id , affiliation , roleStr , attrs , registrar , memberMetadata )
586
588
587
589
var enrollID , tok string
588
590
var err error
@@ -606,11 +608,21 @@ func (ca *CA) registerUser(id, affiliation string, role pb.Role, registrar, memb
606
608
if err != nil {
607
609
return "" , err
608
610
}
611
+
609
612
tok , err = ca .registerUserWithEnrollID (id , enrollID , role , memberMetadata , opt ... )
610
613
if err != nil {
611
614
return "" , err
612
615
}
613
- return tok , nil
616
+
617
+ if attrs != nil && aca != nil {
618
+ var pairs []* AttributePair
619
+ pairs , err = toAttributePairs (id , affiliation , attrs )
620
+ if err == nil {
621
+ err = aca .PopulateAttributes (pairs )
622
+ }
623
+ }
624
+
625
+ return tok , err
614
626
}
615
627
616
628
// registerUserWithEnrollID registers a new user and its enrollmentID, role and state
@@ -870,25 +882,36 @@ func (mm *MemberMetadata) canRegister(registrar string, newRole string, newMembe
870
882
caLogger .Debugf ("MM.canRegister: role %s can't be registered by %s\n " , newRole , registrar )
871
883
return errors .New ("member " + registrar + " may not register member of type " + newRole )
872
884
}
885
+
873
886
// The registrar privileges that are being registered must not be larger than the registrar's
874
887
if newMemberMetadata == nil {
875
888
// Not requesting registrar privileges for this member, so we are OK
876
889
caLogger .Debug ("MM.canRegister: not requesting registrar privileges" )
877
890
return nil
878
891
}
879
- return strsContained (newMemberMetadata .Registrar .Roles , mm .Registrar .DelegateRoles , registrar , "delegateRoles" )
892
+
893
+ // Make sure this registrar is not delegating an invalid role
894
+ err := checkDelegateRoles (newMemberMetadata .Registrar .Roles , mm .Registrar .DelegateRoles , registrar )
895
+ if err != nil {
896
+ caLogger .Debug ("MM.canRegister: checkDelegateRoles failure" )
897
+ return err
898
+ }
899
+
900
+ // Can register OK
901
+ caLogger .Debug ("MM.canRegister: OK" )
902
+ return nil
880
903
}
881
904
882
905
// Return an error if all strings in 'strs1' are not contained in 'strs2'
883
- func strsContained (strs1 []string , strs2 []string , registrar string , field string ) error {
884
- caLogger .Debugf ("CA.strsContained : registrar=%s, field=%s, strs1=%+v, strs2=%+v\n " , registrar , field , strs1 , strs2 )
906
+ func checkDelegateRoles (strs1 []string , strs2 []string , registrar string ) error {
907
+ caLogger .Debugf ("CA.checkDelegateRoles : registrar=%s, strs1=%+v, strs2=%+v\n " , registrar , strs1 , strs2 )
885
908
for _ , s := range strs1 {
886
909
if ! strContained (s , strs2 ) {
887
- caLogger .Debugf ("CA.strsContained : no: %s not in %+v\n " , s , strs2 )
888
- return errors .New ("user " + registrar + " may not register " + field + " " + s )
910
+ caLogger .Debugf ("CA.checkDelegateRoles : no: %s not in %+v\n " , s , strs2 )
911
+ return errors .New ("user " + registrar + " may not register delegateRoles " + s )
889
912
}
890
913
}
891
- caLogger .Debug ("CA.strsContained : ok" )
914
+ caLogger .Debug ("CA.checkDelegateRoles : ok" )
892
915
return nil
893
916
}
894
917
@@ -902,6 +925,16 @@ func strContained(str string, strs []string) bool {
902
925
return false
903
926
}
904
927
928
+ // Return true if 'str' is prefixed by any string in 'strs'; otherwise return false
929
+ func isPrefixed (str string , strs []string ) bool {
930
+ for _ , s := range strs {
931
+ if strings .HasPrefix (str , s ) {
932
+ return true
933
+ }
934
+ }
935
+ return false
936
+ }
937
+
905
938
// convert a role to a string
906
939
func role2String (role int ) string {
907
940
if role == int (pb .Role_CLIENT ) {
@@ -928,3 +961,30 @@ func removeQuotes(str string) string {
928
961
caLogger .Debugf ("removeQuotes: %s\n " , str )
929
962
return str
930
963
}
964
+
965
+ // Convert the protobuf array of attributes to the AttributePair array format
966
+ // as required by the ACA code to populate the table
967
+ func toAttributePairs (id , affiliation string , attrs []* pb.Attribute ) ([]* AttributePair , error ) {
968
+ var pairs = make ([]* AttributePair , 0 )
969
+ for _ , attr := range attrs {
970
+ vals := []string {id , affiliation , attr .Name , attr .Value , attr .NotBefore , attr .NotAfter }
971
+ pair , err := NewAttributePair (vals , nil )
972
+ if err != nil {
973
+ return nil , err
974
+ }
975
+ pairs = append (pairs , pair )
976
+ }
977
+ caLogger .Debugf ("toAttributePairs: id=%s, affiliation=%s, attrs=%v, pairs=%v\n " ,
978
+ id , affiliation , attrs , pairs )
979
+ return pairs , nil
980
+ }
981
+
982
+ func convertTime (ts * gp.Timestamp ) time.Time {
983
+ var t time.Time
984
+ if ts == nil {
985
+ t = time .Unix (0 , 0 ).UTC ()
986
+ } else {
987
+ t = time .Unix (ts .Seconds , int64 (ts .Nanos )).UTC ()
988
+ }
989
+ return t
990
+ }
0 commit comments