Skip to content
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

Add wrapper to execute shell commands #261

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ require (

require (
github.com/bufbuild/buf v1.15.1
github.com/evilmartians/lefthook v1.3.7
github.com/evilmartians/lefthook v1.3.8
github.com/go-resty/resty/v2 v2.7.0
github.com/go-swagger/go-swagger v0.30.4
github.com/goreleaser/nfpm/v2 v2.27.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/envoyproxy/protoc-gen-validate v0.3.0-java h1:bV5JGEB1ouEzZa0hgVDFFiClrUEuGWRaAc/3mxR2QK0=
github.com/envoyproxy/protoc-gen-validate v0.3.0-java/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evilmartians/lefthook v1.3.7 h1:h2lvNl5OSlnAZdV3bdA283itH6pHKaTnzk837dQqrCk=
github.com/evilmartians/lefthook v1.3.7/go.mod h1:FTw9v/+5Tg5AlrLPxS2kJeqHhQNqhUA+6/WdGeaf41s=
github.com/evilmartians/lefthook v1.3.8 h1:5SOqN6vd7S7FYvr9MDNM2g2lq/kTE3uTWY7Qt63uuj8=
github.com/evilmartians/lefthook v1.3.8/go.mod h1:FTw9v/+5Tg5AlrLPxS2kJeqHhQNqhUA+6/WdGeaf41s=
github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg=
github.com/felixge/fgprof v0.9.3 h1:VvyZxILNuCiUCSXtPtYmmtGvb65nqXh2QFWc0Wpf2/g=
Expand Down
192 changes: 192 additions & 0 deletions go.work.sum

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions sdk/proto/events/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ message Event {
ActivityEvent ActivityEvent = 2 [(gogoproto.jsontag) = "activity_event"]; // Activity event

/**
* While generating the SecurityViolationEvent, the Metadata for a SecurityViolationEvent
* would look as shown below:
* - Module = Agent
* - UUID = A UUID generated by the Agent for the EventReport
* - CorrelationID = The UUID will be used as the Correlation ID to track the EventReport
* - Timestamp = The timestamp when NGINX Agent received the violation event
* - EventLevel = All the SecurityViolationEvent would be generated at an ERROR_EVENT_LEVEL ("ERROR") level
* In future, the levels might be dynamically chosen based on Request Outcome of SecurityViolationEvent
* - Type = NGINX_EVENT_TYPE ("Nginx")
* - Category = APP_PROTECT_CATEGORY ("AppProtect")
*/
* While generating the SecurityViolationEvent, the Metadata for a SecurityViolationEvent
* would look as shown below:
* - Module = Agent
* - UUID = A UUID generated by the Agent for the EventReport
* - CorrelationID = The UUID will be used as the Correlation ID to track the EventReport
* - Timestamp = The timestamp when NGINX Agent received the violation event
* - EventLevel = All the SecurityViolationEvent would be generated at an ERROR_EVENT_LEVEL ("ERROR") level
* In future, the levels might be dynamically chosen based on Request Outcome of SecurityViolationEvent
* - Type = NGINX_EVENT_TYPE ("Nginx")
* - Category = APP_PROTECT_CATEGORY ("AppProtect")
*/

SecurityViolationEvent SecurityViolationEvent = 3 [(gogoproto.jsontag) = "security_violation_event"]; // Security violation event
}
Expand Down
20 changes: 18 additions & 2 deletions src/core/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,15 +654,31 @@ func processorCache(item cpu.InfoStat) map[string]string {
return cache
}

type Shell interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: recommend having doc comments on all exported symbols

Suggested change
type Shell interface {
// Shell runs child processes, exists to enable testing.
type Shell interface {

Exec(cmd string, arg ...string) ([]byte, error)
}

type execShellCommand struct {
}

func (e execShellCommand) Exec(cmd string, arg ...string) ([]byte, error) {
execCmd := exec.Command(name, arg...)
return execCmd.Output()
}

var shell Shell = execShellCommand{}

func getProcessorCacheInfo(cpuInfo cpuid.CPUInfo) map[string]string {
cache := getDefaultProcessorCacheInfo(cpuInfo)
return getCacheInfo(cache)
}

out, err := exec.Command("lscpu").Output()
func getCacheInfo(cache map[string]string) map[string]string {
out, err := shell.Exec("lscpu")
if err != nil {
log.Warnf("Install lscpu on host to get processor info: %v", err)
return cache
}

return parseLscpu(string(out), cache)
}

Expand Down
64 changes: 56 additions & 8 deletions src/core/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
package core

import (
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
Expand Down Expand Up @@ -469,17 +471,58 @@ func TestProcessors(t *testing.T) {
assert.Equal(t, "arm64", processorInfo[0].GetArchitecture())
}

func TestParseLscpu(t *testing.T) {
type fakeShell struct {
output map[string]string
errors map[string]error
}

func (f *fakeShell) Exec(cmd string, arg ...string) ([]byte, error) {
key := strings.Join(append([]string{cmd}, arg...), " ")
if err, ok := f.errors[key]; ok {
return nil, err
}
if out, ok := f.output[key]; ok {
return []byte(out), nil
}
return nil, fmt.Errorf("unexpected command %s", key)
}

func TestGetCacheInfo(t *testing.T) {
tempShellCommander := shell
defer func() { shell = tempShellCommander }()
tests := []struct {
name string
shell Shell
defaultCacheInfo map[string]string
cpuInfoCache proto.CpuInfo
lscpuContent string
expect map[string]string
}{
{
name: "os-release present",
lscpuContent: lscpuInfo1,
name: "lscpu error",
shell: &fakeShell{
errors: map[string]error{
"lscpu": errors.New("nope"),
},
},
defaultCacheInfo: map[string]string{
"L1d": "64 KiB",
"L1i": "96 KiB",
"L2": "2 MiB",
"L3": "1 MiB",
},
expect: map[string]string{
"L1d": "64 KiB",
"L1i": "96 KiB",
"L2": "2 MiB",
"L3": "1 MiB",
},
},
{
name: "default cache info absent",
shell: &fakeShell{
output: map[string]string{
"lscpu": lscpuInfo1,
},
},
defaultCacheInfo: map[string]string{
"L1d": "-1",
"L1i": "-1",
Expand All @@ -494,8 +537,12 @@ func TestParseLscpu(t *testing.T) {
},
},
{
name: "os-release present with quote",
lscpuContent: lscpuInfo2,
name: "os-release present with quote",
shell: &fakeShell{
output: map[string]string{
"lscpu": lscpuInfo2,
},
},
defaultCacheInfo: map[string]string{
"L1d": "-1",
"L1i": "-1",
Expand All @@ -512,7 +559,8 @@ func TestParseLscpu(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := parseLscpu(tt.lscpuContent, tt.defaultCacheInfo)
shell = tt.shell
actual := getCacheInfo(tt.defaultCacheInfo)
assert.Equal(t, tt.expect, actual)
})
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ github.com/emirpasic/gods/utils
# github.com/envoyproxy/protoc-gen-validate v0.3.0-java
## explicit
github.com/envoyproxy/protoc-gen-validate/validate
# github.com/evilmartians/lefthook v1.3.7
# github.com/evilmartians/lefthook v1.3.8
## explicit; go 1.19
github.com/evilmartians/lefthook
github.com/evilmartians/lefthook/cmd
Expand Down