[#840] adm: Update FrostFS ID deploy arguments
All checks were successful
Vulncheck / Vulncheck (pull_request) Successful in 1m28s
DCO action / DCO (pull_request) Successful in 3m9s
Build / Build Components (1.21) (pull_request) Successful in 4m15s
Build / Build Components (1.20) (pull_request) Successful in 4m35s
Tests and linters / Staticcheck (pull_request) Successful in 4m58s
Tests and linters / Tests (1.20) (pull_request) Successful in 6m48s
Tests and linters / Tests (1.21) (pull_request) Successful in 7m6s
Tests and linters / Lint (pull_request) Successful in 7m25s
Tests and linters / Tests with -race (pull_request) Successful in 7m9s

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-12-04 16:12:59 +03:00
parent b2c63e57ba
commit f1db468d48
3 changed files with 47 additions and 48 deletions

View file

@ -5,38 +5,29 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
// neo-go doesn't support []util.Uint160 type: func getFrostfsIDAdmin(v *viper.Viper) (util.Uint160, bool, error) {
// https://github.com/nspcc-dev/neo-go/blob/v0.103.0/pkg/smartcontract/parameter.go#L262 admin := v.GetString(frostfsIDAdminConfigKey)
// Thus, return []smartcontract.Parameter. if admin == "" {
func getFrostfsIDAuthorizedKeys(v *viper.Viper, defaultOwner util.Uint160) ([]smartcontract.Parameter, error) { return util.Uint160{}, false, nil
var res []smartcontract.Parameter
res = append(res, smartcontract.Parameter{Type: smartcontract.Hash160Type, Value: defaultOwner})
ks := v.GetStringSlice(frostfsIDAuthorizedKeysConfigKey)
for i := range ks {
h, err := address.StringToUint160(ks[i])
if err == nil {
res = append(res, smartcontract.Parameter{Type: smartcontract.Hash160Type, Value: h})
continue
}
h, err = util.Uint160DecodeStringLE(ks[i])
if err == nil {
res = append(res, smartcontract.Parameter{Type: smartcontract.Hash160Type, Value: h})
continue
}
pk, err := keys.NewPublicKeyFromString(ks[i])
if err == nil {
res = append(res, smartcontract.Parameter{Type: smartcontract.Hash160Type, Value: pk.GetScriptHash()})
continue
}
return nil, fmt.Errorf("frostfsid: #%d item in authorized_keys is invalid: '%s'", i, ks[i])
} }
return res, nil
h, err := address.StringToUint160(admin)
if err == nil {
return h, true, nil
}
h, err = util.Uint160DecodeStringLE(admin)
if err == nil {
return h, true, nil
}
pk, err := keys.NewPublicKeyFromString(admin)
if err == nil {
return pk.GetScriptHash(), true, nil
}
return util.Uint160{}, true, fmt.Errorf("frostfsid: admin is invalid: '%s'", admin)
} }

View file

@ -6,8 +6,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -20,30 +18,36 @@ func TestFrostfsIDConfig(t *testing.T) {
pks[i] = pk pks[i] = pk
} }
v := viper.New() fmts := []string{
v.Set("frostfsid.authorized_keys", []string{
pks[0].GetScriptHash().StringLE(), pks[0].GetScriptHash().StringLE(),
address.Uint160ToString(pks[1].GetScriptHash()), address.Uint160ToString(pks[1].GetScriptHash()),
hex.EncodeToString(pks[2].PublicKey().UncompressedBytes()), hex.EncodeToString(pks[2].PublicKey().UncompressedBytes()),
hex.EncodeToString(pks[3].PublicKey().Bytes()), hex.EncodeToString(pks[3].PublicKey().Bytes()),
}) }
comm := util.Uint160{1, 2, 3} for i := range fmts {
actual, err := getFrostfsIDAuthorizedKeys(v, comm) v := viper.New()
require.NoError(t, err) v.Set("frostfsid.admin", fmts[i])
require.Equal(t, len(pks)+1, len(actual))
require.Equal(t, smartcontract.Hash160Type, actual[0].Type) actual, found, err := getFrostfsIDAdmin(v)
require.Equal(t, comm, actual[0].Value) require.NoError(t, err)
for i := range pks { require.True(t, found)
require.Equal(t, smartcontract.Hash160Type, actual[i+1].Type) require.Equal(t, pks[i].GetScriptHash(), actual)
require.Equal(t, pks[i].GetScriptHash(), actual[i+1].Value)
} }
t.Run("bad key", func(t *testing.T) { t.Run("bad key", func(t *testing.T) {
v := viper.New() v := viper.New()
v.Set("frostfsid.authorized_keys", []string{"abc"}) v.Set("frostfsid.admin", "abc")
_, err := getFrostfsIDAuthorizedKeys(v, comm) _, found, err := getFrostfsIDAdmin(v)
require.Error(t, err) require.Error(t, err)
require.True(t, found)
})
t.Run("missing key", func(t *testing.T) {
v := viper.New()
_, found, err := getFrostfsIDAdmin(v)
require.NoError(t, err)
require.False(t, found)
}) })
} }

View file

@ -47,7 +47,7 @@ const (
proxyContract = "proxy" proxyContract = "proxy"
) )
const frostfsIDAuthorizedKeysConfigKey = "frostfsid.authorized_keys" const frostfsIDAdminConfigKey = "frostfsid.admin"
var ( var (
contractList = []string{ contractList = []string{
@ -538,12 +538,16 @@ func (c *initializeContext) getContractDeployData(ctrName string, keysParam []an
nnsCs.Hash, nnsCs.Hash,
"container") "container")
case frostfsIDContract: case frostfsIDContract:
hs, err := getFrostfsIDAuthorizedKeys(viper.GetViper(), c.CommitteeAcc.PublicKey().GetScriptHash()) h, found, err := getFrostfsIDAdmin(viper.GetViper())
if err != nil { if err != nil {
panic(err) panic(err)
} }
items = append(items, hs) if found {
items = append(items, h)
} else {
items = append(items, nil)
}
case netmapContract: case netmapContract:
md := getDefaultNetmapContractConfigMap() md := getDefaultNetmapContractConfigMap()
if method == updateMethodName { if method == updateMethodName {