Skip to content

Commit d04956f

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

File tree

8 files changed

+99
-0
lines changed

8 files changed

+99
-0
lines changed

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

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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/localpath"
27+
"k8s.io/minikube/pkg/minikube/out/register"
28+
)
29+
30+
// getUser pulls the user flag, if empty gets the os user
31+
func getUser() string {
32+
u := viper.GetString(userFlag)
33+
if u != "" {
34+
return u
35+
}
36+
osUser, err := user.Current()
37+
if err != nil {
38+
return "unable to get user"
39+
}
40+
return osUser.Username
41+
}
42+
43+
// getArgs concats the args into space delimited string
44+
func getArgs() string {
45+
if len(os.Args) < 3 {
46+
return ""
47+
}
48+
return strings.Join(os.Args[2:], " ")
49+
}
50+
51+
// audit logs details about the executed command
52+
func audit(startTime time.Time) {
53+
register.SetEventLogPath(localpath.EventLog("audit"))
54+
register.RecordAudit(os.Args[1], getArgs(), getUser(), startTime, time.Now())
55+
}

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/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/start_flags.go

+2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ const (
110110
network = "network"
111111
startNamespace = "namespace"
112112
trace = "trace"
113+
userFlag = "user"
113114
)
114115

115116
var (
@@ -156,6 +157,7 @@ func initMinikubeFlags() {
156157
startCmd.Flags().StringP(network, "", "", "network to run minikube with. Only available with the docker/podman drivers. If left empty, minikube will create a new network.")
157158
startCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "Format to print stdout in. Options include: [text,json]")
158159
startCmd.Flags().StringP(trace, "", "", "Send trace events. Options include: [gcp]")
160+
startCmd.Flags().String(userFlag, "", "Sets who will be logged as executing the command, will use os user if none provided")
159161
}
160162

161163
// initKubernetesFlags inits the commandline flags for Kubernetes related options

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/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/start.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ minikube start [flags]
8989
--registry-mirror strings Registry mirrors to pass to the Docker daemon
9090
--service-cluster-ip-range string The CIDR to be used for service cluster IPs. (default "10.96.0.0/12")
9191
--trace string Send trace events. Options include: [gcp]
92+
--user string Sets who will be logged as executing the command, will use os user if none provided
9293
--uuid string Provide VM UUID to restore MAC address (hyperkit driver only)
9394
--vm Filter to use only VM Drivers
9495
--vm-driver driver DEPRECATED, use driver instead.

0 commit comments

Comments
 (0)