Skip to content

Commit 9495238

Browse files
committed
return bool from verification function
1 parent b79f1ec commit 9495238

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

pkg/provision/engine.go

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ import (
55
"encoding/hex"
66
"encoding/json"
77
"fmt"
8-
"io"
98
"net/http"
9+
"net/url"
1010
"os"
1111
"path/filepath"
1212
"sort"
1313
"time"
1414

15+
"github.com/cenkalti/backoff/v3"
1516
"github.com/joncrlsn/dque"
1617
"github.com/pkg/errors"
1718
"github.com/rs/zerolog/log"
1819
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
1920
"github.com/threefoldtech/zos4/pkg"
21+
"github.com/threefoldtech/zos4/pkg/environment"
2022
"github.com/threefoldtech/zos4/pkg/gridtypes"
2123
"github.com/threefoldtech/zos4/pkg/gridtypes/zos"
2224
"github.com/threefoldtech/zos4/pkg/stubs"
@@ -1022,8 +1024,15 @@ func (n *NativeEngine) CreateOrUpdate(twin uint32, deployment gridtypes.Deployme
10221024
}
10231025

10241026
// make sure the account used is verified
1025-
if getTwinVerificationState(twin) != "VERIFIED" {
1026-
return fmt.Errorf("user is not verified")
1027+
check := func() error {
1028+
if !isTwinVerified(twin) {
1029+
return fmt.Errorf("user is not verified")
1030+
}
1031+
return nil
1032+
}
1033+
1034+
if err := backoff.Retry(check, backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 5)); err != nil {
1035+
return err
10271036
}
10281037

10291038
if err := deployment.Verify(n.twins); err != nil {
@@ -1180,18 +1189,23 @@ func (e *NativeEngine) GetWorkloadStatus(id string) (gridtypes.ResultState, bool
11801189
return wl.Result.State, true, nil
11811190
}
11821191

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"
1192+
// isTwinVerified make sure the account used is verified
1193+
func isTwinVerified(twinID uint32) (verified bool) {
1194+
const verifiedStatus = "VERIFIED"
1195+
env := environment.MustGet()
1196+
1197+
verificationServiceURL, err := url.JoinPath(env.KycURL, "/api/v1/status")
1198+
if err != nil {
1199+
return
1200+
}
11871201

11881202
request, err := http.NewRequest(http.MethodGet, verificationServiceURL, nil)
11891203
if err != nil {
11901204
return
11911205
}
11921206

11931207
q := request.URL.Query()
1194-
q.Set("twinID", fmt.Sprint(twinID))
1208+
q.Set("twin_id", fmt.Sprint(twinID))
11951209
request.URL.RawQuery = q.Encode()
11961210

11971211
cl := &http.Client{
@@ -1204,21 +1218,17 @@ func getTwinVerificationState(twinID uint32) (status string) {
12041218
}
12051219
defer response.Body.Close()
12061220

1207-
body, err := io.ReadAll(response.Body)
1208-
if err != nil {
1221+
if response.StatusCode != http.StatusOK {
1222+
log.Error().Msg("failed to get user status")
12091223
return
12101224
}
12111225

1212-
bodyMap := map[string]string{}
1213-
err = json.Unmarshal(body, &bodyMap)
1214-
if err != nil {
1215-
return
1216-
}
1226+
var result struct{ Result struct{ Status string } }
12171227

1218-
if response.StatusCode != http.StatusOK {
1219-
log.Error().Msgf("failed to verify user status: %s", bodyMap["error"])
1228+
err = json.NewDecoder(response.Body).Decode(&result)
1229+
if err != nil {
12201230
return
12211231
}
12221232

1223-
return bodyMap["status"]
1233+
return result.Result.Status == verifiedStatus
12241234
}

0 commit comments

Comments
 (0)