Skip to content

Commit 0e767b2

Browse files
committed
Merge branch 'main' into allowedDirsFix
2 parents 279dcdd + 240cf41 commit 0e767b2

File tree

10 files changed

+104
-38
lines changed

10 files changed

+104
-38
lines changed

Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ GOBIN ?= $$(go env GOPATH)/bin
1717
# | OS_RELEASE | OS_VERSION | NOTES |
1818
# | ---------------- | ----------------------------------------- | -------------------------------------------------------------- |
1919
# | amazonlinux | 2, 2023 | |
20-
# | ubuntu | 20.04, 22.04 24.04 | |
20+
# | ubuntu | 22.04, 24.04 25.04 | |
2121
# | debian | bullseye-slim, bookworm-slim | |
22-
# | redhatenterprise | 8, 9 | |
23-
# | rockylinux | 8, 9 | |
24-
# | almalinux | 8, 9 | |
25-
# | alpine | 3.18, 3.19, 3.20, 3.21 3.22 | |
26-
# | oraclelinux | 8, 9 | |
22+
# | redhatenterprise | 8, 9, 10 | |
23+
# | rockylinux | 8, 9, 10 | |
24+
# | almalinux | 8, 9, 10 | |
25+
# | alpine | 3.19, 3.20, 3.21 3.22 | |
26+
# | oraclelinux | 8, 9, 10 | |
2727
# | suse | sle15 | |
2828
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
2929
OS_RELEASE ?= ubuntu

internal/command/command_plugin.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,28 +106,29 @@ func (cp *CommandPlugin) Info() *bus.Info {
106106

107107
func (cp *CommandPlugin) Process(ctx context.Context, msg *bus.Message) {
108108
slog.DebugContext(ctx, "Processing command")
109+
ctxWithMetadata := cp.config.NewContextWithLabels(ctx)
109110

110-
if logger.ServerType(ctx) == "" {
111-
ctx = context.WithValue(
112-
ctx,
111+
if logger.ServerType(ctxWithMetadata) == "" {
112+
ctxWithMetadata = context.WithValue(
113+
ctxWithMetadata,
113114
logger.ServerTypeContextKey, slog.Any(logger.ServerTypeKey, cp.commandServerType.String()),
114115
)
115116
}
116117

117-
if logger.ServerType(ctx) == cp.commandServerType.String() {
118+
if logger.ServerType(ctxWithMetadata) == cp.commandServerType.String() {
118119
switch msg.Topic {
119120
case bus.ConnectionResetTopic:
120-
cp.processConnectionReset(ctx, msg)
121+
cp.processConnectionReset(ctxWithMetadata, msg)
121122
case bus.ResourceUpdateTopic:
122-
cp.processResourceUpdate(ctx, msg)
123+
cp.processResourceUpdate(ctxWithMetadata, msg)
123124
case bus.InstanceHealthTopic:
124-
cp.processInstanceHealth(ctx, msg)
125+
cp.processInstanceHealth(ctxWithMetadata, msg)
125126
case bus.DataPlaneHealthResponseTopic:
126-
cp.processDataPlaneHealth(ctx, msg)
127+
cp.processDataPlaneHealth(ctxWithMetadata, msg)
127128
case bus.DataPlaneResponseTopic:
128-
cp.processDataPlaneResponse(ctx, msg)
129+
cp.processDataPlaneResponse(ctxWithMetadata, msg)
129130
default:
130-
slog.DebugContext(ctx, "Command plugin received unknown topic", "topic", msg.Topic)
131+
slog.DebugContext(ctxWithMetadata, "Command plugin received unknown topic", "topic", msg.Topic)
131132
}
132133
}
133134
}

internal/config/config.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ func ResolveConfig() (*Config, error) {
120120
}
121121

122122
checkCollectorConfiguration(collector, config)
123+
addLabelsAsOTelHeaders(collector, config.Labels)
123124

124125
slog.Info(
125126
"Excluded files from being watched for file changes",
@@ -227,6 +228,22 @@ func defaultCollector(collector *Collector, config *Config) {
227228
}
228229
}
229230

231+
func addLabelsAsOTelHeaders(collector *Collector, labels map[string]any) {
232+
slog.Debug("Adding labels as headers to collector", "labels", labels)
233+
if collector.Extensions.HeadersSetter != nil {
234+
for key, value := range labels {
235+
valueString, ok := value.(string)
236+
if ok {
237+
collector.Extensions.HeadersSetter.Headers = append(collector.Extensions.HeadersSetter.Headers, Header{
238+
Action: "insert",
239+
Key: key,
240+
Value: valueString,
241+
})
242+
}
243+
}
244+
}
245+
}
246+
230247
func setVersion(version, commit string) {
231248
RootCommand.Version = version + "-" + commit
232249
viperInstance.SetDefault(VersionKey, version)

internal/config/config_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"errors"
1010
"os"
1111
"path"
12+
"sort"
1213
"strings"
1314
"testing"
1415
"time"
@@ -63,6 +64,10 @@ func TestResolveConfig(t *testing.T) {
6364

6465
actual, err := ResolveConfig()
6566
require.NoError(t, err)
67+
sort.Slice(actual.Collector.Extensions.HeadersSetter.Headers, func(i, j int) bool {
68+
headers := actual.Collector.Extensions.HeadersSetter.Headers
69+
return headers[i].Key < headers[j].Key
70+
})
6671
assert.Equal(t, createConfig(), actual)
6772
}
6873

@@ -1067,6 +1072,16 @@ func createConfig() *Config {
10671072
Key: "key",
10681073
Value: "value",
10691074
},
1075+
{
1076+
Action: "insert",
1077+
Key: "label1",
1078+
Value: "label 1",
1079+
},
1080+
{
1081+
Action: "insert",
1082+
Key: "label2",
1083+
Value: "new-value",
1084+
},
10701085
},
10711086
},
10721087
},

internal/config/types.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package config
77

88
import (
9+
"context"
910
"errors"
1011
"fmt"
1112
"log/slog"
@@ -15,6 +16,8 @@ import (
1516
"strings"
1617
"time"
1718

19+
"google.golang.org/grpc/metadata"
20+
1821
"github.com/google/uuid"
1922
)
2023

@@ -449,6 +452,18 @@ func (c *Config) AreReceiversConfigured() bool {
449452
len(c.Collector.Receivers.TcplogReceivers) > 0
450453
}
451454

455+
func (c *Config) NewContextWithLabels(ctx context.Context) context.Context {
456+
md := metadata.Pairs()
457+
for key, value := range c.Labels {
458+
valueString, ok := value.(string)
459+
if ok {
460+
md.Set(key, valueString)
461+
}
462+
}
463+
464+
return metadata.NewOutgoingContext(ctx, md)
465+
}
466+
452467
// isAllowedDir checks if the given path is in the list of allowed directories.
453468
// It returns true if the path is allowed, false otherwise.
454469
// If the path is allowed but does not exist, it also logs a warning.

internal/file/file_plugin.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,34 +83,36 @@ func (fp *FilePlugin) Info() *bus.Info {
8383

8484
// nolint: cyclop, revive
8585
func (fp *FilePlugin) Process(ctx context.Context, msg *bus.Message) {
86+
ctxWithMetadata := fp.config.NewContextWithLabels(ctx)
87+
8688
if logger.ServerType(ctx) == "" {
87-
ctx = context.WithValue(
88-
ctx,
89+
ctxWithMetadata = context.WithValue(
90+
ctxWithMetadata,
8991
logger.ServerTypeContextKey, slog.Any(logger.ServerTypeKey, fp.serverType.String()),
9092
)
9193
}
9294

93-
if logger.ServerType(ctx) == fp.serverType.String() {
95+
if logger.ServerType(ctxWithMetadata) == fp.serverType.String() {
9496
switch msg.Topic {
9597
case bus.ConnectionResetTopic:
96-
fp.handleConnectionReset(ctx, msg)
98+
fp.handleConnectionReset(ctxWithMetadata, msg)
9799
case bus.ConnectionCreatedTopic:
98-
slog.DebugContext(ctx, "File plugin received connection created message")
100+
slog.DebugContext(ctxWithMetadata, "File plugin received connection created message")
99101
fp.fileManagerService.SetIsConnected(true)
100102
case bus.NginxConfigUpdateTopic:
101-
fp.handleNginxConfigUpdate(ctx, msg)
103+
fp.handleNginxConfigUpdate(ctxWithMetadata, msg)
102104
case bus.ConfigUploadRequestTopic:
103-
fp.handleConfigUploadRequest(ctx, msg)
105+
fp.handleConfigUploadRequest(ctxWithMetadata, msg)
104106
case bus.ConfigApplyRequestTopic:
105-
fp.handleConfigApplyRequest(ctx, msg)
107+
fp.handleConfigApplyRequest(ctxWithMetadata, msg)
106108
case bus.ConfigApplyCompleteTopic:
107-
fp.handleConfigApplyComplete(ctx, msg)
109+
fp.handleConfigApplyComplete(ctxWithMetadata, msg)
108110
case bus.ConfigApplySuccessfulTopic:
109-
fp.handleConfigApplySuccess(ctx, msg)
111+
fp.handleConfigApplySuccess(ctxWithMetadata, msg)
110112
case bus.ConfigApplyFailedTopic:
111-
fp.handleConfigApplyFailedRequest(ctx, msg)
113+
fp.handleConfigApplyFailedRequest(ctxWithMetadata, msg)
112114
default:
113-
slog.DebugContext(ctx, "File plugin received unknown topic", "topic", msg.Topic)
115+
slog.DebugContext(ctxWithMetadata, "File plugin received unknown topic", "topic", msg.Topic)
114116
}
115117
}
116118
}

scripts/packages/package-check.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,16 @@ APK=(
5555
alpine/v3.21/main/x86_64/nginx-agent-$VERSION.apk
5656
alpine/v3.20/main/aarch64/nginx-agent-$VERSION.apk
5757
alpine/v3.20/main/x86_64/nginx-agent-$VERSION.apk
58-
alpine/v3.18/main/aarch64/nginx-agent-$VERSION.apk
59-
alpine/v3.18/main/x86_64/nginx-agent-$VERSION.apk
6058
alpine/v3.19/main/aarch64/nginx-agent-$VERSION.apk
6159
alpine/v3.19/main/x86_64/nginx-agent-$VERSION.apk
6260
)
6361
UBUNTU=(
64-
ubuntu/pool/agent/n/nginx-agent/nginx-agent_$VERSION~focal_arm64.deb
6562
ubuntu/pool/agent/n/nginx-agent/nginx-agent_$VERSION~jammy_amd64.deb
6663
ubuntu/pool/agent/n/nginx-agent/nginx-agent_$VERSION~noble_arm64.deb
64+
ubuntu/pool/agent/n/nginx-agent/nginx-agent_$VERSION~plucky_arm64.deb
6765
ubuntu/pool/agent/n/nginx-agent/nginx-agent_$VERSION~jammy_arm64.deb
6866
ubuntu/pool/agent/n/nginx-agent/nginx-agent_$VERSION~noble_amd64.deb
69-
ubuntu/pool/agent/n/nginx-agent/nginx-agent_$VERSION~focal_amd64.deb
67+
ubuntu/pool/agent/n/nginx-agent/nginx-agent_$VERSION~plucky_amd64.deb
7068
)
7169
DEBIAN=(
7270
debian/pool/agent/n/nginx-agent/nginx-agent_$VERSION~bullseye_arm64.deb
@@ -85,6 +83,8 @@ SUSE=(
8583
sles/15/x86_64/RPMS/nginx-agent-$VERSION.sles15.ngx.x86_64.rpm
8684
)
8785
CENTOS=(
86+
centos/10/aarch64/RPMS/nginx-agent-$VERSION.el10.ngx.aarch64.rpm
87+
centos/10/x86_64/RPMS/nginx-agent-$VERSION.el10.ngx.x86_64.rpm
8888
centos/9/aarch64/RPMS/nginx-agent-$VERSION.el9.ngx.aarch64.rpm
8989
centos/9/x86_64/RPMS/nginx-agent-$VERSION.el9.ngx.x86_64.rpm
9090
centos/8/aarch64/RPMS/nginx-agent-$VERSION.el8.ngx.aarch64.rpm

test/docker/entrypoint.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ echo "starting nginx ..."
3333
nginx_pid=$!
3434

3535
SECONDS=0
36-
37-
while ! ps -ef | grep "nginx: master process" | grep -v grep; do
36+
while [[ ! -f /var/run/nginx.pid ]] && [[ ! -f /var/run/nginx/nginx.pid ]]; do
3837
if (( SECONDS > 30 )); then
3938
echo "couldn't find nginx master process"
4039
exit 1
4140
fi
41+
sleep 1
4242
done
4343

4444
cat /etc/nginx-agent/nginx-agent.conf;

test/mock/collector/nginx-agent.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ allowed_directories:
2626
- /usr/local/etc/nginx
2727
- /usr/share/nginx/modules
2828
- /var/run/nginx
29+
30+
labels:
31+
product-type: mock-product
32+
product-version: v1.0.0
2933

3034
client:
3135
http:

test/mock/grpc/mock_management_server.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ var (
5252
Time: keepAliveTime,
5353
Timeout: keepAliveTimeout,
5454
}
55+
56+
errMissingMetadata = status.Errorf(codes.InvalidArgument, "missing metadata")
57+
errInvalidToken = status.Errorf(codes.Unauthenticated, "invalid token")
5558
)
5659

5760
type MockManagementServer struct {
@@ -146,13 +149,15 @@ func serverOptions(agentConfig *config.Config) []grpc.ServerOption {
146149
opts = append(opts, grpc.ChainUnaryInterceptor(
147150
grpcvalidator.UnaryServerInterceptor(),
148151
protovalidateInterceptor.UnaryServerInterceptor(validator),
152+
logHeaders,
149153
),
150154
)
151155
} else {
152156
opts = append(opts, grpc.ChainUnaryInterceptor(
153157
grpcvalidator.UnaryServerInterceptor(),
154158
protovalidateInterceptor.UnaryServerInterceptor(validator),
155159
ensureValidToken,
160+
logHeaders,
156161
),
157162
)
158163
}
@@ -242,10 +247,6 @@ func reportHealth(healthcheck *health.Server, agentConfig *config.Config) {
242247
}
243248

244249
func ensureValidToken(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
245-
var (
246-
errMissingMetadata = status.Errorf(codes.InvalidArgument, "missing metadata")
247-
errInvalidToken = status.Errorf(codes.Unauthenticated, "invalid token")
248-
)
249250
md, ok := metadata.FromIncomingContext(ctx)
250251
if !ok {
251252
return nil, errMissingMetadata
@@ -270,3 +271,14 @@ func valid(authorization []string) bool {
270271
// for a token matching an arbitrary string.
271272
return token == "1234"
272273
}
274+
275+
func logHeaders(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
276+
md, ok := metadata.FromIncomingContext(ctx)
277+
if !ok {
278+
return nil, errMissingMetadata
279+
}
280+
281+
slog.InfoContext(ctx, "Request headers", "headers", md)
282+
283+
return handler(ctx, req)
284+
}

0 commit comments

Comments
 (0)