Manager: add a small wrapper around Google's UUID library

Add a small wrapper around github.com/google/uuid. That way it's clearer
which functionality is used by Flamenco, doesn't link most of the code to
any specific UUID library, and allows a bit of customisation.

The only customisation now is that Flamenco is a bit stricter in the
formats it accepts; only the `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx` is
accepted. This makes things a little bit stricter, with the advantage
that we don't need to do any normalisation of received UUID strings.
This commit is contained in:
Sybren A. Stüvel 2022-05-20 15:34:19 +02:00
parent 4562e98df5
commit f77b11d85e
9 changed files with 50 additions and 25 deletions

@ -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")
}

@ -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")
}

@ -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.

@ -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,

@ -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"

@ -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,

@ -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: "",

@ -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))

25
internal/uuid/uuid.go Normal file

@ -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
}