2023-10-31 15:36:42 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-11-16 07:58:55 +00:00
|
|
|
"errors"
|
2023-10-31 15:36:42 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control"
|
2024-01-30 14:07:05 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control/server/ape"
|
2023-11-16 07:58:55 +00:00
|
|
|
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
2023-12-21 12:05:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
2023-10-31 08:55:42 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2023-10-31 15:36:42 +00:00
|
|
|
)
|
|
|
|
|
2023-11-20 16:35:16 +00:00
|
|
|
func apeTarget(chainTarget *control.ChainTarget) (engine.Target, error) {
|
|
|
|
switch chainTarget.GetType() {
|
|
|
|
case control.ChainTarget_CONTAINER:
|
|
|
|
return engine.ContainerTarget(chainTarget.GetName()), nil
|
|
|
|
case control.ChainTarget_NAMESPACE:
|
2024-01-12 14:56:35 +00:00
|
|
|
namespace := chainTarget.GetName()
|
|
|
|
// If namespace is empty, we take it for root namespace.
|
|
|
|
if namespace == "" {
|
|
|
|
namespace = "root"
|
|
|
|
}
|
|
|
|
return engine.NamespaceTarget(namespace), nil
|
2023-11-20 16:35:16 +00:00
|
|
|
default:
|
2023-12-05 09:11:28 +00:00
|
|
|
}
|
2023-11-20 16:35:16 +00:00
|
|
|
return engine.Target{}, status.Error(codes.InvalidArgument,
|
|
|
|
fmt.Errorf("target type is not supported: %s", chainTarget.GetType().String()).Error())
|
2023-12-05 09:11:28 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 08:55:42 +00:00
|
|
|
func (s *Server) AddChainLocalOverride(_ context.Context, req *control.AddChainLocalOverrideRequest) (*control.AddChainLocalOverrideResponse, error) {
|
|
|
|
if err := s.isValidRequest(req); err != nil {
|
|
|
|
return nil, status.Error(codes.PermissionDenied, err.Error())
|
|
|
|
}
|
|
|
|
|
2023-11-16 07:58:55 +00:00
|
|
|
var chain apechain.Chain
|
2023-11-20 16:35:16 +00:00
|
|
|
if err := chain.DecodeBytes(req.GetBody().GetChain()); err != nil {
|
2023-10-31 08:55:42 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
2024-01-30 14:07:05 +00:00
|
|
|
for _, rule := range chain.Rules {
|
|
|
|
for _, name := range rule.Resources.Names {
|
|
|
|
if err := ape.ValidateResourceName(name); err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, fmt.Errorf("invalid resource: %w", err).Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-31 08:55:42 +00:00
|
|
|
|
|
|
|
s.apeChainCounter.Add(1)
|
|
|
|
// TODO (aarifullin): the such chain id is not well-designed yet.
|
2023-12-05 09:11:28 +00:00
|
|
|
if chain.ID == "" {
|
|
|
|
chain.ID = apechain.ID(fmt.Sprintf("%s:%d", apechain.Ingress, s.apeChainCounter.Load()))
|
|
|
|
}
|
2023-10-31 08:55:42 +00:00
|
|
|
|
2023-11-20 16:35:16 +00:00
|
|
|
target, err := apeTarget(req.GetBody().GetTarget())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = s.localOverrideStorage.LocalStorage().AddOverride(apechain.Ingress, target, &chain); err != nil {
|
2023-11-16 07:58:55 +00:00
|
|
|
return nil, status.Error(getCodeByLocalStorageErr(err), err.Error())
|
|
|
|
}
|
2023-10-31 08:55:42 +00:00
|
|
|
|
|
|
|
resp := &control.AddChainLocalOverrideResponse{
|
|
|
|
Body: &control.AddChainLocalOverrideResponse_Body{
|
2023-12-21 12:05:45 +00:00
|
|
|
ChainId: []byte(chain.ID),
|
2023-10-31 08:55:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
err = SignMessage(s.key, resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
return resp, nil
|
2023-10-31 15:36:42 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 08:55:42 +00:00
|
|
|
func (s *Server) GetChainLocalOverride(_ context.Context, req *control.GetChainLocalOverrideRequest) (*control.GetChainLocalOverrideResponse, error) {
|
|
|
|
if err := s.isValidRequest(req); err != nil {
|
|
|
|
return nil, status.Error(codes.PermissionDenied, err.Error())
|
|
|
|
}
|
|
|
|
|
2023-11-20 16:35:16 +00:00
|
|
|
target, err := apeTarget(req.GetBody().GetTarget())
|
2023-10-31 08:55:42 +00:00
|
|
|
if err != nil {
|
2023-11-20 16:35:16 +00:00
|
|
|
return nil, err
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
2023-11-20 16:35:16 +00:00
|
|
|
chain, err := s.localOverrideStorage.LocalStorage().GetOverride(apechain.Ingress, target, apechain.ID(req.GetBody().GetChainId()))
|
2023-11-16 07:58:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(getCodeByLocalStorageErr(err), err.Error())
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp := &control.GetChainLocalOverrideResponse{
|
|
|
|
Body: &control.GetChainLocalOverrideResponse_Body{
|
|
|
|
Chain: chain.Bytes(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = SignMessage(s.key, resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
return resp, nil
|
2023-10-31 15:36:42 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 08:55:42 +00:00
|
|
|
func (s *Server) ListChainLocalOverrides(_ context.Context, req *control.ListChainLocalOverridesRequest) (*control.ListChainLocalOverridesResponse, error) {
|
|
|
|
if err := s.isValidRequest(req); err != nil {
|
|
|
|
return nil, status.Error(codes.PermissionDenied, err.Error())
|
|
|
|
}
|
|
|
|
|
2023-11-20 16:35:16 +00:00
|
|
|
target, err := apeTarget(req.GetBody().GetTarget())
|
2023-10-31 08:55:42 +00:00
|
|
|
if err != nil {
|
2023-11-20 16:35:16 +00:00
|
|
|
return nil, err
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 16:35:16 +00:00
|
|
|
chains, err := s.localOverrideStorage.LocalStorage().ListOverrides(apechain.Ingress, target)
|
2023-11-16 07:58:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(getCodeByLocalStorageErr(err), err.Error())
|
|
|
|
}
|
2023-10-31 08:55:42 +00:00
|
|
|
serializedChains := make([][]byte, 0, len(chains))
|
|
|
|
for _, chain := range chains {
|
|
|
|
serializedChains = append(serializedChains, chain.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &control.ListChainLocalOverridesResponse{
|
|
|
|
Body: &control.ListChainLocalOverridesResponse_Body{
|
|
|
|
Chains: serializedChains,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = SignMessage(s.key, resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
return resp, nil
|
2023-10-31 15:36:42 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 08:55:42 +00:00
|
|
|
func (s *Server) RemoveChainLocalOverride(_ context.Context, req *control.RemoveChainLocalOverrideRequest) (*control.RemoveChainLocalOverrideResponse, error) {
|
|
|
|
if err := s.isValidRequest(req); err != nil {
|
|
|
|
return nil, status.Error(codes.PermissionDenied, err.Error())
|
|
|
|
}
|
|
|
|
|
2023-11-20 16:35:16 +00:00
|
|
|
target, err := apeTarget(req.GetBody().GetTarget())
|
2023-10-31 08:55:42 +00:00
|
|
|
if err != nil {
|
2023-11-20 16:35:16 +00:00
|
|
|
return nil, err
|
2023-10-31 08:55:42 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:51:29 +00:00
|
|
|
removed := true
|
2023-11-20 16:35:16 +00:00
|
|
|
if err = s.localOverrideStorage.LocalStorage().RemoveOverride(apechain.Ingress, target, apechain.ID(req.GetBody().GetChainId())); err != nil {
|
2024-01-11 08:51:29 +00:00
|
|
|
code := getCodeByLocalStorageErr(err)
|
|
|
|
if code == codes.NotFound {
|
|
|
|
removed = false
|
|
|
|
} else {
|
|
|
|
return nil, status.Error(code, err.Error())
|
|
|
|
}
|
2023-11-16 07:58:55 +00:00
|
|
|
}
|
2023-10-31 08:55:42 +00:00
|
|
|
resp := &control.RemoveChainLocalOverrideResponse{
|
|
|
|
Body: &control.RemoveChainLocalOverrideResponse_Body{
|
2024-01-11 08:51:29 +00:00
|
|
|
Removed: removed,
|
2023-10-31 08:55:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
err = SignMessage(s.key, resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
return resp, nil
|
2023-10-31 15:36:42 +00:00
|
|
|
}
|
2023-11-16 07:58:55 +00:00
|
|
|
|
|
|
|
func getCodeByLocalStorageErr(err error) codes.Code {
|
2024-01-11 08:51:29 +00:00
|
|
|
if errors.Is(err, engine.ErrChainNotFound) || errors.Is(err, engine.ErrChainNameNotFound) {
|
2023-11-16 07:58:55 +00:00
|
|
|
return codes.NotFound
|
|
|
|
}
|
|
|
|
return codes.Internal
|
|
|
|
}
|