2020-08-17 13:02:24 +00:00
|
|
|
package acl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/util/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-08-17 13:57:51 +00:00
|
|
|
filterHeaderTypeField = 1
|
|
|
|
filterMatchTypeField = 2
|
|
|
|
filterNameField = 3
|
|
|
|
filterValueField = 4
|
|
|
|
|
|
|
|
targetTypeField = 1
|
|
|
|
targetKeysField = 2
|
|
|
|
|
|
|
|
recordOperationField = 1
|
|
|
|
recordActionField = 2
|
|
|
|
recordFiltersField = 3
|
|
|
|
recordTargetsField = 4
|
|
|
|
|
2020-09-04 07:50:36 +00:00
|
|
|
tableVersionField = 1
|
|
|
|
tableContainerIDField = 2
|
|
|
|
tableRecordsField = 3
|
2020-08-20 10:07:35 +00:00
|
|
|
|
|
|
|
lifetimeExpirationField = 1
|
|
|
|
lifetimeNotValidBeforeField = 2
|
|
|
|
lifetimeIssuedAtField = 3
|
|
|
|
|
|
|
|
bearerTokenBodyACLField = 1
|
|
|
|
bearerTokenBodyOwnerField = 2
|
|
|
|
bearerTokenBodyLifetimeField = 3
|
|
|
|
|
|
|
|
bearerTokenBodyField = 1
|
|
|
|
bearerTokenSignatureField = 2
|
2020-08-17 13:02:24 +00:00
|
|
|
)
|
|
|
|
|
2020-08-17 14:47:43 +00:00
|
|
|
// StableMarshal marshals unified acl table structure in a protobuf
|
|
|
|
// compatible way without field order shuffle.
|
2020-08-17 13:02:24 +00:00
|
|
|
func (t *Table) StableMarshal(buf []byte) ([]byte, error) {
|
2020-08-17 13:57:51 +00:00
|
|
|
if t == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, t.StableSize())
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
offset, n int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2020-09-04 07:50:36 +00:00
|
|
|
n, err = proto.NestedStructureMarshal(tableVersionField, buf[offset:], t.version)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n
|
|
|
|
|
2020-08-18 07:42:47 +00:00
|
|
|
n, err = proto.NestedStructureMarshal(tableContainerIDField, buf[offset:], t.cid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-08-17 13:57:51 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 07:42:47 +00:00
|
|
|
offset += n
|
2020-08-17 13:57:51 +00:00
|
|
|
|
|
|
|
for i := range t.records {
|
2020-08-18 07:42:47 +00:00
|
|
|
n, err = proto.NestedStructureMarshal(tableRecordsField, buf[offset:], t.records[i])
|
2020-08-17 13:57:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
2020-08-17 13:02:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 14:47:43 +00:00
|
|
|
// StableSize of acl table structure marshalled by StableMarshal function.
|
2020-08-17 13:57:51 +00:00
|
|
|
func (t *Table) StableSize() (size int) {
|
|
|
|
if t == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-09-04 07:50:36 +00:00
|
|
|
size += proto.NestedStructureSize(tableVersionField, t.version)
|
2020-08-18 07:42:47 +00:00
|
|
|
size += proto.NestedStructureSize(tableContainerIDField, t.cid)
|
2020-08-17 13:57:51 +00:00
|
|
|
|
|
|
|
for i := range t.records {
|
2020-08-18 07:42:47 +00:00
|
|
|
size += proto.NestedStructureSize(tableRecordsField, t.records[i])
|
2020-08-17 13:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return size
|
2020-08-17 13:02:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 14:47:43 +00:00
|
|
|
// StableMarshal marshals unified acl record structure in a protobuf
|
|
|
|
// compatible way without field order shuffle.
|
2020-08-17 13:02:24 +00:00
|
|
|
func (r *Record) StableMarshal(buf []byte) ([]byte, error) {
|
2020-08-17 13:49:19 +00:00
|
|
|
if r == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, r.StableSize())
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
offset, n int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2020-08-18 07:42:47 +00:00
|
|
|
n, err = proto.EnumMarshal(recordOperationField, buf[offset:], int32(r.op))
|
2020-08-17 13:49:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n
|
|
|
|
|
2020-08-17 13:57:51 +00:00
|
|
|
n, err = proto.EnumMarshal(recordActionField, buf[offset:], int32(r.action))
|
2020-08-17 13:49:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n
|
|
|
|
|
|
|
|
for i := range r.filters {
|
2020-08-18 07:42:47 +00:00
|
|
|
n, err = proto.NestedStructureMarshal(recordFiltersField, buf[offset:], r.filters[i])
|
2020-08-17 13:49:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range r.targets {
|
2020-08-18 07:42:47 +00:00
|
|
|
n, err = proto.NestedStructureMarshal(recordTargetsField, buf[offset:], r.targets[i])
|
2020-08-17 13:49:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
2020-08-17 13:02:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 14:47:43 +00:00
|
|
|
// StableSize of acl record structure marshalled by StableMarshal function.
|
2020-08-17 13:49:19 +00:00
|
|
|
func (r *Record) StableSize() (size int) {
|
|
|
|
if r == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-08-17 13:57:51 +00:00
|
|
|
size += proto.EnumSize(recordOperationField, int32(r.op))
|
2020-09-04 11:25:35 +00:00
|
|
|
size += proto.EnumSize(recordActionField, int32(r.action))
|
2020-08-17 13:49:19 +00:00
|
|
|
|
|
|
|
for i := range r.filters {
|
2020-08-18 07:42:47 +00:00
|
|
|
size += proto.NestedStructureSize(recordFiltersField, r.filters[i])
|
2020-08-17 13:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range r.targets {
|
2020-08-18 07:42:47 +00:00
|
|
|
size += proto.NestedStructureSize(recordTargetsField, r.targets[i])
|
2020-08-17 13:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return size
|
2020-08-17 13:02:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 14:47:43 +00:00
|
|
|
// StableMarshal marshals unified header filter structure in a protobuf
|
|
|
|
// compatible way without field order shuffle.
|
2020-08-17 13:02:24 +00:00
|
|
|
func (f *HeaderFilter) StableMarshal(buf []byte) ([]byte, error) {
|
|
|
|
if f == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, f.StableSize())
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
offset, n int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2020-08-18 07:42:47 +00:00
|
|
|
n, err = proto.EnumMarshal(filterHeaderTypeField, buf[offset:], int32(f.hdrType))
|
2020-08-17 13:02:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n
|
|
|
|
|
2020-08-17 13:57:51 +00:00
|
|
|
n, err = proto.EnumMarshal(filterMatchTypeField, buf[offset:], int32(f.matchType))
|
2020-08-17 13:02:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n
|
|
|
|
|
2020-08-17 13:57:51 +00:00
|
|
|
n, err = proto.StringMarshal(filterNameField, buf[offset:], f.name)
|
2020-08-17 13:02:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n
|
|
|
|
|
2020-08-17 14:47:43 +00:00
|
|
|
_, err = proto.StringMarshal(filterValueField, buf[offset:], f.value)
|
2020-08-17 13:02:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2020-08-17 14:47:43 +00:00
|
|
|
// StableSize of header filter structure marshalled by StableMarshal function.
|
2020-08-17 13:02:24 +00:00
|
|
|
func (f *HeaderFilter) StableSize() (size int) {
|
|
|
|
if f == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-08-17 13:57:51 +00:00
|
|
|
size += proto.EnumSize(filterHeaderTypeField, int32(f.hdrType))
|
|
|
|
size += proto.EnumSize(filterMatchTypeField, int32(f.matchType))
|
|
|
|
size += proto.StringSize(filterNameField, f.name)
|
|
|
|
size += proto.StringSize(filterValueField, f.value)
|
2020-08-17 13:02:24 +00:00
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2020-09-04 07:50:36 +00:00
|
|
|
// StableMarshal marshals unified role info structure in a protobuf
|
2020-08-17 14:47:43 +00:00
|
|
|
// compatible way without field order shuffle.
|
2020-08-17 13:09:09 +00:00
|
|
|
func (t *TargetInfo) StableMarshal(buf []byte) ([]byte, error) {
|
|
|
|
if t == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, t.StableSize())
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
offset, n int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2020-09-04 07:50:36 +00:00
|
|
|
n, err = proto.EnumMarshal(targetTypeField, buf[offset:], int32(t.role))
|
2020-08-17 13:09:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n
|
|
|
|
|
2020-08-17 14:47:43 +00:00
|
|
|
_, err = proto.RepeatedBytesMarshal(targetKeysField, buf[offset:], t.keys)
|
2020-08-17 13:09:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
2020-08-17 13:02:24 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 07:50:36 +00:00
|
|
|
// StableSize of role info structure marshalled by StableMarshal function.
|
2020-08-17 13:09:09 +00:00
|
|
|
func (t *TargetInfo) StableSize() (size int) {
|
|
|
|
if t == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-09-04 07:50:36 +00:00
|
|
|
size += proto.EnumSize(targetTypeField, int32(t.role))
|
2020-08-17 13:57:51 +00:00
|
|
|
size += proto.RepeatedBytesSize(targetKeysField, t.keys)
|
2020-08-17 13:09:09 +00:00
|
|
|
|
|
|
|
return size
|
2020-08-17 13:02:24 +00:00
|
|
|
}
|
2020-08-20 10:07:35 +00:00
|
|
|
|
|
|
|
func (l *TokenLifetime) StableMarshal(buf []byte) ([]byte, error) {
|
|
|
|
if l == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, l.StableSize())
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
offset, n int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
n, err = proto.UInt64Marshal(lifetimeExpirationField, buf[offset:], l.exp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n
|
|
|
|
|
|
|
|
n, err = proto.UInt64Marshal(lifetimeNotValidBeforeField, buf[offset:], l.nbf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n
|
|
|
|
|
|
|
|
_, err = proto.UInt64Marshal(lifetimeIssuedAtField, buf[offset:], l.iat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *TokenLifetime) StableSize() (size int) {
|
|
|
|
if l == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
size += proto.UInt64Size(lifetimeExpirationField, l.exp)
|
|
|
|
size += proto.UInt64Size(lifetimeNotValidBeforeField, l.nbf)
|
|
|
|
size += proto.UInt64Size(lifetimeIssuedAtField, l.iat)
|
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bt *BearerTokenBody) StableMarshal(buf []byte) ([]byte, error) {
|
|
|
|
if bt == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, bt.StableSize())
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
offset, n int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
n, err = proto.NestedStructureMarshal(bearerTokenBodyACLField, buf[offset:], bt.eacl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n
|
|
|
|
|
|
|
|
n, err = proto.NestedStructureMarshal(bearerTokenBodyOwnerField, buf[offset:], bt.ownerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n
|
|
|
|
|
|
|
|
_, err = proto.NestedStructureMarshal(bearerTokenBodyLifetimeField, buf[offset:], bt.lifetime)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bt *BearerTokenBody) StableSize() (size int) {
|
|
|
|
if bt == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
size += proto.NestedStructureSize(bearerTokenBodyACLField, bt.eacl)
|
|
|
|
size += proto.NestedStructureSize(bearerTokenBodyOwnerField, bt.ownerID)
|
|
|
|
size += proto.NestedStructureSize(bearerTokenBodyLifetimeField, bt.lifetime)
|
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bt *BearerToken) StableMarshal(buf []byte) ([]byte, error) {
|
|
|
|
if bt == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, bt.StableSize())
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
offset, n int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
n, err = proto.NestedStructureMarshal(bearerTokenBodyField, buf[offset:], bt.body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n
|
|
|
|
|
|
|
|
_, err = proto.NestedStructureMarshal(bearerTokenSignatureField, buf[offset:], bt.sig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bt *BearerToken) StableSize() (size int) {
|
|
|
|
if bt == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
size += proto.NestedStructureSize(bearerTokenBodyField, bt.body)
|
|
|
|
size += proto.NestedStructureSize(bearerTokenSignatureField, bt.sig)
|
|
|
|
|
|
|
|
return size
|
|
|
|
}
|