@@ -163,6 +163,84 @@ func TestNormalizeFunc(t *testing.T) {
163163 assert .Equal (t , expected , result )
164164}
165165
166+ func TestResolveAllowedDirectories (t * testing.T ) {
167+ tests := []struct {
168+ name string
169+ configuredDirs []string
170+ expected []string
171+ }{
172+ {
173+ name : "Test 1: Empty path" ,
174+ configuredDirs : []string {"" },
175+ expected : []string {"/etc/nginx-agent" },
176+ },
177+ {
178+ name : "Test 2: Absolute path" ,
179+ configuredDirs : []string {"/etc/agent/" },
180+ expected : []string {"/etc/nginx-agent" , "/etc/agent" },
181+ },
182+ {
183+ name : "Test 3: Absolute paths" ,
184+ configuredDirs : []string {"/etc/nginx/" },
185+ expected : []string {"/etc/nginx-agent" , "/etc/nginx" },
186+ },
187+ {
188+ name : "Test 4: Absolute path with multiple slashes" ,
189+ configuredDirs : []string {"/etc///////////nginx-agent/" },
190+ expected : []string {"/etc/nginx-agent" },
191+ },
192+ {
193+ name : "Test 5: Absolute path with directory traversal" ,
194+ configuredDirs : []string {"/etc/nginx/../nginx-agent" },
195+ expected : []string {"/etc/nginx-agent" },
196+ },
197+ {
198+ name : "Test 6: Absolute path with repeat directory traversal" ,
199+ configuredDirs : []string {"/etc/nginx-agent/../../../../../nginx-agent" },
200+ expected : []string {"/etc/nginx-agent" },
201+ },
202+ {
203+ name : "Test 7: Absolute path with control characters" ,
204+ configuredDirs : []string {"/etc/nginx-agent/\\ x08../tmp/" },
205+ expected : []string {"/etc/nginx-agent" },
206+ },
207+ {
208+ name : "Test 8: Absolute path with invisible characters" ,
209+ configuredDirs : []string {"/etc/nginx-agent/ㅤㅤㅤ/tmp/" },
210+ expected : []string {"/etc/nginx-agent" },
211+ },
212+ {
213+ name : "Test 9: Absolute path with escaped invisible characters" ,
214+ configuredDirs : []string {"/etc/nginx-agent/\\ \\ ㅤ/tmp/" },
215+ expected : []string {"/etc/nginx-agent" },
216+ },
217+ {
218+ name : "Test 10: Mixed paths" ,
219+ configuredDirs : []string {
220+ "nginx-agent" ,
221+ "" ,
222+ ".." ,
223+ "/" ,
224+ "\\ /" ,
225+ "." ,
226+ "/etc/nginx/" ,
227+ },
228+ expected : []string {"/etc/nginx-agent" , "/etc/nginx" },
229+ },
230+ {
231+ name : "Test 11: Relative path" ,
232+ configuredDirs : []string {"nginx-agent" },
233+ expected : []string {"/etc/nginx-agent" },
234+ },
235+ }
236+ for _ , test := range tests {
237+ t .Run (test .name , func (t * testing.T ) {
238+ allowed := resolveAllowedDirectories (test .configuredDirs )
239+ assert .Equal (t , test .expected , allowed )
240+ })
241+ }
242+ }
243+
166244func TestResolveLog (t * testing.T ) {
167245 viperInstance = viper .NewWithOptions (viper .KeyDelimiter (KeyDelimiter ))
168246 viperInstance .Set (LogLevelKey , "error" )
@@ -867,89 +945,7 @@ func agentConfig() *Config {
867945 "/etc/nginx/" , "/etc/nginx-agent/" , "/usr/local/etc/nginx/" , "/var/run/nginx/" , "/var/log/nginx/" ,
868946 "/usr/share/nginx/modules/" , "/etc/app_protect/" ,
869947 },
870- Collector : & Collector {
871- ConfigPath : "/etc/nginx-agent/nginx-agent-otelcol.yaml" ,
872- Exporters : Exporters {
873- OtlpExporters : map [string ]* OtlpExporter {
874- "default" : {
875- Server : & ServerConfig {
876- Host : "127.0.0.1" ,
877- Port : 1234 ,
878- Type : Grpc ,
879- },
880- TLS : & TLSConfig {
881- Cert : "/path/to/server-cert.pem" ,
882- Key : "/path/to/server-cert.pem" ,
883- Ca : "/path/to/server-cert.pem" ,
884- SkipVerify : true ,
885- ServerName : "remote-saas-server" ,
886- },
887- },
888- },
889- },
890- Processors : Processors {
891- Batch : map [string ]* Batch {
892- "default_logs" : {
893- SendBatchMaxSize : DefCollectorLogsBatchProcessorSendBatchMaxSize ,
894- SendBatchSize : DefCollectorLogsBatchProcessorSendBatchSize ,
895- Timeout : DefCollectorLogsBatchProcessorTimeout ,
896- },
897- },
898- LogsGzip : map [string ]* LogsGzip {
899- "default" : {},
900- },
901- },
902- Receivers : Receivers {
903- OtlpReceivers : map [string ]* OtlpReceiver {
904- "default" : {
905- Server : & ServerConfig {
906- Host : "localhost" ,
907- Port : 4317 ,
908- Type : Grpc ,
909- },
910- Auth : & AuthConfig {
911- Token : "even-secreter-token" ,
912- },
913- OtlpTLSConfig : & OtlpTLSConfig {
914- GenerateSelfSignedCert : false ,
915- Cert : "/path/to/server-cert.pem" ,
916- Key : "/path/to/server-cert.pem" ,
917- Ca : "/path/to/server-cert.pem" ,
918- SkipVerify : true ,
919- ServerName : "local-data-plane-server" ,
920- },
921- },
922- },
923- NginxReceivers : []NginxReceiver {
924- {
925- InstanceID : "cd7b8911-c2c5-4daf-b311-dbead151d938" ,
926- StubStatus : APIDetails {
927- URL : "http://localhost:4321/status" ,
928- Listen : "" ,
929- },
930- AccessLogs : []AccessLog {
931- {
932- LogFormat : accessLogFormat ,
933- FilePath : "/var/log/nginx/access-custom.conf" ,
934- },
935- },
936- },
937- },
938- },
939- Extensions : Extensions {
940- Health : & Health {
941- Server : & ServerConfig {
942- Host : "localhost" ,
943- Port : 1337 ,
944- },
945- Path : "/" ,
946- },
947- },
948- Log : & Log {
949- Level : "INFO" ,
950- Path : "/var/log/nginx-agent/opentelemetry-collector-agent.log" ,
951- },
952- },
948+ Collector : createDefaultCollectorConfig (),
953949 Command : & Command {
954950 Server : & ServerConfig {
955951 Host : "127.0.0.1" ,
@@ -1002,8 +998,8 @@ func createConfig() *Config {
1002998 },
1003999 },
10041000 AllowedDirectories : []string {
1005- "/etc/nginx-agent/ " , "/etc/nginx/ " , "/usr/local/etc/nginx/ " , "/var/run/nginx/ " ,
1006- "/usr/share/nginx/modules/ " , "/var/log/nginx/ " ,
1001+ "/etc/nginx-agent" , "/etc/nginx" , "/usr/local/etc/nginx" , "/var/run/nginx" ,
1002+ "/usr/share/nginx/modules" , "/var/log/nginx" ,
10071003 },
10081004 DataPlaneConfig : & DataPlaneConfig {
10091005 Nginx : & NginxDataPlaneConfig {
@@ -1226,3 +1222,89 @@ func createConfig() *Config {
12261222 },
12271223 }
12281224}
1225+
1226+ func createDefaultCollectorConfig () * Collector {
1227+ return & Collector {
1228+ ConfigPath : "/etc/nginx-agent/nginx-agent-otelcol.yaml" ,
1229+ Exporters : Exporters {
1230+ OtlpExporters : map [string ]* OtlpExporter {
1231+ "default" : {
1232+ Server : & ServerConfig {
1233+ Host : "127.0.0.1" ,
1234+ Port : 1234 ,
1235+ Type : Grpc ,
1236+ },
1237+ TLS : & TLSConfig {
1238+ Cert : "/path/to/server-cert.pem" ,
1239+ Key : "/path/to/server-cert.pem" ,
1240+ Ca : "/path/to/server-cert.pem" ,
1241+ SkipVerify : true ,
1242+ ServerName : "remote-saas-server" ,
1243+ },
1244+ },
1245+ },
1246+ },
1247+ Processors : Processors {
1248+ Batch : map [string ]* Batch {
1249+ "default_logs" : {
1250+ SendBatchMaxSize : DefCollectorLogsBatchProcessorSendBatchMaxSize ,
1251+ SendBatchSize : DefCollectorLogsBatchProcessorSendBatchSize ,
1252+ Timeout : DefCollectorLogsBatchProcessorTimeout ,
1253+ },
1254+ },
1255+ LogsGzip : map [string ]* LogsGzip {
1256+ "default" : {},
1257+ },
1258+ },
1259+ Receivers : Receivers {
1260+ OtlpReceivers : map [string ]* OtlpReceiver {
1261+ "default" : {
1262+ Server : & ServerConfig {
1263+ Host : "localhost" ,
1264+ Port : 4317 ,
1265+ Type : Grpc ,
1266+ },
1267+ Auth : & AuthConfig {
1268+ Token : "even-secreter-token" ,
1269+ },
1270+ OtlpTLSConfig : & OtlpTLSConfig {
1271+ GenerateSelfSignedCert : false ,
1272+ Cert : "/path/to/server-cert.pem" ,
1273+ Key : "/path/to/server-cert.pem" ,
1274+ Ca : "/path/to/server-cert.pem" ,
1275+ SkipVerify : true ,
1276+ ServerName : "local-data-plane-server" ,
1277+ },
1278+ },
1279+ },
1280+ NginxReceivers : []NginxReceiver {
1281+ {
1282+ InstanceID : "cd7b8911-c2c5-4daf-b311-dbead151d938" ,
1283+ StubStatus : APIDetails {
1284+ URL : "http://localhost:4321/status" ,
1285+ Listen : "" ,
1286+ },
1287+ AccessLogs : []AccessLog {
1288+ {
1289+ LogFormat : accessLogFormat ,
1290+ FilePath : "/var/log/nginx/access-custom.conf" ,
1291+ },
1292+ },
1293+ },
1294+ },
1295+ },
1296+ Extensions : Extensions {
1297+ Health : & Health {
1298+ Server : & ServerConfig {
1299+ Host : "localhost" ,
1300+ Port : 1337 ,
1301+ },
1302+ Path : "/" ,
1303+ },
1304+ },
1305+ Log : & Log {
1306+ Level : "INFO" ,
1307+ Path : "/var/log/nginx-agent/opentelemetry-collector-agent.log" ,
1308+ },
1309+ }
1310+ }
0 commit comments