2024-04-24 17:23:15 +00:00
|
|
|
package apemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
ape_contract "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/ape/contract_storage"
|
|
|
|
containercore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
|
|
|
|
apemanager_errors "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/apemanager/errors"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
2024-11-07 14:32:10 +00:00
|
|
|
apeV2 "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/ape"
|
|
|
|
apemanagerV2 "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/apemanager"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
|
|
|
session "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/session"
|
2024-04-24 17:23:15 +00:00
|
|
|
cidSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
|
|
|
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
|
|
|
policy_engine "git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
|
|
|
"github.com/mr-tron/base58/base58"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2024-05-27 12:57:02 +00:00
|
|
|
var errEmptyBodySignature = errors.New("malformed request: empty body signature")
|
2024-04-24 17:23:15 +00:00
|
|
|
|
|
|
|
type cfg struct {
|
|
|
|
log *logger.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
type Service struct {
|
|
|
|
cfg
|
|
|
|
|
|
|
|
cnrSrc containercore.Source
|
|
|
|
|
|
|
|
contractStorage ape_contract.ProxyAdaptedContractStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*cfg)
|
|
|
|
|
|
|
|
func New(cnrSrc containercore.Source, contractStorage ape_contract.ProxyAdaptedContractStorage, opts ...Option) *Service {
|
|
|
|
s := &Service{
|
|
|
|
cnrSrc: cnrSrc,
|
|
|
|
|
|
|
|
contractStorage: contractStorage,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](&s.cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.log == nil {
|
2024-10-21 07:22:54 +00:00
|
|
|
s.log = logger.NewLoggerWrapper(zap.NewNop())
|
2024-04-24 17:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithLogger(log *logger.Logger) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.log = log
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Server = (*Service)(nil)
|
|
|
|
|
|
|
|
// validateContainerTargetRequest validates request for the container target.
|
|
|
|
// It checks if request actor is the owner of the container, otherwise it denies the request.
|
|
|
|
func (s *Service) validateContainerTargetRequest(cid string, pubKey *keys.PublicKey) error {
|
|
|
|
var cidSDK cidSDK.ID
|
|
|
|
if err := cidSDK.DecodeString(cid); err != nil {
|
|
|
|
return fmt.Errorf("invalid CID format: %w", err)
|
|
|
|
}
|
|
|
|
isOwner, err := s.isActorContainerOwner(cidSDK, pubKey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to check owner: %w", err)
|
|
|
|
}
|
|
|
|
if !isOwner {
|
|
|
|
return apemanager_errors.ErrAPEManagerAccessDenied("actor must be container owner")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-28 13:12:14 +00:00
|
|
|
func (s *Service) AddChain(_ context.Context, req *apemanagerV2.AddChainRequest) (*apemanagerV2.AddChainResponse, error) {
|
2024-04-24 17:23:15 +00:00
|
|
|
pub, err := getSignaturePublicKey(req.GetVerificationHeader())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-28 13:12:14 +00:00
|
|
|
chain, err := decodeAndValidateChain(req.GetBody().GetChain().GetKind().(*apeV2.ChainRaw).GetRaw())
|
2024-04-24 17:23:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(chain.ID) == 0 {
|
|
|
|
const randomIDLength = 10
|
|
|
|
randID, err := base58Str(randomIDLength)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("randomize chain ID error: %w", err)
|
|
|
|
}
|
|
|
|
chain.ID = apechain.ID(randID)
|
|
|
|
}
|
|
|
|
|
|
|
|
var target policy_engine.Target
|
|
|
|
|
|
|
|
switch targetType := req.GetBody().GetTarget().GetTargetType(); targetType {
|
2024-05-28 13:12:14 +00:00
|
|
|
case apeV2.TargetTypeContainer:
|
2024-04-24 17:23:15 +00:00
|
|
|
reqCID := req.GetBody().GetTarget().GetName()
|
|
|
|
if err = s.validateContainerTargetRequest(reqCID, pub); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
target = policy_engine.ContainerTarget(reqCID)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported target type: %s", targetType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, _, err = s.contractStorage.AddMorphRuleChain(apechain.Ingress, target, &chain); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-28 13:12:14 +00:00
|
|
|
body := new(apemanagerV2.AddChainResponseBody)
|
2024-04-24 17:23:15 +00:00
|
|
|
body.SetChainID(chain.ID)
|
|
|
|
|
2024-05-28 13:12:14 +00:00
|
|
|
resp := new(apemanagerV2.AddChainResponse)
|
2024-04-24 17:23:15 +00:00
|
|
|
resp.SetBody(body)
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2024-05-28 13:12:14 +00:00
|
|
|
func (s *Service) RemoveChain(_ context.Context, req *apemanagerV2.RemoveChainRequest) (*apemanagerV2.RemoveChainResponse, error) {
|
2024-04-24 17:23:15 +00:00
|
|
|
pub, err := getSignaturePublicKey(req.GetVerificationHeader())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var target policy_engine.Target
|
|
|
|
|
|
|
|
switch targetType := req.GetBody().GetTarget().GetTargetType(); targetType {
|
2024-05-28 13:12:14 +00:00
|
|
|
case apeV2.TargetTypeContainer:
|
2024-04-24 17:23:15 +00:00
|
|
|
reqCID := req.GetBody().GetTarget().GetName()
|
|
|
|
if err = s.validateContainerTargetRequest(reqCID, pub); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
target = policy_engine.ContainerTarget(reqCID)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported target type: %s", targetType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, _, err = s.contractStorage.RemoveMorphRuleChain(apechain.Ingress, target, req.GetBody().GetChainID()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-28 13:12:14 +00:00
|
|
|
body := new(apemanagerV2.RemoveChainResponseBody)
|
2024-04-24 17:23:15 +00:00
|
|
|
|
2024-05-28 13:12:14 +00:00
|
|
|
resp := new(apemanagerV2.RemoveChainResponse)
|
2024-04-24 17:23:15 +00:00
|
|
|
resp.SetBody(body)
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2024-05-28 13:12:14 +00:00
|
|
|
func (s *Service) ListChains(_ context.Context, req *apemanagerV2.ListChainsRequest) (*apemanagerV2.ListChainsResponse, error) {
|
2024-04-24 17:23:15 +00:00
|
|
|
pub, err := getSignaturePublicKey(req.GetVerificationHeader())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var target policy_engine.Target
|
|
|
|
|
|
|
|
switch targetType := req.GetBody().GetTarget().GetTargetType(); targetType {
|
2024-05-28 13:12:14 +00:00
|
|
|
case apeV2.TargetTypeContainer:
|
2024-04-24 17:23:15 +00:00
|
|
|
reqCID := req.GetBody().GetTarget().GetName()
|
|
|
|
if err = s.validateContainerTargetRequest(reqCID, pub); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
target = policy_engine.ContainerTarget(reqCID)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported target type: %s", targetType)
|
|
|
|
}
|
|
|
|
|
|
|
|
chs, err := s.contractStorage.ListMorphRuleChains(apechain.Ingress, target)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-28 13:12:14 +00:00
|
|
|
res := make([]*apeV2.Chain, 0, len(chs))
|
2024-04-24 17:23:15 +00:00
|
|
|
for _, ch := range chs {
|
2024-05-28 13:12:14 +00:00
|
|
|
v2chraw := new(apeV2.ChainRaw)
|
2024-04-24 17:23:15 +00:00
|
|
|
v2chraw.SetRaw(ch.Bytes())
|
|
|
|
|
2024-05-28 13:12:14 +00:00
|
|
|
v2ch := new(apeV2.Chain)
|
2024-04-24 17:23:15 +00:00
|
|
|
v2ch.SetKind(v2chraw)
|
|
|
|
|
|
|
|
res = append(res, v2ch)
|
|
|
|
}
|
|
|
|
|
2024-05-28 13:12:14 +00:00
|
|
|
body := new(apemanagerV2.ListChainsResponseBody)
|
2024-04-24 17:23:15 +00:00
|
|
|
body.SetChains(res)
|
|
|
|
|
2024-05-28 13:12:14 +00:00
|
|
|
resp := new(apemanagerV2.ListChainsResponse)
|
2024-04-24 17:23:15 +00:00
|
|
|
resp.SetBody(body)
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSignaturePublicKey(vh *session.RequestVerificationHeader) (*keys.PublicKey, error) {
|
|
|
|
for vh.GetOrigin() != nil {
|
|
|
|
vh = vh.GetOrigin()
|
|
|
|
}
|
|
|
|
sig := vh.GetBodySignature()
|
|
|
|
if sig == nil {
|
|
|
|
return nil, errEmptyBodySignature
|
|
|
|
}
|
|
|
|
key, err := keys.NewPublicKeyFromBytes(sig.GetKey(), elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid signature key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) isActorContainerOwner(cid cidSDK.ID, pk *keys.PublicKey) (bool, error) {
|
|
|
|
var actor user.ID
|
|
|
|
user.IDFromKey(&actor, (ecdsa.PublicKey)(*pk))
|
|
|
|
actorOwnerID := new(refs.OwnerID)
|
|
|
|
actor.WriteToV2(actorOwnerID)
|
|
|
|
|
|
|
|
cnr, err := s.cnrSrc.Get(cid)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("get container error: %w", err)
|
|
|
|
}
|
|
|
|
return cnr.Value.Owner().Equals(actor), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// base58Str generates base58 string.
|
|
|
|
func base58Str(n int) (string, error) {
|
|
|
|
b := make([]byte, n)
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return base58.FastBase58Encoding(b), nil
|
|
|
|
}
|