Skip to content

Commit

Permalink
feat(policy): service stubs and registration for unsafe service (#1009)
Browse files Browse the repository at this point in the history
PR Number 2 for #115
  • Loading branch information
jakedoublev authored Jun 20, 2024
1 parent c298e36 commit 5667895
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 1 deletion.
2 changes: 1 addition & 1 deletion service/internal/auth/casbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ p, role:org-admin, /subject-mappings*, *, allow
p, role:org-admin, /resource-mappings*, *, allow
p, role:org-admin, /key-access-servers*, *, allow
p, role:org-admin, /kas/v2/rewrap, *, allow
# add unsafe actions to the org-admin role
p, role:org-admin, /unsafe*, *, allow
# Role: Admin
## gRPC routes
Expand Down
2 changes: 2 additions & 0 deletions service/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/opentdf/platform/service/policy/namespaces"
"github.com/opentdf/platform/service/policy/resourcemapping"
"github.com/opentdf/platform/service/policy/subjectmapping"
// "github.com/opentdf/platform/service/policy/unsafe"
)

var Migrations *embed.FS
Expand All @@ -32,6 +33,7 @@ func NewRegistrations() []serviceregistry.Registration {
resourcemapping.NewRegistration(),
subjectmapping.NewRegistration(),
kasregistry.NewRegistration(),
// unsafe.NewRegistration(),
} {
r.Namespace = namespace
r.DB = dbRegister
Expand Down
207 changes: 207 additions & 0 deletions service/policy/unsafe/unsafe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package unsafe

import (
"context"
"fmt"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/opentdf/platform/protocol/go/policy"
"github.com/opentdf/platform/protocol/go/policy/unsafe"
"github.com/opentdf/platform/service/internal/logger"
"github.com/opentdf/platform/service/pkg/serviceregistry"
policydb "github.com/opentdf/platform/service/policy/db"
)

type UnsafeService struct { //nolint:revive // UnsafeService is a valid name for this struct
unsafe.UnimplementedUnsafeServiceServer
dbClient policydb.PolicyDBClient
logger *logger.Logger
}

func NewRegistration() serviceregistry.Registration {
return serviceregistry.Registration{
ServiceDesc: &unsafe.UnsafeService_ServiceDesc,
RegisterFunc: func(srp serviceregistry.RegistrationParams) (any, serviceregistry.HandlerServer) {
return &UnsafeService{dbClient: policydb.NewClient(srp.DBClient), logger: srp.Logger}, func(ctx context.Context, mux *runtime.ServeMux, server any) error {
if srv, ok := server.(unsafe.UnsafeServiceServer); ok {
return unsafe.RegisterUnsafeServiceHandlerServer(ctx, mux, srv)
}
return fmt.Errorf("failed to assert server as unsafe.UnsafeServiceServer")
}
},
}
}

//
// Unsafe Namespace RPCs
//

func (s *UnsafeService) UpdateNamespace(_ context.Context, req *unsafe.UpdateNamespaceRequest) (*unsafe.UpdateNamespaceResponse, error) {
// _, err := s.dbClient.GetNamespace(ctx, req.GetId())
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", req.GetId()))
// }

// item, err := s.dbClient.UnsafeUpdateNamespace(ctx, req.GetId(), req)
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextUpdateFailed, slog.String("id", req.GetId()), slog.String("namespace", req.String()))
// }

return &unsafe.UpdateNamespaceResponse{
Namespace: &policy.Namespace{
Id: req.GetId(), // stubbed
},
}, nil
}

func (s *UnsafeService) ReactivateNamespace(_ context.Context, req *unsafe.ReactivateNamespaceRequest) (*unsafe.ReactivateNamespaceResponse, error) {
// _, err := s.dbClient.GetNamespace(ctx, req.GetId())
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", req.GetId()))
// }

// item, err := s.dbClient.UnsafeReactivateNamespace(ctx, req.GetId())
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextUpdateFailed, slog.String("id", req.GetId()))
// }

return &unsafe.ReactivateNamespaceResponse{
Namespace: &policy.Namespace{
Id: req.GetId(), // stubbed
},
}, nil
}

func (s *UnsafeService) DeleteNamespace(_ context.Context, req *unsafe.DeleteNamespaceRequest) (*unsafe.DeleteNamespaceResponse, error) {
// _, err := s.dbClient.GetNamespace(ctx, req.GetId())
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", req.GetId()))
// }

// err = s.dbClient.UnsafeDeleteNamespace(ctx, req.GetId())
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextDeleteFailed, slog.String("id", req.GetId()))
// }

return &unsafe.DeleteNamespaceResponse{
Namespace: &policy.Namespace{
Id: req.GetId(), // stubbed
},
}, nil
}

//
// Unsafe Attribute Definition RPCs
//

func (s *UnsafeService) UpdateAttribute(_ context.Context, req *unsafe.UpdateAttributeRequest) (*unsafe.UpdateAttributeResponse, error) {
// _, err := s.dbClient.GetAttribute(ctx, req.GetId())
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", req.GetId()))
// }

// item, err := s.dbClient.UnsafeUpdateAttribute(ctx, req.GetId(), req)
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextUpdateFailed, slog.String("id", req.GetId()), slog.String("attribute", req.String()))
// }

return &unsafe.UpdateAttributeResponse{
Attribute: &policy.Attribute{
Id: req.GetId(), // stubbed
},
}, nil
}

func (s *UnsafeService) ReactivateAttribute(_ context.Context, req *unsafe.ReactivateAttributeRequest) (*unsafe.ReactivateAttributeResponse, error) {
// _, err := s.dbClient.GetAttribute(ctx, req.GetId())
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", req.GetId()))
// }

// item, err := s.dbClient.UnsafeReactivateAttribute(ctx, req.GetId())
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextUpdateFailed, slog.String("id", req.GetId()))
// }

return &unsafe.ReactivateAttributeResponse{
Attribute: &policy.Attribute{
Id: req.GetId(), // stubbed
},
}, nil
}

func (s *UnsafeService) DeleteAttribute(_ context.Context, req *unsafe.DeleteAttributeRequest) (*unsafe.DeleteAttributeResponse, error) {
// _, err := s.dbClient.GetAttribute(ctx, req.GetId())
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", req.GetId()))
// }

// err = s.dbClient.UnsafeDeleteAttribute(ctx, req.GetId())
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextDeleteFailed, slog.String("id", req.GetId()))
// }

return &unsafe.DeleteAttributeResponse{
Attribute: &policy.Attribute{
Id: req.GetId(), // stubbed
},
}, nil
}

//
// Unsafe Attribute Value RPCs
//

func (s *UnsafeService) UpdateAttributeValue(_ context.Context, req *unsafe.UpdateAttributeValueRequest) (*unsafe.UpdateAttributeValueResponse, error) {
// _, err := s.dbClient.GetAttributeValue(ctx, req.GetId())
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", req.GetId()))
// }

// item, err := s.dbClient.UnsafeUpdateAttributeValue(ctx, req.GetId(), req)
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextUpdateFailed, slog.String("id", req.GetId()), slog.String("attribute_value", req.String()))
// }

return &unsafe.UpdateAttributeValueResponse{
Value: &policy.Value{
Id: req.GetId(), // stubbed
},
}, nil
}

func (s *UnsafeService) ReactivateAttributeValue(_ context.Context, req *unsafe.ReactivateAttributeValueRequest) (*unsafe.ReactivateAttributeValueResponse, error) {
// _, err := s.dbClient.GetAttributeValue(ctx, req.GetId())
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", req.GetId()))
// }

// item, err := s.dbClient.UnsafeReactivateAttributeValue(ctx, req.GetId())
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextUpdateFailed, slog.String("id", req.GetId()))
// }

return &unsafe.ReactivateAttributeValueResponse{
Value: &policy.Value{
Id: req.GetId(), // stubbed
},
}, nil
}

func (s *UnsafeService) DeleteAttributeValue(_ context.Context, req *unsafe.DeleteAttributeValueRequest) (*unsafe.DeleteAttributeValueResponse, error) {
// _, err := s.dbClient.GetAttributeValue(ctx, req.GetId())
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextGetRetrievalFailed, slog.String("id", req.GetId()))
// }

// err = s.dbClient.UnsafeDeleteAttributeValue(ctx, req.GetId())
// if err != nil {
// return nil, db.StatusifyError(err, db.ErrTextDeleteFailed, slog.String("id", req.GetId()))
// }

return &unsafe.DeleteAttributeValueResponse{
Value: &policy.Value{
Id: req.GetId(), // stubbed
},
}, nil
}

0 comments on commit 5667895

Please sign in to comment.