From 23127356efd42d1358dc087c66507463602e6e4b Mon Sep 17 00:00:00 2001 From: franzmueller Date: Tue, 26 Nov 2024 13:04:10 +0100 Subject: [PATCH] add permv2 migration --- lib/controller/controller.go | 26 ++++++++++++++++++++++++++ lib/controller/import_types.go | 5 ----- lib/lib.go | 6 ++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/controller/controller.go b/lib/controller/controller.go index 9858139..0692474 100644 --- a/lib/controller/controller.go +++ b/lib/controller/controller.go @@ -23,6 +23,7 @@ import ( deviceRepo "github.com/SENERGY-Platform/device-repository/lib/client" "github.com/SENERGY-Platform/import-repository/lib/config" "github.com/SENERGY-Platform/import-repository/lib/database" + "github.com/SENERGY-Platform/import-repository/lib/model" permV2 "github.com/SENERGY-Platform/permissions-v2/pkg/client" ) @@ -59,3 +60,28 @@ type Controller struct { func getTimeoutContext() (context.Context, context.CancelFunc) { return context.WithTimeout(context.Background(), 10*time.Second) } + +func (this *Controller) Migrate() error { + ctx, _ := getTimeoutContext() + importTypes, _, err := this.db.ListImportTypes(ctx, model.ImportTypeListOptions{}) + if err != nil { + return err + } + for _, importType := range importTypes { + resource, err, _ := this.permV2Client.GetResource(permV2.InternalAdminToken, PermV2Topic, importType.Id) + if err != nil { + return err + } + resource.UserPermissions[importType.Owner] = permV2.PermissionsMap{ + Read: true, + Write: true, + Execute: true, + Administrate: true, + } + _, err, _ = this.permV2Client.SetPermission(permV2.InternalAdminToken, PermV2Topic, importType.Id, resource.ResourcePermissions) + if err != nil { + return err + } + } + return nil +} diff --git a/lib/controller/import_types.go b/lib/controller/import_types.go index 2a6535d..37a5599 100644 --- a/lib/controller/import_types.go +++ b/lib/controller/import_types.go @@ -58,11 +58,6 @@ func (this *Controller) CreateImportType(importType model.ImportType, token jwt. } func (this *Controller) ReadImportType(id string, token jwt.Token) (result model.ImportType, err error, errCode int) { - err, code := this.CheckAccessToImportType(token, id, permV2Model.Read) - if err != nil { - result = model.ImportType{} - return result, err, code - } ctx, _ := getTimeoutContext() result, exists, err := this.db.GetImportType(ctx, id) if err != nil { diff --git a/lib/lib.go b/lib/lib.go index 277e385..a975be0 100644 --- a/lib/lib.go +++ b/lib/lib.go @@ -47,6 +47,12 @@ func StartWithPermv2Client(conf config.Config, ctx context.Context, wg *sync.Wai return err } + err = ctrl.Migrate() + if err != nil { + log.Println("ERROR: unable to migrate", err) + return err + } + _, err = consumer.NewConsumer(ctx, wg, conf.KafkaBootstrap, []string{conf.UsersTopic}, conf.GroupId, consumer.Earliest, listener.UsersListenerFactory(ctrl), consumer.HandleError, conf.Debug) if err != nil {