Skip to content

Commit e983cc0

Browse files
committed
use kyc url retrived from stack info
1 parent 0ec7c73 commit e983cc0

File tree

3 files changed

+91
-76
lines changed

3 files changed

+91
-76
lines changed

pkg/environment/environment.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type Environment struct {
4949
RelayURL []string
5050
ActivationURL string
5151
GraphQL string
52+
KycURL string
5253

5354
// private vlan to join
5455
// if set, zos will use this as its priv vlan
@@ -116,6 +117,7 @@ var (
116117
FlistURL: "redis://hub.grid.tf:9900",
117118
BinRepo: "tf-zos-v3-bins.dev",
118119
GraphQL: "https://graphql.dev.grid.tf/graphql",
120+
KycURL: "",
119121
}
120122

121123
envTest = Environment{
@@ -131,6 +133,7 @@ var (
131133
FlistURL: "redis://hub.grid.tf:9900",
132134
BinRepo: "tf-zos-v3-bins.test",
133135
GraphQL: "https://graphql.test.grid.tf/graphql",
136+
KycURL: "",
134137
}
135138

136139
envQA = Environment{
@@ -146,6 +149,7 @@ var (
146149
FlistURL: "redis://hub.grid.tf:9900",
147150
BinRepo: "tf-zos-v3-bins.qanet",
148151
GraphQL: "https://graphql.qa.grid.tf/graphql",
152+
KycURL: "",
149153
}
150154

151155
envProd = Environment{
@@ -164,6 +168,7 @@ var (
164168
FlistURL: "redis://hub.grid.tf:9900",
165169
BinRepo: "tf-zos-v3-bins",
166170
GraphQL: "https://graphql.grid.tf/graphql",
171+
KycURL: "https://kyc1.gent01.dev.grid.tf",
167172
}
168173
)
169174

pkg/gridtypes/deployment.go

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@ import (
55
"crypto/ed25519"
66
"crypto/md5"
77
"encoding/hex"
8-
"encoding/json"
98
"fmt"
109
"io"
11-
"net/http"
12-
"time"
1310

1411
sr25519 "github.com/ChainSafe/go-schnorrkel"
1512
"github.com/gtank/merlin"
1613
"github.com/pkg/errors"
1714
"github.com/rs/zerolog/log"
1815
)
1916

20-
// ErrWorkloadNotFound error
21-
var ErrWorkloadNotFound = fmt.Errorf("workload not found")
17+
var (
18+
// ErrWorkloadNotFound error
19+
ErrWorkloadNotFound = fmt.Errorf("workload not found")
20+
)
2221

2322
const (
2423
SignatureTypeEd25519 = "ed25519"
@@ -33,10 +32,8 @@ type Verifier interface {
3332
Verify(msg []byte, sig []byte) bool
3433
}
3534

36-
type (
37-
Ed25519VerifyingKey []byte
38-
Sr25519VerifyingKey []byte
39-
)
35+
type Ed25519VerifyingKey []byte
36+
type Sr25519VerifyingKey []byte
4037

4138
func (k Ed25519VerifyingKey) Verify(msg []byte, sig []byte) bool {
4239
return ed25519.Verify([]byte(k), msg, sig)
@@ -388,11 +385,6 @@ func (d *Deployment) Sign(twin uint32, sk Signer) error {
388385
// Verify verifies user signatures is mainly used by the node
389386
// to verify that all attached signatures are valid.
390387
func (d *Deployment) Verify(getter KeyGetter) error {
391-
// make sure the account used is verified
392-
if getTwinVerificationState(d.TwinID) != "VERIFIED" {
393-
return fmt.Errorf("user is not verified")
394-
}
395-
396388
message, err := d.ChallengeHash()
397389
if err != nil {
398390
return err
@@ -625,6 +617,7 @@ func (d *Deployment) Upgrade(n *Deployment) ([]UpgradeOp, error) {
625617
wl,
626618
OpUpdate,
627619
})
620+
628621
}
629622
// other wise. we leave it untouched
630623
}
@@ -672,46 +665,3 @@ func (o JobOperation) String() string {
672665
return "unknown"
673666
}
674667
}
675-
676-
// getTwinVerificationState make sure the account used is verified we have the user public key in bytes(pkBytes)
677-
func getTwinVerificationState(twinID uint32) (status string) {
678-
verificationServiceURL := "https://kyc1.gent01.dev.grid.tf/api/v1/status"
679-
status = "FAILED"
680-
681-
request, err := http.NewRequest(http.MethodGet, verificationServiceURL, nil)
682-
if err != nil {
683-
return
684-
}
685-
686-
q := request.URL.Query()
687-
q.Set("twinID", fmt.Sprint(twinID))
688-
request.URL.RawQuery = q.Encode()
689-
690-
cl := &http.Client{
691-
Timeout: 10 * time.Second,
692-
}
693-
694-
response, err := cl.Do(request)
695-
if err != nil {
696-
return
697-
}
698-
defer response.Body.Close()
699-
700-
body, err := io.ReadAll(response.Body)
701-
if err != nil {
702-
return
703-
}
704-
705-
bodyMap := map[string]string{}
706-
err = json.Unmarshal(body, &bodyMap)
707-
if err != nil {
708-
return
709-
}
710-
711-
if response.StatusCode != http.StatusOK {
712-
log.Error().Msgf("failed to verify user status: %s", bodyMap["error"])
713-
return
714-
}
715-
716-
return bodyMap["status"]
717-
}

pkg/provision/engine.go

Lines changed: 79 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package provision
33
import (
44
"context"
55
"encoding/hex"
6+
"encoding/json"
67
"fmt"
8+
"io"
9+
"net/http"
710
"os"
811
"path/filepath"
912
"sort"
@@ -108,21 +111,23 @@ type NativeEngine struct {
108111

109112
queue *dque.DQue
110113

111-
//options
114+
// options
112115
// janitor Janitor
113116
twins Twins
114117
admins Twins
115118
order []gridtypes.WorkloadType
116119
typeIndex map[gridtypes.WorkloadType]int
117120
rerunAll bool
118-
//substrate specific attributes
121+
// substrate specific attributes
119122
nodeID uint32
120123
substrateGateway *stubs.SubstrateGatewayStub
121124
callback Callback
122125
}
123126

124-
var _ Engine = (*NativeEngine)(nil)
125-
var _ pkg.Provision = (*NativeEngine)(nil)
127+
var (
128+
_ Engine = (*NativeEngine)(nil)
129+
_ pkg.Provision = (*NativeEngine)(nil)
130+
)
126131

127132
type withUserKeyGetter struct {
128133
g Twins
@@ -199,14 +204,19 @@ func (n *nullKeyGetter) GetKey(id uint32) ([]byte, error) {
199204
return nil, fmt.Errorf("null user key getter")
200205
}
201206

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+
)
210220

211221
// GetEngine gets engine from context
212222
func GetEngine(ctx context.Context) Engine {
@@ -498,7 +508,7 @@ func (e *NativeEngine) Run(root context.Context) error {
498508
ctx, err = e.validate(ctx, &job.Target, job.Op == opProvisionNoValidation)
499509
if err != nil {
500510
l.Error().Err(err).Msg("contact validation fails")
501-
//job.Target.SetError(err)
511+
// job.Target.SetError(err)
502512
if err := e.storage.Error(job.Target.TwinID, job.Target.ContractID, err); err != nil {
503513
l.Error().Err(err).Msg("failed to set deployment global error")
504514
}
@@ -718,7 +728,7 @@ func (e *NativeEngine) installWorkload(ctx context.Context, wl *gridtypes.Worklo
718728
// if it has been deleted, error state, we do nothing.
719729
// otherwise, we-reinstall it
720730
if current.Result.State.IsAny(gridtypes.StateDeleted, gridtypes.StateError) {
721-
//nothing to do!
731+
// nothing to do!
722732
return nil
723733
}
724734
}
@@ -797,7 +807,7 @@ func (e *NativeEngine) lockWorkload(ctx context.Context, wl *gridtypes.WorkloadW
797807
return errors.Wrapf(err, "failed to get last transaction for '%s'", wl.ID.String())
798808
} else {
799809
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.
801811
return nil
802812
}
803813
}
@@ -857,7 +867,6 @@ func (e *NativeEngine) uninstallDeployment(ctx context.Context, dl *gridtypes.De
857867
Uint64("contract", dl.ContractID).
858868
Msg("failed to delete deployment")
859869
}
860-
861870
}
862871

863872
func getMountSize(wl *gridtypes.Workload) (gridtypes.Unit, error) {
@@ -985,11 +994,11 @@ func (e *NativeEngine) DecommissionCached(id string, reason string) error {
985994

986995
if wl.Result.State == gridtypes.StateDeleted ||
987996
wl.Result.State == gridtypes.StateError {
988-
//nothing to do!
997+
// nothing to do!
989998
return nil
990999
}
9911000

992-
//to bad we have to repeat this here
1001+
// to bad we have to repeat this here
9931002
ctx := context.WithValue(context.Background(), engineKey{}, e)
9941003
ctx = withDeployment(ctx, twin, dlID)
9951004

@@ -1012,6 +1021,11 @@ func (n *NativeEngine) CreateOrUpdate(twin uint32, deployment gridtypes.Deployme
10121021
return fmt.Errorf("twin id mismatch (deployment: %d, message: %d)", deployment.TwinID, twin)
10131022
}
10141023

1024+
// make sure the account used is verified
1025+
if getTwinVerificationState(twin) != "VERIFIED" {
1026+
return fmt.Errorf("user is not verified")
1027+
}
1028+
10151029
if err := deployment.Verify(n.twins); err != nil {
10161030
return err
10171031
}
@@ -1028,7 +1042,6 @@ func (n *NativeEngine) CreateOrUpdate(twin uint32, deployment gridtypes.Deployme
10281042
}
10291043

10301044
return action(ctx, deployment)
1031-
10321045
}
10331046

10341047
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
10411054

10421055
return deployment, nil
10431056
}
1057+
10441058
func (n *NativeEngine) List(twin uint32) ([]gridtypes.Deployment, error) {
10451059
deploymentIDs, err := n.storage.ByTwin(twin)
10461060
if err != nil {
@@ -1059,6 +1073,7 @@ func (n *NativeEngine) List(twin uint32) ([]gridtypes.Deployment, error) {
10591073
}
10601074
return deployments, nil
10611075
}
1076+
10621077
func (n *NativeEngine) Changes(twin uint32, contractID uint64) ([]gridtypes.Workload, error) {
10631078
changes, err := n.storage.Changes(twin, contractID)
10641079
if errors.Is(err, ErrDeploymentNotExists) {
@@ -1068,6 +1083,7 @@ func (n *NativeEngine) Changes(twin uint32, contractID uint64) ([]gridtypes.Work
10681083
}
10691084
return changes, nil
10701085
}
1086+
10711087
func (n *NativeEngine) ListPublicIPs() ([]string, error) {
10721088
// for efficiency this method should just find out configured public Ips.
10731089
// but currently the only way to do this is by scanning the nft rules
@@ -1110,6 +1126,7 @@ func (n *NativeEngine) ListPublicIPs() ([]string, error) {
11101126

11111127
return ips, nil
11121128
}
1129+
11131130
func (n *NativeEngine) ListPrivateIPs(twin uint32, network gridtypes.Name) ([]string, error) {
11141131
deployments, err := n.List(twin)
11151132
if err != nil {
@@ -1162,3 +1179,46 @@ func (e *NativeEngine) GetWorkloadStatus(id string) (gridtypes.ResultState, bool
11621179

11631180
return wl.Result.State, true, nil
11641181
}
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

Comments
 (0)