Skip to content

Commit f4cf4f0

Browse files
committed
Add config parser tests for SSLCerts
1 parent 497d1f1 commit f4cf4f0

File tree

4 files changed

+252
-0
lines changed

4 files changed

+252
-0
lines changed

internal/watcher/instance/nginx_config_parser_test.go

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http"
1313
"net/http/httptest"
1414
"os"
15+
"sort"
1516
"testing"
1617

1718
"google.golang.org/protobuf/types/known/timestamppb"
@@ -260,6 +261,7 @@ var (
260261
`
261262
)
262263

264+
// nolint: maintidx
263265
func TestNginxConfigParser_Parse(t *testing.T) {
264266
ctx := context.Background()
265267
dir := t.TempDir()
@@ -288,6 +290,20 @@ func TestNginxConfigParser_Parse(t *testing.T) {
288290
fileMetaAllowedFiles, err := files.FileMeta(allowedFile.Name())
289291
require.NoError(t, err)
290292

293+
_, cert := helpers.GenerateSelfSignedCert(t)
294+
certContents := helpers.Cert{Name: "nginx.cert", Type: "CERTIFICATE", Contents: cert}
295+
certFile := helpers.WriteCertFiles(t, dir, certContents)
296+
require.NotNil(t, certFile)
297+
fileMetaCertFiles, err := files.FileMetaWithCertificate(certFile)
298+
require.NoError(t, err)
299+
300+
_, diffCert := helpers.GenerateSelfSignedCert(t)
301+
diffCertContents := helpers.Cert{Name: "nginx1.cert", Type: "CERTIFICATE", Contents: diffCert}
302+
diffCertFile := helpers.WriteCertFiles(t, dir, diffCertContents)
303+
require.NotNil(t, diffCertFile)
304+
diffFileMetaCertFiles, err := files.FileMetaWithCertificate(diffCertFile)
305+
require.NoError(t, err)
306+
291307
tests := []struct {
292308
instance *mpi.Instance
293309
name string
@@ -368,6 +384,125 @@ func TestNginxConfigParser_Parse(t *testing.T) {
368384
},
369385
allowedDirectories: []string{dir},
370386
},
387+
{
388+
name: "Test 4: Check Parser for SSL Certs",
389+
instance: protos.GetNginxPlusInstance([]string{}),
390+
content: testconfig.GetNginxConfigWithSSLCerts(
391+
errorLog.Name(),
392+
accessLog.Name(),
393+
certFile,
394+
),
395+
expectedConfigContext: &model.NginxConfigContext{
396+
StubStatus: &model.APIDetails{},
397+
PlusAPI: &model.APIDetails{},
398+
InstanceID: protos.GetNginxPlusInstance([]string{}).GetInstanceMeta().GetInstanceId(),
399+
Files: []*mpi.File{
400+
{
401+
FileMeta: fileMetaCertFiles,
402+
},
403+
},
404+
AccessLogs: []*model.AccessLog{
405+
{
406+
Name: accessLog.Name(),
407+
Format: "$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent " +
408+
"\"$http_referer\" \"$http_user_agent\" \"$http_x_forwarded_for\" \"$bytes_sent\" " +
409+
"\"$request_length\" \"$request_time\" \"$gzip_ratio\" $server_protocol ",
410+
Permissions: "0600",
411+
Readable: true,
412+
},
413+
},
414+
ErrorLogs: []*model.ErrorLog{
415+
{
416+
Name: errorLog.Name(),
417+
Permissions: "0600",
418+
Readable: true,
419+
},
420+
},
421+
NAPSysLogServers: nil,
422+
},
423+
allowedDirectories: []string{dir},
424+
},
425+
{
426+
name: "Test 5: Check for multiple different SSL Certs",
427+
instance: protos.GetNginxPlusInstance([]string{}),
428+
content: testconfig.GetNginxConfigWithMultipleSSLCerts(
429+
errorLog.Name(),
430+
accessLog.Name(),
431+
certFile,
432+
diffCertFile,
433+
),
434+
expectedConfigContext: &model.NginxConfigContext{
435+
StubStatus: &model.APIDetails{},
436+
PlusAPI: &model.APIDetails{},
437+
InstanceID: protos.GetNginxPlusInstance([]string{}).GetInstanceMeta().GetInstanceId(),
438+
Files: []*mpi.File{
439+
{
440+
FileMeta: fileMetaCertFiles,
441+
},
442+
{
443+
FileMeta: diffFileMetaCertFiles,
444+
},
445+
},
446+
AccessLogs: []*model.AccessLog{
447+
{
448+
Name: accessLog.Name(),
449+
Format: "$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent " +
450+
"\"$http_referer\" \"$http_user_agent\" \"$http_x_forwarded_for\" \"$bytes_sent\" " +
451+
"\"$request_length\" \"$request_time\" \"$gzip_ratio\" $server_protocol ",
452+
Permissions: "0600",
453+
Readable: true,
454+
},
455+
},
456+
ErrorLogs: []*model.ErrorLog{
457+
{
458+
Name: errorLog.Name(),
459+
Permissions: "0600",
460+
Readable: true,
461+
},
462+
},
463+
NAPSysLogServers: nil,
464+
},
465+
allowedDirectories: []string{dir},
466+
},
467+
{
468+
name: "Test 6: Check for multiple same SSL Certs",
469+
instance: protos.GetNginxPlusInstance([]string{}),
470+
content: testconfig.GetNginxConfigWithMultipleSSLCerts(
471+
errorLog.Name(),
472+
accessLog.Name(),
473+
certFile,
474+
certFile,
475+
),
476+
expectedConfigContext: &model.NginxConfigContext{
477+
StubStatus: &model.APIDetails{},
478+
PlusAPI: &model.APIDetails{},
479+
InstanceID: protos.GetNginxPlusInstance([]string{}).GetInstanceMeta().GetInstanceId(),
480+
Files: []*mpi.File{
481+
{
482+
FileMeta: fileMetaCertFiles,
483+
},
484+
},
485+
AccessLogs: []*model.AccessLog{
486+
{
487+
Name: accessLog.Name(),
488+
Format: "$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent " +
489+
"\"$http_referer\" \"$http_user_agent\" \"$http_x_forwarded_for\" \"$bytes_sent\" " +
490+
"\"$request_length\" \"$request_time\" \"$gzip_ratio\" $server_protocol ",
491+
Permissions: "0600",
492+
Readable: true,
493+
},
494+
},
495+
ErrorLogs: []*model.ErrorLog{
496+
{
497+
Name: errorLog.Name(),
498+
Permissions: "0600",
499+
Readable: true,
500+
},
501+
},
502+
NAPSysLogServers: nil,
503+
},
504+
allowedDirectories: []string{dir},
505+
},
371506
}
372507

373508
for _, test := range tests {
@@ -391,13 +526,19 @@ func TestNginxConfigParser_Parse(t *testing.T) {
391526
result, parseError := nginxConfig.Parse(ctx, test.instance)
392527
require.NoError(t, parseError)
393528

529+
sort.Slice(test.expectedConfigContext.Files, func(i, j int) bool {
530+
return test.expectedConfigContext.Files[i].GetFileMeta().GetName() >
531+
test.expectedConfigContext.Files[j].GetFileMeta().GetName()
532+
})
533+
394534
assert.ElementsMatch(t, test.expectedConfigContext.Files, result.Files)
395535
assert.Equal(t, test.expectedConfigContext.NAPSysLogServers, result.NAPSysLogServers)
396536
assert.Equal(t, test.expectedConfigContext.PlusAPI, result.PlusAPI)
397537
assert.ElementsMatch(t, test.expectedConfigContext.AccessLogs, result.AccessLogs)
398538
assert.ElementsMatch(t, test.expectedConfigContext.ErrorLogs, result.ErrorLogs)
399539
assert.Equal(t, test.expectedConfigContext.StubStatus, result.StubStatus)
400540
assert.Equal(t, test.expectedConfigContext.InstanceID, result.InstanceID)
541+
assert.Equal(t, len(test.expectedConfigContext.Files), len(result.Files))
401542
})
402543
}
403544
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
worker_processes 1;
2+
error_log %s;
3+
events {
4+
worker_connections 1024;
5+
}
6+
7+
http {
8+
default_type application/octet-stream;
9+
10+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
11+
'$status $body_bytes_sent "$http_referer" '
12+
'"$http_user_agent" "$http_x_forwarded_for" '
13+
'"$bytes_sent" "$request_length" "$request_time" '
14+
'"$gzip_ratio" $server_protocol ';
15+
16+
access_log %s main;
17+
18+
sendfile on;
19+
keepalive_timeout 65;
20+
21+
server {
22+
listen 8080;
23+
server_name localhost;
24+
25+
location / {
26+
root /usr/share/nginx/html;
27+
index index.html index.htm;
28+
}
29+
30+
ssl_certificate %s;
31+
ssl_certificate %s;
32+
33+
##
34+
# Enable Metrics
35+
##
36+
location /api {
37+
stub_status;
38+
allow 127.0.0.1;
39+
deny all;
40+
}
41+
42+
# redirect server error pages to the static page /50x.html
43+
#
44+
error_page 500 502 503 504 /50x.html;
45+
location = /50x.html {
46+
root /usr/share/nginx/html;
47+
}
48+
}
49+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
worker_processes 1;
2+
error_log %s;
3+
events {
4+
worker_connections 1024;
5+
}
6+
7+
http {
8+
default_type application/octet-stream;
9+
10+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
11+
'$status $body_bytes_sent "$http_referer" '
12+
'"$http_user_agent" "$http_x_forwarded_for" '
13+
'"$bytes_sent" "$request_length" "$request_time" '
14+
'"$gzip_ratio" $server_protocol ';
15+
16+
access_log %s main;
17+
18+
sendfile on;
19+
keepalive_timeout 65;
20+
21+
server {
22+
listen 8080;
23+
server_name localhost;
24+
25+
location / {
26+
root /usr/share/nginx/html;
27+
index index.html index.htm;
28+
}
29+
30+
ssl_certificate %s;
31+
32+
##
33+
# Enable Metrics
34+
##
35+
location /api {
36+
stub_status;
37+
allow 127.0.0.1;
38+
deny all;
39+
}
40+
41+
# redirect server error pages to the static page /50x.html
42+
#
43+
error_page 500 502 503 504 /50x.html;
44+
location = /50x.html {
45+
root /usr/share/nginx/html;
46+
}
47+
}
48+
}

test/config/nginx_config.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ var embedNginxConfWithMultipleAccessLogs string
1616
//go:embed nginx/nginx-not-allowed-dir.conf
1717
var embedNginxConfWithNotAllowedDir string
1818

19+
//go:embed nginx/nginx-with-ssl-certs.conf
20+
var embedNginxConfWithSSLCerts string
21+
22+
//go:embed nginx/nginx-with-multiple-ssl-certs.conf
23+
var embedNginxConfWithMultipleSSLCerts string
24+
1925
func GetNginxConfigWithMultipleAccessLogs(
2026
errorLogName,
2127
accessLogName,
@@ -34,3 +40,11 @@ func GetNginxConfigWithMultipleAccessLogs(
3440
func GetNginxConfigWithNotAllowedDir(errorLogFile, notAllowedFile, allowedFileDir, accessLogFile string) string {
3541
return fmt.Sprintf(embedNginxConfWithNotAllowedDir, errorLogFile, notAllowedFile, allowedFileDir, accessLogFile)
3642
}
43+
44+
func GetNginxConfigWithSSLCerts(errorLogFile, accessLogFile, certFile string) string {
45+
return fmt.Sprintf(embedNginxConfWithSSLCerts, errorLogFile, accessLogFile, certFile)
46+
}
47+
48+
func GetNginxConfigWithMultipleSSLCerts(errorLogFile, accessLogFile, certFile1, certFile2 string) string {
49+
return fmt.Sprintf(embedNginxConfWithMultipleSSLCerts, errorLogFile, accessLogFile, certFile1, certFile2)
50+
}

0 commit comments

Comments
 (0)