Skip to content

Commit c5bbfec

Browse files
committed
worked on PR comments
1 parent a5e6be4 commit c5bbfec

File tree

2 files changed

+104
-7
lines changed

2 files changed

+104
-7
lines changed

internal/datasource/config/nginx_config_parser.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
260260
nginxConfigContext.PlusAPIs = append(nginxConfigContext.PlusAPIs, plusAPIs...)
261261
}
262262

263-
nginxConfigContext.PlusAPIs = ncp.sortPlusAPIs(nginxConfigContext.PlusAPIs)
263+
nginxConfigContext.PlusAPIs = ncp.sortPlusAPIs(ctx, nginxConfigContext.PlusAPIs)
264264

265265
if len(napSyslogServersFound) > 0 {
266266
var napSyslogServer []string
@@ -927,7 +927,21 @@ func (ncp *NginxConfigParser) isWriteEnabled(locChild *crossplane.Directive) boo
927927
return false
928928
}
929929

930-
func (ncp *NginxConfigParser) sortPlusAPIs(apis []*model.APIDetails) []*model.APIDetails {
930+
// sort the API endpoints by prioritising any API that has 'write=on'.
931+
func (ncp *NginxConfigParser) sortPlusAPIs(ctx context.Context, apis []*model.APIDetails) []*model.APIDetails {
932+
foundWriteEnabled := false
933+
for _, api := range apis {
934+
if api.WriteEnabled {
935+
foundWriteEnabled = true
936+
break
937+
}
938+
}
939+
940+
if !foundWriteEnabled && len(apis) > 0 {
941+
slog.WarnContext(ctx, "No write-enabled NGINX Plus API found. Defaulting to read-only API")
942+
return apis
943+
}
944+
931945
slices.SortFunc(apis, func(a, b *model.APIDetails) int {
932946
if a.WriteEnabled && !b.WriteEnabled {
933947
return -1

internal/datasource/config/nginx_config_parser_test.go

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,54 @@ server {
293293
allow 127.0.0.1;
294294
deny all;
295295
}
296+
}`
297+
testConf24 = `server {
298+
listen 127.0.0.1:9090;
299+
location /writeapi {
300+
api write=off;
301+
allow 127.0.0.1;
302+
deny all;
303+
}
304+
}
305+
server {
306+
listen 127.0.0.1:9091;
307+
location /writeapi {
308+
api write=off;
309+
allow 127.0.0.1;
310+
deny all;
311+
}
312+
}`
313+
testConf25 = `server {
314+
listen 127.0.0.1:9090;
315+
location /writeapi {
316+
api write=off;
317+
allow 127.0.0.1;
318+
deny all;
319+
}
320+
}
321+
server {
322+
listen 127.0.0.1:9091;
323+
location /writeapi {
324+
api write=off;
325+
allow 127.0.0.1;
326+
deny all;
327+
}
328+
}
329+
server {
330+
listen 127.0.0.1:9092;
331+
location /writeapi {
332+
api write=on;
333+
allow 127.0.0.1;
334+
deny all;
335+
}
336+
}
337+
server {
338+
listen 127.0.0.1:9093;
339+
location /writeapi {
340+
api write=off;
341+
allow 127.0.0.1;
342+
deny all;
343+
}
296344
}`
297345
)
298346

@@ -813,13 +861,15 @@ func TestNginxConfigParser_checkLog(t *testing.T) {
813861
}
814862
}
815863

864+
//nolint:maintidx // Does not make sense to add a new test case to do the exact same checks.
816865
func TestNginxConfigParser_urlsForLocationDirective(t *testing.T) {
817866
tmpDir := t.TempDir()
818867
tests := []struct {
819-
oss *model.APIDetails
820-
plus *model.APIDetails
821-
name string
822-
conf string
868+
oss *model.APIDetails
869+
plus *model.APIDetails
870+
name string
871+
conf string
872+
expectedLog string
823873
}{
824874
{
825875
plus: &model.APIDetails{
@@ -1048,11 +1098,34 @@ func TestNginxConfigParser_urlsForLocationDirective(t *testing.T) {
10481098
name: "Test 23: Explicitly Write-Enabled Plus API",
10491099
conf: testConf23,
10501100
},
1101+
{
1102+
plus: &model.APIDetails{
1103+
URL: "http://localhost:9090/writeapi",
1104+
Listen: "localhost:9090",
1105+
Location: "/writeapi",
1106+
WriteEnabled: false,
1107+
},
1108+
name: "Test 24: Multiple Plus APIs, all with Write=off, keep the order",
1109+
expectedLog: "No write-enabled NGINX Plus API found. Defaulting to read-only API",
1110+
conf: testConf24,
1111+
},
1112+
{
1113+
plus: &model.APIDetails{
1114+
URL: "http://localhost:9092/writeapi",
1115+
Listen: "localhost:9092",
1116+
Location: "/writeapi",
1117+
WriteEnabled: true,
1118+
},
1119+
name: "Test 25: Multiple Plus APIs, Prioritize Write-Enabled (9092)",
1120+
conf: testConf25,
1121+
},
10511122
}
10521123

10531124
for _, test := range tests {
10541125
t.Run(test.name, func(t *testing.T) {
10551126
ctx := context.Background()
1127+
logBuf := &bytes.Buffer{}
1128+
stub.StubLoggerWith(logBuf)
10561129
f, err := os.CreateTemp(tmpDir, "conf")
10571130
require.NoError(t, err)
10581131
parseOptions := &crossplane.ParseOptions{
@@ -1074,6 +1147,9 @@ func TestNginxConfigParser_urlsForLocationDirective(t *testing.T) {
10741147
if test.plus != nil {
10751148
assert.Equal(t, test.plus, plus[0])
10761149
}
1150+
helpers.ValidateLog(t, test.expectedLog, logBuf)
1151+
1152+
logBuf.Reset()
10771153
})
10781154
}
10791155
}
@@ -1085,6 +1161,8 @@ func traverseConfigForAPIs(
10851161

10861162
ncp := NewNginxConfigParser(types.AgentConfig())
10871163

1164+
allPlusAPIs := []*model.APIDetails{}
1165+
10881166
assert.Len(t, payload.Config, 1)
10891167
for _, xpConf := range payload.Config {
10901168
assert.Len(t, xpConf.Parsed, 1)
@@ -1096,14 +1174,19 @@ func traverseConfigForAPIs(
10961174
}
10971175
_plus := ncp.apiDetailsFromLocationDirective(ctx, parent, directive, plusAPIDirective)
10981176
if _plus != nil {
1099-
plus = _plus
1177+
allPlusAPIs = append(allPlusAPIs, _plus...)
11001178
}
11011179

11021180
return nil
11031181
})
11041182
require.NoError(t, err)
11051183
}
11061184

1185+
if len(allPlusAPIs) > 0 {
1186+
sortedAPIs := ncp.sortPlusAPIs(ctx, allPlusAPIs)
1187+
plus = []*model.APIDetails{sortedAPIs[0]}
1188+
}
1189+
11071190
return oss, plus
11081191
}
11091192

0 commit comments

Comments
 (0)