Skip to content

Commit c9fd4b2

Browse files
authored
Fix NGINX stub_status and plus api discovery (#1247)
1 parent d267a13 commit c9fd4b2

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

internal/datasource/config/nginx_config_parser.go

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ const (
3737
" \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\""
3838
ltsvArg = "ltsv"
3939
defaultNumberOfDirectiveArguments = 2
40-
plusAPIName = "plus"
41-
stubStatusAPIName = "stub status"
4240
plusAPIDirective = "api"
4341
stubStatusAPIDirective = "stub_status"
4442
unixStubStatusFormat = "http://config-status%s"
@@ -66,7 +64,7 @@ var _ ConfigParser = (*NginxConfigParser)(nil)
6664
type (
6765
crossplaneTraverseCallback = func(ctx context.Context, parent, current *crossplane.Directive) error
6866
crossplaneTraverseCallbackAPIDetails = func(ctx context.Context, parent,
69-
current *crossplane.Directive, apiType string) *model.APIDetails
67+
current *crossplane.Directive, apiType string) []*model.APIDetails
7068
)
7169

7270
func NewNginxConfigParser(agentConfig *config.Config) *NginxConfigParser {
@@ -111,7 +109,7 @@ func (ncp *NginxConfigParser) FindStubStatusAPI(
111109
) *model.APIDetails {
112110
for _, stubStatus := range nginxConfigContext.StubStatuses {
113111
if stubStatus != nil && stubStatus.URL != "" {
114-
if ncp.pingAPIEndpoint(ctx, stubStatus, stubStatusAPIName) {
112+
if ncp.pingAPIEndpoint(ctx, stubStatus, stubStatusAPIDirective) {
115113
slog.InfoContext(ctx, "Found NGINX stub status API", "url", stubStatus.URL)
116114
return stubStatus
117115
}
@@ -131,7 +129,7 @@ func (ncp *NginxConfigParser) FindPlusAPI(
131129
) *model.APIDetails {
132130
for _, plusAPI := range nginxConfigContext.PlusAPIs {
133131
if plusAPI != nil && plusAPI.URL != "" {
134-
if ncp.pingAPIEndpoint(ctx, plusAPI, plusAPIName) {
132+
if ncp.pingAPIEndpoint(ctx, plusAPI, plusAPIDirective) {
135133
slog.InfoContext(ctx, "Found NGINX Plus API", "url", plusAPI.URL)
136134
return plusAPI
137135
}
@@ -385,12 +383,12 @@ func (ncp *NginxConfigParser) crossplaneConfigTraverseAPIDetails(
385383
for _, dir := range root.Parsed {
386384
response := callback(ctx, nil, dir, apiType)
387385
if response != nil {
388-
responses = append(responses, response)
386+
responses = append(responses, response...)
389387
continue
390388
}
391389
response = traverseAPIDetails(ctx, dir, callback, &stop, apiType)
392390
if response != nil {
393-
responses = append(responses, response)
391+
responses = append(responses, response...)
394392
}
395393
}
396394

@@ -403,14 +401,14 @@ func traverseAPIDetails(
403401
callback crossplaneTraverseCallbackAPIDetails,
404402
stop *bool,
405403
apiType string,
406-
) (response *model.APIDetails) {
404+
) (response []*model.APIDetails) {
407405
if *stop {
408406
return nil
409407
}
410408

411409
for _, child := range root.Block {
412410
response = callback(ctx, root, child, apiType)
413-
if response != nil && response.URL != "" {
411+
if len(response) > 0 {
414412
*stop = true
415413
return response
416414
}
@@ -573,9 +571,9 @@ func (ncp *NginxConfigParser) sslCert(ctx context.Context, file, rootDir string)
573571

574572
func (ncp *NginxConfigParser) apiCallback(
575573
ctx context.Context, parent, current *crossplane.Directive, apiType string,
576-
) *model.APIDetails {
577-
details := ncp.apiDetailsFromLocationDirective(ctx, parent, current, apiType)
578-
if details != nil {
574+
) (details []*model.APIDetails) {
575+
details = append(details, ncp.apiDetailsFromLocationDirective(ctx, parent, current, apiType)...)
576+
if len(details) > 0 {
579577
slog.DebugContext(ctx, "Found "+apiType, "api_details", details)
580578
}
581579

@@ -679,7 +677,7 @@ func validateAPIResponse(apiType string, bodyBytes []byte) error {
679677
func (ncp *NginxConfigParser) apiDetailsFromLocationDirective(
680678
ctx context.Context, parent, current *crossplane.Directive,
681679
locationDirectiveName string,
682-
) (details *model.APIDetails) {
680+
) (details []*model.APIDetails) {
683681
// Check if SSL is enabled in the server block
684682
isSSL := ncp.isSSLEnabled(parent)
685683

@@ -698,11 +696,16 @@ func (ncp *NginxConfigParser) apiDetailsFromLocationDirective(
698696
continue
699697
}
700698

701-
address := ncp.parseAddressFromServerDirective(parent)
699+
addresses := ncp.parseAddressFromServerDirective(parent)
702700
path := ncp.parsePathFromLocationDirective(current)
703701

704702
if locChild.Directive == locationDirectiveName {
705-
details = ncp.createAPIDetails(locationDirectiveName, address, path, caCertLocation, isSSL)
703+
for _, address := range addresses {
704+
details = append(
705+
details,
706+
ncp.createAPIDetails(locationDirectiveName, address, path, caCertLocation, isSSL),
707+
)
708+
}
706709
}
707710
}
708711

@@ -738,21 +741,24 @@ func (ncp *NginxConfigParser) createAPIDetails(
738741
return details
739742
}
740743

741-
func (ncp *NginxConfigParser) parseAddressFromServerDirective(parent *crossplane.Directive) string {
744+
func (ncp *NginxConfigParser) parseAddressFromServerDirective(parent *crossplane.Directive) (addresses []string) {
742745
port := "80"
743-
host := "localhost"
746+
hosts := []string{"localhost", "127.0.0.1"}
744747

745748
if parent == nil {
746-
return ""
749+
return addresses
747750
}
748751

749752
for _, dir := range parent.Block {
750753
if dir.Directive == "listen" {
751-
port, host = ncp.parseListenDirectiveAddress(dir, port, host)
754+
for _, host := range hosts {
755+
port, host = ncp.parseListenDirectiveAddress(dir, port, host)
756+
addresses = append(addresses, host+":"+port)
757+
}
752758
}
753759
}
754760

755-
return host + ":" + port
761+
return addresses
756762
}
757763

758764
func (ncp *NginxConfigParser) parseListenDirectiveAddress(

internal/datasource/config/nginx_config_parser_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,15 +1036,19 @@ func TestNginxConfigParser_urlsForLocationDirective(t *testing.T) {
10361036

10371037
oss, plus := traverseConfigForAPIs(t, ctx, payload)
10381038

1039-
assert.Equal(t, test.plus, plus)
1040-
assert.Equal(t, test.oss, oss)
1039+
if test.oss != nil {
1040+
assert.Equal(t, test.oss, oss[0])
1041+
}
1042+
if test.plus != nil {
1043+
assert.Equal(t, test.plus, plus[0])
1044+
}
10411045
})
10421046
}
10431047
}
10441048

10451049
func traverseConfigForAPIs(
10461050
t *testing.T, ctx context.Context, payload *crossplane.Payload,
1047-
) (oss, plus *model.APIDetails) {
1051+
) (oss, plus []*model.APIDetails) {
10481052
t.Helper()
10491053

10501054
ncp := NewNginxConfigParser(types.AgentConfig())

0 commit comments

Comments
 (0)