@@ -28,8 +28,10 @@ type ExpectedNginxField struct {
28
28
File string
29
29
// Location is the location name that the directive should exist in.
30
30
Location string
31
- // Servers are the server names that the directive should exist in.
32
- Servers []string
31
+ // Server is the server name that the directive should exist in.
32
+ Server string
33
+ // Upstream is the upstream name that the directive should exist in.
34
+ Upstream string
33
35
// ValueSubstringAllowed allows the expected value to be a substring of the real value.
34
36
// This makes it easier for cases when real values are complex file names or contain things we
35
37
// don't care about, and we just want to check if a substring exists.
@@ -39,40 +41,66 @@ type ExpectedNginxField struct {
39
41
// ValidateNginxFieldExists accepts the nginx config and the configuration for the expected field,
40
42
// and returns whether or not that field exists where it should.
41
43
func ValidateNginxFieldExists (conf * Payload , expFieldCfg ExpectedNginxField ) error {
44
+ b , err := json .Marshal (conf )
45
+ if err != nil {
46
+ return fmt .Errorf ("error marshaling nginx config: %w" , err )
47
+ }
48
+
42
49
for _ , config := range conf .Config {
43
50
if ! strings .Contains (config .File , expFieldCfg .File ) {
44
51
continue
45
52
}
46
53
47
54
for _ , directive := range config .Parsed {
48
- if len ( expFieldCfg .Servers ) == 0 {
55
+ if expFieldCfg .Server == "" && expFieldCfg . Upstream == "" {
49
56
if expFieldCfg .fieldFound (directive ) {
50
57
return nil
51
58
}
52
59
continue
53
60
}
54
61
55
- for _ , serverName := range expFieldCfg .Servers {
56
- if directive .Directive == "server" && getServerName (directive .Block ) == serverName {
57
- for _ , serverDirective := range directive .Block {
58
- if expFieldCfg .Location == "" && expFieldCfg .fieldFound (serverDirective ) {
59
- return nil
60
- } else if serverDirective .Directive == "location" &&
61
- fieldExistsInLocation (serverDirective , expFieldCfg ) {
62
- return nil
63
- }
64
- }
65
- }
62
+ if expFieldCfg .Server != "" && fieldExistsInServer (expFieldCfg , * directive ) {
63
+ return nil
64
+ }
65
+
66
+ if expFieldCfg .Upstream != "" && fieldExistsInUpstream (expFieldCfg , * directive ) {
67
+ return nil
66
68
}
67
69
}
68
70
}
69
71
70
- b , err := json .Marshal (conf )
71
- if err != nil {
72
- return fmt .Errorf ("error marshaling nginx config: %w" , err )
72
+ return fmt .Errorf ("directive %s not found in: nginx config %s" , expFieldCfg .Directive , string (b ))
73
+ }
74
+
75
+ func fieldExistsInServer (
76
+ expFieldCfg ExpectedNginxField ,
77
+ directive Directive ,
78
+ ) bool {
79
+ if directive .Directive == "server" && getServerName (directive .Block ) == expFieldCfg .Server {
80
+ for _ , serverDirective := range directive .Block {
81
+ if expFieldCfg .Location == "" && expFieldCfg .fieldFound (serverDirective ) {
82
+ return true
83
+ } else if serverDirective .Directive == "location" &&
84
+ fieldExistsInLocation (serverDirective , expFieldCfg ) {
85
+ return true
86
+ }
87
+ }
73
88
}
89
+ return false
90
+ }
74
91
75
- return fmt .Errorf ("field not found; expected: %+v\n NGINX conf: %s" , expFieldCfg , string (b ))
92
+ func fieldExistsInUpstream (
93
+ expFieldCfg ExpectedNginxField ,
94
+ directive Directive ,
95
+ ) bool {
96
+ if directive .Directive == "upstream" && directive .Args [0 ] == expFieldCfg .Upstream {
97
+ for _ , directive := range directive .Block {
98
+ if expFieldCfg .fieldFound (directive ) {
99
+ return true
100
+ }
101
+ }
102
+ }
103
+ return false
76
104
}
77
105
78
106
func getServerName (serverBlock Directives ) string {
0 commit comments