Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade NeoFS SDK with eacl changes #2618

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions cmd/neofs-cli/internal/common/eacl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import (
"errors"
"os"

"github.com/nspcc-dev/neofs-node/pkg/core/version"
"github.com/nspcc-dev/neofs-sdk-go/eacl"
versionSDK "github.com/nspcc-dev/neofs-sdk-go/version"
"github.com/spf13/cobra"
)

var errUnsupportedEACLFormat = errors.New("unsupported eACL format")

// ReadEACL reads extended ACL table from eaclPath.
func ReadEACL(cmd *cobra.Command, eaclPath string) *eacl.Table {
func ReadEACL(cmd *cobra.Command, eaclPath string) eacl.Table {
_, err := os.Stat(eaclPath) // check if `eaclPath` is an existing file
if err != nil {
ExitOnErr(cmd, "", errors.New("incorrect path to file with EACL"))
Expand All @@ -24,26 +22,18 @@ func ReadEACL(cmd *cobra.Command, eaclPath string) *eacl.Table {
data, err := os.ReadFile(eaclPath)
ExitOnErr(cmd, "can't read file with EACL: %w", err)

table := eacl.NewTable()
var table eacl.Table

if err = table.UnmarshalJSON(data); err == nil {
validateAndFixEACLVersion(table)
PrintVerbose(cmd, "Parsed JSON encoded EACL table")
return table
}

if err = table.Unmarshal(data); err == nil {
validateAndFixEACLVersion(table)
PrintVerbose(cmd, "Parsed binary encoded EACL table")
return table
}

ExitOnErr(cmd, "", errUnsupportedEACLFormat)
return nil
}

func validateAndFixEACLVersion(table *eacl.Table) {
if !version.IsValid(table.Version()) {
table.SetVersion(versionSDK.Current())
}
return table
}
7 changes: 3 additions & 4 deletions cmd/neofs-cli/modules/acl/extended/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/util"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
"github.com/nspcc-dev/neofs-sdk-go/eacl"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -84,13 +83,13 @@ func createEACL(cmd *cobra.Command, _ []string) {
os.Exit(1)
}

tb := eacl.NewTable()
common.ExitOnErr(cmd, "unable to parse provided rules: %w", util.ParseEACLRules(tb, rules))
tb, err := util.ParseEACLRules(rules)
common.ExitOnErr(cmd, "unable to parse provided rules: %w", err)

err = util.ValidateEACLTable(tb)
common.ExitOnErr(cmd, "table validation: %w", err)

tb.SetCID(containerID)
tb.LimitByContainer(containerID)

data, err := tb.MarshalJSON()
if err != nil {
Expand Down
23 changes: 5 additions & 18 deletions cmd/neofs-cli/modules/acl/extended/create_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package extended

import (
"fmt"
"testing"

"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/util"
Expand Down Expand Up @@ -59,32 +60,18 @@ func TestParseTable(t *testing.T) {
},
}

eaclTable := eacl.NewTable()

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := util.ParseEACLRule(eaclTable, test.rule)
recs, err := util.ParseEACLRule(test.rule)
ok := len(test.jsonRecord) > 0
require.Equal(t, ok, err == nil, err)
if ok {
expectedRecord := eacl.NewRecord()
err = expectedRecord.UnmarshalJSON([]byte(test.jsonRecord))
var expectedTable eacl.Table
err = expectedTable.UnmarshalJSON([]byte(fmt.Sprintf(`{"records": [%s]}`, test.jsonRecord)))
require.NoError(t, err)

actualRecord := eaclTable.Records()[len(eaclTable.Records())-1]

equalRecords(t, expectedRecord, &actualRecord)
require.Equal(t, expectedTable.Records(), recs)
}
})
}
}

func equalRecords(t *testing.T, r1, r2 *eacl.Record) {
d1, err := r1.Marshal()
require.NoError(t, err)

d2, err := r2.Marshal()
require.NoError(t, err)

require.Equal(t, d1, d2)
}
5 changes: 3 additions & 2 deletions cmd/neofs-cli/modules/acl/extended/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ func init() {

func printEACL(cmd *cobra.Command, _ []string) {
file, _ := cmd.Flags().GetString("file")
eaclTable := new(eacl.Table)
var eaclTable eacl.Table
data, err := os.ReadFile(file)
common.ExitOnErr(cmd, "can't read file with EACL: %w", err)
if strings.HasSuffix(file, ".json") {
common.ExitOnErr(cmd, "unable to parse json: %w", eaclTable.UnmarshalJSON(data))
} else {
rules := strings.Split(strings.TrimSpace(string(data)), "\n")
common.ExitOnErr(cmd, "can't parse file with EACL: %w", util.ParseEACLRules(eaclTable, rules))
eaclTable, err = util.ParseEACLRules(rules)
common.ExitOnErr(cmd, "can't parse file with EACL: %w", err)
}
util.PrettyPrintTableEACL(cmd, eaclTable)
}
6 changes: 3 additions & 3 deletions cmd/neofs-cli/modules/bearer/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ func createToken(cmd *cobra.Command, _ []string) {

eaclPath, _ := cmd.Flags().GetString(eaclFlag)
if eaclPath != "" {
table := eaclSDK.NewTable()
var table eaclSDK.Table
raw, err := os.ReadFile(eaclPath)
common.ExitOnErr(cmd, "can't read extended ACL file: %w", err)
common.ExitOnErr(cmd, "can't parse extended ACL: %w", json.Unmarshal(raw, table))
b.SetEACLTable(*table)
common.ExitOnErr(cmd, "can't parse extended ACL: %w", json.Unmarshal(raw, &table))
b.SetEACLTable(table)
}

var data []byte
Expand Down
3 changes: 1 addition & 2 deletions cmd/neofs-cli/modules/container/get_eacl.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ var getExtendedACLCmd = &cobra.Command{
data, err = eaclTable.MarshalJSON()
common.ExitOnErr(cmd, "can't encode to JSON: %w", err)
} else {
data, err = eaclTable.Marshal()
common.ExitOnErr(cmd, "can't encode to binary: %w", err)
data = eaclTable.Marshal()
}

cmd.Println("dumping data to file:", containerPathTo)
Expand Down
12 changes: 4 additions & 8 deletions cmd/neofs-cli/modules/container/set_eacl.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Container ID in EACL table will be substituted with ID from the CLI.`,

tok := getSession(cmd)

eaclTable.SetCID(id)
eaclTable.LimitByContainer(id)

pk := key.GetOrGenerate(cmd)
cli := internalclient.GetSDKClientByFlag(ctx, cmd, commonflags.RPC)
Expand Down Expand Up @@ -86,7 +86,7 @@ Container ID in EACL table will be substituted with ID from the CLI.`,

var setEACLPrm internalclient.SetEACLPrm
setEACLPrm.SetClient(cli)
setEACLPrm.SetTable(*eaclTable)
setEACLPrm.SetTable(eaclTable)
setEACLPrm.SetPrivateKey(*pk)

if tok != nil {
Expand All @@ -99,8 +99,7 @@ Container ID in EACL table will be substituted with ID from the CLI.`,
cmd.Println("eACL modification request accepted for processing (the operation may not be completed yet)")

if containerAwait {
exp, err := eaclTable.Marshal()
common.ExitOnErr(cmd, "broken EACL table: %w", err)
exp := eaclTable.Marshal()

cmd.Println("awaiting...")

Expand All @@ -124,10 +123,7 @@ Container ID in EACL table will be substituted with ID from the CLI.`,
if err == nil {
// compare binary values because EACL could have been set already
table := res.EACL()
got, err := table.Marshal()
if err != nil {
continue
}
got := table.Marshal()

if bytes.Equal(exp, got) {
cmd.Println("EACL has been persisted on sidechain")
Expand Down
Loading