Merge pull request #110813 from vpnachev/apiserver/create-dir-for-audit-log-path

Ensure the directory for the file in flag `--audit-log-path` exists
This commit is contained in:
Kubernetes Prow Robot 2022-07-18 09:35:15 -07:00 committed by GitHub
commit ff20035ef8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 {