More data delete endpoints (#363)

This commit is contained in:
Dominic Wong
2022-02-03 17:32:08 +00:00
committed by GitHub
parent ba77f7b2f4
commit 0663f196d5
9 changed files with 144 additions and 14 deletions

View File

@@ -2,12 +2,16 @@ package handler
import (
"context"
"strings"
"time"
"github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger"
pb "github.com/micro/services/otp/proto"
pauth "github.com/micro/services/pkg/auth"
"github.com/micro/services/pkg/cache"
adminpb "github.com/micro/services/pkg/service/proto"
"github.com/micro/services/pkg/tenant"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
@@ -121,3 +125,30 @@ func (e *Otp) Validate(ctx context.Context, req *pb.ValidateRequest, rsp *pb.Val
return nil
}
func (e *Otp) DeleteData(ctx context.Context, request *adminpb.DeleteDataRequest, response *adminpb.DeleteDataResponse) error {
method := "admin.DeleteData"
_, err := pauth.VerifyMicroAdmin(ctx, method)
if err != nil {
return err
}
if len(request.TenantId) < 10 { // deliberate length check so we don't delete all the things
return errors.BadRequest(method, "Missing tenant ID")
}
split := strings.Split(request.TenantId, "/")
tctx := tenant.NewContext(split[1], split[0], split[1])
keys, err := cache.Context(tctx).ListKeys()
if err != nil {
return err
}
for _, k := range keys {
if err := cache.Context(tctx).Delete(k); err != nil {
return err
}
}
logger.Infof("Deleted %d keys for %s", len(keys), request.TenantId)
return nil
}

View File

@@ -3,6 +3,7 @@ package main
import (
"github.com/micro/services/otp/handler"
pb "github.com/micro/services/otp/proto"
admin "github.com/micro/services/pkg/service/proto"
"github.com/micro/services/pkg/tracing"
"github.com/micro/micro/v3/service"
@@ -15,8 +16,10 @@ func main() {
service.Name("otp"),
)
h := new(handler.Otp)
// Register handler
pb.RegisterOtpHandler(srv.Server(), new(handler.Otp))
pb.RegisterOtpHandler(srv.Server(), h)
admin.RegisterAdminHandler(srv.Server(), h)
traceCloser := tracing.SetupOpentracing("otp")
defer traceCloser.Close()