diff --git a/internal/manager/api_impl/jobs_query.go b/internal/manager/api_impl/jobs_query.go index c3e69509..ebcfc419 100644 --- a/internal/manager/api_impl/jobs_query.go +++ b/internal/manager/api_impl/jobs_query.go @@ -6,8 +6,8 @@ import ( "net/http" "git.blender.org/flamenco/internal/manager/persistence" + "git.blender.org/flamenco/internal/uuid" "git.blender.org/flamenco/pkg/api" - "github.com/google/uuid" "github.com/labstack/echo/v4" ) @@ -16,7 +16,7 @@ func (f *Flamenco) FetchJob(e echo.Context, jobID string) error { Str("job", jobID). Logger() - if _, err := uuid.Parse(jobID); err != nil { + if !uuid.IsValid(jobID) { logger.Debug().Msg("invalid job ID received") return sendAPIError(e, http.StatusBadRequest, "job ID not valid") } @@ -66,7 +66,7 @@ func (f *Flamenco) FetchJobTasks(e echo.Context, jobID string) error { Logger() ctx := e.Request().Context() - if _, err := uuid.Parse(jobID); err != nil { + if !uuid.IsValid(jobID) { logger.Debug().Msg("invalid job ID received") return sendAPIError(e, http.StatusBadRequest, "job ID not valid") } @@ -93,7 +93,7 @@ func (f *Flamenco) FetchTask(e echo.Context, taskID string) error { Logger() ctx := e.Request().Context() - if _, err := uuid.Parse(taskID); err != nil { + if !uuid.IsValid(taskID) { logger.Debug().Msg("invalid job ID received") return sendAPIError(e, http.StatusBadRequest, "job ID not valid") } diff --git a/internal/manager/api_impl/workers.go b/internal/manager/api_impl/workers.go index 0cab1ccc..8e0fa43d 100644 --- a/internal/manager/api_impl/workers.go +++ b/internal/manager/api_impl/workers.go @@ -10,7 +10,6 @@ import ( "strings" "time" - "github.com/google/uuid" "github.com/labstack/echo/v4" "github.com/rs/zerolog" "golang.org/x/crypto/bcrypt" @@ -18,6 +17,7 @@ import ( "git.blender.org/flamenco/internal/manager/persistence" "git.blender.org/flamenco/internal/manager/task_state_machine" "git.blender.org/flamenco/internal/manager/webupdates" + "git.blender.org/flamenco/internal/uuid" "git.blender.org/flamenco/pkg/api" ) @@ -47,7 +47,7 @@ func (f *Flamenco) RegisterWorker(e echo.Context) error { } dbWorker := persistence.Worker{ - UUID: uuid.New().String(), + UUID: uuid.New(), Name: req.Nickname, Secret: string(hashedPassword), Platform: req.Platform, @@ -328,7 +328,7 @@ func (f *Flamenco) TaskUpdate(e echo.Context, taskID string) error { logger := requestLogger(e) worker := requestWorkerOrPanic(e) - if _, err := uuid.Parse(taskID); err != nil { + if !uuid.IsValid(taskID) { logger.Debug().Msg("invalid task ID received") return sendAPIError(e, http.StatusBadRequest, "task ID not valid") } @@ -437,7 +437,7 @@ func (f *Flamenco) MayWorkerRun(e echo.Context, taskID string) error { logger := requestLogger(e) worker := requestWorkerOrPanic(e) - if _, err := uuid.Parse(taskID); err != nil { + if !uuid.IsValid(taskID) { logger.Debug().Msg("invalid task ID received") return sendAPIError(e, http.StatusBadRequest, "task ID not valid") } diff --git a/internal/manager/job_compilers/author.go b/internal/manager/job_compilers/author.go index ea1b3591..87e0b7bf 100644 --- a/internal/manager/job_compilers/author.go +++ b/internal/manager/job_compilers/author.go @@ -8,9 +8,9 @@ import ( "time" "github.com/dop251/goja" - "github.com/google/uuid" "github.com/rs/zerolog/log" + "git.blender.org/flamenco/internal/uuid" "git.blender.org/flamenco/pkg/api" ) @@ -69,7 +69,7 @@ func (a *Author) Task(name string, taskType string) (*AuthoredTask, error) { } at := AuthoredTask{ - uuid.New().String(), + uuid.New(), name, taskType, 50, // TODO: handle default priority somehow. diff --git a/internal/manager/job_compilers/job_compilers.go b/internal/manager/job_compilers/job_compilers.go index 306f70cd..5ec9af17 100644 --- a/internal/manager/job_compilers/job_compilers.go +++ b/internal/manager/job_compilers/job_compilers.go @@ -12,9 +12,9 @@ import ( "github.com/dop251/goja" "github.com/dop251/goja_nodejs/require" - "github.com/google/uuid" "github.com/rs/zerolog/log" + "git.blender.org/flamenco/internal/uuid" "git.blender.org/flamenco/pkg/api" ) @@ -85,7 +85,7 @@ func (s *Service) Compile(ctx context.Context, sj api.SubmittedJob) (*AuthoredJo // Create an AuthoredJob from this SubmittedJob. aj := AuthoredJob{ - JobID: uuid.New().String(), + JobID: uuid.New(), Created: s.timeService.Now(), Name: sj.Name, JobType: sj.Type, diff --git a/internal/manager/persistence/jobs_query_test.go b/internal/manager/persistence/jobs_query_test.go index 16d3dff6..ed07e7e2 100644 --- a/internal/manager/persistence/jobs_query_test.go +++ b/internal/manager/persistence/jobs_query_test.go @@ -6,10 +6,10 @@ package persistence import ( "testing" - "github.com/google/uuid" "github.com/stretchr/testify/assert" "git.blender.org/flamenco/internal/manager/job_compilers" + "git.blender.org/flamenco/internal/uuid" "git.blender.org/flamenco/pkg/api" ) @@ -119,7 +119,7 @@ func TestQueryJobTaskSummaries(t *testing.T) { otherAuthoredJob := createTestAuthoredJobWithTasks() otherAuthoredJob.Status = api.JobStatusActive for i := range otherAuthoredJob.Tasks { - otherAuthoredJob.Tasks[i].UUID = uuid.NewString() + otherAuthoredJob.Tasks[i].UUID = uuid.New() otherAuthoredJob.Tasks[i].Dependencies = []*job_compilers.AuthoredTask{} } otherAuthoredJob.JobID = "138678c8-efd0-452b-ac05-397ff4c02b26" diff --git a/internal/manager/persistence/task_scheduler_test.go b/internal/manager/persistence/task_scheduler_test.go index 1b78b9ae..7f6bdce5 100644 --- a/internal/manager/persistence/task_scheduler_test.go +++ b/internal/manager/persistence/task_scheduler_test.go @@ -7,10 +7,10 @@ import ( "testing" "time" - "github.com/google/uuid" "github.com/stretchr/testify/assert" "git.blender.org/flamenco/internal/manager/job_compilers" + "git.blender.org/flamenco/internal/uuid" "git.blender.org/flamenco/pkg/api" ) @@ -301,7 +301,7 @@ func authorTestTask(name, taskType string, dependencies ...*job_compilers.Author task := job_compilers.AuthoredTask{ Name: name, Type: taskType, - UUID: uuid.NewString(), + UUID: uuid.New(), Priority: 50, Commands: make([]job_compilers.AuthoredCommand, 0), Dependencies: dependencies, diff --git a/internal/manager/persistence/workers_test.go b/internal/manager/persistence/workers_test.go index 1ad025ee..228c0701 100644 --- a/internal/manager/persistence/workers_test.go +++ b/internal/manager/persistence/workers_test.go @@ -7,9 +7,9 @@ import ( "testing" "time" - "github.com/google/uuid" "github.com/stretchr/testify/assert" + "git.blender.org/flamenco/internal/uuid" "git.blender.org/flamenco/pkg/api" ) @@ -18,7 +18,7 @@ func TestCreateFetchWorker(t *testing.T) { defer cancel() w := Worker{ - UUID: uuid.New().String(), + UUID: uuid.New(), Name: "дрон", Address: "fe80::5054:ff:fede:2ad7", LastActivity: "", @@ -52,7 +52,7 @@ func TestSaveWorker(t *testing.T) { defer cancel() w := Worker{ - UUID: uuid.New().String(), + UUID: uuid.New(), Name: "дрон", Address: "fe80::5054:ff:fede:2ad7", LastActivity: "", diff --git a/internal/manager/webupdates/sio_rooms.go b/internal/manager/webupdates/sio_rooms.go index 8189dc3d..5b75493a 100644 --- a/internal/manager/webupdates/sio_rooms.go +++ b/internal/manager/webupdates/sio_rooms.go @@ -5,8 +5,9 @@ import ( "fmt" "git.blender.org/flamenco/pkg/api" - "github.com/google/uuid" gosocketio "github.com/graarh/golang-socketio" + + "git.blender.org/flamenco/internal/uuid" ) // Separate type aliases for room names and event types; it's otherwise too easy @@ -50,9 +51,7 @@ func (b *BiDirComms) handleRoomSubscription(c *gosocketio.Channel, subs api.Sock Str("uuid", string(subs.Uuid)). Logger() - // Make sure the UUID is actually a valid one. - uuid, err := uuid.Parse(subs.Uuid) - if err != nil { + if !uuid.IsValid(subs.Uuid) { logger.Warn().Msg("socketIO: invalid UUID, ignoring subscription request") return "invalid UUID, ignoring request" } @@ -60,14 +59,15 @@ func (b *BiDirComms) handleRoomSubscription(c *gosocketio.Channel, subs api.Sock var sioRoom SocketIORoomName switch subs.Type { case api.SocketIOSubscriptionTypeJob: - sioRoom = roomForJob(uuid.String()) + sioRoom = roomForJob(subs.Uuid) case api.SocketIOSubscriptionTypeTasklog: - sioRoom = roomForTaskLog(uuid.String()) + sioRoom = roomForTaskLog(subs.Uuid) default: logger.Warn().Msg("socketIO: unknown subscription type, ignoring") return "unknown subscription type, ignoring request" } + var err error switch subs.Op { case api.SocketIOSubscriptionOperationSubscribe: err = c.Join(string(sioRoom)) diff --git a/internal/uuid/uuid.go b/internal/uuid/uuid.go new file mode 100644 index 00000000..ad3999f1 --- /dev/null +++ b/internal/uuid/uuid.go @@ -0,0 +1,25 @@ +// uuid is a thin wrapper around github.com/google/uuid. +package uuid + +// SPDX-License-Identifier: GPL-3.0-or-later + +import ( + "github.com/google/uuid" +) + +// New generates a random UUID. +func New() string { + return uuid.New().String() +} + +// IsValid returns true when the string can be parsed as UUID. +func IsValid(value string) bool { + // uuid.Parse() accepts a few different notations for UUIDs, but Flamenco only + // works with the xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format. + if len(value) != 36 { + return false + } + + _, err := uuid.Parse(value) + return err == nil +}