2020-09-10 14:51:58 +00:00
|
|
|
package eacl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
2021-05-24 16:42:36 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
2020-09-10 14:51:58 +00:00
|
|
|
v2acl "github.com/nspcc-dev/neofs-api-go/v2/acl"
|
|
|
|
)
|
|
|
|
|
2020-11-16 14:24:06 +00:00
|
|
|
// Table is a group of EACL records for single container.
|
|
|
|
//
|
|
|
|
// Table is compatible with v2 acl.EACLTable message.
|
|
|
|
type Table struct {
|
|
|
|
version pkg.Version
|
2021-05-24 16:42:36 +00:00
|
|
|
cid *cid.ID
|
2020-11-16 14:24:06 +00:00
|
|
|
records []*Record
|
|
|
|
}
|
2020-09-10 14:51:58 +00:00
|
|
|
|
2020-11-16 14:24:06 +00:00
|
|
|
// CID returns identifier of the container that should use given access control rules.
|
2021-05-24 16:42:36 +00:00
|
|
|
func (t Table) CID() *cid.ID {
|
2020-09-10 14:51:58 +00:00
|
|
|
return t.cid
|
|
|
|
}
|
|
|
|
|
2020-11-16 14:24:06 +00:00
|
|
|
// SetCID sets identifier of the container that should use given access control rules.
|
2021-05-24 16:42:36 +00:00
|
|
|
func (t *Table) SetCID(cid *cid.ID) {
|
2020-09-10 14:51:58 +00:00
|
|
|
t.cid = cid
|
|
|
|
}
|
|
|
|
|
2020-11-16 14:24:06 +00:00
|
|
|
// Version returns version of eACL format.
|
2020-09-10 14:51:58 +00:00
|
|
|
func (t Table) Version() pkg.Version {
|
|
|
|
return t.version
|
|
|
|
}
|
|
|
|
|
2020-11-16 14:24:06 +00:00
|
|
|
// SetVersion sets version of eACL format.
|
2020-09-10 14:51:58 +00:00
|
|
|
func (t *Table) SetVersion(version pkg.Version) {
|
|
|
|
t.version = version
|
|
|
|
}
|
|
|
|
|
2020-11-16 14:24:06 +00:00
|
|
|
// Records returns list of extended ACL rules.
|
|
|
|
func (t Table) Records() []*Record {
|
2020-09-10 14:51:58 +00:00
|
|
|
return t.records
|
|
|
|
}
|
|
|
|
|
2020-11-16 14:24:06 +00:00
|
|
|
// AddRecord adds single eACL rule.
|
2020-09-10 14:51:58 +00:00
|
|
|
func (t *Table) AddRecord(r *Record) {
|
|
|
|
if r != nil {
|
2020-11-16 14:24:06 +00:00
|
|
|
t.records = append(t.records, r)
|
2020-09-10 14:51:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 14:24:06 +00:00
|
|
|
// ToV2 converts Table to v2 acl.EACLTable message.
|
2020-09-10 14:51:58 +00:00
|
|
|
func (t *Table) ToV2() *v2acl.Table {
|
|
|
|
v2 := new(v2acl.Table)
|
|
|
|
|
|
|
|
if t.cid != nil {
|
|
|
|
v2.SetContainerID(t.cid.ToV2())
|
|
|
|
}
|
|
|
|
|
|
|
|
records := make([]*v2acl.Record, 0, len(t.records))
|
|
|
|
for _, record := range t.records {
|
|
|
|
records = append(records, record.ToV2())
|
|
|
|
}
|
|
|
|
|
|
|
|
v2.SetVersion(t.version.ToV2())
|
|
|
|
v2.SetRecords(records)
|
|
|
|
|
|
|
|
return v2
|
|
|
|
}
|
|
|
|
|
2020-11-16 14:24:06 +00:00
|
|
|
// NewTable creates, initializes and returns blank Table instance.
|
2020-09-10 14:51:58 +00:00
|
|
|
func NewTable() *Table {
|
|
|
|
t := new(Table)
|
|
|
|
t.SetVersion(*pkg.SDKVersion())
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2020-11-16 14:24:06 +00:00
|
|
|
// CreateTable creates, initializes with parameters and returns Table instance.
|
2021-05-24 16:42:36 +00:00
|
|
|
func CreateTable(cid cid.ID) *Table {
|
2020-09-10 14:51:58 +00:00
|
|
|
t := NewTable()
|
|
|
|
t.SetCID(&cid)
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2020-11-16 14:24:06 +00:00
|
|
|
// NewTableFromV2 converts v2 acl.EACLTable message to Table.
|
2020-09-10 14:51:58 +00:00
|
|
|
func NewTableFromV2(table *v2acl.Table) *Table {
|
|
|
|
t := new(Table)
|
|
|
|
|
|
|
|
if table == nil {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// set version
|
|
|
|
if v := table.GetVersion(); v != nil {
|
|
|
|
version := pkg.Version{}
|
|
|
|
version.SetMajor(v.GetMajor())
|
|
|
|
version.SetMinor(v.GetMinor())
|
|
|
|
|
|
|
|
t.SetVersion(version)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set container id
|
2021-05-24 16:42:36 +00:00
|
|
|
if id := table.GetContainerID(); id != nil {
|
2020-09-10 14:51:58 +00:00
|
|
|
if t.cid == nil {
|
2021-05-24 16:42:36 +00:00
|
|
|
t.cid = new(cid.ID)
|
2020-09-10 14:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var h [sha256.Size]byte
|
2020-11-16 14:24:06 +00:00
|
|
|
|
2021-05-24 16:42:36 +00:00
|
|
|
copy(h[:], id.GetValue())
|
2020-09-10 14:51:58 +00:00
|
|
|
t.cid.SetSHA256(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set eacl records
|
|
|
|
v2records := table.GetRecords()
|
2020-11-16 14:24:06 +00:00
|
|
|
t.records = make([]*Record, 0, len(v2records))
|
|
|
|
|
2020-09-10 14:51:58 +00:00
|
|
|
for i := range v2records {
|
2020-11-16 14:24:06 +00:00
|
|
|
t.records = append(t.records, NewRecordFromV2(v2records[i]))
|
2020-09-10 14:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
2020-11-13 11:55:45 +00:00
|
|
|
|
|
|
|
// Marshal marshals Table into a protobuf binary form.
|
|
|
|
//
|
|
|
|
// Buffer is allocated when the argument is empty.
|
|
|
|
// Otherwise, the first buffer is used.
|
|
|
|
func (t *Table) Marshal(b ...[]byte) ([]byte, error) {
|
|
|
|
var buf []byte
|
|
|
|
if len(b) > 0 {
|
|
|
|
buf = b[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.ToV2().
|
|
|
|
StableMarshal(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals protobuf binary representation of Table.
|
|
|
|
func (t *Table) Unmarshal(data []byte) error {
|
|
|
|
fV2 := new(v2acl.Table)
|
|
|
|
if err := fV2.Unmarshal(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*t = *NewTableFromV2(fV2)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes Table to protobuf JSON format.
|
|
|
|
func (t *Table) MarshalJSON() ([]byte, error) {
|
|
|
|
return t.ToV2().
|
|
|
|
MarshalJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes Table from protobuf JSON format.
|
|
|
|
func (t *Table) UnmarshalJSON(data []byte) error {
|
|
|
|
tV2 := new(v2acl.Table)
|
|
|
|
if err := tV2.UnmarshalJSON(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*t = *NewTableFromV2(tV2)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|