forked from turbot/steampipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (114 loc) · 3.64 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
"github.com/hashicorp/go-version"
_ "github.com/jackc/pgx/v4/stdlib"
filehelpers "github.com/turbot/go-kit/files"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/steampipe/cmd"
"github.com/turbot/steampipe/pkg/constants"
"github.com/turbot/steampipe/pkg/utils"
)
var exitCode int
func main() {
ctx := context.Background()
utils.LogTime("main start")
exitCode := constants.ExitCodeSuccessful
defer func() {
if r := recover(); r != nil {
utils.ShowError(ctx, helpers.ToError(r))
}
utils.LogTime("main end")
utils.DisplayProfileData()
os.Exit(exitCode)
}()
// ensure steampipe is not being run as root
checkRoot(ctx)
// ensure steampipe is not run on WSL1
checkWsl1(ctx)
// increase the soft ULIMIT to match the hard limit
err := setULimit()
utils.FailOnErrorWithMessage(err, "failed to increase the file limit")
cmd.InitCmd()
// execute the command
exitCode = cmd.Execute()
}
// set the current to the max to avoid any file handle shortages
func setULimit() error {
ulimit, err := filehelpers.GetULimit()
if err != nil {
return err
}
// set the current ulimit to 8192 (or the max, if less)
// this is to ensure we do not run out of file handler when watching files
var newULimit uint64 = 8192
if newULimit > ulimit.Max {
newULimit = ulimit.Max
}
err = filehelpers.SetULimit(newULimit)
return err
}
// this is to replicate the user security mechanism of out underlying
// postgresql engine.
func checkRoot(ctx context.Context) {
if os.Geteuid() == 0 {
exitCode = constants.ExitCodeUnknownErrorPanic
utils.ShowError(ctx, fmt.Errorf(`Steampipe cannot be run as the "root" user.
To reduce security risk, use an unprivileged user account instead.`))
os.Exit(exitCode)
}
/*
* Also make sure that real and effective uids are the same. Executing as
* a setuid program from a root shell is a security hole, since on many
* platforms a nefarious subroutine could setuid back to root if real uid
* is root. (Since nobody actually uses postgres as a setuid program,
* trying to actively fix this situation seems more trouble than it's
* worth; we'll just expend the effort to check for it.)
*/
if os.Geteuid() != os.Getuid() {
exitCode = constants.ExitCodeUnknownErrorPanic
utils.ShowError(ctx, fmt.Errorf("real and effective user IDs must match."))
os.Exit(exitCode)
}
}
func checkWsl1(ctx context.Context) {
// store the 'uname -r' output
output, err := exec.Command("uname", "-r").Output()
if err != nil {
utils.ShowErrorWithMessage(ctx, err, "Error while checking uname")
return
}
// convert the ouptut to a string of lowercase characters for ease of use
op := strings.ToLower(string(output))
// if WSL2, return
if strings.Contains(op, "wsl2") {
return
}
// if output contains 'microsoft' or 'wsl', check the kernel version
if strings.Contains(op, "microsoft") || strings.Contains(op, "wsl") {
// store the system kernel version
sys_kernel, _, _ := strings.Cut(string(output), "-")
sys_kernel_ver, err := version.NewVersion(sys_kernel)
if err != nil {
utils.ShowErrorWithMessage(ctx, err, "Error while checking system kernel version")
return
}
// if the kernel version >= 4.19, it's WSL Version 2.
kernel_ver, err := version.NewVersion("4.19")
if err != nil {
utils.ShowErrorWithMessage(ctx, err, "Error while checking system kernel version")
return
}
// if the kernel version >= 4.19, it's WSL version 2, else version 1
if sys_kernel_ver.GreaterThanOrEqual(kernel_ver) {
return
} else {
utils.ShowError(ctx, fmt.Errorf("Steampipe requires WSL2, please upgrade and try again."))
os.Exit(1)
}
}
}