Skip to content

Commit 103b4fd

Browse files
Merge pull request #2733 from ncdc/vw-enable-audit
🌱 Enable virtual workspace server audit logging
2 parents 752666a + f00df0a commit 103b4fd

File tree

4 files changed

+63
-9
lines changed

4 files changed

+63
-9
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apiVersion: audit.k8s.io/v1
2+
kind: Policy
3+
omitStages:
4+
- RequestReceived
5+
omitManagedFields: true
6+
rules:
7+
- level: None
8+
nonResourceURLs:
9+
- "/api*"
10+
- "/version"
11+
12+
- level: Metadata
13+
resources:
14+
- group: ""
15+
resources: ["secrets", "configmaps"]
16+
- group: "authorization.k8s.io"
17+
resources: ["subjectaccessreviews"]
18+
19+
- level: Metadata
20+
verbs: ["list", "watch"]
21+
22+
- level: Metadata
23+
verbs: ["get", "delete"]
24+
omitStages:
25+
- ResponseStarted
26+
27+
- level: RequestResponse
28+
verbs: ["create", "update", "patch"]
29+
omitStages:
30+
- ResponseStarted

cmd/sharded-test-server/shard.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ func newShard(ctx context.Context, n int, args []string, standaloneVW bool, serv
6464
os.Exit(1)
6565
}
6666

67-
if err != nil {
68-
return nil, err
69-
}
7067
logFilePath := filepath.Join(workDirPath, fmt.Sprintf(".kcp-%d", n), "kcp.log")
7168
auditFilePath := filepath.Join(workDirPath, fmt.Sprintf(".kcp-%d", n), "audit.log")
7269
if logDirPath != "" {

cmd/sharded-test-server/virtual.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818

1919
import (
2020
"context"
21+
"embed"
2122
"fmt"
2223
"io"
2324
"net"
@@ -44,6 +45,9 @@ import (
4445
"github.com/kcp-dev/kcp/test/e2e/framework"
4546
)
4647

48+
//go:embed *.yaml
49+
var embeddedResources embed.FS
50+
4751
type headWriter interface {
4852
io.Writer
4953
StopOut()
@@ -128,7 +132,17 @@ func newVirtualWorkspace(ctx context.Context, index int, servingCA *crypto.CA, h
128132
authenticationKubeconfigPath := filepath.Join(workDirPath, fmt.Sprintf(".kcp-%d", index), "admin.kubeconfig")
129133
clientCAFilePath := filepath.Join(workDirPath, ".kcp", "client-ca.crt")
130134

131-
args := []string{}
135+
// write audit policy
136+
bs, err := embeddedResources.ReadFile("audit-policy.yaml")
137+
if err != nil {
138+
return nil, err
139+
}
140+
auditPolicyFile := filepath.Join(workDirPath, fmt.Sprintf(".kcp-virtual-workspaces-%d", index), "audit-policy.yaml")
141+
if err := os.WriteFile(auditPolicyFile, bs, 0644); err != nil {
142+
return nil, err
143+
}
144+
145+
var args []string
132146
args = append(args,
133147
fmt.Sprintf("--kubeconfig=%s", kubeconfigPath),
134148
fmt.Sprintf("--cache-kubeconfig=%s", cacheServerConfigPath),
@@ -137,6 +151,15 @@ func newVirtualWorkspace(ctx context.Context, index int, servingCA *crypto.CA, h
137151
fmt.Sprintf("--tls-private-key-file=%s", servingKeyFile),
138152
fmt.Sprintf("--tls-cert-file=%s", servingCertFile),
139153
fmt.Sprintf("--secure-port=%s", virtualWorkspacePort(index)),
154+
"--audit-log-maxsize=1024",
155+
"--audit-log-mode=batch",
156+
"--audit-log-batch-max-wait=1s",
157+
"--audit-log-batch-max-size=1000",
158+
"--audit-log-batch-buffer-size=10000",
159+
"--audit-log-batch-throttle-burst=15",
160+
"--audit-log-batch-throttle-enable=true",
161+
"--audit-log-batch-throttle-qps=10",
162+
fmt.Sprintf("--audit-policy-file=%s", auditPolicyFile),
140163
)
141164

142165
return &VirtualWorkspace{
@@ -155,6 +178,13 @@ func (v *VirtualWorkspace) start(ctx context.Context) error {
155178
lineprefix.Color(color.New(color.FgHiYellow)),
156179
)
157180

181+
logFilePath := filepath.Join(v.workDirPath, fmt.Sprintf(".kcp-virtual-workspaces-%d/virtualworkspace.log", v.index))
182+
auditFilePath := filepath.Join(v.workDirPath, fmt.Sprintf(".kcp-virtual-workspaces-%d", v.index), "audit.log")
183+
if v.logDirPath != "" {
184+
logFilePath = filepath.Join(v.logDirPath, fmt.Sprintf("kcp-virtual-workspaces-%d.log", v.index))
185+
auditFilePath = filepath.Join(v.logDirPath, fmt.Sprintf("kcp-virtual-workspaces-%d-audit.log", v.index))
186+
}
187+
158188
commandLine := framework.DirectOrGoRunCommand("virtual-workspaces")
159189
commandLine = append(commandLine, v.args...)
160190
commandLine = append(
@@ -164,16 +194,12 @@ func (v *VirtualWorkspace) start(ctx context.Context) error {
164194
"--requestheader-group-headers=X-Remote-Group",
165195
fmt.Sprintf("--requestheader-client-ca-file=%s", filepath.Join(v.workDirPath, ".kcp/requestheader-ca.crt")),
166196
"--v=4",
197+
"--audit-log-path", auditFilePath,
167198
)
168199
fmt.Fprintf(out, "running: %v\n", strings.Join(commandLine, " "))
169200

170201
cmd := exec.CommandContext(ctx, commandLine[0], commandLine[1:]...) //nolint:gosec
171202

172-
logFilePath := filepath.Join(v.workDirPath, fmt.Sprintf(".kcp-virtual-workspaces-%d/virtualworkspace.log", v.index))
173-
if v.logDirPath != "" {
174-
logFilePath = filepath.Join(v.logDirPath, fmt.Sprintf("kcp-virtual-workspaces-%d.log", v.index))
175-
}
176-
177203
if err := os.MkdirAll(filepath.Dir(logFilePath), 0755); err != nil {
178204
return err
179205
}

cmd/virtual-workspaces/options/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func (o *Options) AddFlags(flags *pflag.FlagSet) {
8585
o.Cache.AddFlags(flags)
8686
o.SecureServing.AddFlags(flags)
8787
o.Authentication.AddFlags(flags)
88+
o.Audit.AddFlags(flags)
8889
o.Logs.AddFlags(flags)
8990
o.VirtualWorkspaces.AddFlags(flags)
9091

0 commit comments

Comments
 (0)