@@ -3,17 +3,22 @@ package provision
3
3
import (
4
4
"context"
5
5
"encoding/hex"
6
+ "encoding/json"
6
7
"fmt"
8
+ "net/http"
9
+ "net/url"
7
10
"os"
8
11
"path/filepath"
9
12
"sort"
10
13
"time"
11
14
15
+ "github.com/cenkalti/backoff/v3"
12
16
"github.com/joncrlsn/dque"
13
17
"github.com/pkg/errors"
14
18
"github.com/rs/zerolog/log"
15
19
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
16
20
"github.com/threefoldtech/zos4/pkg"
21
+ "github.com/threefoldtech/zos4/pkg/environment"
17
22
"github.com/threefoldtech/zos4/pkg/gridtypes"
18
23
"github.com/threefoldtech/zos4/pkg/gridtypes/zos"
19
24
"github.com/threefoldtech/zos4/pkg/stubs"
@@ -108,21 +113,23 @@ type NativeEngine struct {
108
113
109
114
queue * dque.DQue
110
115
111
- //options
116
+ // options
112
117
// janitor Janitor
113
118
twins Twins
114
119
admins Twins
115
120
order []gridtypes.WorkloadType
116
121
typeIndex map [gridtypes.WorkloadType ]int
117
122
rerunAll bool
118
- //substrate specific attributes
123
+ // substrate specific attributes
119
124
nodeID uint32
120
125
substrateGateway * stubs.SubstrateGatewayStub
121
126
callback Callback
122
127
}
123
128
124
- var _ Engine = (* NativeEngine )(nil )
125
- var _ pkg.Provision = (* NativeEngine )(nil )
129
+ var (
130
+ _ Engine = (* NativeEngine )(nil )
131
+ _ pkg.Provision = (* NativeEngine )(nil )
132
+ )
126
133
127
134
type withUserKeyGetter struct {
128
135
g Twins
@@ -199,14 +206,19 @@ func (n *nullKeyGetter) GetKey(id uint32) ([]byte, error) {
199
206
return nil , fmt .Errorf ("null user key getter" )
200
207
}
201
208
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 {}
209
+ type (
210
+ engineKey struct {}
211
+ deploymentKey struct {}
212
+ deploymentValue struct {
213
+ twin uint32
214
+ deployment uint64
215
+ }
216
+ )
217
+
218
+ type (
219
+ contractKey struct {}
220
+ rentKey struct {}
221
+ )
210
222
211
223
// GetEngine gets engine from context
212
224
func GetEngine (ctx context.Context ) Engine {
@@ -498,7 +510,7 @@ func (e *NativeEngine) Run(root context.Context) error {
498
510
ctx , err = e .validate (ctx , & job .Target , job .Op == opProvisionNoValidation )
499
511
if err != nil {
500
512
l .Error ().Err (err ).Msg ("contact validation fails" )
501
- //job.Target.SetError(err)
513
+ // job.Target.SetError(err)
502
514
if err := e .storage .Error (job .Target .TwinID , job .Target .ContractID , err ); err != nil {
503
515
l .Error ().Err (err ).Msg ("failed to set deployment global error" )
504
516
}
@@ -718,7 +730,7 @@ func (e *NativeEngine) installWorkload(ctx context.Context, wl *gridtypes.Worklo
718
730
// if it has been deleted, error state, we do nothing.
719
731
// otherwise, we-reinstall it
720
732
if current .Result .State .IsAny (gridtypes .StateDeleted , gridtypes .StateError ) {
721
- //nothing to do!
733
+ // nothing to do!
722
734
return nil
723
735
}
724
736
}
@@ -797,7 +809,7 @@ func (e *NativeEngine) lockWorkload(ctx context.Context, wl *gridtypes.WorkloadW
797
809
return errors .Wrapf (err , "failed to get last transaction for '%s'" , wl .ID .String ())
798
810
} else {
799
811
if ! current .Result .State .IsOkay () {
800
- //nothing to do! it's either in error state or something else.
812
+ // nothing to do! it's either in error state or something else.
801
813
return nil
802
814
}
803
815
}
@@ -857,7 +869,6 @@ func (e *NativeEngine) uninstallDeployment(ctx context.Context, dl *gridtypes.De
857
869
Uint64 ("contract" , dl .ContractID ).
858
870
Msg ("failed to delete deployment" )
859
871
}
860
-
861
872
}
862
873
863
874
func getMountSize (wl * gridtypes.Workload ) (gridtypes.Unit , error ) {
@@ -985,11 +996,11 @@ func (e *NativeEngine) DecommissionCached(id string, reason string) error {
985
996
986
997
if wl .Result .State == gridtypes .StateDeleted ||
987
998
wl .Result .State == gridtypes .StateError {
988
- //nothing to do!
999
+ // nothing to do!
989
1000
return nil
990
1001
}
991
1002
992
- //to bad we have to repeat this here
1003
+ // to bad we have to repeat this here
993
1004
ctx := context .WithValue (context .Background (), engineKey {}, e )
994
1005
ctx = withDeployment (ctx , twin , dlID )
995
1006
@@ -1012,6 +1023,20 @@ func (n *NativeEngine) CreateOrUpdate(twin uint32, deployment gridtypes.Deployme
1012
1023
return fmt .Errorf ("twin id mismatch (deployment: %d, message: %d)" , deployment .TwinID , twin )
1013
1024
}
1014
1025
1026
+ // make sure the account used is verified
1027
+ check := func () error {
1028
+ if ok , err := isTwinVerified (twin ); err != nil {
1029
+ return err
1030
+ } else if ! ok {
1031
+ return fmt .Errorf ("user with twin id %d is not verified" , twin )
1032
+ }
1033
+ return nil
1034
+ }
1035
+
1036
+ if err := backoff .Retry (check , backoff .WithMaxRetries (backoff .NewExponentialBackOff (), 5 )); err != nil {
1037
+ return err
1038
+ }
1039
+
1015
1040
if err := deployment .Verify (n .twins ); err != nil {
1016
1041
return err
1017
1042
}
@@ -1028,7 +1053,6 @@ func (n *NativeEngine) CreateOrUpdate(twin uint32, deployment gridtypes.Deployme
1028
1053
}
1029
1054
1030
1055
return action (ctx , deployment )
1031
-
1032
1056
}
1033
1057
1034
1058
func (n * NativeEngine ) Get (twin uint32 , contractID uint64 ) (gridtypes.Deployment , error ) {
@@ -1041,6 +1065,7 @@ func (n *NativeEngine) Get(twin uint32, contractID uint64) (gridtypes.Deployment
1041
1065
1042
1066
return deployment , nil
1043
1067
}
1068
+
1044
1069
func (n * NativeEngine ) List (twin uint32 ) ([]gridtypes.Deployment , error ) {
1045
1070
deploymentIDs , err := n .storage .ByTwin (twin )
1046
1071
if err != nil {
@@ -1059,6 +1084,7 @@ func (n *NativeEngine) List(twin uint32) ([]gridtypes.Deployment, error) {
1059
1084
}
1060
1085
return deployments , nil
1061
1086
}
1087
+
1062
1088
func (n * NativeEngine ) Changes (twin uint32 , contractID uint64 ) ([]gridtypes.Workload , error ) {
1063
1089
changes , err := n .storage .Changes (twin , contractID )
1064
1090
if errors .Is (err , ErrDeploymentNotExists ) {
@@ -1068,6 +1094,7 @@ func (n *NativeEngine) Changes(twin uint32, contractID uint64) ([]gridtypes.Work
1068
1094
}
1069
1095
return changes , nil
1070
1096
}
1097
+
1071
1098
func (n * NativeEngine ) ListPublicIPs () ([]string , error ) {
1072
1099
// for efficiency this method should just find out configured public Ips.
1073
1100
// but currently the only way to do this is by scanning the nft rules
@@ -1110,6 +1137,7 @@ func (n *NativeEngine) ListPublicIPs() ([]string, error) {
1110
1137
1111
1138
return ips , nil
1112
1139
}
1140
+
1113
1141
func (n * NativeEngine ) ListPrivateIPs (twin uint32 , network gridtypes.Name ) ([]string , error ) {
1114
1142
deployments , err := n .List (twin )
1115
1143
if err != nil {
@@ -1162,3 +1190,46 @@ func (e *NativeEngine) GetWorkloadStatus(id string) (gridtypes.ResultState, bool
1162
1190
1163
1191
return wl .Result .State , true , nil
1164
1192
}
1193
+
1194
+ // isTwinVerified make sure the account used is verified
1195
+ func isTwinVerified (twinID uint32 ) (verified bool , err error ) {
1196
+ const verifiedStatus = "VERIFIED"
1197
+ env := environment .MustGet ()
1198
+
1199
+ verificationServiceURL , err := url .JoinPath (env .KycURL , "/api/v1/status" )
1200
+ if err != nil {
1201
+ return
1202
+ }
1203
+
1204
+ request , err := http .NewRequest (http .MethodGet , verificationServiceURL , nil )
1205
+ if err != nil {
1206
+ return
1207
+ }
1208
+
1209
+ q := request .URL .Query ()
1210
+ q .Set ("twin_id" , fmt .Sprint (twinID ))
1211
+ request .URL .RawQuery = q .Encode ()
1212
+
1213
+ cl := & http.Client {
1214
+ Timeout : 10 * time .Second ,
1215
+ }
1216
+
1217
+ response , err := cl .Do (request )
1218
+ if err != nil {
1219
+ return
1220
+ }
1221
+ defer response .Body .Close ()
1222
+
1223
+ if response .StatusCode != http .StatusOK {
1224
+ return verified , errors .New ("failed to get twin verification status" )
1225
+ }
1226
+
1227
+ var result struct { Result struct { Status string } }
1228
+
1229
+ err = json .NewDecoder (response .Body ).Decode (& result )
1230
+ if err != nil {
1231
+ return
1232
+ }
1233
+
1234
+ return result .Result .Status == verifiedStatus , nil
1235
+ }
0 commit comments