File tree Expand file tree Collapse file tree 2 files changed +36
-5
lines changed Expand file tree Collapse file tree 2 files changed +36
-5
lines changed Original file line number Diff line number Diff line change @@ -73,6 +73,22 @@ func createURIs(cfg *config.Config) []string {
73
73
return []string {cfg .Collector .ConfigPath }
74
74
}
75
75
76
+ func createFile (confPath string ) error {
77
+ // Create if doesn't exist.
78
+ _ , createErr := os .Create (confPath )
79
+ if createErr != nil {
80
+ return createErr
81
+ }
82
+
83
+ // Set the file permissions to 600.
84
+ permissionErr := os .Chmod (confPath , configFilePermission )
85
+ if permissionErr != nil {
86
+ return permissionErr
87
+ }
88
+
89
+ return nil
90
+ }
91
+
76
92
// Generates a OTel Collector config to a file by injecting the Metrics Config to a Go template.
77
93
func writeCollectorConfig (conf * config.Collector ) error {
78
94
otelcolTemplate , err := template .New (otelTemplatePath ).Parse (otelcolTemplate )
@@ -82,17 +98,16 @@ func writeCollectorConfig(conf *config.Collector) error {
82
98
83
99
confPath := filepath .Clean (conf .ConfigPath )
84
100
85
- // Check if file exists.
101
+ // Check if file exists, if not create it .
86
102
_ , err = os .Stat (confPath )
87
103
if err != nil {
88
104
if ! os .IsNotExist (err ) {
89
105
return err
90
106
}
91
107
92
- // Create if doesn't exist.
93
- _ , createErr := os .Create (confPath )
94
- if createErr != nil {
95
- return createErr
108
+ fileErr := createFile (confPath )
109
+ if fileErr != nil {
110
+ return fileErr
96
111
}
97
112
}
98
113
Original file line number Diff line number Diff line change @@ -171,3 +171,19 @@ func TestTemplateWrite(t *testing.T) {
171
171
// Convert to string for human readable error messages.
172
172
assert .Equal (t , string (expected ), string (actual ))
173
173
}
174
+
175
+ func TestFilePermissions (t * testing.T ) {
176
+ tmpDir := t .TempDir ()
177
+
178
+ cfg := types .AgentConfig ()
179
+ actualConfPath := filepath .Join (tmpDir , "nginx-agent-otelcol-test.yaml" )
180
+ cfg .Collector .ConfigPath = actualConfPath
181
+
182
+ err := writeCollectorConfig (cfg .Collector )
183
+ require .NoError (t , err )
184
+
185
+ // Check file permissions are 600
186
+ fileInfo , err := os .Stat (actualConfPath )
187
+ require .NoError (t , err )
188
+ assert .Equal (t , os .FileMode (0o600 ), fileInfo .Mode ())
189
+ }
You can’t perform that action at this time.
0 commit comments