@@ -3,7 +3,10 @@ package provision
3
3
import (
4
4
"context"
5
5
"encoding/hex"
6
+ "encoding/json"
6
7
"fmt"
8
+ "io"
9
+ "net/http"
7
10
"os"
8
11
"path/filepath"
9
12
"sort"
@@ -108,21 +111,23 @@ type NativeEngine struct {
108
111
109
112
queue * dque.DQue
110
113
111
- //options
114
+ // options
112
115
// janitor Janitor
113
116
twins Twins
114
117
admins Twins
115
118
order []gridtypes.WorkloadType
116
119
typeIndex map [gridtypes.WorkloadType ]int
117
120
rerunAll bool
118
- //substrate specific attributes
121
+ // substrate specific attributes
119
122
nodeID uint32
120
123
substrateGateway * stubs.SubstrateGatewayStub
121
124
callback Callback
122
125
}
123
126
124
- var _ Engine = (* NativeEngine )(nil )
125
- var _ pkg.Provision = (* NativeEngine )(nil )
127
+ var (
128
+ _ Engine = (* NativeEngine )(nil )
129
+ _ pkg.Provision = (* NativeEngine )(nil )
130
+ )
126
131
127
132
type withUserKeyGetter struct {
128
133
g Twins
@@ -199,14 +204,19 @@ func (n *nullKeyGetter) GetKey(id uint32) ([]byte, error) {
199
204
return nil , fmt .Errorf ("null user key getter" )
200
205
}
201
206
202
- type engineKey struct {}
203
- type deploymentKey struct {}
204
- type deploymentValue struct {
205
- twin uint32
206
- deployment uint64
207
- }
208
- type contractKey struct {}
209
- type rentKey struct {}
207
+ type (
208
+ engineKey struct {}
209
+ deploymentKey struct {}
210
+ deploymentValue struct {
211
+ twin uint32
212
+ deployment uint64
213
+ }
214
+ )
215
+
216
+ type (
217
+ contractKey struct {}
218
+ rentKey struct {}
219
+ )
210
220
211
221
// GetEngine gets engine from context
212
222
func GetEngine (ctx context.Context ) Engine {
@@ -498,7 +508,7 @@ func (e *NativeEngine) Run(root context.Context) error {
498
508
ctx , err = e .validate (ctx , & job .Target , job .Op == opProvisionNoValidation )
499
509
if err != nil {
500
510
l .Error ().Err (err ).Msg ("contact validation fails" )
501
- //job.Target.SetError(err)
511
+ // job.Target.SetError(err)
502
512
if err := e .storage .Error (job .Target .TwinID , job .Target .ContractID , err ); err != nil {
503
513
l .Error ().Err (err ).Msg ("failed to set deployment global error" )
504
514
}
@@ -718,7 +728,7 @@ func (e *NativeEngine) installWorkload(ctx context.Context, wl *gridtypes.Worklo
718
728
// if it has been deleted, error state, we do nothing.
719
729
// otherwise, we-reinstall it
720
730
if current .Result .State .IsAny (gridtypes .StateDeleted , gridtypes .StateError ) {
721
- //nothing to do!
731
+ // nothing to do!
722
732
return nil
723
733
}
724
734
}
@@ -797,7 +807,7 @@ func (e *NativeEngine) lockWorkload(ctx context.Context, wl *gridtypes.WorkloadW
797
807
return errors .Wrapf (err , "failed to get last transaction for '%s'" , wl .ID .String ())
798
808
} else {
799
809
if ! current .Result .State .IsOkay () {
800
- //nothing to do! it's either in error state or something else.
810
+ // nothing to do! it's either in error state or something else.
801
811
return nil
802
812
}
803
813
}
@@ -857,7 +867,6 @@ func (e *NativeEngine) uninstallDeployment(ctx context.Context, dl *gridtypes.De
857
867
Uint64 ("contract" , dl .ContractID ).
858
868
Msg ("failed to delete deployment" )
859
869
}
860
-
861
870
}
862
871
863
872
func getMountSize (wl * gridtypes.Workload ) (gridtypes.Unit , error ) {
@@ -985,11 +994,11 @@ func (e *NativeEngine) DecommissionCached(id string, reason string) error {
985
994
986
995
if wl .Result .State == gridtypes .StateDeleted ||
987
996
wl .Result .State == gridtypes .StateError {
988
- //nothing to do!
997
+ // nothing to do!
989
998
return nil
990
999
}
991
1000
992
- //to bad we have to repeat this here
1001
+ // to bad we have to repeat this here
993
1002
ctx := context .WithValue (context .Background (), engineKey {}, e )
994
1003
ctx = withDeployment (ctx , twin , dlID )
995
1004
@@ -1012,6 +1021,11 @@ func (n *NativeEngine) CreateOrUpdate(twin uint32, deployment gridtypes.Deployme
1012
1021
return fmt .Errorf ("twin id mismatch (deployment: %d, message: %d)" , deployment .TwinID , twin )
1013
1022
}
1014
1023
1024
+ // make sure the account used is verified
1025
+ if getTwinVerificationState (twin ) != "VERIFIED" {
1026
+ return fmt .Errorf ("user is not verified" )
1027
+ }
1028
+
1015
1029
if err := deployment .Verify (n .twins ); err != nil {
1016
1030
return err
1017
1031
}
@@ -1028,7 +1042,6 @@ func (n *NativeEngine) CreateOrUpdate(twin uint32, deployment gridtypes.Deployme
1028
1042
}
1029
1043
1030
1044
return action (ctx , deployment )
1031
-
1032
1045
}
1033
1046
1034
1047
func (n * NativeEngine ) Get (twin uint32 , contractID uint64 ) (gridtypes.Deployment , error ) {
@@ -1041,6 +1054,7 @@ func (n *NativeEngine) Get(twin uint32, contractID uint64) (gridtypes.Deployment
1041
1054
1042
1055
return deployment , nil
1043
1056
}
1057
+
1044
1058
func (n * NativeEngine ) List (twin uint32 ) ([]gridtypes.Deployment , error ) {
1045
1059
deploymentIDs , err := n .storage .ByTwin (twin )
1046
1060
if err != nil {
@@ -1059,6 +1073,7 @@ func (n *NativeEngine) List(twin uint32) ([]gridtypes.Deployment, error) {
1059
1073
}
1060
1074
return deployments , nil
1061
1075
}
1076
+
1062
1077
func (n * NativeEngine ) Changes (twin uint32 , contractID uint64 ) ([]gridtypes.Workload , error ) {
1063
1078
changes , err := n .storage .Changes (twin , contractID )
1064
1079
if errors .Is (err , ErrDeploymentNotExists ) {
@@ -1068,6 +1083,7 @@ func (n *NativeEngine) Changes(twin uint32, contractID uint64) ([]gridtypes.Work
1068
1083
}
1069
1084
return changes , nil
1070
1085
}
1086
+
1071
1087
func (n * NativeEngine ) ListPublicIPs () ([]string , error ) {
1072
1088
// for efficiency this method should just find out configured public Ips.
1073
1089
// but currently the only way to do this is by scanning the nft rules
@@ -1110,6 +1126,7 @@ func (n *NativeEngine) ListPublicIPs() ([]string, error) {
1110
1126
1111
1127
return ips , nil
1112
1128
}
1129
+
1113
1130
func (n * NativeEngine ) ListPrivateIPs (twin uint32 , network gridtypes.Name ) ([]string , error ) {
1114
1131
deployments , err := n .List (twin )
1115
1132
if err != nil {
@@ -1162,3 +1179,46 @@ func (e *NativeEngine) GetWorkloadStatus(id string) (gridtypes.ResultState, bool
1162
1179
1163
1180
return wl .Result .State , true , nil
1164
1181
}
1182
+
1183
+ // getTwinVerificationState make sure the account used is verified we have the user public key in bytes(pkBytes)
1184
+ func getTwinVerificationState (twinID uint32 ) (status string ) {
1185
+ verificationServiceURL := "https://kyc1.gent01.dev.grid.tf/api/v1/status"
1186
+ status = "FAILED"
1187
+
1188
+ request , err := http .NewRequest (http .MethodGet , verificationServiceURL , nil )
1189
+ if err != nil {
1190
+ return
1191
+ }
1192
+
1193
+ q := request .URL .Query ()
1194
+ q .Set ("twinID" , fmt .Sprint (twinID ))
1195
+ request .URL .RawQuery = q .Encode ()
1196
+
1197
+ cl := & http.Client {
1198
+ Timeout : 10 * time .Second ,
1199
+ }
1200
+
1201
+ response , err := cl .Do (request )
1202
+ if err != nil {
1203
+ return
1204
+ }
1205
+ defer response .Body .Close ()
1206
+
1207
+ body , err := io .ReadAll (response .Body )
1208
+ if err != nil {
1209
+ return
1210
+ }
1211
+
1212
+ bodyMap := map [string ]string {}
1213
+ err = json .Unmarshal (body , & bodyMap )
1214
+ if err != nil {
1215
+ return
1216
+ }
1217
+
1218
+ if response .StatusCode != http .StatusOK {
1219
+ log .Error ().Msgf ("failed to verify user status: %s" , bodyMap ["error" ])
1220
+ return
1221
+ }
1222
+
1223
+ return bodyMap ["status" ]
1224
+ }
0 commit comments