2022-05-25 16:15:27 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/core/version"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/eacl"
|
|
|
|
versionSDK "github.com/TrueCloudLab/frostfs-sdk-go/version"
|
2022-05-25 16:15:27 +00:00
|
|
|
"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 {
|
|
|
|
_, err := os.Stat(eaclPath) // check if `eaclPath` is an existing file
|
|
|
|
if err != nil {
|
|
|
|
ExitOnErr(cmd, "", errors.New("incorrect path to file with EACL"))
|
|
|
|
}
|
|
|
|
|
2022-12-27 09:36:30 +00:00
|
|
|
PrintVerbose(cmd, "Reading EACL from file: %s", eaclPath)
|
2022-05-25 16:15:27 +00:00
|
|
|
|
|
|
|
data, err := os.ReadFile(eaclPath)
|
|
|
|
ExitOnErr(cmd, "can't read file with EACL: %w", err)
|
|
|
|
|
|
|
|
table := eacl.NewTable()
|
|
|
|
|
|
|
|
if err = table.UnmarshalJSON(data); err == nil {
|
|
|
|
validateAndFixEACLVersion(table)
|
2022-12-27 09:36:30 +00:00
|
|
|
PrintVerbose(cmd, "Parsed JSON encoded EACL table")
|
2022-05-25 16:15:27 +00:00
|
|
|
return table
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = table.Unmarshal(data); err == nil {
|
|
|
|
validateAndFixEACLVersion(table)
|
2022-12-27 09:36:30 +00:00
|
|
|
PrintVerbose(cmd, "Parsed binary encoded EACL table")
|
2022-05-25 16:15:27 +00:00
|
|
|
return table
|
|
|
|
}
|
|
|
|
|
|
|
|
ExitOnErr(cmd, "", errUnsupportedEACLFormat)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateAndFixEACLVersion(table *eacl.Table) {
|
|
|
|
if !version.IsValid(table.Version()) {
|
|
|
|
table.SetVersion(versionSDK.Current())
|
|
|
|
}
|
|
|
|
}
|