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"
|
2023-11-16 07:58:55 +00:00
|
|
|
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
|
|
|
engine "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:
|
|
|
|
return engine.NamespaceTarget(chainTarget.GetName()), nil
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
|
|
|
ChainId: string(chain.ID),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-11-20 16:35:16 +00:00
|
|
|
if err = s.localOverrideStorage.LocalStorage().RemoveOverride(apechain.Ingress, target, apechain.ID(req.GetBody().GetChainId())); 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.RemoveChainLocalOverrideResponse{
|
|
|
|
Body: &control.RemoveChainLocalOverrideResponse_Body{
|
2023-11-16 07:58:55 +00:00
|
|
|
Removed: true,
|
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 {
|
|
|
|
if errors.Is(err, engine.ErrChainNotFound) {
|
|
|
|
return codes.NotFound
|
|
|
|
}
|
|
|
|
return codes.Internal
|
|
|
|
}
|