-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warnings that k8s service may not work #657
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3cb808d
Warnings that k8s service may not work
maci3jka 9b6bb63
Added validation for running in lxd
maci3jka 727fb4a
moved validation to app
maci3jka 925ccd5
k8s on lxd url fix
maci3jka 7b122e1
revert changes
maci3jka 3a0b835
review fixes
maci3jka 061ed37
review fixes
maci3jka 4eb2a79
review fixes
maci3jka 00ef325
review fixes
maci3jka 50ef960
review fixes
maci3jka 7b14054
vet fix
maci3jka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,118 @@ | ||
package cmdutil | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
const initialProcesEnvironmentVariables = "/proc/1/environ" | ||
|
||
// paths to validate if root in the owner | ||
maci3jka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var pathsOwnershipCheck = []string{"/sys", "/proc", "/dev/kmsg"} | ||
|
||
// HookVerifyResources checks ownership of dirs required for k8s to run. | ||
// HookVerifyResources validates AppArmor configurations. | ||
// If potential issue found pops up warning. | ||
func HookVerifyResources() func(*cobra.Command, []string) { | ||
maci3jka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return func(cmd *cobra.Command, args []string) { | ||
maci3jka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var warnList []string | ||
|
||
// check ownership of required dirs | ||
for _, path := range pathsOwnershipCheck { | ||
if msg, err := validateRootOwnership(path); err != nil { | ||
cmd.PrintErrf(err.Error()) | ||
} else if len(msg) > 0 { | ||
warnList = append(warnList, msg) | ||
} | ||
} | ||
|
||
// check App Armor | ||
if armor, err := checkAppArmor(); err != nil { | ||
cmd.PrintErr(err.Error()) | ||
} else if len(armor) > 0 { | ||
warnList = append(warnList, armor) | ||
} | ||
|
||
// check LXD | ||
if lxd, err := validateLXD(); err != nil { | ||
cmd.PrintErr(err.Error()) | ||
} else if len(warnList) > 0 && len(lxd) > 0 { | ||
warnList = append(warnList, lxd) | ||
|
||
} | ||
|
||
// generate report | ||
if len(warnList) > 0 { | ||
cmd.PrintErrf("Warning: k8s may not run correctly due to reasons:\n%s", | ||
strings.Join(warnList, "")) | ||
} | ||
} | ||
} | ||
|
||
// validateLXD checks if k8s runs in lxd container if so returns link to documentation | ||
func validateLXD() (string, error) { | ||
dat, err := os.ReadFile(initialProcesEnvironmentVariables) | ||
if err != nil { | ||
return "", err | ||
} | ||
env := string(dat) | ||
if strings.Contains(env, "container=lxc") { | ||
return "For running k8s inside LXD container refer to " + | ||
"https://documentation.ubuntu.com/canonical-kubernetes/latest/snap/howto/install/lxd/.\n", nil | ||
} | ||
return "", nil | ||
} | ||
|
||
// validateRootOwnership checks if given path owner root and root group. | ||
func validateRootOwnership(path string) (string, error) { | ||
|
||
info, err := os.Stat(path) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return fmt.Sprintf("%s do not exist.\n", path), nil | ||
} else { | ||
return "", err | ||
} | ||
} | ||
var UID int | ||
var GID int | ||
if stat, ok := info.Sys().(*syscall.Stat_t); ok { | ||
UID = int(stat.Uid) | ||
GID = int(stat.Gid) | ||
} else { | ||
return "", errors.New(fmt.Sprintf("cannot access path %s", path)) | ||
} | ||
var warnList string | ||
if UID != 0 { | ||
warnList += fmt.Sprintf("owner of %s is user with UID %d expected 0.\n", path, UID) | ||
} | ||
if GID != 0 { | ||
warnList += fmt.Sprintf("owner of %s is group with GID %d expected 0.\n", path, GID) | ||
} | ||
return warnList, nil | ||
} | ||
|
||
// checkAppArmor checks AppArmor status. | ||
func checkAppArmor() (string, error) { | ||
cmd := exec.Command("journalctl", "-u", "apparmor") | ||
maci3jka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
out, err := cmd.CombinedOutput() | ||
if err != nil { | ||
return "", err | ||
} | ||
output := string(out) | ||
// AppArmor configured for container or service not present | ||
if strings.Contains(output, "Not starting AppArmor in container") || strings.Contains(output, "-- No entries --") { | ||
return "", nil | ||
// cannot read status of AppArmor | ||
} else if strings.Contains(output, "Users in groups 'adm', 'systemd-journal' can see all messages.") { | ||
return "could not validate AppArmor status.\n", nil | ||
} | ||
|
||
return "AppArmor may block hosting of nested containers.\n", nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Process
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also add a short comment please