From 656be50d1160d2050d0549c5e843376c2f922527 Mon Sep 17 00:00:00 2001 From: Alexander Trost Date: Sun, 19 Jan 2025 20:08:00 +0100 Subject: [PATCH] fix: issues with dbsync grpc transport Signed-off-by: Alexander Trost --- app/components/citizens/CitizensList.vue | 2 +- dbsync.example.yaml | 1 + pkg/dbsync/config.go | 5 +++-- pkg/dbsync/sync.go | 11 ++++++++++- pkg/dbsync/sync_jobs.go | 5 ++++- pkg/dbsync/sync_licenses.go | 5 ++++- pkg/dbsync/sync_users.go | 5 ++++- pkg/dbsync/sync_vehicles.go | 5 ++++- services/sync/data.go | 12 +++++++----- 9 files changed, 38 insertions(+), 13 deletions(-) diff --git a/app/components/citizens/CitizensList.vue b/app/components/citizens/CitizensList.vue index e4d383783..4215f7e2f 100644 --- a/app/components/citizens/CitizensList.vue +++ b/app/components/citizens/CitizensList.vue @@ -202,7 +202,7 @@ defineShortcuts({ v-maska type="text" name="dateofbirth" - data-maska="##.##.####" + data-maska="##[./]##[./]####" :placeholder="`${$t('common.date_of_birth')} (DD.MM.YYYY)`" block /> diff --git a/dbsync.example.yaml b/dbsync.example.yaml index 4d9f3df44..83ddf9234 100644 --- a/dbsync.example.yaml +++ b/dbsync.example.yaml @@ -6,6 +6,7 @@ destination: # Host + port to your FiveNet instance (requires HTTPS/valid TLS certs) url: "example.fivenet.app" token: "YOUR_SYNC_API_TOKEN" + insecure: false source: # Refer to https://github.com/go-sql-driver/mysql#dsn-data-source-name for details diff --git a/pkg/dbsync/config.go b/pkg/dbsync/config.go index 5085b62a2..051694c53 100644 --- a/pkg/dbsync/config.go +++ b/pkg/dbsync/config.go @@ -58,8 +58,9 @@ type DBSyncSource struct { } type DBSyncDestination struct { - URL string `yaml:"url"` - Token string `yaml:"token"` + URL string `yaml:"url"` + Token string `yaml:"token"` + Insecure bool `yaml:"insecure"` } type DBSyncSourceTables struct { diff --git a/pkg/dbsync/sync.go b/pkg/dbsync/sync.go index aa0ba1776..145149f49 100644 --- a/pkg/dbsync/sync.go +++ b/pkg/dbsync/sync.go @@ -2,6 +2,7 @@ package dbsync import ( "context" + "crypto/tls" "database/sql" "fmt" "strconv" @@ -20,6 +21,7 @@ import ( "go.uber.org/multierr" "go.uber.org/zap" "google.golang.org/grpc" + "google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials/insecure" ) @@ -106,8 +108,15 @@ func New(p Params) (*Sync, error) { // Create GRPC client for sync if destination is given if s.cfg.Destination.URL != "" { + transportCreds := insecure.NewCredentials() + if !s.cfg.Destination.Insecure { + transportCreds = credentials.NewTLS(&tls.Config{ + ClientAuth: tls.NoClientCert, + }) + } + cli, err := grpc.NewClient(s.cfg.Destination.URL, - grpc.WithTransportCredentials(insecure.NewCredentials()), + grpc.WithTransportCredentials(transportCreds), // Require transport security for release mode grpc.WithPerRPCCredentials(auth.NewClientTokenAuth(s.cfg.Destination.Token, s.acfg.Mode == "release")), ) diff --git a/pkg/dbsync/sync_jobs.go b/pkg/dbsync/sync_jobs.go index 6aacbf20f..579ae31ef 100644 --- a/pkg/dbsync/sync_jobs.go +++ b/pkg/dbsync/sync_jobs.go @@ -2,6 +2,7 @@ package dbsync import ( "context" + "errors" "strings" "github.com/fivenet-app/fivenet/gen/go/proto/resources/sync" @@ -35,7 +36,9 @@ func (s *jobsSync) Sync(ctx context.Context) error { jobs := []*users.Job{} if _, err := qrm.Query(ctx, s.db, query, []interface{}{}, &jobs); err != nil { - return err + if !errors.Is(err, qrm.ErrNoRows) { + return err + } } if len(jobs) == 0 { diff --git a/pkg/dbsync/sync_licenses.go b/pkg/dbsync/sync_licenses.go index 1712e168c..3ac7b57c7 100644 --- a/pkg/dbsync/sync_licenses.go +++ b/pkg/dbsync/sync_licenses.go @@ -2,6 +2,7 @@ package dbsync import ( "context" + "errors" "github.com/fivenet-app/fivenet/gen/go/proto/resources/sync" "github.com/fivenet-app/fivenet/gen/go/proto/resources/users" @@ -34,7 +35,9 @@ func (s *licensesSync) Sync(ctx context.Context) error { licenses := []*users.License{} if _, err := qrm.Query(ctx, s.db, query, []interface{}{}, &licenses); err != nil { - return err + if !errors.Is(err, qrm.ErrNoRows) { + return err + } } if len(licenses) == 0 { diff --git a/pkg/dbsync/sync_users.go b/pkg/dbsync/sync_users.go index fce2f74e0..8798669ee 100644 --- a/pkg/dbsync/sync_users.go +++ b/pkg/dbsync/sync_users.go @@ -2,6 +2,7 @@ package dbsync import ( "context" + "errors" "strconv" "strings" @@ -46,7 +47,9 @@ func (s *usersSync) Sync(ctx context.Context) error { users := []*users.User{} if _, err := qrm.Query(ctx, s.db, query, []interface{}{}, &users); err != nil { - return err + if !errors.Is(err, qrm.ErrNoRows) { + return err + } } if len(users) == 0 { diff --git a/pkg/dbsync/sync_vehicles.go b/pkg/dbsync/sync_vehicles.go index 5b0405802..6d5110bd2 100644 --- a/pkg/dbsync/sync_vehicles.go +++ b/pkg/dbsync/sync_vehicles.go @@ -2,6 +2,7 @@ package dbsync import ( "context" + "errors" "github.com/fivenet-app/fivenet/gen/go/proto/resources/sync" "github.com/fivenet-app/fivenet/gen/go/proto/resources/vehicles" @@ -43,7 +44,9 @@ func (s *vehiclesSync) Sync(ctx context.Context) error { vehicles := []*vehicles.Vehicle{} if _, err := qrm.Query(ctx, s.db, query, []interface{}{}, &vehicles); err != nil { - return err + if !errors.Is(err, qrm.ErrNoRows) { + return err + } } if len(vehicles) == 0 { diff --git a/services/sync/data.go b/services/sync/data.go index a97ab42c8..47db8e8f6 100644 --- a/services/sync/data.go +++ b/services/sync/data.go @@ -334,13 +334,13 @@ func (s *Server) handleUsersData(ctx context.Context, data *pbsync.SendDataReque if len(existing) == 0 { toCreate = data.Users.Users } else { - for _, userId := range existing { - if idx := slices.IndexFunc(data.Users.Users, func(user *users.User) bool { - return user.UserId == userId + for _, user := range data.Users.Users { + if idx := slices.IndexFunc(existing, func(userId int32) bool { + return userId == user.UserId }); idx == -1 { - toCreate = append(toCreate, data.Users.Users[idx]) + toCreate = append(toCreate, user) } else { - toUpdate = append(toUpdate, data.Users.Users[idx]) + toUpdate = append(toUpdate, user) } } } @@ -349,6 +349,7 @@ func (s *Server) handleUsersData(ctx context.Context, data *pbsync.SendDataReque if len(toCreate) > 0 { stmt := tUsers. INSERT( + tUsers.ID, tUsers.Identifier, tUsers.Group, tUsers.Firstname, @@ -366,6 +367,7 @@ func (s *Server) handleUsersData(ctx context.Context, data *pbsync.SendDataReque for _, user := range toCreate { insertStmt := stmt. VALUES( + user.UserId, user.Identifier, user.Group, user.Firstname,