diff --git a/internal/command/command_plugin.go b/internal/command/command_plugin.go index 304b74a5f..6a950bda0 100644 --- a/internal/command/command_plugin.go +++ b/internal/command/command_plugin.go @@ -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/id" ) var _ bus.Plugin = (*CommandPlugin)(nil) @@ -275,7 +275,7 @@ func (cp *CommandPlugin) createDataPlaneResponse(correlationID string, status mp ) *mpi.DataPlaneResponse { return &mpi.DataPlaneResponse{ MessageMeta: &mpi.MessageMeta{ - MessageId: proto.GenerateMessageID(), + MessageId: id.GenerateMessageID(), CorrelationId: correlationID, Timestamp: timestamppb.Now(), }, diff --git a/internal/command/command_plugin_test.go b/internal/command/command_plugin_test.go index ee36f5d73..c98713f9f 100644 --- a/internal/command/command_plugin_test.go +++ b/internal/command/command_plugin_test.go @@ -12,8 +12,8 @@ import ( "time" pkg "github.com/nginx/agent/v3/pkg/config" + "github.com/nginx/agent/v3/pkg/id" - "github.com/nginx/agent/v3/internal/datasource/proto" "google.golang.org/protobuf/types/known/timestamppb" "github.com/nginx/agent/v3/internal/bus/busfakes" @@ -340,7 +340,7 @@ func TestMonitorSubscribeChannel(t *testing.T) { func Test_createDataPlaneResponse(t *testing.T) { expected := &mpi.DataPlaneResponse{ MessageMeta: &mpi.MessageMeta{ - MessageId: proto.GenerateMessageID(), + MessageId: id.GenerateMessageID(), CorrelationId: "dfsbhj6-bc92-30c1-a9c9-85591422068e", Timestamp: timestamppb.Now(), }, diff --git a/internal/command/command_service.go b/internal/command/command_service.go index 61127db9e..7dd20c966 100644 --- a/internal/command/command_service.go +++ b/internal/command/command_service.go @@ -20,9 +20,9 @@ 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/id" "google.golang.org/protobuf/types/known/timestamppb" @@ -96,7 +96,7 @@ func (cs *CommandService) UpdateDataPlaneStatus( request := &mpi.UpdateDataPlaneStatusRequest{ MessageMeta: &mpi.MessageMeta{ - MessageId: proto.GenerateMessageID(), + MessageId: id.GenerateMessageID(), CorrelationId: correlationID, Timestamp: timestamppb.Now(), }, @@ -150,7 +150,7 @@ func (cs *CommandService) UpdateDataPlaneHealth(ctx context.Context, instanceHea request := &mpi.UpdateDataPlaneHealthRequest{ MessageMeta: &mpi.MessageMeta{ - MessageId: proto.GenerateMessageID(), + MessageId: id.GenerateMessageID(), CorrelationId: correlationID, Timestamp: timestamppb.Now(), }, @@ -233,7 +233,7 @@ func (cs *CommandService) CreateConnection( request := &mpi.CreateConnectionRequest{ MessageMeta: &mpi.MessageMeta{ - MessageId: proto.GenerateMessageID(), + MessageId: id.GenerateMessageID(), CorrelationId: correlationID, Timestamp: timestamppb.Now(), }, @@ -332,7 +332,7 @@ func (cs *CommandService) sendResponseForQueuedConfigApplyRequests( for i := 0; i < indexOfConfigApplyRequest; i++ { newResponse := response - newResponse.GetMessageMeta().MessageId = proto.GenerateMessageID() + newResponse.GetMessageMeta().MessageId = id.GenerateMessageID() request := cs.configApplyRequestQueue[instanceID][i] newResponse.GetMessageMeta().CorrelationId = request.GetMessageMeta().GetCorrelationId() @@ -525,7 +525,7 @@ func (cs *CommandService) checkIfInstanceExists( response := &mpi.DataPlaneResponse{ MessageMeta: &mpi.MessageMeta{ - MessageId: proto.GenerateMessageID(), + MessageId: id.GenerateMessageID(), CorrelationId: request.GetMessageMeta().GetCorrelationId(), Timestamp: timestamppb.Now(), }, diff --git a/internal/config/config.go b/internal/config/config.go index d99dada1c..0ab7eb557 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -17,8 +17,8 @@ import ( "strconv" "strings" + uuidLibrary "github.com/nginx/agent/v3/pkg/id" selfsignedcerts "github.com/nginx/agent/v3/pkg/tls" - uuidLibrary "github.com/nginx/agent/v3/pkg/uuid" "github.com/spf13/cobra" flag "github.com/spf13/pflag" "github.com/spf13/viper" diff --git a/internal/datasource/proto/message.go b/internal/datasource/proto/message.go deleted file mode 100644 index a665355fc..000000000 --- a/internal/datasource/proto/message.go +++ /dev/null @@ -1,52 +0,0 @@ -// 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" - - mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1" - "google.golang.org/protobuf/types/known/timestamppb" - - "github.com/google/uuid" - agentUuid "github.com/nginx/agent/v3/pkg/uuid" -) - -// UUIDGenerator defines a function type for generating UUIDs. -type UUIDGenerator func() (uuid.UUID, error) - -// DefaultUUIDGenerator is the production implementation for generating UUIDv7. -var defaultUUIDGenerator UUIDGenerator = uuid.NewUUID - -// GenerateMessageID generates a unique message ID, falling back to sha256 and timestamp if UUID generation fails. -func GenerateMessageID() string { - uuidv7, err := defaultUUIDGenerator() - if err != nil { - slog.Debug("Issue generating uuidv7, using sha256 and timestamp instead", "error", err) - return agentUuid.Generate("%s", time.Now().String()) - } - - return uuidv7.String() -} - -func CreateDataPlaneResponse(correlationID string, status mpi.CommandResponse_CommandStatus, - message, instanceID, err string, -) *mpi.DataPlaneResponse { - return &mpi.DataPlaneResponse{ - MessageMeta: &mpi.MessageMeta{ - MessageId: GenerateMessageID(), - CorrelationId: correlationID, - Timestamp: timestamppb.Now(), - }, - CommandResponse: &mpi.CommandResponse{ - Status: status, - Message: message, - Error: err, - }, - InstanceId: instanceID, - } -} diff --git a/internal/datasource/proto/response.go b/internal/datasource/proto/response.go new file mode 100644 index 000000000..f2c778a7f --- /dev/null +++ b/internal/datasource/proto/response.go @@ -0,0 +1,30 @@ +// 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 ( + mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1" + agentid "github.com/nginx/agent/v3/pkg/id" + "google.golang.org/protobuf/types/known/timestamppb" +) + +func CreateDataPlaneResponse(correlationID string, status mpi.CommandResponse_CommandStatus, + message, instanceID, err string, +) *mpi.DataPlaneResponse { + return &mpi.DataPlaneResponse{ + MessageMeta: &mpi.MessageMeta{ + MessageId: agentid.GenerateMessageID(), + CorrelationId: correlationID, + Timestamp: timestamppb.Now(), + }, + CommandResponse: &mpi.CommandResponse{ + Status: status, + Message: message, + Error: err, + }, + InstanceId: instanceID, + } +} diff --git a/internal/file/file_manager_service.go b/internal/file/file_manager_service.go index 466bf01be..f368077da 100644 --- a/internal/file/file_manager_service.go +++ b/internal/file/file_manager_service.go @@ -16,7 +16,6 @@ import ( "sync" "sync/atomic" - "github.com/nginx/agent/v3/internal/datasource/proto" "github.com/nginx/agent/v3/internal/model" "github.com/cenkalti/backoff/v4" @@ -26,6 +25,7 @@ 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/id" "google.golang.org/protobuf/types/known/timestamppb" backoffHelpers "github.com/nginx/agent/v3/internal/backoff" @@ -104,7 +104,7 @@ func (fms *FileManagerService) UpdateOverview( request := &mpi.UpdateOverviewRequest{ MessageMeta: &mpi.MessageMeta{ - MessageId: proto.GenerateMessageID(), + MessageId: id.GenerateMessageID(), CorrelationId: correlationID, Timestamp: timestamppb.Now(), }, @@ -224,7 +224,7 @@ func (fms *FileManagerService) UpdateFile( Contents: contents, }, MessageMeta: &mpi.MessageMeta{ - MessageId: proto.GenerateMessageID(), + MessageId: id.GenerateMessageID(), CorrelationId: correlationID, Timestamp: timestamppb.Now(), }, @@ -382,7 +382,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: proto.GenerateMessageID(), + MessageId: id.GenerateMessageID(), CorrelationId: logger.GetCorrelationID(ctx), Timestamp: timestamppb.Now(), }, diff --git a/internal/file/file_plugin.go b/internal/file/file_plugin.go index 8acfdef00..28a24c6c2 100644 --- a/internal/file/file_plugin.go +++ b/internal/file/file_plugin.go @@ -11,11 +11,11 @@ import ( "log/slog" "github.com/nginx/agent/v3/pkg/files" + "github.com/nginx/agent/v3/pkg/id" 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" @@ -319,7 +319,7 @@ func (fp *FilePlugin) handleConfigUploadRequest(ctx context.Context, msg *bus.Me response := &mpi.DataPlaneResponse{ MessageMeta: &mpi.MessageMeta{ - MessageId: proto.GenerateMessageID(), + MessageId: id.GenerateMessageID(), CorrelationId: correlationID, Timestamp: timestamppb.Now(), }, @@ -343,7 +343,7 @@ func (fp *FilePlugin) createDataPlaneResponse(correlationID string, status mpi.C ) *mpi.DataPlaneResponse { return &mpi.DataPlaneResponse{ MessageMeta: &mpi.MessageMeta{ - MessageId: proto.GenerateMessageID(), + MessageId: id.GenerateMessageID(), CorrelationId: correlationID, Timestamp: timestamppb.Now(), }, diff --git a/internal/file/file_plugin_test.go b/internal/file/file_plugin_test.go index e3e2270a5..8ff0a07b7 100644 --- a/internal/file/file_plugin_test.go +++ b/internal/file/file_plugin_test.go @@ -13,8 +13,6 @@ 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" mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1" @@ -24,6 +22,7 @@ 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/id" "github.com/nginx/agent/v3/test/helpers" "github.com/nginx/agent/v3/test/protos" "github.com/nginx/agent/v3/test/types" @@ -445,7 +444,7 @@ func TestFilePlugin_Process_ConfigApplyRollbackCompleteTopic(t *testing.T) { expectedResponse := &mpi.DataPlaneResponse{ MessageMeta: &mpi.MessageMeta{ - MessageId: proto.GenerateMessageID(), + MessageId: id.GenerateMessageID(), CorrelationId: "dfsbhj6-bc92-30c1-a9c9-85591422068e", Timestamp: timestamppb.Now(), }, diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 811d55820..9a6d7b064 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -14,7 +14,7 @@ import ( "strings" "github.com/nginx/agent/v3/internal/config" - "github.com/nginx/agent/v3/internal/datasource/proto" + "github.com/nginx/agent/v3/pkg/id" ) const ( @@ -121,7 +121,7 @@ func (h contextHandler) observe(ctx context.Context) (as []slog.Attr) { } func GenerateCorrelationID() slog.Attr { - return slog.Any(CorrelationIDKey, proto.GenerateMessageID()) + return slog.Any(CorrelationIDKey, id.GenerateMessageID()) } func GetCorrelationID(ctx context.Context) string { diff --git a/internal/resource/nginx_plus_actions.go b/internal/resource/nginx_plus_actions.go index 84b34d250..e0bcdc819 100644 --- a/internal/resource/nginx_plus_actions.go +++ b/internal/resource/nginx_plus_actions.go @@ -11,7 +11,7 @@ import ( "log/slog" mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1" - "github.com/nginx/agent/v3/internal/datasource/proto" + response "github.com/nginx/agent/v3/internal/datasource/proto" "github.com/nginx/agent/v3/internal/logger" ) @@ -33,7 +33,7 @@ func (a *APIAction) HandleUpdateStreamServersRequest(ctx context.Context, action slog.ErrorContext(ctx, "Unable to update stream servers of upstream", "request", action.GetUpdateHttpUpstreamServers(), "error", err) - return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, + return response.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, "", instanceID, err.Error()) } @@ -41,7 +41,7 @@ func (a *APIAction) HandleUpdateStreamServersRequest(ctx context.Context, action action.GetUpdateHttpUpstreamServers().GetHttpUpstreamName(), "add", len(add), "update", len(update), "delete", len(del)) - return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK, + return response.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK, "Successfully updated stream upstream servers", instanceID, "") } @@ -55,7 +55,7 @@ func (a *APIAction) HandleGetStreamUpstreamsRequest(ctx context.Context, streamUpstreams, err := a.ResourceService.GetStreamUpstreams(ctx, instance) if err != nil { slog.ErrorContext(ctx, "Unable to get stream upstreams", "error", err) - return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, + return response.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, "", instanceID, err.Error()) } @@ -67,7 +67,7 @@ func (a *APIAction) HandleGetStreamUpstreamsRequest(ctx context.Context, streamUpstreamsResponse = string(streamUpstreamsJSON) } - return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK, + return response.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK, streamUpstreamsResponse, instanceID, "") } @@ -80,7 +80,7 @@ func (a *APIAction) HandleGetUpstreamsRequest(ctx context.Context, instance *mpi if err != nil { slog.InfoContext(ctx, "Unable to get upstreams", "error", err) - return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, + return response.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, "", instanceID, err.Error()) } @@ -92,7 +92,7 @@ func (a *APIAction) HandleGetUpstreamsRequest(ctx context.Context, instance *mpi upstreamsResponse = string(upstreamsJSON) } - return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK, + return response.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK, upstreamsResponse, instanceID, "") } @@ -109,7 +109,7 @@ func (a *APIAction) HandleUpdateHTTPUpstreamsRequest(ctx context.Context, action slog.ErrorContext(ctx, "Unable to update HTTP servers of upstream", "request", action.GetUpdateHttpUpstreamServers(), "error", err) - return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, + return response.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, "", instanceID, err.Error()) } @@ -117,7 +117,7 @@ func (a *APIAction) HandleUpdateHTTPUpstreamsRequest(ctx context.Context, action action.GetUpdateHttpUpstreamServers().GetHttpUpstreamName(), "add", len(add), "update", len(update), "delete", len(del)) - return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK, + return response.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK, "Successfully updated HTTP Upstreams", instanceID, "") } @@ -132,7 +132,7 @@ func (a *APIAction) HandleGetHTTPUpstreamsServersRequest(ctx context.Context, ac action.GetGetHttpUpstreamServers().GetHttpUpstreamName()) if err != nil { slog.ErrorContext(ctx, "Unable to get HTTP servers of upstream", "error", err) - return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, + return response.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, "", instanceID, err.Error()) } @@ -144,6 +144,6 @@ func (a *APIAction) HandleGetHTTPUpstreamsServersRequest(ctx context.Context, ac upstreamsResponse = string(upstreamsJSON) } - return proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK, + return response.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK, upstreamsResponse, instanceID, "") } diff --git a/internal/resource/resource_plugin.go b/internal/resource/resource_plugin.go index a32aa3581..9b683b92b 100644 --- a/internal/resource/resource_plugin.go +++ b/internal/resource/resource_plugin.go @@ -13,7 +13,7 @@ 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" + response "github.com/nginx/agent/v3/internal/datasource/proto" "github.com/nginx/agent/v3/internal/logger" "github.com/nginx/agent/v3/internal/model" @@ -168,7 +168,7 @@ func (r *Resource) handleNginxPlusActionRequest(ctx context.Context, action *mpi } if instance == nil { slog.ErrorContext(ctx, "Unable to find instance with ID", "id", instanceID) - resp := proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, + resp := response.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, "", instanceID, fmt.Sprintf("failed to preform API "+ "action, could not find instance with ID: %s", instanceID)) @@ -179,7 +179,7 @@ func (r *Resource) handleNginxPlusActionRequest(ctx context.Context, action *mpi if instance.GetInstanceMeta().GetInstanceType() != mpi.InstanceMeta_INSTANCE_TYPE_NGINX_PLUS { slog.ErrorContext(ctx, "Failed to preform API action", "error", errors.New("instance is not NGINX Plus")) - resp := proto.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, + resp := response.CreateDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, "", instanceID, "failed to preform API action, instance is not NGINX Plus") r.messagePipe.Process(ctx, &bus.Message{Topic: bus.DataPlaneResponseTopic, Data: resp}) @@ -224,23 +224,23 @@ func (r *Resource) handleWriteConfigSuccessful(ctx context.Context, msg *bus.Mes if err != nil { data.Error = err slog.Error("errors found during config apply, sending error status, rolling back config", "err", err) - response := proto.CreateDataPlaneResponse(data.CorrelationID, mpi.CommandResponse_COMMAND_STATUS_ERROR, + dpResponse := response.CreateDataPlaneResponse(data.CorrelationID, mpi.CommandResponse_COMMAND_STATUS_ERROR, "Config apply failed, rolling back config", data.InstanceID, err.Error()) - r.messagePipe.Process(ctx, &bus.Message{Topic: bus.DataPlaneResponseTopic, Data: response}) + r.messagePipe.Process(ctx, &bus.Message{Topic: bus.DataPlaneResponseTopic, Data: dpResponse}) r.messagePipe.Process(ctx, &bus.Message{Topic: bus.ConfigApplyFailedTopic, Data: data}) return } - response := proto.CreateDataPlaneResponse(data.CorrelationID, mpi.CommandResponse_COMMAND_STATUS_OK, + dpResponse := response.CreateDataPlaneResponse(data.CorrelationID, mpi.CommandResponse_COMMAND_STATUS_OK, "Config apply successful", data.InstanceID, "") r.messagePipe.Process( ctx, &bus.Message{ Topic: bus.ConfigApplySuccessfulTopic, - Data: response, + Data: dpResponse, }, ) } @@ -256,10 +256,10 @@ func (r *Resource) handleRollbackWrite(ctx context.Context, msg *bus.Message) { if err != nil { slog.Error("errors found during rollback, sending failure status", "err", err) - rollbackResponse := proto.CreateDataPlaneResponse(data.CorrelationID, + rollbackResponse := response.CreateDataPlaneResponse(data.CorrelationID, mpi.CommandResponse_COMMAND_STATUS_ERROR, "Rollback failed", data.InstanceID, err.Error()) - applyResponse := proto.CreateDataPlaneResponse(data.CorrelationID, + applyResponse := response.CreateDataPlaneResponse(data.CorrelationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, "Config apply failed, rollback failed", data.InstanceID, data.Error.Error()) @@ -269,7 +269,7 @@ func (r *Resource) handleRollbackWrite(ctx context.Context, msg *bus.Message) { return } - applyResponse := proto.CreateDataPlaneResponse(data.CorrelationID, + applyResponse := response.CreateDataPlaneResponse(data.CorrelationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, "Config apply failed, rollback successful", data.InstanceID, data.Error.Error()) diff --git a/internal/watcher/instance/nginx_process_parser.go b/internal/watcher/instance/nginx_process_parser.go index 7ad875af1..d5bcc8144 100644 --- a/internal/watcher/instance/nginx_process_parser.go +++ b/internal/watcher/instance/nginx_process_parser.go @@ -20,7 +20,7 @@ import ( mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1" "github.com/nginx/agent/v3/internal/datasource/host/exec" "github.com/nginx/agent/v3/internal/model" - "github.com/nginx/agent/v3/pkg/uuid" + "github.com/nginx/agent/v3/pkg/id" ) const ( @@ -244,7 +244,7 @@ func convertInfoToInstance(nginxInfo Info) *mpi.Instance { return &mpi.Instance{ InstanceMeta: &mpi.InstanceMeta{ - InstanceId: uuid.Generate("%s_%s_%s", nginxInfo.ExePath, nginxInfo.ConfPath, nginxInfo.Prefix), + InstanceId: id.Generate("%s_%s_%s", nginxInfo.ExePath, nginxInfo.ConfPath, nginxInfo.Prefix), InstanceType: nginxType, Version: version, }, diff --git a/internal/watcher/watcher_plugin_test.go b/internal/watcher/watcher_plugin_test.go index 427b76af2..16a3d4bb1 100644 --- a/internal/watcher/watcher_plugin_test.go +++ b/internal/watcher/watcher_plugin_test.go @@ -11,8 +11,6 @@ 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" "github.com/nginx/agent/v3/internal/watcher/health" @@ -22,6 +20,7 @@ import ( mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1" "github.com/nginx/agent/v3/internal/bus" "github.com/nginx/agent/v3/internal/logger" + "github.com/nginx/agent/v3/pkg/id" "github.com/nginx/agent/v3/test/model" "github.com/nginx/agent/v3/test/protos" "github.com/nginx/agent/v3/test/types" @@ -138,7 +137,7 @@ func TestWatcher_Process_ConfigApplySuccessfulTopic(t *testing.T) { response := &mpi.DataPlaneResponse{ MessageMeta: &mpi.MessageMeta{ - MessageId: proto.GenerateMessageID(), + MessageId: id.GenerateMessageID(), CorrelationId: "dfsbhj6-bc92-30c1-a9c9-85591422068e", Timestamp: timestamppb.Now(), }, @@ -172,7 +171,7 @@ func TestWatcher_Process_RollbackCompleteTopic(t *testing.T) { response := &mpi.DataPlaneResponse{ MessageMeta: &mpi.MessageMeta{ - MessageId: proto.GenerateMessageID(), + MessageId: id.GenerateMessageID(), CorrelationId: "dfsbhj6-bc92-30c1-a9c9-85591422068e", Timestamp: timestamppb.Now(), }, diff --git a/pkg/id/message.go b/pkg/id/message.go new file mode 100644 index 000000000..214b56928 --- /dev/null +++ b/pkg/id/message.go @@ -0,0 +1,28 @@ +// 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 id + +import ( + "time" + + "github.com/google/uuid" +) + +// UUIDGenerator defines a function type for generating UUIDs. +type UUIDGenerator func() (uuid.UUID, error) + +// DefaultUUIDGenerator is the production implementation for generating UUIDv7. +var defaultUUIDGenerator UUIDGenerator = uuid.NewUUID + +// GenerateMessageID generates a unique message ID, falling back to sha256 and timestamp if UUID generation fails. +func GenerateMessageID() string { + uuidv7, err := defaultUUIDGenerator() + if err != nil { + return Generate("%s", time.Now().String()) + } + + return uuidv7.String() +} diff --git a/internal/datasource/proto/message_test.go b/pkg/id/message_test.go similarity index 94% rename from internal/datasource/proto/message_test.go rename to pkg/id/message_test.go index 10418d44b..08f88f4a7 100644 --- a/internal/datasource/proto/message_test.go +++ b/pkg/id/message_test.go @@ -3,7 +3,7 @@ // 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 +package id import ( "bytes" @@ -11,7 +11,6 @@ import ( "regexp" "testing" - "github.com/nginx/agent/v3/test/helpers" "github.com/nginx/agent/v3/test/stub" "github.com/stretchr/testify/assert" @@ -105,9 +104,6 @@ func TestGenerateMessageID(t *testing.T) { got := GenerateMessageID() assert.NotEmpty(t, got) - // Inspect logs - helpers.ValidateLog(t, "Issue generating uuidv7, using sha256 and timestamp instead", logBuf) - logBuf.Reset() } else { got := GenerateMessageID() diff --git a/pkg/uuid/uuid.go b/pkg/id/uuid.go similarity index 98% rename from pkg/uuid/uuid.go rename to pkg/id/uuid.go index b10d5bb88..043b7a534 100644 --- a/pkg/uuid/uuid.go +++ b/pkg/id/uuid.go @@ -3,7 +3,7 @@ // 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 uuid +package id import ( "crypto/sha256" diff --git a/pkg/uuid/uuid_test.go b/pkg/id/uuid_test.go similarity index 97% rename from pkg/uuid/uuid_test.go rename to pkg/id/uuid_test.go index ca843603a..45160767c 100644 --- a/pkg/uuid/uuid_test.go +++ b/pkg/id/uuid_test.go @@ -3,7 +3,7 @@ // 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 uuid +package id import ( "testing" diff --git a/test/mock/grpc/mock_management_command_service.go b/test/mock/grpc/mock_management_command_service.go index f6df717a6..3cae35f43 100644 --- a/test/mock/grpc/mock_management_command_service.go +++ b/test/mock/grpc/mock_management_command_service.go @@ -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/id" "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/types/known/timestamppb" @@ -363,8 +363,8 @@ func (cs *CommandService) addConfigApplyEndpoint() { request := mpi.ManagementPlaneRequest{ MessageMeta: &mpi.MessageMeta{ - MessageId: proto.GenerateMessageID(), - CorrelationId: proto.GenerateMessageID(), + MessageId: id.GenerateMessageID(), + CorrelationId: id.GenerateMessageID(), Timestamp: timestamppb.Now(), }, Request: &mpi.ManagementPlaneRequest_ConfigApplyRequest{ diff --git a/test/mock/grpc/mock_management_file_service.go b/test/mock/grpc/mock_management_file_service.go index b7352edb2..3e42af704 100644 --- a/test/mock/grpc/mock_management_file_service.go +++ b/test/mock/grpc/mock_management_file_service.go @@ -14,7 +14,7 @@ import ( "strconv" "github.com/nginx/agent/v3/api/grpc/mpi/v1" - "github.com/nginx/agent/v3/internal/datasource/proto" + "github.com/nginx/agent/v3/pkg/id" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/protobuf/encoding/protojson" @@ -76,7 +76,7 @@ func (mgs *FileService) UpdateOverview( configUploadRequest := &v1.ManagementPlaneRequest{ MessageMeta: &v1.MessageMeta{ - MessageId: proto.GenerateMessageID(), + MessageId: id.GenerateMessageID(), CorrelationId: request.GetMessageMeta().GetCorrelationId(), Timestamp: timestamppb.Now(), }, diff --git a/test/protos/data_plane_response.go b/test/protos/data_plane_response.go index 5ce05c5c0..9ddda632c 100644 --- a/test/protos/data_plane_response.go +++ b/test/protos/data_plane_response.go @@ -7,7 +7,7 @@ package protos import ( mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1" - "github.com/nginx/agent/v3/internal/datasource/proto" + "github.com/nginx/agent/v3/pkg/id" "google.golang.org/protobuf/types/known/timestamppb" ) @@ -16,8 +16,8 @@ const success = "Success" func OKDataPlaneResponse() *mpi.DataPlaneResponse { return &mpi.DataPlaneResponse{ MessageMeta: &mpi.MessageMeta{ - MessageId: proto.GenerateMessageID(), - CorrelationId: proto.GenerateMessageID(), + MessageId: id.GenerateMessageID(), + CorrelationId: id.GenerateMessageID(), Timestamp: timestamppb.Now(), }, CommandResponse: &mpi.CommandResponse{