Skip to content

Commit ad599b0

Browse files
committed
Merge branch 'v3' into fix-manifest-file-updates
2 parents b6dd077 + 55e4cbc commit ad599b0

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

internal/collector/nginxplusreceiver/factory.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/nginx/agent/v3/internal/collector/nginxplusreceiver/internal/metadata"
2020
)
2121

22+
// nolint: ireturn
2223
const defaultTimeout = 10 * time.Second
2324

2425
// nolint: ireturn

internal/watcher/instance/nginx_process_parser.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ func (npp *NginxProcessParser) getInfo(ctx context.Context, proc *nginxprocess.P
136136
}
137137
}
138138

139+
confPath := getConfPathFromCommand(proc.Cmd)
140+
139141
var nginxInfo *Info
140142

141143
outputBuffer, err := npp.executer.RunCmd(ctx, exePath, "-V")
@@ -148,6 +150,10 @@ func (npp *NginxProcessParser) getInfo(ctx context.Context, proc *nginxprocess.P
148150
nginxInfo.ExePath = exePath
149151
nginxInfo.ProcessID = proc.PID
150152

153+
if nginxInfo.ConfPath = getNginxConfPath(ctx, nginxInfo); confPath != "" {
154+
nginxInfo.ConfPath = confPath
155+
}
156+
151157
loadableModules := getLoadableModules(nginxInfo)
152158
nginxInfo.LoadableModules = loadableModules
153159

@@ -268,7 +274,6 @@ func parseNginxVersionCommandOutput(ctx context.Context, output *bytes.Buffer) *
268274
}
269275

270276
nginxInfo.Prefix = getNginxPrefix(ctx, nginxInfo)
271-
nginxInfo.ConfPath = getNginxConfPath(ctx, nginxInfo)
272277

273278
return nginxInfo
274279
}
@@ -392,3 +397,17 @@ func convertToMap(processes []*nginxprocess.Process) map[int32]*nginxprocess.Pro
392397

393398
return processesByPID
394399
}
400+
401+
func getConfPathFromCommand(command string) string {
402+
commands := strings.Split(command, " ")
403+
404+
for i, command := range commands {
405+
if command == "-c" {
406+
if i < len(commands)-1 {
407+
return commands[i+1]
408+
}
409+
}
410+
}
411+
412+
return ""
413+
}

internal/watcher/instance/nginx_process_parser_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,3 +616,14 @@ func TestNginxProcessParser_GetExe(t *testing.T) {
616616
})
617617
}
618618
}
619+
620+
func TestGetConfigPathFromCommand(t *testing.T) {
621+
result := getConfPathFromCommand("nginx: master process nginx -c /tmp/nginx.conf")
622+
assert.Equal(t, "/tmp/nginx.conf", result)
623+
624+
result = getConfPathFromCommand("nginx: master process nginx -c")
625+
assert.Equal(t, "", result)
626+
627+
result = getConfPathFromCommand("")
628+
assert.Equal(t, "", result)
629+
}

pkg/files/file_helpers.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ package files
88

99
import (
1010
"cmp"
11+
"crypto/sha256"
1112
"crypto/x509"
13+
"encoding/base64"
1214
"fmt"
1315
"net"
1416
"os"
1517
"slices"
1618
"strconv"
1719

18-
"github.com/google/uuid"
19-
2020
mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
2121
"github.com/nginx/agent/v3/internal/datasource/cert"
2222
"google.golang.org/protobuf/types/known/timestamppb"
@@ -135,7 +135,10 @@ func GenerateConfigVersion(fileSlice []*mpi.File) string {
135135

136136
// GenerateHash returns the hash value of a file's contents.
137137
func GenerateHash(b []byte) string {
138-
return uuid.NewMD5(uuid.Nil, b).String()
138+
hash := sha256.New()
139+
hash.Write(b)
140+
141+
return base64.StdEncoding.EncodeToString(hash.Sum(nil))
139142
}
140143

141144
// ConvertToMapOfFiles converts a list of files to a map of files with the file name as the key

pkg/files/file_helpers_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
package files
77

88
import (
9+
"crypto/sha256"
910
"crypto/x509"
11+
"encoding/base64"
1012
"net"
1113
"os"
1214
"testing"
1315

14-
"github.com/google/uuid"
1516
"github.com/stretchr/testify/assert"
1617
"github.com/stretchr/testify/require"
1718

@@ -136,6 +137,10 @@ func Test_GenerateConfigVersion(t *testing.T) {
136137
}
137138

138139
func TestGenerateHash(t *testing.T) {
140+
hash1 := sha256.New()
141+
hash2 := sha256.New()
142+
hash1.Write([]byte(""))
143+
hash2.Write([]byte("test"))
139144
tests := []struct {
140145
name string
141146
expected string
@@ -144,12 +149,12 @@ func TestGenerateHash(t *testing.T) {
144149
{
145150
name: "Test 1: empty byte slice",
146151
input: []byte{},
147-
expected: uuid.NewMD5(uuid.Nil, []byte("")).String(),
152+
expected: base64.StdEncoding.EncodeToString(hash1.Sum(nil)),
148153
},
149154
{
150155
name: "Test 2: non-empty byte slice",
151156
input: []byte("test"),
152-
expected: uuid.NewMD5(uuid.Nil, []byte("test")).String(),
157+
expected: base64.StdEncoding.EncodeToString(hash2.Sum(nil)),
153158
},
154159
}
155160

0 commit comments

Comments
 (0)