Skip to content

Commit

Permalink
fix: issues with dbsync grpc transport
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Trost <[email protected]>
  • Loading branch information
galexrt committed Jan 19, 2025
1 parent 54206c9 commit 656be50
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/components/citizens/CitizensList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
/>
Expand Down
1 change: 1 addition & 0 deletions dbsync.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions pkg/dbsync/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 10 additions & 1 deletion pkg/dbsync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dbsync

import (
"context"
"crypto/tls"
"database/sql"
"fmt"
"strconv"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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")),
)
Expand Down
5 changes: 4 additions & 1 deletion pkg/dbsync/sync_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dbsync

import (
"context"
"errors"
"strings"

"github.com/fivenet-app/fivenet/gen/go/proto/resources/sync"
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion pkg/dbsync/sync_licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion pkg/dbsync/sync_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dbsync

import (
"context"
"errors"
"strconv"
"strings"

Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion pkg/dbsync/sync_vehicles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 7 additions & 5 deletions services/sync/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 656be50

Please sign in to comment.