Skip to content

Commit 593cf3e

Browse files
committed
[#2166] Expand Kea name-only UNIX socket paths
1 parent a6d93d1 commit 593cf3e

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

backend/agent/kea.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"os"
1212
"path"
13+
"path/filepath"
1314
"regexp"
1415
"strings"
1516

@@ -328,9 +329,19 @@ func (sm *monitor) detectKeaDaemons(ctx context.Context, p supportedProcess) ([]
328329
}
329330
}
330331

332+
// Normalize socket path if the socket type is unix. Translate into full path if only name is given.
333+
socketAddress := controlSocket.GetAddress()
334+
if controlSocket.GetProtocol() == protocoltype.Socket && !strings.Contains(socketAddress, "/") {
335+
exe, err := p.getExe()
336+
if err != nil {
337+
return nil, errors.WithMessagef(err, "could not get path to executable")
338+
}
339+
socketAddress = filepath.Join(filepath.Dir(exe), "..", "var", "run", "kea", socketAddress)
340+
}
341+
331342
accessPoint := AccessPoint{
332343
Type: AccessPointControl,
333-
Address: controlSocket.GetAddress(),
344+
Address: socketAddress,
334345
Port: controlSocket.GetPort(),
335346
Protocol: controlSocket.GetProtocol(),
336347
Key: key,

backend/agent/kea_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,65 @@ func TestDetectKeaDHCPOnSocketPost3_0(t *testing.T) {
11381138
require.Equal(t, protocoltype.Socket, accessPoint.Protocol)
11391139
}
11401140

1141+
// Test that the Kea DHCP listening on socket configured with name only is detected.
1142+
func TestDetectKeaDHCPOnSocketNameOnly(t *testing.T) {
1143+
// Arrange
1144+
ctrl := gomock.NewController(t)
1145+
defer ctrl.Finish()
1146+
1147+
sb := testutil.NewSandbox()
1148+
defer sb.Close()
1149+
1150+
// Create a configuration file for Kea.
1151+
configPath, _ := sb.Write("kea-dhcp4.conf", `{
1152+
"Dhcp4": {
1153+
"control-socket": {
1154+
"socket-type": "unix",
1155+
"socket-name": "kea4-ctrl-socket"
1156+
}
1157+
}
1158+
}`)
1159+
exePath, _ := sb.Join("kea-dhcp4")
1160+
1161+
httpConfig := HTTPClientConfig{}
1162+
1163+
// Kea process mock.
1164+
process := NewMockSupportedProcess(ctrl)
1165+
process.EXPECT().getName().Return("kea-dhcp4", nil)
1166+
process.EXPECT().getDaemonName().Return(daemonname.DHCPv4)
1167+
process.EXPECT().getCmdline().Return(
1168+
fmt.Sprintf("%s -c %s", exePath, configPath),
1169+
nil,
1170+
)
1171+
process.EXPECT().getCwd().Return(sb.BasePath, nil)
1172+
process.EXPECT().getExe().Return(sb.BasePath, nil)
1173+
1174+
// System calls mock.
1175+
commander := NewMockCommandExecutor(ctrl)
1176+
commander.EXPECT().Output(exePath, "-v").Return([]byte("3.0.0\n"), nil)
1177+
1178+
monitor := newMonitor("", "", httpConfig)
1179+
monitor.commander = commander
1180+
1181+
// Act
1182+
daemons, err := monitor.detectKeaDaemons(t.Context(), process)
1183+
1184+
// Assert
1185+
require.False(t, gock.HasUnmatchedRequest())
1186+
require.NoError(t, err)
1187+
1188+
require.Len(t, daemons, 1)
1189+
require.Equal(t, daemonname.DHCPv4, daemons[0].GetName())
1190+
1191+
accessPoints := daemons[0].GetAccessPoints()
1192+
require.Len(t, accessPoints, 1)
1193+
accessPoint := accessPoints[0]
1194+
require.Equal(t, AccessPointControl, accessPoint.Type)
1195+
require.Equal(t, "/var/run/kea/kea4-ctrl-socket", accessPoint.Address)
1196+
require.Zero(t, accessPoint.Port)
1197+
require.Equal(t, protocoltype.Socket, accessPoint.Protocol)
1198+
}
1199+
11411200
// Test that the Kea DHCP post 3.0 listening on HTTP is detected.
11421201
func TestDetectKeaDHCPOnHTTPPost3_0(t *testing.T) {
11431202
// Arrange

backend/agent/process.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var (
1818
type supportedProcess interface {
1919
getCmdline() (string, error)
2020
getCwd() (string, error)
21+
getExe() (string, error)
2122
getName() (string, error)
2223
getPid() int32
2324
getParentPid() (int32, error)
@@ -44,6 +45,13 @@ func (p *processWrapper) getCwd() (string, error) {
4445
return cwd, err
4546
}
4647

48+
// Returns the process path to the executable.
49+
func (p *processWrapper) getExe() (string, error) {
50+
exe, err := p.process.Exe()
51+
err = errors.Wrapf(err, "failed to get process executable path for pid %d", p.getPid())
52+
return exe, err
53+
}
54+
4755
// Returns the process pid.
4856
func (p *processWrapper) getPid() int32 {
4957
return p.process.Pid
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[func] andrei
2+
3+
The agent can now communicate to UNIX control sockets if they are
4+
configured by name only in Kea.
5+
(Gitlab #2166)

0 commit comments

Comments
 (0)