@@ -74,7 +74,7 @@ type Client struct {
74
74
isMainNode bool
75
75
76
76
publicConnection * grpchelpers.GRPCConn
77
- protectedConnection * grpc. ClientConn
77
+ protectedConnection * grpchelpers. GRPCConn
78
78
79
79
publicService pb.IAMPublicServiceClient
80
80
identService pb.IAMPublicIdentityServiceClient
@@ -133,13 +133,14 @@ func New(
133
133
cryptocontext * cryptutils.CryptoContext , insecure bool ,
134
134
) (client * Client , err error ) {
135
135
localClient := & Client {
136
- publicURL : publicURL ,
137
- protectedURL : protectedURL ,
138
- certStorage : certStorage ,
139
- sender : sender ,
140
- cryptocontext : cryptocontext ,
141
- insecure : insecure ,
142
- publicConnection : grpchelpers .NewGRPCConn (),
136
+ publicURL : publicURL ,
137
+ protectedURL : protectedURL ,
138
+ certStorage : certStorage ,
139
+ sender : sender ,
140
+ cryptocontext : cryptocontext ,
141
+ insecure : insecure ,
142
+ publicConnection : grpchelpers .NewGRPCConn (),
143
+ protectedConnection : grpchelpers .NewGRPCConn (),
143
144
nodeInfoSubs : & nodeInfoChangeSub {
144
145
listeners : make ([]chan cloudprotocol.NodeInfo , 0 ),
145
146
},
@@ -613,7 +614,6 @@ func (client *Client) Close() error {
613
614
client .isReconnecting .Store (true )
614
615
615
616
client .closeGRPCConnection ()
616
- client .publicConnection .Close ()
617
617
618
618
log .Debug ("Disconnected from IAM" )
619
619
@@ -657,7 +657,8 @@ func (client *Client) SubscribeCertChanged(certType string) (<-chan *pb.CertInfo
657
657
ch := make (chan * pb.CertInfo , 1 )
658
658
659
659
if _ , ok := client .certChangeSub [certType ]; ! ok {
660
- grpcStream , err := client .subscribeCertChange (certType )
660
+ grpcStream , err := client .publicService .SubscribeCertChanged (
661
+ context .Background (), & pb.SubscribeCertChangedRequest {Type : certType })
661
662
if err != nil {
662
663
return nil , aoserrors .Wrap (err )
663
664
}
@@ -839,38 +840,55 @@ func (client *Client) openGRPCConnection() (err error) {
839
840
log .Debug ("Connecting to IAM..." )
840
841
841
842
var publicConn * grpc.ClientConn
843
+ var protectedConn * grpc.ClientConn
842
844
843
845
publicConn , err = grpchelpers .CreatePublicConnection (
844
846
client .publicURL , client .cryptocontext , client .insecure )
845
847
if err != nil {
846
848
return aoserrors .Wrap (err )
847
849
}
848
850
849
- if err := client .publicConnection .Start (publicConn ); err != nil {
850
- return aoserrors .Wrap (err )
851
- }
851
+ client .publicConnection .Set (publicConn )
852
+
853
+ defer func () {
854
+ if err == nil {
855
+ client .publicConnection .Start ()
856
+ } else {
857
+ publicConn .Close ()
858
+ }
859
+ }()
852
860
853
861
client .publicService = pb .NewIAMPublicServiceClient (client .publicConnection )
854
862
client .identService = pb .NewIAMPublicIdentityServiceClient (client .publicConnection )
855
863
client .publicNodesService = pb .NewIAMPublicNodesServiceClient (client .publicConnection )
856
864
client .publicPermissionsService = pb .NewIAMPublicPermissionsServiceClient (client .publicConnection )
857
865
858
- if err = client .restoreCertInfoSubs (); err != nil {
859
- log .Error ("Failed subscribe on CertInfo change" )
860
-
861
- return aoserrors .Wrap (err )
866
+ if err = client .restorePublicSubs (); err != nil {
867
+ return err
862
868
}
863
869
864
870
if ! client .isProtectedConnEnabled () {
865
871
return nil
866
872
}
867
873
868
- client .protectedConnection , err = grpchelpers .CreateProtectedConnection (client .certStorage ,
869
- client .protectedURL , client .cryptocontext , client , client .insecure )
874
+ certProvider := NewCertProvider (publicConn )
875
+
876
+ protectedConn , err = grpchelpers .CreateProtectedConnection (client .certStorage ,
877
+ client .protectedURL , client .cryptocontext , certProvider , client .insecure )
870
878
if err != nil {
871
879
return aoserrors .Wrap (err )
872
880
}
873
881
882
+ client .protectedConnection .Set (protectedConn )
883
+
884
+ defer func () {
885
+ if err == nil {
886
+ client .protectedConnection .Start ()
887
+ } else {
888
+ protectedConn .Close ()
889
+ }
890
+ }()
891
+
874
892
client .certificateService = pb .NewIAMCertificateServiceClient (client .protectedConnection )
875
893
client .provisioningService = pb .NewIAMProvisioningServiceClient (client .protectedConnection )
876
894
client .nodesService = pb .NewIAMNodesServiceClient (client .protectedConnection )
@@ -889,7 +907,7 @@ func (client *Client) closeGRPCConnection() {
889
907
}
890
908
891
909
if client .protectedConnection != nil {
892
- client .protectedConnection .Close ()
910
+ client .protectedConnection .Stop ()
893
911
}
894
912
895
913
for _ , sub := range client .certChangeSub {
@@ -944,20 +962,6 @@ func (client *Client) subscribeUnitSubjectsChange() error {
944
962
return nil
945
963
}
946
964
947
- func (client * Client ) subscribeCertChange (certType string ) (
948
- listener pb.IAMPublicService_SubscribeCertChangedClient , err error ,
949
- ) {
950
- listener , err = client .publicService .SubscribeCertChanged (context .Background (),
951
- & pb.SubscribeCertChangedRequest {Type : certType })
952
- if err != nil {
953
- log .WithField ("error" , err ).Error ("Can't subscribe on CertChange event" )
954
-
955
- return nil , aoserrors .Wrap (err )
956
- }
957
-
958
- return listener , aoserrors .Wrap (err )
959
- }
960
-
961
965
func (client * Client ) processNodeInfoChange (sub * nodeInfoChangeSub ) {
962
966
defer sub .stopWG .Done ()
963
967
@@ -1182,9 +1186,10 @@ func (client *Client) finishProvisioning(nodeID, password string) (errorInfo *cl
1182
1186
1183
1187
func (client * Client ) restoreCertInfoSubs () error {
1184
1188
for certType , sub := range client .certChangeSub {
1185
- grpcStream , err := client .subscribeCertChange (certType )
1189
+ grpcStream , err := client .publicService .SubscribeCertChanged (
1190
+ context .Background (), & pb.SubscribeCertChangedRequest {Type : certType })
1186
1191
if err != nil {
1187
- return err
1192
+ return aoserrors . Wrap ( err )
1188
1193
}
1189
1194
1190
1195
sub .grpcStream = & grpcStream
@@ -1197,6 +1202,32 @@ func (client *Client) restoreCertInfoSubs() error {
1197
1202
return nil
1198
1203
}
1199
1204
1205
+ func (client * Client ) restorePublicSubs () error {
1206
+ var err error
1207
+
1208
+ if err = client .restoreCertInfoSubs (); err != nil {
1209
+ log .Error ("Failed subscribe on CertInfo change" )
1210
+
1211
+ return aoserrors .Wrap (err )
1212
+ }
1213
+
1214
+ if client .isMainNode {
1215
+ if err = client .subscribeNodeInfoChange (); err != nil {
1216
+ log .Error ("Failed subscribe on NodeInfo change" )
1217
+
1218
+ return aoserrors .Wrap (err )
1219
+ }
1220
+
1221
+ if err = client .subscribeUnitSubjectsChange (); err != nil {
1222
+ log .Error ("Failed subscribe on UnitSubject change" )
1223
+
1224
+ return aoserrors .Wrap (err )
1225
+ }
1226
+ }
1227
+
1228
+ return nil
1229
+ }
1230
+
1200
1231
func (client * Client ) onConnectionLost () {
1201
1232
select {
1202
1233
case <- client .closeChannel :
0 commit comments