Ensure the dir of --audit-log-path exists

Signed-off-by: Vladimir Nachev <vladimir.nachev@sap.com>
This commit is contained in:
Vladimir Nachev 2022-06-27 17:21:02 +03:00
parent f4abde9e57
commit a380ef5c41
No known key found for this signature in database
GPG Key ID: D18B0369007025FA
2 changed files with 13 additions and 0 deletions

View File

@ -20,6 +20,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
@ -525,6 +526,9 @@ func (o *AuditLogOptions) getWriter() (io.Writer, error) {
}
func (o *AuditLogOptions) ensureLogFile() error {
if err := os.MkdirAll(filepath.Dir(o.Path), 0700); err != nil {
return err
}
mode := os.FileMode(0600)
f, err := os.OpenFile(o.Path, os.O_CREATE|os.O_APPEND|os.O_RDWR, mode)
if err != nil {

View File

@ -69,6 +69,15 @@ func TestAuditValidOptions(t *testing.T) {
return o
},
expected: "ignoreErrors<log>",
}, {
name: "create audit log path dir",
options: func() *AuditOptions {
o := NewAuditOptions()
o.LogOptions.Path = filepath.Join(tmpDir, "non-existing-dir1", "non-existing-dir2", "audit")
o.PolicyFile = policy
return o
},
expected: "ignoreErrors<log>",
}, {
name: "default log no policy",
options: func() *AuditOptions {