2021-10-27 10:00:15 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
2022-04-07 16:09:15 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
frostfscrypto "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2021-10-27 10:00:15 +00:00
|
|
|
)
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// Container represents token of the FrostFS Container session. A session is opened
|
2022-04-07 16:09:15 +00:00
|
|
|
// between any two sides of the system, and implements a mechanism for transferring
|
|
|
|
// the power of attorney of actions to another network member. The session has a
|
|
|
|
// limited validity period, and applies to a strictly defined set of operations.
|
|
|
|
// See methods for details.
|
2021-10-27 10:00:15 +00:00
|
|
|
//
|
2023-03-07 11:20:03 +00:00
|
|
|
// Container is mutually compatible with git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session.Token
|
2022-04-07 16:09:15 +00:00
|
|
|
// message. See ReadFromV2 / WriteToV2 methods.
|
2021-10-27 10:00:15 +00:00
|
|
|
//
|
2022-04-07 16:09:15 +00:00
|
|
|
// Instances can be created using built-in var declaration.
|
|
|
|
type Container struct {
|
2022-06-02 08:28:20 +00:00
|
|
|
commonData
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
verb ContainerVerb
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
cnrSet bool
|
|
|
|
cnr cid.ID
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
// readContext is a contextReader needed for commonData methods.
|
|
|
|
func (x *Container) readContext(c session.TokenContext, checkFieldPresence bool) error {
|
|
|
|
cCnr, ok := c.(*session.ContainerSessionContext)
|
|
|
|
if !ok || cCnr == nil {
|
|
|
|
return fmt.Errorf("invalid context %T", c)
|
2022-04-07 16:09:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
x.cnrSet = !cCnr.Wildcard()
|
|
|
|
cnr := cCnr.ContainerID()
|
|
|
|
|
|
|
|
if x.cnrSet {
|
|
|
|
if cnr != nil {
|
|
|
|
err := x.cnr.ReadFromV2(*cnr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid container ID: %w", err)
|
|
|
|
}
|
|
|
|
} else if checkFieldPresence {
|
|
|
|
return errors.New("missing container or wildcard flag")
|
|
|
|
}
|
|
|
|
} else if cnr != nil {
|
|
|
|
return errors.New("container conflicts with wildcard flag")
|
2022-04-07 16:09:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
x.verb = ContainerVerb(cCnr.Verb())
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
func (x *Container) readFromV2(m session.Token, checkFieldPresence bool) error {
|
|
|
|
return x.commonData.readFromV2(m, checkFieldPresence, x.readContext)
|
|
|
|
}
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
// ReadFromV2 reads Container from the session.Token message. Checks if the
|
2022-12-29 10:46:18 +00:00
|
|
|
// message conforms to FrostFS API V2 protocol.
|
2022-06-02 08:28:20 +00:00
|
|
|
//
|
|
|
|
// See also WriteToV2.
|
|
|
|
func (x *Container) ReadFromV2(m session.Token) error {
|
|
|
|
return x.readFromV2(m, true)
|
|
|
|
}
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
func (x Container) writeContext() session.TokenContext {
|
|
|
|
var c session.ContainerSessionContext
|
|
|
|
c.SetWildcard(!x.cnrSet)
|
|
|
|
c.SetVerb(session.ContainerSessionVerb(x.verb))
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
if x.cnrSet {
|
|
|
|
var cnr refs.ContainerID
|
|
|
|
x.cnr.WriteToV2(&cnr)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
c.SetContainerID(&cnr)
|
2022-04-07 16:09:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 08:28:20 +00:00
|
|
|
return &c
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
// WriteToV2 writes Container to the session.Token message.
|
|
|
|
// The message must not be nil.
|
|
|
|
//
|
|
|
|
// See also ReadFromV2.
|
|
|
|
func (x Container) WriteToV2(m *session.Token) {
|
2022-06-02 08:28:20 +00:00
|
|
|
x.writeToV2(m, x.writeContext)
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// Marshal encodes Container into a binary format of the FrostFS API protocol
|
2022-04-07 16:09:15 +00:00
|
|
|
// (Protocol Buffers with direct field order).
|
2021-10-27 10:00:15 +00:00
|
|
|
//
|
2022-04-07 16:09:15 +00:00
|
|
|
// See also Unmarshal.
|
|
|
|
func (x Container) Marshal() []byte {
|
2022-06-02 08:28:20 +00:00
|
|
|
return x.marshal(x.writeContext)
|
2022-04-07 16:09:15 +00:00
|
|
|
}
|
2022-04-11 16:25:14 +00:00
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// Unmarshal decodes FrostFS API protocol binary format into the Container
|
2022-04-07 16:09:15 +00:00
|
|
|
// (Protocol Buffers with direct field order). Returns an error describing
|
|
|
|
// a format violation.
|
|
|
|
//
|
|
|
|
// See also Marshal.
|
|
|
|
func (x *Container) Unmarshal(data []byte) error {
|
2022-06-02 08:28:20 +00:00
|
|
|
return x.unmarshal(data, x.readContext)
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// MarshalJSON encodes Container into a JSON format of the FrostFS API protocol
|
2022-04-07 16:09:15 +00:00
|
|
|
// (Protocol Buffers JSON).
|
|
|
|
//
|
|
|
|
// See also UnmarshalJSON.
|
|
|
|
func (x Container) MarshalJSON() ([]byte, error) {
|
2022-06-02 08:28:20 +00:00
|
|
|
return x.marshalJSON(x.writeContext)
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// UnmarshalJSON decodes FrostFS API protocol JSON format into the Container
|
2022-04-07 16:09:15 +00:00
|
|
|
// (Protocol Buffers JSON). Returns an error describing a format violation.
|
2021-10-27 10:00:15 +00:00
|
|
|
//
|
2022-04-07 16:09:15 +00:00
|
|
|
// See also MarshalJSON.
|
|
|
|
func (x *Container) UnmarshalJSON(data []byte) error {
|
2022-06-02 08:28:20 +00:00
|
|
|
return x.unmarshalJSON(data, x.readContext)
|
2022-04-07 16:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sign calculates and writes signature of the Container data.
|
|
|
|
// Returns signature calculation errors.
|
|
|
|
//
|
|
|
|
// Zero Container is unsigned.
|
|
|
|
//
|
|
|
|
// Note that any Container mutation is likely to break the signature, so it is
|
|
|
|
// expected to be calculated as a final stage of Container formation.
|
|
|
|
//
|
|
|
|
// See also VerifySignature.
|
|
|
|
func (x *Container) Sign(key ecdsa.PrivateKey) error {
|
2022-06-02 08:28:20 +00:00
|
|
|
return x.sign(key, x.writeContext)
|
2022-04-07 16:09:15 +00:00
|
|
|
}
|
2022-04-11 16:25:14 +00:00
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
// VerifySignature checks if Container signature is presented and valid.
|
|
|
|
//
|
|
|
|
// Zero Container fails the check.
|
|
|
|
//
|
|
|
|
// See also Sign.
|
|
|
|
func (x Container) VerifySignature() bool {
|
2022-06-02 08:28:20 +00:00
|
|
|
return x.verifySignature(x.writeContext)
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
// ApplyOnlyTo limits session scope to a given author container.
|
|
|
|
//
|
|
|
|
// See also AppliedTo.
|
|
|
|
func (x *Container) ApplyOnlyTo(cnr cid.ID) {
|
2022-06-02 08:28:20 +00:00
|
|
|
x.cnr = cnr
|
2022-04-07 16:09:15 +00:00
|
|
|
x.cnrSet = true
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 10:38:45 +00:00
|
|
|
// AppliedTo checks if the session is propagated to the given container.
|
2022-04-07 16:09:15 +00:00
|
|
|
//
|
|
|
|
// Zero Container is applied to all author's containers.
|
|
|
|
//
|
|
|
|
// See also ApplyOnlyTo.
|
|
|
|
func (x Container) AppliedTo(cnr cid.ID) bool {
|
2022-06-02 08:28:20 +00:00
|
|
|
return !x.cnrSet || x.cnr.Equals(cnr)
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
// ContainerVerb enumerates container operations.
|
|
|
|
type ContainerVerb int8
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ ContainerVerb = iota
|
|
|
|
|
|
|
|
VerbContainerPut // Put rpc
|
|
|
|
VerbContainerDelete // Delete rpc
|
|
|
|
VerbContainerSetEACL // SetExtendedACL rpc
|
|
|
|
)
|
|
|
|
|
|
|
|
// ForVerb specifies the container operation of the session scope. Each
|
|
|
|
// Container is related to the single operation.
|
|
|
|
//
|
|
|
|
// See also AssertVerb.
|
|
|
|
func (x *Container) ForVerb(verb ContainerVerb) {
|
2022-06-02 08:28:20 +00:00
|
|
|
x.verb = verb
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
// AssertVerb checks if Container relates to the given container operation.
|
|
|
|
//
|
|
|
|
// Zero Container relates to zero (unspecified) verb.
|
|
|
|
//
|
|
|
|
// See also ForVerb.
|
|
|
|
func (x Container) AssertVerb(verb ContainerVerb) bool {
|
2022-06-02 08:28:20 +00:00
|
|
|
return x.verb == verb
|
2022-05-23 16:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IssuedBy checks if Container session is issued by the given user.
|
|
|
|
//
|
|
|
|
// See also Container.Issuer.
|
|
|
|
func IssuedBy(cnr Container, id user.ID) bool {
|
|
|
|
return cnr.Issuer().Equals(id)
|
2022-05-04 14:49:55 +00:00
|
|
|
}
|
2022-06-07 17:25:29 +00:00
|
|
|
|
|
|
|
// VerifySessionDataSignature verifies signature of the session data. In practice,
|
|
|
|
// the method is used to authenticate an operation with session data.
|
|
|
|
func (x Container) VerifySessionDataSignature(data, signature []byte) bool {
|
|
|
|
var sigV2 refs.Signature
|
|
|
|
sigV2.SetKey(x.authKey)
|
|
|
|
sigV2.SetScheme(refs.ECDSA_RFC6979_SHA256)
|
|
|
|
sigV2.SetSign(signature)
|
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
var sig frostfscrypto.Signature
|
2022-06-07 17:25:29 +00:00
|
|
|
|
2022-07-07 10:51:05 +00:00
|
|
|
return sig.ReadFromV2(sigV2) == nil && sig.Verify(data)
|
2022-06-07 17:25:29 +00:00
|
|
|
}
|