forked from crowdsecurity/cs-firewall-bouncer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix minor logging issues (crowdsecurity#264)
* Silence terminal detection * CI: debug helper for systemd units * Fix minor logging issues - pre-create a file with chmod 0600, or change them if the file already exists - don't log fatal, panic .. twice to stderr - default log_mode = stdout - calling code from crowdsec/pkg/types would create an empty $LogDir/crowdsec.log (??), this dependency is removed * fix metric parsing error
- Loading branch information
Showing
13 changed files
with
214 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cfg | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// setLogFilePermissions sets the permissions of the log file to 0600. | ||
// If the file does not exist, it will be created. | ||
// lumberjack will then respect our permissions. | ||
// https://github.com/natefinch/lumberjack/issues/82 | ||
func setLogFilePermissions(logDir string, logFile string) (string, error) { | ||
err := os.MkdirAll(logDir, 0755) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to create log directory: %w", err) | ||
} | ||
|
||
logPath := filepath.Join(logDir, logFile) | ||
|
||
st, err := os.Stat(logPath) | ||
if err != nil { | ||
if !os.IsNotExist(err) { | ||
return "", fmt.Errorf("failed to check file existence: %w", err) | ||
} | ||
file, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY, 0600) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to create file: %w", err) | ||
} | ||
file.Close() | ||
return logPath, nil | ||
} | ||
|
||
if st.IsDir() { | ||
return "", fmt.Errorf("expected a file, found a directory: %s", logPath) | ||
} | ||
|
||
err = os.Chmod(logPath, 0600) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to change file permissions: %w", err) | ||
} | ||
|
||
return logPath, nil | ||
} |
Oops, something went wrong.