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

feat: fetch dhcp leases from network-manager via dbus #427

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions docker-compose.dpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ services:
'--device-private-key', '/certs/first_private_key.pem',
'--serial-number', 'first-serial-number']

agent6:
<<: *agent
volumes:
- /var/run/dbus:/var/run/dbus
- client-certs:/certs
- /etc/os-release:/etc/os-release
- /etc/ssh:/etc/ssh
privileged: true
command: ['/opi-sztp-agent', 'run',
'--bootstrap-trust-anchor-cert', '/certs/opi.pem',
'--device-end-entity-cert', '/certs/first_my_cert.pem',
'--device-private-key', '/certs/first_private_key.pem',
'--serial-number', 'first-serial-number']

volumes:
client-certs:
dhcp-leases-folder:
3 changes: 2 additions & 1 deletion scripts/run_agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ docker run --rm -it --network=host \
--mount type=bind,source=/etc/ssh,target=/etc/ssh,readonly \
--mount type=bind,source=/etc/os-release,target=/etc/os-release,readonly \
--mount type=bind,source=/var/lib/NetworkManager,target=/var/lib/NetworkManager,readonly \
--mount type=bind,source=/var/run/dbus,target=/var/run/dbus,readonly \
--privileged \
${DOCKER_SZTP_IMAGE} \
/opi-sztp-agent daemon \
--dhcp-lease-file /var/lib/NetworkManager/dhclient-eth0.lease \
--bootstrap-trust-anchor-cert /mnt/opi.pem \
--device-end-entity-cert /mnt/opi_cert.pem \
--device-private-key /mnt/opi_private_key.pem \
Expand Down
2 changes: 1 addition & 1 deletion sztp-agent/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Daemon() *cobra.Command {
return fmt.Errorf("'--bootstrap-url' and '--dhcp-lease-file' are mutualy exclusive")
}
if bootstrapURL == "" && dhcpLeaseFile == "" {
return fmt.Errorf("'--bootstrap-url' or '--dhcp-lease-file' is required")
fmt.Println("both '--bootstrap-url' and '--dhcp-lease-file' were not provided, will discover the bootstrap URL(s) via NetworkManager")
}
if dhcpLeaseFile != "" {
arrayChecker = append(arrayChecker, dhcpLeaseFile)
Expand Down
2 changes: 1 addition & 1 deletion sztp-agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Run() *cobra.Command {
return fmt.Errorf("'--bootstrap-url' and '--dhcp-lease-file' are mutualy exclusive")
}
if bootstrapURL == "" && dhcpLeaseFile == "" {
return fmt.Errorf("'--bootstrap-url' or '--dhcp-lease-file' is required")
glimchb marked this conversation as resolved.
Show resolved Hide resolved
fmt.Println("both '--bootstrap-url' and '--dhcp-lease-file' were not provided, will discover the bootstrap URL(s) via NetworkManager")
}
if dhcpLeaseFile != "" {
arrayChecker = append(arrayChecker, dhcpLeaseFile)
Expand Down
1 change: 1 addition & 0 deletions sztp-agent/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/godbus/dbus v4.1.0+incompatible
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jaypipes/pcidb v1.0.0 // indirect
github.com/kr/text v0.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions sztp-agent/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3I
github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/godbus/dbus v4.1.0+incompatible h1:WqqLRTsQic3apZUK9qC5sGNfXthmPXzUZ7nQPrNITa4=
github.com/godbus/dbus v4.1.0+incompatible/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jaypipes/ghw v0.12.0 h1:xU2/MDJfWmBhJnujHY9qwXQLs3DBsf0/Xa9vECY0Tho=
Expand Down
63 changes: 63 additions & 0 deletions sztp-agent/pkg/dhcp/dbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,66 @@ Copyright (C) 2022 Red Hat.

// Package dhcp implements the DHCP client
package dhcp

import (
"fmt"
"log"

"github.com/godbus/dbus"
)

// GetBootstrapURLsViaNetworkManager returns the sztp_redirect_urls from the active connections managed by NetworkManager
func GetBootstrapURLsViaNetworkManager() ([]string, error) {
conn, err := dbus.SystemBus()
if err != nil {
return nil, fmt.Errorf("failed to connect to system bus: %v", err)
}

// Get NetworkManager object
nm := conn.Object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")

var activeConnPaths []dbus.ObjectPath
err = nm.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.NetworkManager", "ActiveConnections").Store(&activeConnPaths)
if err != nil {
return nil, fmt.Errorf("failed to get ActiveConnections property: %v", err)
}

log.Println("[INFO] active connection paths: ", activeConnPaths)

var sztpRedirectURLs []string

// Iterate over each active connection
for _, connPath := range activeConnPaths {
// Get Active Connection object
connActive := conn.Object("org.freedesktop.NetworkManager", connPath)

// Get Dhcp4Config property from Active Connection object
var dhcpPath dbus.ObjectPath
err = connActive.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.NetworkManager.Connection.Active", "Dhcp4Config").Store(&dhcpPath)
if err != nil {
log.Println("[INFO] Dhcp4Config is not available for connection:", connPath, err)
continue
}

// Get Options property from DHCP4Config object
dhcp := conn.Object("org.freedesktop.NetworkManager", dhcpPath)
var options map[string]dbus.Variant
err = dhcp.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.NetworkManager.DHCP4Config", "Options").Store(&options)
if err != nil {
log.Println("[INFO] failed to get Options property for connection:", connPath, err)
continue
}

// Logging options
log.Println("[INFO] Options for connection:", connPath, ": ", options)

// Check if sztp_redirect_urls option exists and append
if variant, ok := options[SZTP_REDIRECT_URLs]; ok {
url := variant.Value().(string)
log.Println("sztp_redirect_url found for connection:", connPath, ": ", url)
sztpRedirectURLs = append(sztpRedirectURLs, url)
}
}

return sztpRedirectURLs, nil
}
5 changes: 5 additions & 0 deletions sztp-agent/pkg/dhcp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ Copyright (C) 2022 Red Hat.

// Package dhcp implements the DHCP client
package dhcp

const (
// SZTP_REDIRECT_URLs (custom dhcp-option 143)
SZTP_REDIRECT_URLs = "sztp-redirect-urls" // nolint:all
)
9 changes: 4 additions & 5 deletions sztp-agent/pkg/secureagent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package secureagent
const (
CONTENT_TYPE_YANG = "application/yang-data+json"
OS_RELEASE_FILE = "/etc/os-release"
SZTP_REDIRECT_URL = "sztp-redirect-urls"
ARTIFACTS_PATH = "/tmp/"
)

Expand Down Expand Up @@ -71,7 +70,7 @@ type BootstrapServerErrorOutput struct {
// Agent is the basic structure to define an agent instance
type Agent struct {
InputBootstrapURL string // Bootstrap complete URL given by USER
BootstrapURL string // Bootstrap complete URL
BootstrapURL []string // Bootstrap complete URL
SerialNumber string // Device's Serial Number
DevicePassword string // Device's Password
DevicePrivateKey string // Device's private key
Expand All @@ -89,7 +88,7 @@ type Agent struct {
func NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert string) *Agent {
return &Agent{
InputBootstrapURL: bootstrapURL,
BootstrapURL: "",
BootstrapURL: []string{""},
SerialNumber: GetSerialNumber(serialNumber),
DevicePassword: devicePassword,
DevicePrivateKey: devicePrivateKey,
Expand All @@ -104,7 +103,7 @@ func NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, deviceP
}
}

func (a *Agent) GetBootstrapURL() string {
func (a *Agent) GetBootstrapURL() []string {
return a.BootstrapURL
}

Expand Down Expand Up @@ -140,7 +139,7 @@ func (a *Agent) GetProgressJSON() ProgressJSON {
return a.ProgressJSON
}

func (a *Agent) SetBootstrapURL(url string) {
func (a *Agent) SetBootstrapURL(url []string) {
a.BootstrapURL = url
}

Expand Down
Loading
Loading