-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add user flag and log executed commands
- Loading branch information
Showing
37 changed files
with
326 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
Copyright 2020 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 audit | ||
|
||
import ( | ||
"os" | ||
"os/user" | ||
"strings" | ||
"time" | ||
|
||
"github.com/spf13/viper" | ||
"k8s.io/klog" | ||
"k8s.io/minikube/pkg/minikube/config" | ||
) | ||
|
||
// username pulls the user flag, if empty gets the os username. | ||
func username() string { | ||
u := viper.GetString(config.User) | ||
if u != "" { | ||
return u | ||
} | ||
osUser, err := user.Current() | ||
if err != nil { | ||
return "UNKNOWN" | ||
} | ||
return osUser.Username | ||
} | ||
|
||
// args concats the args into space delimited string. | ||
func args() string { | ||
if len(os.Args) < 3 { | ||
return "" | ||
} | ||
return strings.Join(os.Args[2:], " ") | ||
} | ||
|
||
// Log details about the executed command. | ||
func Log(startTime time.Time) { | ||
e := newEntry(os.Args[1], args(), username(), startTime, time.Now()) | ||
if err := appendToLog(e); err != nil { | ||
klog.Error(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
Copyright 2020 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 audit | ||
|
||
import ( | ||
"os" | ||
"os/user" | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
"k8s.io/minikube/pkg/minikube/config" | ||
) | ||
|
||
func TestAudit(t *testing.T) { | ||
t.Run("UsernameSetFlag", func(t *testing.T) { | ||
want := "test123" | ||
viper.Set(config.User, want) | ||
|
||
got := username() | ||
|
||
if got != want { | ||
t.Errorf("username() = %q; want %q", got, want) | ||
} | ||
}) | ||
|
||
t.Run("UsernameOS", func(t *testing.T) { | ||
viper.Set(config.User, "") | ||
|
||
u, err := user.Current() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
want := u.Username | ||
|
||
got := username() | ||
|
||
if got != want { | ||
t.Errorf("username() = %q; want %q", got, want) | ||
} | ||
}) | ||
|
||
t.Run("ArgsNone", func(t *testing.T) { | ||
oldArgs := os.Args | ||
defer func() { os.Args = oldArgs }() | ||
os.Args = []string{"minikube", "start"} | ||
|
||
want := "" | ||
|
||
got := args() | ||
|
||
if got != want { | ||
t.Errorf("args() = %q; want %q", got, want) | ||
} | ||
}) | ||
|
||
t.Run("Args", func(t *testing.T) { | ||
oldArgs := os.Args | ||
defer func() { os.Args = oldArgs }() | ||
os.Args = []string{"minikube", "start", "--user", "test123"} | ||
|
||
want := "--user test123" | ||
|
||
got := args() | ||
|
||
if got != want { | ||
t.Errorf("args() = %q; want %q", got, want) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
Copyright 2020 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 audit | ||
|
||
import "time" | ||
|
||
// entry represents the execution of a command. | ||
type entry struct { | ||
data map[string]string | ||
} | ||
|
||
// Type returns the cloud events compatible type of this struct. | ||
func (e *entry) Type() string { | ||
return "io.k8s.sigs.minikube.audit" | ||
} | ||
|
||
// newEntry returns a new audit type. | ||
func newEntry(command string, args string, user string, startTime time.Time, endTime time.Time) *entry { | ||
return &entry{ | ||
map[string]string{ | ||
"args": args, | ||
"command": command, | ||
"endTime": endTime.String(), | ||
"startTime": startTime.String(), | ||
"user": user, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
Copyright 2020 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 audit | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"k8s.io/minikube/pkg/minikube/localpath" | ||
"k8s.io/minikube/pkg/minikube/out/register" | ||
) | ||
|
||
var ( | ||
file *os.File | ||
logPath string | ||
) | ||
|
||
// setLogFile sets the logPath and creates the log file if it doesn't exist. | ||
func setLogFile() error { | ||
logPath = localpath.AuditLog() | ||
if _, err := os.Stat(logPath); os.IsNotExist(err) { | ||
return createLogFile() | ||
} | ||
f, err := os.OpenFile(logPath, os.O_APPEND|os.O_WRONLY, os.ModeAppend) | ||
if err != nil { | ||
return fmt.Errorf("unable to open %s: %v", logPath, err) | ||
} | ||
file = f | ||
return nil | ||
} | ||
|
||
// createLogFile creates the file used for logging audits. | ||
func createLogFile() error { | ||
f, err := os.Create(logPath) | ||
if err != nil { | ||
return fmt.Errorf("unable to create file %s: %v", logPath, err) | ||
} | ||
file = f | ||
return nil | ||
} | ||
|
||
// appendToLog appends the audit entry to the log file. | ||
func appendToLog(entry *entry) error { | ||
if file == nil { | ||
if err := setLogFile(); err != nil { | ||
return err | ||
} | ||
} | ||
e := register.CloudEvent(entry, entry.data) | ||
bs, err := e.MarshalJSON() | ||
if err != nil { | ||
return fmt.Errorf("error marshalling event: %v", err) | ||
} | ||
if _, err := file.WriteString(string(bs) + "\n"); err != nil { | ||
return fmt.Errorf("unable to write to %s: %v", logPath, err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.