diff --git a/pkg/morph/policy/policy_contract_storage.go b/pkg/morph/policy/policy_contract_storage.go index 37da1a7..a6e6585 100644 --- a/pkg/morph/policy/policy_contract_storage.go +++ b/pkg/morph/policy/policy_contract_storage.go @@ -1,6 +1,7 @@ package policy import ( + "crypto/sha256" "errors" "fmt" "math/big" @@ -49,7 +50,8 @@ func transformNameIfContainer(target engine.Target) (name string) { if target.Type == engine.Container { // Container name can be too long and, thus, cannot be // used as a key name for policy-contract storage. - name = base58.FastBase58Encoding([]byte(target.Name)) + encoded := sha256.Sum224([]byte(target.Name)) + name = base58.FastBase58Encoding(encoded[:]) } return } diff --git a/pkg/morph/policy/policy_contract_storage_test.go b/pkg/morph/policy/policy_contract_storage_test.go new file mode 100644 index 0000000..907574e --- /dev/null +++ b/pkg/morph/policy/policy_contract_storage_test.go @@ -0,0 +1,93 @@ +package policy + +import ( + "fmt" + "testing" + + "git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine" + "git.frostfs.info/TrueCloudLab/policy-engine/schema/native" + "github.com/stretchr/testify/require" +) + +func TestTransformNameIfContainer(t *testing.T) { + for _, test := range []struct { + Name string + Target engine.Target + }{ + { + Name: "check namespace", + Target: engine.NamespaceTarget("namespace1"), + }, + { + Name: "check ResourceFormatNamespaceObjects", + Target: engine.ContainerTarget( + fmt.Sprintf(native.ResourceFormatNamespaceObjects, + "BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")), + }, + { + Name: "check ResourceFormatNamespaceContainerObjects", + Target: engine.ContainerTarget( + fmt.Sprintf(native.ResourceFormatNamespaceContainerObjects, + "BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R", "AeZa5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")), + }, + { + Name: "check ResourceFormatNamespaceContainerObject", + Target: engine.ContainerTarget( + fmt.Sprintf(native.ResourceFormatNamespaceContainerObject, + "BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R", "AeZa5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R", "LxGy5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")), + }, + { + Name: "check ResourceFormatRootObjects", + Target: engine.ContainerTarget(native.ResourceFormatRootObjects), + }, + { + Name: "check ResourceFormatRootContainerObjects", + Target: engine.ContainerTarget( + fmt.Sprintf(native.ResourceFormatRootContainerObjects, + "BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")), + }, + { + Name: "check ResourceFormatRootContainerObject", + Target: engine.ContainerTarget( + fmt.Sprintf(native.ResourceFormatRootContainerObject, + "BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R", "AeZa5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")), + }, + { + Name: "check ResourceFormatAllObjects", + Target: engine.ContainerTarget(native.ResourceFormatAllObjects), + }, + { + Name: "check ResourceFormatNamespaceContainer", + Target: engine.ContainerTarget( + fmt.Sprintf(native.ResourceFormatNamespaceContainer, + "BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R", "AeZa5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")), + }, + { + Name: "check ResourceFormatNamespaceContainers", + Target: engine.ContainerTarget( + fmt.Sprintf(native.ResourceFormatNamespaceContainers, + "BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")), + }, + { + Name: "check ResourceFormatRootContainer", + Target: engine.ContainerTarget( + fmt.Sprintf(native.ResourceFormatRootContainer, + "BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")), + }, + { + Name: "check ResourceFormatRootContainers", + Target: engine.ContainerTarget(native.ResourceFormatRootContainers), + }, + { + Name: "check ResourceFormatAllContainers", + Target: engine.ContainerTarget(native.ResourceFormatAllContainers), + }, + } { + t.Run(test.Target.Name, func(t *testing.T) { + res := transformNameIfContainer(test.Target) + const contractStorageKeyLength = 64 + const entityTypeLength = 1 // 'n', 'c' or 'i' + require.Less(t, len(res), contractStorageKeyLength-entityTypeLength) + }) + } +}