Skip to content

Commit 5cc5243

Browse files
committed
WIP: Add user flag and log executed commands
1 parent 857e0a2 commit 5cc5243

36 files changed

+155
-0
lines changed

Diff for: cmd/minikube/cmd/audit.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2016 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"os"
21+
"os/user"
22+
"strings"
23+
"time"
24+
25+
"github.com/spf13/viper"
26+
"k8s.io/minikube/pkg/minikube/config"
27+
"k8s.io/minikube/pkg/minikube/localpath"
28+
"k8s.io/minikube/pkg/minikube/out/register"
29+
)
30+
31+
// auditUser pulls the user flag, if empty gets the os user
32+
func auditUser() string {
33+
u := viper.GetString(config.User)
34+
if u != "" {
35+
return u
36+
}
37+
osUser, err := user.Current()
38+
if err != nil {
39+
return "UNKNOWN"
40+
}
41+
return osUser.Username
42+
}
43+
44+
// auditArgs concats the args into space delimited string
45+
func auditArgs() string {
46+
if len(os.Args) < 3 {
47+
return ""
48+
}
49+
return strings.Join(os.Args[2:], " ")
50+
}
51+
52+
// audit logs details about the executed command
53+
func audit(startTime time.Time) {
54+
register.SetEventLogPath(localpath.EventLog("audit"))
55+
register.RecordAudit(os.Args[1], auditArgs(), auditUser(), startTime, time.Now())
56+
}

Diff for: cmd/minikube/cmd/delete.go

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os/exec"
2424
"path/filepath"
2525
"strconv"
26+
"time"
2627

2728
"github.com/docker/machine/libmachine/mcnerror"
2829
"github.com/mitchellh/go-ps"
@@ -129,6 +130,9 @@ func runDelete(cmd *cobra.Command, args []string) {
129130
if len(args) > 0 {
130131
exit.Message(reason.Usage, "Usage: minikube delete")
131132
}
133+
134+
defer audit(time.Now())
135+
132136
// register.SetEventLogPath(localpath.EventLog(ClusterFlagValue()))
133137
register.Reg.SetStep(register.Deleting)
134138

Diff for: cmd/minikube/cmd/root.go

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ func init() {
170170

171171
RootCmd.PersistentFlags().StringP(config.ProfileName, "p", constants.DefaultClusterName, `The name of the minikube VM being used. This can be set to allow having multiple instances of minikube independently.`)
172172
RootCmd.PersistentFlags().StringP(configCmd.Bootstrapper, "b", "kubeadm", "The name of the cluster bootstrapper that will set up the Kubernetes cluster.")
173+
RootCmd.PersistentFlags().String(config.User, "", "Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.")
173174

174175
groups := templates.CommandGroups{
175176
{

Diff for: cmd/minikube/cmd/start.go

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"regexp"
2929
"runtime"
3030
"strings"
31+
"time"
3132

3233
"github.com/blang/semver"
3334
"github.com/docker/machine/libmachine/ssh"
@@ -129,6 +130,8 @@ func platform() string {
129130

130131
// runStart handles the executes the flow of "minikube start"
131132
func runStart(cmd *cobra.Command, args []string) {
133+
defer audit(time.Now())
134+
132135
register.SetEventLogPath(localpath.EventLog(ClusterFlagValue()))
133136

134137
out.SetJSON(outputFormat == "json")

Diff for: cmd/minikube/cmd/stop.go

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ func init() {
7575

7676
// runStop handles the executes the flow of "minikube stop"
7777
func runStop(cmd *cobra.Command, args []string) {
78+
defer audit(time.Now())
79+
7880
out.SetJSON(outputFormat == "json")
7981
register.Reg.SetStep(register.Stopping)
8082

Diff for: pkg/minikube/config/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ const (
4848
ShowDriverDeprecationNotification = "ShowDriverDeprecationNotification"
4949
// ShowBootstrapperDeprecationNotification is the key for ShowBootstrapperDeprecationNotification
5050
ShowBootstrapperDeprecationNotification = "ShowBootstrapperDeprecationNotification"
51+
// User represents the key for the global user parameter
52+
User = "user"
5153
)
5254

5355
var (

Diff for: pkg/minikube/out/register/json.go

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License.
1616

1717
package register
1818

19+
import "time"
20+
1921
// PrintStep prints a Step type in JSON format
2022
func PrintStep(message string) {
2123
s := NewStep(message)
@@ -69,3 +71,9 @@ func PrintWarning(warning string) {
6971
w := NewWarning(warning)
7072
printAndRecordCloudEvent(w, w.data)
7173
}
74+
75+
// RecordAudit records an Audit type in JSON format
76+
func RecordAudit(command string, args string, user string, startTime time.Time, endTime time.Time) {
77+
a := NewAudit(command, args, user, startTime, endTime)
78+
recordCloudEvent(a, a.data)
79+
}

Diff for: pkg/minikube/out/register/log.go

+24
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package register
1919
import (
2020
"fmt"
2121
"strings"
22+
"time"
2223
)
2324

2425
// Log represents the different types of logs that can be output as JSON
@@ -154,3 +155,26 @@ func NewErrorExitCode(err string, exitcode int, additionalData ...map[string]str
154155
func (s *Error) Type() string {
155156
return "io.k8s.sigs.minikube.error"
156157
}
158+
159+
// Audit represents the execution of a command
160+
type Audit struct {
161+
data map[string]string
162+
}
163+
164+
// Type returns the cloud events compatible type of this struct
165+
func (a *Audit) Type() string {
166+
return "io.k8s.sigs.minikube.audit"
167+
}
168+
169+
// NewAudit returns a new audit type
170+
func NewAudit(command string, args string, user string, startTime time.Time, endTime time.Time) *Audit {
171+
return &Audit{
172+
map[string]string{
173+
"args": args,
174+
"command": command,
175+
"endTime": endTime.String(),
176+
"startTime": startTime.String(),
177+
"user": user,
178+
},
179+
}
180+
}

Diff for: site/content/en/docs/commands/addons.md

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ minikube addons SUBCOMMAND [flags]
3434
--skip_headers If true, avoid header prefixes in the log messages
3535
--skip_log_headers If true, avoid headers when opening log files
3636
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
37+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
3738
-v, --v Level number for the log level verbosity
3839
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
3940
```
@@ -67,6 +68,7 @@ minikube addons configure ADDON_NAME [flags]
6768
--skip_headers If true, avoid header prefixes in the log messages
6869
--skip_log_headers If true, avoid headers when opening log files
6970
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
71+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
7072
-v, --v Level number for the log level verbosity
7173
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
7274
```
@@ -100,6 +102,7 @@ minikube addons disable ADDON_NAME [flags]
100102
--skip_headers If true, avoid header prefixes in the log messages
101103
--skip_log_headers If true, avoid headers when opening log files
102104
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
105+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
103106
-v, --v Level number for the log level verbosity
104107
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
105108
```
@@ -133,6 +136,7 @@ minikube addons enable ADDON_NAME [flags]
133136
--skip_headers If true, avoid header prefixes in the log messages
134137
--skip_log_headers If true, avoid headers when opening log files
135138
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
139+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
136140
-v, --v Level number for the log level verbosity
137141
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
138142
```
@@ -167,6 +171,7 @@ minikube addons help [command] [flags]
167171
--skip_headers If true, avoid header prefixes in the log messages
168172
--skip_log_headers If true, avoid headers when opening log files
169173
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
174+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
170175
-v, --v Level number for the log level verbosity
171176
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
172177
```
@@ -206,6 +211,7 @@ minikube addons list [flags]
206211
--skip_headers If true, avoid header prefixes in the log messages
207212
--skip_log_headers If true, avoid headers when opening log files
208213
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
214+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
209215
-v, --v Level number for the log level verbosity
210216
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
211217
```
@@ -249,6 +255,7 @@ minikube addons open ADDON_NAME [flags]
249255
--skip_headers If true, avoid header prefixes in the log messages
250256
--skip_log_headers If true, avoid headers when opening log files
251257
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
258+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
252259
-v, --v Level number for the log level verbosity
253260
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
254261
```

Diff for: site/content/en/docs/commands/cache.md

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Add, delete, or push a local image into minikube
3030
--skip_headers If true, avoid header prefixes in the log messages
3131
--skip_log_headers If true, avoid headers when opening log files
3232
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
33+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
3334
-v, --v Level number for the log level verbosity
3435
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
3536
```
@@ -63,6 +64,7 @@ minikube cache add [flags]
6364
--skip_headers If true, avoid header prefixes in the log messages
6465
--skip_log_headers If true, avoid headers when opening log files
6566
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
67+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
6668
-v, --v Level number for the log level verbosity
6769
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
6870
```
@@ -96,6 +98,7 @@ minikube cache delete [flags]
9698
--skip_headers If true, avoid header prefixes in the log messages
9799
--skip_log_headers If true, avoid headers when opening log files
98100
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
101+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
99102
-v, --v Level number for the log level verbosity
100103
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
101104
```
@@ -130,6 +133,7 @@ minikube cache help [command] [flags]
130133
--skip_headers If true, avoid header prefixes in the log messages
131134
--skip_log_headers If true, avoid headers when opening log files
132135
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
136+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
133137
-v, --v Level number for the log level verbosity
134138
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
135139
```
@@ -170,6 +174,7 @@ minikube cache list [flags]
170174
--skip_headers If true, avoid header prefixes in the log messages
171175
--skip_log_headers If true, avoid headers when opening log files
172176
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
177+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
173178
-v, --v Level number for the log level verbosity
174179
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
175180
```
@@ -203,6 +208,7 @@ minikube cache reload [flags]
203208
--skip_headers If true, avoid header prefixes in the log messages
204209
--skip_log_headers If true, avoid headers when opening log files
205210
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
211+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
206212
-v, --v Level number for the log level verbosity
207213
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
208214
```

Diff for: site/content/en/docs/commands/completion.md

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ minikube completion SHELL [flags]
5555
--skip_headers If true, avoid header prefixes in the log messages
5656
--skip_log_headers If true, avoid headers when opening log files
5757
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
58+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
5859
-v, --v Level number for the log level verbosity
5960
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
6061
```

Diff for: site/content/en/docs/commands/config.md

+7
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ minikube config SUBCOMMAND [flags]
6464
--skip_headers If true, avoid header prefixes in the log messages
6565
--skip_log_headers If true, avoid headers when opening log files
6666
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
67+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
6768
-v, --v Level number for the log level verbosity
6869
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
6970
```
@@ -106,6 +107,7 @@ minikube config defaults PROPERTY_NAME [flags]
106107
--skip_headers If true, avoid header prefixes in the log messages
107108
--skip_log_headers If true, avoid headers when opening log files
108109
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
110+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
109111
-v, --v Level number for the log level verbosity
110112
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
111113
```
@@ -139,6 +141,7 @@ minikube config get PROPERTY_NAME [flags]
139141
--skip_headers If true, avoid header prefixes in the log messages
140142
--skip_log_headers If true, avoid headers when opening log files
141143
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
144+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
142145
-v, --v Level number for the log level verbosity
143146
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
144147
```
@@ -173,6 +176,7 @@ minikube config help [command] [flags]
173176
--skip_headers If true, avoid header prefixes in the log messages
174177
--skip_log_headers If true, avoid headers when opening log files
175178
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
179+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
176180
-v, --v Level number for the log level verbosity
177181
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
178182
```
@@ -207,6 +211,7 @@ minikube config set PROPERTY_NAME PROPERTY_VALUE [flags]
207211
--skip_headers If true, avoid header prefixes in the log messages
208212
--skip_log_headers If true, avoid headers when opening log files
209213
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
214+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
210215
-v, --v Level number for the log level verbosity
211216
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
212217
```
@@ -240,6 +245,7 @@ minikube config unset PROPERTY_NAME [flags]
240245
--skip_headers If true, avoid header prefixes in the log messages
241246
--skip_log_headers If true, avoid headers when opening log files
242247
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
248+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
243249
-v, --v Level number for the log level verbosity
244250
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
245251
```
@@ -280,6 +286,7 @@ minikube config view [flags]
280286
--skip_headers If true, avoid header prefixes in the log messages
281287
--skip_log_headers If true, avoid headers when opening log files
282288
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
289+
--user string Specifies the user executing the operation. Useful for auditing operations executed by 3rd party tools. Defaults to the operating system username.
283290
-v, --v Level number for the log level verbosity
284291
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
285292
```

0 commit comments

Comments
 (0)