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:
parent
4562e98df5
commit
f77b11d85e
@ -6,8 +6,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.blender.org/flamenco/internal/manager/persistence"
|
"git.blender.org/flamenco/internal/manager/persistence"
|
||||||
|
"git.blender.org/flamenco/internal/uuid"
|
||||||
"git.blender.org/flamenco/pkg/api"
|
"git.blender.org/flamenco/pkg/api"
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -16,7 +16,7 @@ func (f *Flamenco) FetchJob(e echo.Context, jobID string) error {
|
|||||||
Str("job", jobID).
|
Str("job", jobID).
|
||||||
Logger()
|
Logger()
|
||||||
|
|
||||||
if _, err := uuid.Parse(jobID); err != nil {
|
if !uuid.IsValid(jobID) {
|
||||||
logger.Debug().Msg("invalid job ID received")
|
logger.Debug().Msg("invalid job ID received")
|
||||||
return sendAPIError(e, http.StatusBadRequest, "job ID not valid")
|
return sendAPIError(e, http.StatusBadRequest, "job ID not valid")
|
||||||
}
|
}
|
||||||
@ -66,7 +66,7 @@ func (f *Flamenco) FetchJobTasks(e echo.Context, jobID string) error {
|
|||||||
Logger()
|
Logger()
|
||||||
ctx := e.Request().Context()
|
ctx := e.Request().Context()
|
||||||
|
|
||||||
if _, err := uuid.Parse(jobID); err != nil {
|
if !uuid.IsValid(jobID) {
|
||||||
logger.Debug().Msg("invalid job ID received")
|
logger.Debug().Msg("invalid job ID received")
|
||||||
return sendAPIError(e, http.StatusBadRequest, "job ID not valid")
|
return sendAPIError(e, http.StatusBadRequest, "job ID not valid")
|
||||||
}
|
}
|
||||||
@ -93,7 +93,7 @@ func (f *Flamenco) FetchTask(e echo.Context, taskID string) error {
|
|||||||
Logger()
|
Logger()
|
||||||
ctx := e.Request().Context()
|
ctx := e.Request().Context()
|
||||||
|
|
||||||
if _, err := uuid.Parse(taskID); err != nil {
|
if !uuid.IsValid(taskID) {
|
||||||
logger.Debug().Msg("invalid job ID received")
|
logger.Debug().Msg("invalid job ID received")
|
||||||
return sendAPIError(e, http.StatusBadRequest, "job ID not valid")
|
return sendAPIError(e, http.StatusBadRequest, "job ID not valid")
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
@ -18,6 +17,7 @@ import (
|
|||||||
"git.blender.org/flamenco/internal/manager/persistence"
|
"git.blender.org/flamenco/internal/manager/persistence"
|
||||||
"git.blender.org/flamenco/internal/manager/task_state_machine"
|
"git.blender.org/flamenco/internal/manager/task_state_machine"
|
||||||
"git.blender.org/flamenco/internal/manager/webupdates"
|
"git.blender.org/flamenco/internal/manager/webupdates"
|
||||||
|
"git.blender.org/flamenco/internal/uuid"
|
||||||
"git.blender.org/flamenco/pkg/api"
|
"git.blender.org/flamenco/pkg/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -47,7 +47,7 @@ func (f *Flamenco) RegisterWorker(e echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dbWorker := persistence.Worker{
|
dbWorker := persistence.Worker{
|
||||||
UUID: uuid.New().String(),
|
UUID: uuid.New(),
|
||||||
Name: req.Nickname,
|
Name: req.Nickname,
|
||||||
Secret: string(hashedPassword),
|
Secret: string(hashedPassword),
|
||||||
Platform: req.Platform,
|
Platform: req.Platform,
|
||||||
@ -328,7 +328,7 @@ func (f *Flamenco) TaskUpdate(e echo.Context, taskID string) error {
|
|||||||
logger := requestLogger(e)
|
logger := requestLogger(e)
|
||||||
worker := requestWorkerOrPanic(e)
|
worker := requestWorkerOrPanic(e)
|
||||||
|
|
||||||
if _, err := uuid.Parse(taskID); err != nil {
|
if !uuid.IsValid(taskID) {
|
||||||
logger.Debug().Msg("invalid task ID received")
|
logger.Debug().Msg("invalid task ID received")
|
||||||
return sendAPIError(e, http.StatusBadRequest, "task ID not valid")
|
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)
|
logger := requestLogger(e)
|
||||||
worker := requestWorkerOrPanic(e)
|
worker := requestWorkerOrPanic(e)
|
||||||
|
|
||||||
if _, err := uuid.Parse(taskID); err != nil {
|
if !uuid.IsValid(taskID) {
|
||||||
logger.Debug().Msg("invalid task ID received")
|
logger.Debug().Msg("invalid task ID received")
|
||||||
return sendAPIError(e, http.StatusBadRequest, "task ID not valid")
|
return sendAPIError(e, http.StatusBadRequest, "task ID not valid")
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,9 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/dop251/goja"
|
"github.com/dop251/goja"
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
|
"git.blender.org/flamenco/internal/uuid"
|
||||||
"git.blender.org/flamenco/pkg/api"
|
"git.blender.org/flamenco/pkg/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ func (a *Author) Task(name string, taskType string) (*AuthoredTask, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
at := AuthoredTask{
|
at := AuthoredTask{
|
||||||
uuid.New().String(),
|
uuid.New(),
|
||||||
name,
|
name,
|
||||||
taskType,
|
taskType,
|
||||||
50, // TODO: handle default priority somehow.
|
50, // TODO: handle default priority somehow.
|
||||||
|
@ -12,9 +12,9 @@ import (
|
|||||||
|
|
||||||
"github.com/dop251/goja"
|
"github.com/dop251/goja"
|
||||||
"github.com/dop251/goja_nodejs/require"
|
"github.com/dop251/goja_nodejs/require"
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
|
"git.blender.org/flamenco/internal/uuid"
|
||||||
"git.blender.org/flamenco/pkg/api"
|
"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.
|
// Create an AuthoredJob from this SubmittedJob.
|
||||||
aj := AuthoredJob{
|
aj := AuthoredJob{
|
||||||
JobID: uuid.New().String(),
|
JobID: uuid.New(),
|
||||||
Created: s.timeService.Now(),
|
Created: s.timeService.Now(),
|
||||||
Name: sj.Name,
|
Name: sj.Name,
|
||||||
JobType: sj.Type,
|
JobType: sj.Type,
|
||||||
|
@ -6,10 +6,10 @@ package persistence
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"git.blender.org/flamenco/internal/manager/job_compilers"
|
"git.blender.org/flamenco/internal/manager/job_compilers"
|
||||||
|
"git.blender.org/flamenco/internal/uuid"
|
||||||
"git.blender.org/flamenco/pkg/api"
|
"git.blender.org/flamenco/pkg/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -119,7 +119,7 @@ func TestQueryJobTaskSummaries(t *testing.T) {
|
|||||||
otherAuthoredJob := createTestAuthoredJobWithTasks()
|
otherAuthoredJob := createTestAuthoredJobWithTasks()
|
||||||
otherAuthoredJob.Status = api.JobStatusActive
|
otherAuthoredJob.Status = api.JobStatusActive
|
||||||
for i := range otherAuthoredJob.Tasks {
|
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.Tasks[i].Dependencies = []*job_compilers.AuthoredTask{}
|
||||||
}
|
}
|
||||||
otherAuthoredJob.JobID = "138678c8-efd0-452b-ac05-397ff4c02b26"
|
otherAuthoredJob.JobID = "138678c8-efd0-452b-ac05-397ff4c02b26"
|
||||||
|
@ -7,10 +7,10 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"git.blender.org/flamenco/internal/manager/job_compilers"
|
"git.blender.org/flamenco/internal/manager/job_compilers"
|
||||||
|
"git.blender.org/flamenco/internal/uuid"
|
||||||
"git.blender.org/flamenco/pkg/api"
|
"git.blender.org/flamenco/pkg/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -301,7 +301,7 @@ func authorTestTask(name, taskType string, dependencies ...*job_compilers.Author
|
|||||||
task := job_compilers.AuthoredTask{
|
task := job_compilers.AuthoredTask{
|
||||||
Name: name,
|
Name: name,
|
||||||
Type: taskType,
|
Type: taskType,
|
||||||
UUID: uuid.NewString(),
|
UUID: uuid.New(),
|
||||||
Priority: 50,
|
Priority: 50,
|
||||||
Commands: make([]job_compilers.AuthoredCommand, 0),
|
Commands: make([]job_compilers.AuthoredCommand, 0),
|
||||||
Dependencies: dependencies,
|
Dependencies: dependencies,
|
||||||
|
@ -7,9 +7,9 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"git.blender.org/flamenco/internal/uuid"
|
||||||
"git.blender.org/flamenco/pkg/api"
|
"git.blender.org/flamenco/pkg/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ func TestCreateFetchWorker(t *testing.T) {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
w := Worker{
|
w := Worker{
|
||||||
UUID: uuid.New().String(),
|
UUID: uuid.New(),
|
||||||
Name: "дрон",
|
Name: "дрон",
|
||||||
Address: "fe80::5054:ff:fede:2ad7",
|
Address: "fe80::5054:ff:fede:2ad7",
|
||||||
LastActivity: "",
|
LastActivity: "",
|
||||||
@ -52,7 +52,7 @@ func TestSaveWorker(t *testing.T) {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
w := Worker{
|
w := Worker{
|
||||||
UUID: uuid.New().String(),
|
UUID: uuid.New(),
|
||||||
Name: "дрон",
|
Name: "дрон",
|
||||||
Address: "fe80::5054:ff:fede:2ad7",
|
Address: "fe80::5054:ff:fede:2ad7",
|
||||||
LastActivity: "",
|
LastActivity: "",
|
||||||
|
@ -5,8 +5,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"git.blender.org/flamenco/pkg/api"
|
"git.blender.org/flamenco/pkg/api"
|
||||||
"github.com/google/uuid"
|
|
||||||
gosocketio "github.com/graarh/golang-socketio"
|
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
|
// 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)).
|
Str("uuid", string(subs.Uuid)).
|
||||||
Logger()
|
Logger()
|
||||||
|
|
||||||
// Make sure the UUID is actually a valid one.
|
if !uuid.IsValid(subs.Uuid) {
|
||||||
uuid, err := uuid.Parse(subs.Uuid)
|
|
||||||
if err != nil {
|
|
||||||
logger.Warn().Msg("socketIO: invalid UUID, ignoring subscription request")
|
logger.Warn().Msg("socketIO: invalid UUID, ignoring subscription request")
|
||||||
return "invalid UUID, ignoring request"
|
return "invalid UUID, ignoring request"
|
||||||
}
|
}
|
||||||
@ -60,14 +59,15 @@ func (b *BiDirComms) handleRoomSubscription(c *gosocketio.Channel, subs api.Sock
|
|||||||
var sioRoom SocketIORoomName
|
var sioRoom SocketIORoomName
|
||||||
switch subs.Type {
|
switch subs.Type {
|
||||||
case api.SocketIOSubscriptionTypeJob:
|
case api.SocketIOSubscriptionTypeJob:
|
||||||
sioRoom = roomForJob(uuid.String())
|
sioRoom = roomForJob(subs.Uuid)
|
||||||
case api.SocketIOSubscriptionTypeTasklog:
|
case api.SocketIOSubscriptionTypeTasklog:
|
||||||
sioRoom = roomForTaskLog(uuid.String())
|
sioRoom = roomForTaskLog(subs.Uuid)
|
||||||
default:
|
default:
|
||||||
logger.Warn().Msg("socketIO: unknown subscription type, ignoring")
|
logger.Warn().Msg("socketIO: unknown subscription type, ignoring")
|
||||||
return "unknown subscription type, ignoring request"
|
return "unknown subscription type, ignoring request"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
switch subs.Op {
|
switch subs.Op {
|
||||||
case api.SocketIOSubscriptionOperationSubscribe:
|
case api.SocketIOSubscriptionOperationSubscribe:
|
||||||
err = c.Join(string(sioRoom))
|
err = c.Join(string(sioRoom))
|
||||||
|
25
internal/uuid/uuid.go
Normal file
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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user