Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion sdk/proto/events/event.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 10 additions & 11 deletions src/core/payloads/register_software_details.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
package payloads

import (
"sync"

"github.com/nginx/agent/sdk/v2/proto"
)

// RegisterWithDataplaneSoftwareDetailsPayload is an internal payload meant to be used as
// part of registration when there are plugins reporting software details.
type RegisterWithDataplaneSoftwareDetailsPayload struct {
dataplaneSoftwareDetails map[string]*proto.DataplaneSoftwareDetails
dataplaneSoftwareDetailsMutex sync.Mutex
dataplaneSoftwareDetails *proto.DataplaneSoftwareDetails
pluginName string
}

// NewRegisterWithDataplaneSoftwareDetailsPayload returns a pointer to an instance of a
// RegisterWithDataplaneSoftwareDetailsPayload object.
func NewRegisterWithDataplaneSoftwareDetailsPayload(details map[string]*proto.DataplaneSoftwareDetails) *RegisterWithDataplaneSoftwareDetailsPayload {
func NewRegisterWithDataplaneSoftwareDetailsPayload(pluginName string, details *proto.DataplaneSoftwareDetails) *RegisterWithDataplaneSoftwareDetailsPayload {
return &RegisterWithDataplaneSoftwareDetailsPayload{
dataplaneSoftwareDetails: details,
pluginName: pluginName,
}
}

// AddDataplaneSoftwareDetails adds the dataplane software details passed into the function to
// the dataplane software details map object that has been sent as part of the payload.
func (p *RegisterWithDataplaneSoftwareDetailsPayload) AddDataplaneSoftwareDetails(pluginName string, details *proto.DataplaneSoftwareDetails) {
p.dataplaneSoftwareDetailsMutex.Lock()
p.dataplaneSoftwareDetails[pluginName] = details
p.dataplaneSoftwareDetailsMutex.Unlock()
func (r *RegisterWithDataplaneSoftwareDetailsPayload) GetPluginName() string {
return r.pluginName
}

func (r *RegisterWithDataplaneSoftwareDetailsPayload) GetDataplaneSoftwareDetails() *proto.DataplaneSoftwareDetails {
return r.dataplaneSoftwareDetails
}
3 changes: 2 additions & 1 deletion src/plugins/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func TestExtensions_Process(t *testing.T) {
time.Sleep(250 * time.Millisecond)

processedMessages := messagePipe.GetProcessedMessages()
assert.Equal(t, 1, len(processedMessages))
assert.GreaterOrEqual(t, len(processedMessages), 1)
assert.Equal(t, core.EnableExtension, processedMessages[0].Topic())

assert.Equal(t, 2, len(messagePipe.GetPlugins()))
assert.Equal(t, "Extensions Plugin", messagePipe.GetPlugins()[0].Info().Name())
Expand Down
28 changes: 10 additions & 18 deletions src/plugins/nginx_app_protect.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,14 @@ func (n *NginxAppProtect) Init(pipeline core.MessagePipeInterface) {
ctx, cancel := context.WithCancel(n.messagePipeline.Context())
n.ctx = ctx
n.ctxCancel = cancel
n.addSoftwareDetailsToRegistration()
go n.monitor()
}

func (n *NginxAppProtect) Process(msg *core.Message) {
switch {
case msg.Exact(core.RegisterWithDataplaneSoftwareDetails):
n.addSoftwareDetailsToRegistration(msg)
}
}
func (n *NginxAppProtect) Process(msg *core.Message) {}

func (n *NginxAppProtect) Subscriptions() []string {
return []string{core.RegisterWithDataplaneSoftwareDetails}
return []string{}
}

func (n *NginxAppProtect) Close() {
Expand All @@ -88,18 +84,14 @@ func (n *NginxAppProtect) Close() {
// addSoftwareDetailsToRegistration adds the dataplane software details produced by the
// NAP plugin to the OneTimeRegistration dataplane software details map that has been sent
// as part of the message.
func (n *NginxAppProtect) addSoftwareDetailsToRegistration(msg *core.Message) {
switch commandData := msg.Data().(type) {
case *payloads.RegisterWithDataplaneSoftwareDetailsPayload:
log.Debugf("%s is adding software details to registration", napPluginName)
napReport := n.generateNAPDetailsProtoCommand()
napSoftwareDetails := &proto.DataplaneSoftwareDetails{
Data: napReport,
}
commandData.AddDataplaneSoftwareDetails(napPluginName, napSoftwareDetails)
default:
log.Errorf("Expected the type %T but got %T", payloads.RegisterWithDataplaneSoftwareDetailsPayload{}, commandData)
func (n *NginxAppProtect) addSoftwareDetailsToRegistration() {
log.Debugf("%s is adding software details to registration", napPluginName)
napReport := n.generateNAPDetailsProtoCommand()
napSoftwareDetails := &proto.DataplaneSoftwareDetails{
Data: napReport,
}

n.messagePipeline.Process(core.NewMessage(core.RegisterWithDataplaneSoftwareDetails, payloads.NewRegisterWithDataplaneSoftwareDetailsPayload(napPluginName, napSoftwareDetails)))
}

// monitor Monitors the system for any changes related to NAP, if any changes are detected
Expand Down
7 changes: 7 additions & 0 deletions src/plugins/nginx_app_protect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/nginx/agent/sdk/v2/proto"
"github.com/nginx/agent/v2/src/core"
"github.com/nginx/agent/v2/src/core/config"
"github.com/nginx/agent/v2/src/core/payloads"
"github.com/nginx/agent/v2/src/extensions/nginx-app-protect/nap"
tutils "github.com/nginx/agent/v2/test/utils"
)
Expand Down Expand Up @@ -97,4 +98,10 @@ func TestNginxAppProtect(t *testing.T) {
currentNAPPluginDetails = napPlugin.generateNAPDetailsProtoCommand()
assert.Equal(t, testNAPDetailsDegraded, currentNAPPluginDetails)
})

t.Run("software details are sent on startup", func(t *testing.T) {
assert.Equal(t, core.RegisterWithDataplaneSoftwareDetails, messagePipe.GetProcessedMessages()[0].Topic())
assert.Equal(t, "Nginx App Protect", messagePipe.GetProcessedMessages()[0].Data().(*payloads.RegisterWithDataplaneSoftwareDetailsPayload).GetPluginName())
assert.NotNil(t, messagePipe.GetProcessedMessages()[0].Data().(*payloads.RegisterWithDataplaneSoftwareDetailsPayload).GetDataplaneSoftwareDetails())
})
}
75 changes: 43 additions & 32 deletions src/plugins/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package plugins
import (
"context"
"fmt"
"sync"
"time"

"github.com/gogo/protobuf/types"
Expand All @@ -23,15 +24,16 @@ const (
)

type OneTimeRegistration struct {
agentVersion string
tags *[]string
meta *proto.Metadata
config *config.Config
env core.Environment
host *proto.HostInfo
binary core.NginxBinary
dataplaneSoftwareDetails map[string]*proto.DataplaneSoftwareDetails
pipeline core.MessagePipeInterface
agentVersion string
tags *[]string
meta *proto.Metadata
config *config.Config
env core.Environment
host *proto.HostInfo
binary core.NginxBinary
dataplaneSoftwareDetails map[string]*proto.DataplaneSoftwareDetails
pipeline core.MessagePipeInterface
dataplaneSoftwareDetailsMutex sync.Mutex
}

func NewOneTimeRegistration(
Expand All @@ -44,14 +46,15 @@ func NewOneTimeRegistration(
// this might be slow so do on startup
host := env.NewHostInfo(version, &config.Tags, config.ConfigDirs, true)
return &OneTimeRegistration{
tags: &config.Tags,
agentVersion: version,
meta: meta,
config: config,
env: env,
host: host,
binary: binary,
dataplaneSoftwareDetails: make(map[string]*proto.DataplaneSoftwareDetails),
tags: &config.Tags,
agentVersion: version,
meta: meta,
config: config,
env: env,
host: host,
binary: binary,
dataplaneSoftwareDetails: make(map[string]*proto.DataplaneSoftwareDetails),
dataplaneSoftwareDetailsMutex: sync.Mutex{},
}
}

Expand All @@ -73,18 +76,33 @@ func (r *OneTimeRegistration) Process(msg *core.Message) {
switch {
case msg.Exact(core.RegistrationCompletedTopic):
log.Info("OneTimeRegistration completed")
case msg.Exact(core.RegisterWithDataplaneSoftwareDetails):
switch data := msg.Data().(type) {
case *payloads.RegisterWithDataplaneSoftwareDetailsPayload:
r.dataplaneSoftwareDetailsMutex.Lock()
defer r.dataplaneSoftwareDetailsMutex.Unlock()
r.dataplaneSoftwareDetails[data.GetPluginName()] = data.GetDataplaneSoftwareDetails()
}
}
}

func (r *OneTimeRegistration) Subscriptions() []string {
return []string{core.RegistrationCompletedTopic}
return []string{
core.RegistrationCompletedTopic,
core.RegisterWithDataplaneSoftwareDetails,
}
}

func (r *OneTimeRegistration) startRegistration() {
// Check if there are any plugins that will report dataplane software details upon registration and
// if they have already reported their details or not.
if pluginsReportingDataplaneSoftwareDetails(*r.config) && r.dataplaneSoftwareDetailsMissing() {
r.registerWithDataplaneSoftwareDetails()
r.dataplaneSoftwareDetailsMutex.Lock()
defer r.dataplaneSoftwareDetailsMutex.Unlock()
for _, plugin := range getPluginsReportingDataplaneSoftwareDetails(*r.config) {
r.dataplaneSoftwareDetails[plugin] = nil
}
go r.waitAndRegister()
return
}
r.registerAgent()
Expand Down Expand Up @@ -152,19 +170,6 @@ func (r *OneTimeRegistration) registerAgent() {
)
}

// registerWithDataplaneSoftwareDetails Attempts to ensure that the plugins enabled that transmit dataplane
// software details have transmitted their details to OneTimeRegistration then registers.
func (r *OneTimeRegistration) registerWithDataplaneSoftwareDetails() {
for _, plugin := range getPluginsReportingDataplaneSoftwareDetails(*r.config) {
r.dataplaneSoftwareDetails[plugin] = nil
}

registrationPayload := payloads.NewRegisterWithDataplaneSoftwareDetailsPayload(r.dataplaneSoftwareDetails)
r.pipeline.Process(core.NewMessage(core.RegisterWithDataplaneSoftwareDetails, registrationPayload))

go r.waitAndRegister()
}

// waitAndRegister checks in a retry loop if the plugins enabled that transmit dataplane
// software details have transmitted their details to OneTimeRegistration then registers.
// If the plugins do not successfully transmit their details before the max retries is
Expand All @@ -190,6 +195,8 @@ func (r *OneTimeRegistration) waitAndRegister() {
func (r *OneTimeRegistration) dataplaneSoftwareDetailsReady() error {
pluginsMissingDetails := []string{}

r.dataplaneSoftwareDetailsMutex.Lock()
defer r.dataplaneSoftwareDetailsMutex.Unlock()
for pluginName, detailsReported := range r.dataplaneSoftwareDetails {
if detailsReported == nil {
pluginsMissingDetails = append(pluginsMissingDetails, pluginName)
Expand All @@ -209,6 +216,8 @@ func (r *OneTimeRegistration) dataplaneSoftwareDetailsReady() error {
// transmit dataplane software details have already transmitted their details to
// OneTimeRegistration. If they have then false is returned, if not then true is returned.
func (r *OneTimeRegistration) dataplaneSoftwareDetailsMissing() bool {
r.dataplaneSoftwareDetailsMutex.Lock()
defer r.dataplaneSoftwareDetailsMutex.Unlock()
for _, plugin := range getPluginsReportingDataplaneSoftwareDetails(*r.config) {
if _, ok := r.dataplaneSoftwareDetails[plugin]; !ok {
return true
Expand All @@ -222,6 +231,8 @@ func (r *OneTimeRegistration) dataplaneSoftwareDetailsMissing() bool {
func (r *OneTimeRegistration) dataplaneSoftwareDetailsSlice() []*proto.DataplaneSoftwareDetails {
allDetails := []*proto.DataplaneSoftwareDetails{}

r.dataplaneSoftwareDetailsMutex.Lock()
defer r.dataplaneSoftwareDetailsMutex.Unlock()
for _, details := range r.dataplaneSoftwareDetails {
if details != nil {
allDetails = append(allDetails, details)
Expand Down
25 changes: 1 addition & 24 deletions src/plugins/registration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,6 @@ func TestRegistration_Process(t *testing.T) {
}
}

func TestRegistration_RegisterWithDataplaneSoftwareDetails(t *testing.T) {
cfg := &config.Config{
NginxAppProtect: config.NginxAppProtect{
ReportInterval: time.Duration(1) * time.Millisecond,
},
}

pluginUnderTest := NewOneTimeRegistration(cfg, tutils.GetMockNginxBinary(), tutils.GetMockEnvWithHostAndProcess(), nil, "")
messagePipe := core.SetupMockMessagePipe(t, context.TODO(), pluginUnderTest)

expectedMessages := []string{core.RegisterWithDataplaneSoftwareDetails}

messagePipe.Run()

messages := messagePipe.GetProcessedMessages()
assert.Len(t, messages, len(expectedMessages))

for idx, message := range messages {
assert.EqualValues(t, expectedMessages[idx], message.Topic())
}
messagePipe.ClearMessages()
}

func TestRegistration_DataplaneReady(t *testing.T) {
conf := tutils.GetMockAgentConfig()
conf.NginxAppProtect = config.NginxAppProtect{ReportInterval: time.Duration(15) * time.Second}
Expand All @@ -96,7 +73,7 @@ func TestRegistration_DataplaneReady(t *testing.T) {
func TestRegistration_Subscriptions(t *testing.T) {
pluginUnderTest := NewOneTimeRegistration(tutils.GetMockAgentConfig(), nil, tutils.GetMockEnv(), nil, "")

assert.Equal(t, []string{core.RegistrationCompletedTopic}, pluginUnderTest.Subscriptions())
assert.Equal(t, []string{core.RegistrationCompletedTopic, core.RegisterWithDataplaneSoftwareDetails}, pluginUnderTest.Subscriptions())
}

func TestRegistration_Info(t *testing.T) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading