Skip to content

Commit

Permalink
the public facing uuid generation no longer uses slog
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveromahony committed Dec 9, 2024
1 parent 9d1a6ad commit befb601
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 42 deletions.
4 changes: 2 additions & 2 deletions internal/command/command_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/internal/bus"
"github.com/nginx/agent/v3/internal/config"
"github.com/nginx/agent/v3/internal/datasource/proto"
"github.com/nginx/agent/v3/internal/grpc"
"github.com/nginx/agent/v3/internal/logger"
pkgConfig "github.com/nginx/agent/v3/pkg/config"
"github.com/nginx/agent/v3/pkg/uuid"
)

var _ bus.Plugin = (*CommandPlugin)(nil)
Expand Down Expand Up @@ -248,7 +248,7 @@ func (cp *CommandPlugin) createDataPlaneResponse(correlationID string, status mp
) *mpi.DataPlaneResponse {
return &mpi.DataPlaneResponse{
MessageMeta: &mpi.MessageMeta{
MessageId: uuid.GenerateUUIDV7(),
MessageId: proto.GenerateMessageID(),
CorrelationId: correlationID,
Timestamp: timestamppb.Now(),
},
Expand Down
9 changes: 5 additions & 4 deletions internal/command/command_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (

mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/internal/config"
"github.com/nginx/agent/v3/internal/datasource/proto"
"github.com/nginx/agent/v3/internal/grpc"
"github.com/nginx/agent/v3/internal/logger"
"github.com/nginx/agent/v3/pkg/uuid"

"google.golang.org/protobuf/types/known/timestamppb"

backoffHelpers "github.com/nginx/agent/v3/internal/backoff"
Expand Down Expand Up @@ -87,7 +88,7 @@ func (cs *CommandService) UpdateDataPlaneStatus(

request := &mpi.UpdateDataPlaneStatusRequest{
MessageMeta: &mpi.MessageMeta{
MessageId: uuid.GenerateUUIDV7(),
MessageId: proto.GenerateMessageID(),
CorrelationId: correlationID,
Timestamp: timestamppb.Now(),
},
Expand Down Expand Up @@ -137,7 +138,7 @@ func (cs *CommandService) UpdateDataPlaneHealth(ctx context.Context, instanceHea

request := &mpi.UpdateDataPlaneHealthRequest{
MessageMeta: &mpi.MessageMeta{
MessageId: uuid.GenerateUUIDV7(),
MessageId: proto.GenerateMessageID(),
CorrelationId: correlationID,
Timestamp: timestamppb.Now(),
},
Expand Down Expand Up @@ -215,7 +216,7 @@ func (cs *CommandService) CreateConnection(

request := &mpi.CreateConnectionRequest{
MessageMeta: &mpi.MessageMeta{
MessageId: uuid.GenerateUUIDV7(),
MessageId: proto.GenerateMessageID(),
CorrelationId: correlationID,
Timestamp: timestamppb.Now(),
},
Expand Down
23 changes: 23 additions & 0 deletions internal/datasource/proto/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) F5, Inc.
//
// This source code is licensed under the Apache License, Version 2.0 license found in the
// LICENSE file in the root directory of this source tree.

package proto

import (
"log/slog"
"time"

"github.com/nginx/agent/v3/pkg/uuid"
)

func GenerateMessageID() string {
uuidv7, err := uuid.GenerateUUIDV7()
if err != nil {
slog.Debug("issue generating uuidv7, using sha256 and timestamp instead", "error", err)
return uuid.Generate("%s", time.Now().String())
}

return uuidv7
}
8 changes: 4 additions & 4 deletions internal/file/file_manager_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"sync"
"sync/atomic"

"github.com/nginx/agent/v3/internal/datasource/proto"
"github.com/nginx/agent/v3/internal/model"

"github.com/cenkalti/backoff/v4"
Expand All @@ -23,7 +24,6 @@ import (
"github.com/nginx/agent/v3/internal/grpc"
"github.com/nginx/agent/v3/internal/logger"
"github.com/nginx/agent/v3/pkg/files"
"github.com/nginx/agent/v3/pkg/uuid"
"google.golang.org/protobuf/types/known/timestamppb"

backoffHelpers "github.com/nginx/agent/v3/internal/backoff"
Expand Down Expand Up @@ -97,7 +97,7 @@ func (fms *FileManagerService) UpdateOverview(

request := &mpi.UpdateOverviewRequest{
MessageMeta: &mpi.MessageMeta{
MessageId: uuid.GenerateUUIDV7(),
MessageId: proto.GenerateMessageID(),
CorrelationId: requestCorrelationID.Value.String(),
Timestamp: timestamppb.Now(),
},
Expand Down Expand Up @@ -169,7 +169,7 @@ func (fms *FileManagerService) UpdateFile(
Contents: contents,
},
MessageMeta: &mpi.MessageMeta{
MessageId: uuid.GenerateUUIDV7(),
MessageId: proto.GenerateMessageID(),
CorrelationId: correlationID,
Timestamp: timestamppb.Now(),
},
Expand Down Expand Up @@ -326,7 +326,7 @@ func (fms *FileManagerService) fileUpdate(ctx context.Context, file *mpi.File) e
getFile := func() (*mpi.GetFileResponse, error) {
return fms.fileServiceClient.GetFile(ctx, &mpi.GetFileRequest{
MessageMeta: &mpi.MessageMeta{
MessageId: uuid.GenerateUUIDV7(),
MessageId: proto.GenerateMessageID(),
CorrelationId: logger.GetCorrelationID(ctx),
Timestamp: timestamppb.Now(),
},
Expand Down
6 changes: 3 additions & 3 deletions internal/file/file_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"log/slog"

"github.com/nginx/agent/v3/pkg/files"
"github.com/nginx/agent/v3/pkg/uuid"

mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/internal/bus"
"github.com/nginx/agent/v3/internal/config"
"github.com/nginx/agent/v3/internal/datasource/proto"
"github.com/nginx/agent/v3/internal/grpc"
"github.com/nginx/agent/v3/internal/logger"
"github.com/nginx/agent/v3/internal/model"
Expand Down Expand Up @@ -319,7 +319,7 @@ func (fp *FilePlugin) handleConfigUploadRequest(ctx context.Context, msg *bus.Me

response := &mpi.DataPlaneResponse{
MessageMeta: &mpi.MessageMeta{
MessageId: uuid.GenerateUUIDV7(),
MessageId: proto.GenerateMessageID(),
CorrelationId: correlationID,
Timestamp: timestamppb.Now(),
},
Expand All @@ -343,7 +343,7 @@ func (fp *FilePlugin) createDataPlaneResponse(correlationID string, status mpi.C
) *mpi.DataPlaneResponse {
return &mpi.DataPlaneResponse{
MessageMeta: &mpi.MessageMeta{
MessageId: uuid.GenerateUUIDV7(),
MessageId: proto.GenerateMessageID(),
CorrelationId: correlationID,
Timestamp: timestamppb.Now(),
},
Expand Down
4 changes: 2 additions & 2 deletions internal/file/file_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/nginx/agent/v3/internal/bus/busfakes"
"github.com/nginx/agent/v3/internal/datasource/proto"

"google.golang.org/protobuf/types/known/timestamppb"

Expand All @@ -23,7 +24,6 @@ import (
"github.com/nginx/agent/v3/internal/grpc/grpcfakes"
"github.com/nginx/agent/v3/internal/model"
"github.com/nginx/agent/v3/pkg/files"
"github.com/nginx/agent/v3/pkg/uuid"
"github.com/nginx/agent/v3/test/helpers"
"github.com/nginx/agent/v3/test/protos"
"github.com/nginx/agent/v3/test/types"
Expand Down Expand Up @@ -441,7 +441,7 @@ func TestFilePlugin_Process_ConfigApplyRollbackCompleteTopic(t *testing.T) {

expectedResponse := &mpi.DataPlaneResponse{
MessageMeta: &mpi.MessageMeta{
MessageId: uuid.GenerateUUIDV7(),
MessageId: proto.GenerateMessageID(),
CorrelationId: "dfsbhj6-bc92-30c1-a9c9-85591422068e",
Timestamp: timestamppb.Now(),
},
Expand Down
4 changes: 2 additions & 2 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"strings"

"github.com/nginx/agent/v3/internal/config"
"github.com/nginx/agent/v3/pkg/uuid"
"github.com/nginx/agent/v3/internal/datasource/proto"
)

const (
Expand Down Expand Up @@ -121,7 +121,7 @@ func (h contextHandler) observe(ctx context.Context) (as []slog.Attr) {
}

func GenerateCorrelationID() slog.Attr {
return slog.Any(CorrelationIDKey, uuid.GenerateUUIDV7())
return slog.Any(CorrelationIDKey, proto.GenerateMessageID())
}

func GetCorrelationID(ctx context.Context) string {
Expand Down
4 changes: 2 additions & 2 deletions internal/resource/resource_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"log/slog"

"github.com/nginx/agent/v3/internal/config"
"github.com/nginx/agent/v3/internal/datasource/proto"
"github.com/nginx/agent/v3/internal/model"
"github.com/nginx/agent/v3/pkg/uuid"
"google.golang.org/protobuf/types/known/timestamppb"

mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
Expand Down Expand Up @@ -186,7 +186,7 @@ func (*Resource) createDataPlaneResponse(correlationID string, status mpi.Comman
) *mpi.DataPlaneResponse {
return &mpi.DataPlaneResponse{
MessageMeta: &mpi.MessageMeta{
MessageId: uuid.GenerateUUIDV7(),
MessageId: proto.GenerateMessageID(),
CorrelationId: correlationID,
Timestamp: timestamppb.Now(),
},
Expand Down
6 changes: 3 additions & 3 deletions internal/watcher/watcher_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"

"github.com/nginx/agent/v3/internal/bus/busfakes"
"github.com/nginx/agent/v3/pkg/uuid"
"github.com/nginx/agent/v3/internal/datasource/proto"

"google.golang.org/protobuf/types/known/timestamppb"

Expand Down Expand Up @@ -138,7 +138,7 @@ func TestWatcher_Process_ConfigApplySuccessfulTopic(t *testing.T) {

response := &mpi.DataPlaneResponse{
MessageMeta: &mpi.MessageMeta{
MessageId: uuid.GenerateUUIDV7(),
MessageId: proto.GenerateMessageID(),
CorrelationId: "dfsbhj6-bc92-30c1-a9c9-85591422068e",
Timestamp: timestamppb.Now(),
},
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestWatcher_Process_RollbackCompleteTopic(t *testing.T) {

response := &mpi.DataPlaneResponse{
MessageMeta: &mpi.MessageMeta{
MessageId: uuid.GenerateUUIDV7(),
MessageId: proto.GenerateMessageID(),
CorrelationId: "dfsbhj6-bc92-30c1-a9c9-85591422068e",
Timestamp: timestamppb.Now(),
},
Expand Down
12 changes: 5 additions & 7 deletions pkg/uuid/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ package uuid
import (
"crypto/sha256"
"fmt"
"log/slog"
"time"

"github.com/google/uuid"
)
Expand Down Expand Up @@ -50,13 +48,13 @@ func Generate(format string, a ...interface{}) string {
//
// Returns:
//
// A string representation of the generated UUID.
func GenerateUUIDV7() string {
// A string representation of the generated UUID if successful. An empty string if unsuccessful
// An error
func GenerateUUIDV7() (string, error) {
id, err := uuid.NewV7()
if err != nil {
slog.Debug("issuing generating uuidv7, using sha256 and timestamp instead", "error", err)
return Generate("%s", time.Now().String())
return "", err
}

return id.String()
return id.String(), err
}
4 changes: 1 addition & 3 deletions pkg/uuid/uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package uuid
import (
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)

Expand All @@ -19,7 +18,6 @@ func TestGenerate(t *testing.T) {
}

func TestGenerateUUIDV7_Success(t *testing.T) {
result := GenerateUUIDV7()
_, err := uuid.Parse(result)
_, err := GenerateUUIDV7()
assert.NoError(t, err, "Generated UUIDv7 should be valid")
}
5 changes: 4 additions & 1 deletion test/helpers/os_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ package helpers
import (
"os"
"regexp"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

const (
filePermission = 0o700
specialChars = "#$%\x00\x01\n"
)

func CreateDirWithErrorCheck(t testing.TB, dirName string) {
Expand Down Expand Up @@ -49,6 +51,7 @@ func RemoveASCIIControlSignals(t testing.TB, input string) string {
// Use a regex to match and remove ASCII control characters (0x00 to 0x1F and 0x7F).
// by matching all control characters (ASCII 0–31 and 127).
re := regexp.MustCompile(`[[:cntrl:]]`)
output := strings.Trim(re.ReplaceAllString(input, ""), specialChars)

return re.ReplaceAllString(input, "")
return output
}
5 changes: 5 additions & 0 deletions test/helpers/os_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func TestRemoveASCIIControlSignals(t *testing.T) {
name: "Agent version example",
input: " nginx-agent version v3.0.0-4a64a94", expected: "nginx-agent version v3.0.0-4a64a94",
},
{
name: "Agent version example alpine",
input: "#nginx-agent version v3.0.0-f94d93a",
expected: "nginx-agent version v3.0.0-f94d93a",
},
}

for _, test := range tests {
Expand Down
6 changes: 3 additions & 3 deletions test/mock/grpc/mock_management_command_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"github.com/gin-gonic/gin"

mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/internal/datasource/proto"
"github.com/nginx/agent/v3/pkg/files"
"github.com/nginx/agent/v3/pkg/uuid"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/timestamppb"

Expand Down Expand Up @@ -363,8 +363,8 @@ func (cs *CommandService) addConfigApplyEndpoint() {

request := mpi.ManagementPlaneRequest{
MessageMeta: &mpi.MessageMeta{
MessageId: uuid.GenerateUUIDV7(),
CorrelationId: uuid.GenerateUUIDV7(),
MessageId: proto.GenerateMessageID(),
CorrelationId: proto.GenerateMessageID(),
Timestamp: timestamppb.Now(),
},
Request: &mpi.ManagementPlaneRequest_ConfigApplyRequest{
Expand Down
4 changes: 2 additions & 2 deletions test/mock/grpc/mock_management_file_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"strconv"

"github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/pkg/uuid"
"github.com/nginx/agent/v3/internal/datasource/proto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
Expand Down Expand Up @@ -76,7 +76,7 @@ func (mgs *FileService) UpdateOverview(

configUploadRequest := &v1.ManagementPlaneRequest{
MessageMeta: &v1.MessageMeta{
MessageId: uuid.GenerateUUIDV7(),
MessageId: proto.GenerateMessageID(),
CorrelationId: request.GetMessageMeta().GetCorrelationId(),
Timestamp: timestamppb.Now(),
},
Expand Down
10 changes: 6 additions & 4 deletions test/protos/data_plane_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ package protos

import (
mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/pkg/uuid"
"github.com/nginx/agent/v3/internal/datasource/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)

const success = "Success"

func OKDataPlaneResponse() *mpi.DataPlaneResponse {
return &mpi.DataPlaneResponse{
MessageMeta: &mpi.MessageMeta{
MessageId: uuid.GenerateUUIDV7(),
CorrelationId: uuid.GenerateUUIDV7(),
MessageId: proto.GenerateMessageID(),
CorrelationId: proto.GenerateMessageID(),
Timestamp: timestamppb.Now(),
},
CommandResponse: &mpi.CommandResponse{
Status: mpi.CommandResponse_COMMAND_STATUS_OK,
Message: "Success",
Message: success,
},
InstanceId: ossInstanceID,
}
Expand Down

0 comments on commit befb601

Please sign in to comment.