Skip to content

Commit

Permalink
WIP: Add user flag and log executed commands
Browse files Browse the repository at this point in the history
  • Loading branch information
spowelljr committed Jan 7, 2021
1 parent 857e0a2 commit 3f62f18
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 0 deletions.
55 changes: 55 additions & 0 deletions cmd/minikube/cmd/audit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"os"
"os/user"
"strings"
"time"

"github.com/spf13/viper"
"k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/minikube/out/register"
)

// getUser pulls the user flag, if empty gets the os user
func getUser() string {
u := viper.GetString(userFlag)
if u != "" {
return u
}
osUser, err := user.Current()
if err != nil {
return "unable to get user"
}
return osUser.Username
}

// getArgs concats the args into space delimited string
func getArgs() string {
if len(os.Args) < 3 {
return ""
}
return strings.Join(os.Args[2:], " ")
}

// audit logs details about the executed command
func audit(startTime time.Time) {
register.SetEventLogPath(localpath.EventLog("audit"))
register.RecordAudit(os.Args[1], getArgs(), getUser(), startTime, time.Now())
}
4 changes: 4 additions & 0 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os/exec"
"path/filepath"
"strconv"
"time"

"github.com/docker/machine/libmachine/mcnerror"
"github.com/mitchellh/go-ps"
Expand Down Expand Up @@ -129,6 +130,9 @@ func runDelete(cmd *cobra.Command, args []string) {
if len(args) > 0 {
exit.Message(reason.Usage, "Usage: minikube delete")
}

defer audit(time.Now())

// register.SetEventLogPath(localpath.EventLog(ClusterFlagValue()))
register.Reg.SetStep(register.Deleting)

Expand Down
3 changes: 3 additions & 0 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"regexp"
"runtime"
"strings"
"time"

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

// runStart handles the executes the flow of "minikube start"
func runStart(cmd *cobra.Command, args []string) {
defer audit(time.Now())

register.SetEventLogPath(localpath.EventLog(ClusterFlagValue()))

out.SetJSON(outputFormat == "json")
Expand Down
2 changes: 2 additions & 0 deletions cmd/minikube/cmd/start_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const (
network = "network"
startNamespace = "namespace"
trace = "trace"
userFlag = "user"
)

var (
Expand Down Expand Up @@ -156,6 +157,7 @@ func initMinikubeFlags() {
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.")
startCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "Format to print stdout in. Options include: [text,json]")
startCmd.Flags().StringP(trace, "", "", "Send trace events. Options include: [gcp]")
startCmd.Flags().String(userFlag, "", "Sets who will be logged as executing the command, will use os user if none provided")
}

// initKubernetesFlags inits the commandline flags for Kubernetes related options
Expand Down
2 changes: 2 additions & 0 deletions cmd/minikube/cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func init() {

// runStop handles the executes the flow of "minikube stop"
func runStop(cmd *cobra.Command, args []string) {
defer audit(time.Now())

out.SetJSON(outputFormat == "json")
register.Reg.SetStep(register.Stopping)

Expand Down
8 changes: 8 additions & 0 deletions pkg/minikube/out/register/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package register

import "time"

// PrintStep prints a Step type in JSON format
func PrintStep(message string) {
s := NewStep(message)
Expand Down Expand Up @@ -69,3 +71,9 @@ func PrintWarning(warning string) {
w := NewWarning(warning)
printAndRecordCloudEvent(w, w.data)
}

// RecordAudit records an Audit type in JSON format
func RecordAudit(command string, args string, user string, startTime time.Time, endTime time.Time) {
a := NewAudit(command, args, user, startTime, endTime)
recordCloudEvent(a, a.data)
}
24 changes: 24 additions & 0 deletions pkg/minikube/out/register/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package register
import (
"fmt"
"strings"
"time"
)

// Log represents the different types of logs that can be output as JSON
Expand Down Expand Up @@ -154,3 +155,26 @@ func NewErrorExitCode(err string, exitcode int, additionalData ...map[string]str
func (s *Error) Type() string {
return "io.k8s.sigs.minikube.error"
}

// Audit represents the execution of a command
type Audit struct {
data map[string]string
}

// Type returns the cloud events compatible type of this struct
func (a *Audit) Type() string {
return "io.k8s.sigs.minikube.audit"
}

// NewAudit returns a new audit type
func NewAudit(command string, args string, user string, startTime time.Time, endTime time.Time) *Audit {
return &Audit{
map[string]string{
"args": args,
"command": command,
"endTime": endTime.String(),
"startTime": startTime.String(),
"user": user,
},
}
}

0 comments on commit 3f62f18

Please sign in to comment.