Skip to content

Commit 83ea6bf

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

34 files changed

+165
-4
lines changed

cmd/minikube/cmd/root.go

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"path/filepath"
2424
"runtime"
2525
"strings"
26+
"time"
2627

2728
"github.com/spf13/cobra"
2829
"github.com/spf13/pflag"
@@ -31,6 +32,7 @@ import (
3132
"k8s.io/kubectl/pkg/util/templates"
3233
configCmd "k8s.io/minikube/cmd/minikube/cmd/config"
3334
"k8s.io/minikube/pkg/drivers/kic/oci"
35+
"k8s.io/minikube/pkg/minikube/audit"
3436
"k8s.io/minikube/pkg/minikube/config"
3537
"k8s.io/minikube/pkg/minikube/constants"
3638
"k8s.io/minikube/pkg/minikube/exit"
@@ -68,6 +70,8 @@ var RootCmd = &cobra.Command{
6870
// Execute adds all child commands to the root command sets flags appropriately.
6971
// This is called by main.main(). It only needs to happen once to the rootCmd.
7072
func Execute() {
73+
defer audit.Log(time.Now())
74+
7175
_, callingCmd := filepath.Split(os.Args[0])
7276

7377
if callingCmd == "kubectl" {
@@ -170,6 +174,7 @@ func init() {
170174

171175
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.`)
172176
RootCmd.PersistentFlags().StringP(configCmd.Bootstrapper, "b", "kubeadm", "The name of the cluster bootstrapper that will set up the Kubernetes cluster.")
177+
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.")
173178

174179
groups := templates.CommandGroups{
175180
{

pkg/minikube/audit/audit.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2020 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 audit
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+
// Log details about the executed command
53+
func Log(startTime time.Time) {
54+
register.SetEventLogPath(localpath.EventLog("audit"))
55+
e := newEntry(os.Args[1], auditArgs(), auditUser(), startTime, time.Now())
56+
register.RecordCloudEvent(e, e.data)
57+
}

pkg/minikube/audit/entry.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright 2020 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 audit
18+
19+
import "time"
20+
21+
// entry represents the execution of a command
22+
type entry struct {
23+
data map[string]string
24+
}
25+
26+
// Type returns the cloud events compatible type of this struct
27+
func (e *entry) Type() string {
28+
return "io.k8s.sigs.minikube.audit"
29+
}
30+
31+
// newEntry returns a new audit type
32+
func newEntry(command string, args string, user string, startTime time.Time, endTime time.Time) *entry {
33+
return &entry{
34+
map[string]string{
35+
"args": args,
36+
"command": command,
37+
"endTime": endTime.String(),
38+
"startTime": startTime.String(),
39+
"user": user,
40+
},
41+
}
42+
}

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 (

pkg/minikube/out/register/cloud_events.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ func storeEvent(bs []byte) {
111111
}
112112
}
113113

114-
// record cloud event to disk
115-
func recordCloudEvent(log Log, data map[string]string) {
114+
// RecordCloudEvent records cloud event to disk
115+
func RecordCloudEvent(log Log, data map[string]string) {
116116
if eventFile == nil {
117117
return
118118
}

pkg/minikube/out/register/json.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func PrintStep(message string) {
2525
// RecordStep records a Step type in JSON format
2626
func RecordStep(message string) {
2727
s := NewStep(message)
28-
recordCloudEvent(s, s.data)
28+
RecordCloudEvent(s, s.data)
2929
}
3030

3131
// PrintInfo prints an Info type in JSON format
@@ -55,7 +55,7 @@ func PrintError(err string) {
5555
// RecordError records a Record type in JSON format
5656
func RecordError(err string) {
5757
e := NewError(err)
58-
recordCloudEvent(e, e.data)
58+
RecordCloudEvent(e, e.data)
5959
}
6060

6161
// PrintErrorExitCode prints an error in JSON format and includes an exit code

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
```

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
```

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
```

0 commit comments

Comments
 (0)