Skip to content

Commit e61bd60

Browse files
committed
add fallback mechanism go version decoding
1 parent 91cd572 commit e61bd60

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

node-registrar/client/zos_version.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package client
22

33
import (
44
"bytes"
5+
"encoding/base64"
56
"encoding/json"
7+
"io"
68
"net/http"
79
"net/url"
10+
"strings"
811
"time"
912

1013
"github.com/pkg/errors"
@@ -35,11 +38,31 @@ func (c *RegistrarClient) getZosVersion() (version ZosVersion, err error) {
3538
err = parseResponseError(resp.Body)
3639
return version, errors.Wrapf(err, "failed to get zos version with status code %s", resp.Status)
3740
}
38-
3941
defer resp.Body.Close()
4042

41-
err = json.NewDecoder(resp.Body).Decode(&version)
43+
bodyBytes, err := io.ReadAll(resp.Body)
44+
if err != nil {
45+
return version, err
46+
}
47+
48+
err = json.Unmarshal(bodyBytes, &version)
4249
if err != nil {
50+
// try decoding base64 version
51+
var versionString string
52+
err = json.Unmarshal(bodyBytes, &versionString)
53+
if err != nil {
54+
return version, err
55+
}
56+
57+
decodedVersion, err := base64.StdEncoding.DecodeString(versionString)
58+
if err != nil {
59+
return version, err
60+
}
61+
62+
correctedJSON := strings.ReplaceAll(string(decodedVersion), "'", "\"")
63+
64+
err = json.Unmarshal([]byte(correctedJSON), &version)
65+
4366
return version, err
4467
}
4568

node-registrar/pkg/server/handlers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,6 @@ type ZOSVersionRequest struct {
697697
SafeToUpgrade bool `json:"safe_to_upgrade" binding:"required"`
698698
}
699699

700-
var versionRegex = regexp.MustCompile(`^v\d+\.\d+\.\d+$`)
701-
702700
// @Summary Set ZOS Version
703701
// @Description Sets the ZOS version
704702
// @Tags ZOS
@@ -795,8 +793,10 @@ func ensureOwner(c *gin.Context, twinID uint64) {
795793
}
796794
}
797795

796+
// addVersionValidato registers a custom validator for version
798797
func addVersionValidator() error {
799-
// Register the custom validation
798+
versionRegex := regexp.MustCompile(`^v\d+\.\d+\.\d+$`)
799+
800800
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
801801
if err := v.RegisterValidation("versionfmt", func(fl validator.FieldLevel) bool {
802802
version := fl.Field().String()

0 commit comments

Comments
 (0)