mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
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:
commit
ff20035ef8
@ -20,6 +20,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -525,6 +526,9 @@ func (o *AuditLogOptions) getWriter() (io.Writer, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *AuditLogOptions) ensureLogFile() error {
|
func (o *AuditLogOptions) ensureLogFile() error {
|
||||||
|
if err := os.MkdirAll(filepath.Dir(o.Path), 0700); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
mode := os.FileMode(0600)
|
mode := os.FileMode(0600)
|
||||||
f, err := os.OpenFile(o.Path, os.O_CREATE|os.O_APPEND|os.O_RDWR, mode)
|
f, err := os.OpenFile(o.Path, os.O_CREATE|os.O_APPEND|os.O_RDWR, mode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -69,6 +69,15 @@ func TestAuditValidOptions(t *testing.T) {
|
|||||||
return o
|
return o
|
||||||
},
|
},
|
||||||
expected: "ignoreErrors<log>",
|
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",
|
name: "default log no policy",
|
||||||
options: func() *AuditOptions {
|
options: func() *AuditOptions {
|
||||||
|
Loading…
Reference in New Issue
Block a user