From 69d5484a5be81f309a9e8718ffdb458fa2fd24c7 Mon Sep 17 00:00:00 2001 From: Bhoopesh Date: Sun, 21 Jul 2024 00:20:22 +0530 Subject: [PATCH] fix: agent func arg type, minor fixes Signed-off-by: Bhoopesh --- scripts/run_agent.sh | 1 - sztp-agent/Dockerfile | 2 +- sztp-agent/NetworkManager.conf | 9 ++++++++ sztp-agent/cmd/daemon.go | 4 ++-- sztp-agent/cmd/disable.go | 2 +- sztp-agent/cmd/enable.go | 2 +- sztp-agent/cmd/run.go | 4 ++-- sztp-agent/cmd/status.go | 2 +- sztp-agent/pkg/dhcp/bootstrap_url.go | 25 ----------------------- sztp-agent/pkg/dhcp/bootstrap_url_test.go | 15 -------------- sztp-agent/pkg/dhcp/dhcp_lease.go | 8 ++++---- sztp-agent/pkg/dhcp/dhcp_lease_test.go | 2 +- sztp-agent/pkg/dhcp/network_manager.go | 24 +++++++++++++--------- sztp-agent/pkg/secureagent/agent.go | 4 ++-- sztp-agent/pkg/secureagent/agent_test.go | 4 ++-- sztp-agent/pkg/secureagent/daemon.go | 24 +++++++++++++--------- 16 files changed, 54 insertions(+), 78 deletions(-) create mode 100644 sztp-agent/NetworkManager.conf delete mode 100644 sztp-agent/pkg/dhcp/bootstrap_url.go delete mode 100644 sztp-agent/pkg/dhcp/bootstrap_url_test.go diff --git a/scripts/run_agent.sh b/scripts/run_agent.sh index 4d724d24..45884f55 100755 --- a/scripts/run_agent.sh +++ b/scripts/run_agent.sh @@ -21,7 +21,6 @@ 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/NetworkManager,target=/var/run/NetworkManager,readonly \ --mount type=bind,source=/var/run/dbus,target=/var/run/dbus,readonly \ --privileged \ ${DOCKER_SZTP_IMAGE} \ diff --git a/sztp-agent/Dockerfile b/sztp-agent/Dockerfile index fde633ae..c728366b 100644 --- a/sztp-agent/Dockerfile +++ b/sztp-agent/Dockerfile @@ -21,4 +21,4 @@ RUN apk add --no-cache --no-check-certificate curl && rm -rf /var/cache/apk/* COPY --from=builder /opi-sztp-agent / -CMD ["/opi-sztp-agent"] +CMD [ "/opi-sztp-agent" ] diff --git a/sztp-agent/NetworkManager.conf b/sztp-agent/NetworkManager.conf new file mode 100644 index 00000000..8bfd9ece --- /dev/null +++ b/sztp-agent/NetworkManager.conf @@ -0,0 +1,9 @@ +[main] +plugins=keyfile +dhcp=dhclient + +[ifupdown] +managed=false + +[device] +wifi.scan-rand-mac-address=no diff --git a/sztp-agent/cmd/daemon.go b/sztp-agent/cmd/daemon.go index 8b504a65..88baa297 100644 --- a/sztp-agent/cmd/daemon.go +++ b/sztp-agent/cmd/daemon.go @@ -43,7 +43,7 @@ func Daemon() *cobra.Command { return fmt.Errorf("'--bootstrap-url' and '--dhcp-lease-file' are mutualy exclusive") } if bootstrapURL == "" && dhcpLeaseFile == "" { - fmt.Println("both '--bootstrap-url' and '--dhcp-lease-file' were not provided, trying to get the bootstrap URL via NetworkManager") + fmt.Println("both '--bootstrap-url' and '--dhcp-lease-file' were not provided, will try to get the bootstrap URL via NetworkManager") } if dhcpLeaseFile != "" { arrayChecker = append(arrayChecker, dhcpLeaseFile) @@ -59,7 +59,7 @@ func Daemon() *cobra.Command { return fmt.Errorf("must not be folder: %q", filePath) } } - a := secureagent.NewAgent([]string{bootstrapURL}, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) + a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) return a.RunCommandDaemon() }, } diff --git a/sztp-agent/cmd/disable.go b/sztp-agent/cmd/disable.go index 886b54e2..3ce70a4d 100644 --- a/sztp-agent/cmd/disable.go +++ b/sztp-agent/cmd/disable.go @@ -34,7 +34,7 @@ func Disable() *cobra.Command { Use: "disable", Short: "Run the disable command", RunE: func(_ *cobra.Command, _ []string) error { - a := secureagent.NewAgent([]string{bootstrapURL}, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) + a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) return a.RunCommandDisable() }, } diff --git a/sztp-agent/cmd/enable.go b/sztp-agent/cmd/enable.go index 9159f7b2..745bd795 100644 --- a/sztp-agent/cmd/enable.go +++ b/sztp-agent/cmd/enable.go @@ -34,7 +34,7 @@ func Enable() *cobra.Command { Use: "enable", Short: "Run the enable command", RunE: func(_ *cobra.Command, _ []string) error { - a := secureagent.NewAgent([]string{bootstrapURL}, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) + a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) return a.RunCommandEnable() }, } diff --git a/sztp-agent/cmd/run.go b/sztp-agent/cmd/run.go index e8d56e45..4d7f5c84 100644 --- a/sztp-agent/cmd/run.go +++ b/sztp-agent/cmd/run.go @@ -43,7 +43,7 @@ func Run() *cobra.Command { return fmt.Errorf("'--bootstrap-url' and '--dhcp-lease-file' are mutualy exclusive") } if bootstrapURL == "" && dhcpLeaseFile == "" { - fmt.Println("both '--bootstrap-url' and '--dhcp-lease-file' were not provided, trying to get the bootstrap URL via NetworkManager") + fmt.Println("both '--bootstrap-url' and '--dhcp-lease-file' were not provided, will try to get the bootstrap URL via NetworkManager") } if dhcpLeaseFile != "" { arrayChecker = append(arrayChecker, dhcpLeaseFile) @@ -59,7 +59,7 @@ func Run() *cobra.Command { return fmt.Errorf("must not be folder: %q", filePath) } } - a := secureagent.NewAgent([]string{bootstrapURL}, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) + a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) return a.RunCommand() }, } diff --git a/sztp-agent/cmd/status.go b/sztp-agent/cmd/status.go index ce6eef5d..cf5043a7 100644 --- a/sztp-agent/cmd/status.go +++ b/sztp-agent/cmd/status.go @@ -34,7 +34,7 @@ func Status() *cobra.Command { Use: "status", Short: "Run the status command", RunE: func(_ *cobra.Command, _ []string) error { - a := secureagent.NewAgent([]string{bootstrapURL}, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) + a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) return a.RunCommandStatus() }, } diff --git a/sztp-agent/pkg/dhcp/bootstrap_url.go b/sztp-agent/pkg/dhcp/bootstrap_url.go deleted file mode 100644 index ef6c4425..00000000 --- a/sztp-agent/pkg/dhcp/bootstrap_url.go +++ /dev/null @@ -1,25 +0,0 @@ -/* -SPDX-License-Identifier: Apache-2.0 -Copyright (C) 2022-2023 Intel Corporation -Copyright (c) 2022 Dell Inc, or its subsidiaries. -Copyright (C) 2022 Red Hat. -*/ - -// Package dhcp implements the DHCP client -package dhcp - -import "log" - -// GetBootstrapURL returns the bootstrap URL -func GetBootstrapURL(dhcpLeaseFile string) ([]string, error) { - url, err := getBootstrapURLViaLeaseFile(dhcpLeaseFile) - if err == nil { - return []string{url}, nil - } - log.Println("[INFO] Trying to get the URL from NetworkManager") - urls, err := getBootstrapURLViaNetworkManager() - if err == nil { - return urls, nil - } - return nil, err -} diff --git a/sztp-agent/pkg/dhcp/bootstrap_url_test.go b/sztp-agent/pkg/dhcp/bootstrap_url_test.go deleted file mode 100644 index fa5eabbc..00000000 --- a/sztp-agent/pkg/dhcp/bootstrap_url_test.go +++ /dev/null @@ -1,15 +0,0 @@ -/* -SPDX-License-Identifier: Apache-2.0 -Copyright (C) 2022-2023 Intel Corporation -Copyright (c) 2022 Dell Inc, or its subsidiaries. -Copyright (C) 2022 Red Hat. -*/ - -// Package dhcp implements the DHCP client -package dhcp - -import "testing" - -func TestGetBootstrapURL(_ *testing.T) { - // TODO: Implement the test -} diff --git a/sztp-agent/pkg/dhcp/dhcp_lease.go b/sztp-agent/pkg/dhcp/dhcp_lease.go index 34105c21..9eb56399 100644 --- a/sztp-agent/pkg/dhcp/dhcp_lease.go +++ b/sztp-agent/pkg/dhcp/dhcp_lease.go @@ -14,14 +14,14 @@ import ( "os" ) -const sztpRedirectUrls = "sztp-redirect-urls" +const sztpRedirectURL = "sztp-redirect-urls" -// getBootstrapURLViaLeaseFile returns the sztp redirect URL via DHCP lease file -func getBootstrapURLViaLeaseFile(dhcpLeaseFile string) (string, error) { +// GetBootstrapURLViaLeaseFile returns the sztp redirect URL via DHCP lease file +func GetBootstrapURLViaLeaseFile(dhcpLeaseFile string) (string, error) { var line string if _, err := os.Stat(dhcpLeaseFile); err == nil { for { - line = LinesInFileContains(dhcpLeaseFile, sztpRedirectUrls) + line = LinesInFileContains(dhcpLeaseFile, sztpRedirectURL) if line != "" { break } diff --git a/sztp-agent/pkg/dhcp/dhcp_lease_test.go b/sztp-agent/pkg/dhcp/dhcp_lease_test.go index a907ebaf..a75ca985 100644 --- a/sztp-agent/pkg/dhcp/dhcp_lease_test.go +++ b/sztp-agent/pkg/dhcp/dhcp_lease_test.go @@ -42,7 +42,7 @@ func TestGetBootstrapURLViaLeaseFile(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := getBootstrapURLViaLeaseFile(tt.fields.DhcpLeaseFile) + got, err := GetBootstrapURLViaLeaseFile(tt.fields.DhcpLeaseFile) if (err != nil) != tt.wantErr { t.Errorf("GetBootstrapURLViaLeaseFile() error = %v, wantErr %v", err, tt.wantErr) } else if got != tt.want { diff --git a/sztp-agent/pkg/dhcp/network_manager.go b/sztp-agent/pkg/dhcp/network_manager.go index c65e6087..68ab113f 100644 --- a/sztp-agent/pkg/dhcp/network_manager.go +++ b/sztp-agent/pkg/dhcp/network_manager.go @@ -15,8 +15,8 @@ import ( "github.com/godbus/dbus/v5" ) -// getBootstrapURLViaNetworkManager returns the sztp redirect URL via NetworkManager -func getBootstrapURLViaNetworkManager() ([]string, error) { +// GetBootstrapURLViaNetworkManager returns the sztp redirect URL via NetworkManager +func GetBootstrapURLViaNetworkManager() ([]string, error) { conn, err := dbus.SystemBus() if err != nil { return nil, fmt.Errorf("failed to connect to system bus: %v", err) @@ -45,19 +45,20 @@ func getBootstrapURLViaNetworkManager() ([]string, error) { continue } - dhcp := conn.Object("org.freedesktop.NetworkManager", dhcpPath) + connDhcp := 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) + err = connDhcp.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 in DHCP4Config ", dhcpPath, ": ", err) + log.Println("[INFO] failed to get Options property: ", err) continue } - if variant, ok := options["sztp_redirect_urls"]; ok { + if variant, ok := options[sztpRedirectURL]; ok { if variant.Signature().String() == "s" { - sztpRedirectURL := variant.Value().(string) - log.Println("[INFO] sztp_redirect_url found: ", sztpRedirectURLs) - sztpRedirectURLs = append(sztpRedirectURLs, sztpRedirectURL) + url := variant.Value().(string) + log.Println("[INFO] sztp_redirect_url found: ", url) + sztpRedirectURLs = append(sztpRedirectURLs, url) continue } log.Println("[INFO] sztp_redirect_urls is not a string in DHCP4Config ", dhcpPath) @@ -65,5 +66,8 @@ func getBootstrapURLViaNetworkManager() ([]string, error) { log.Println("[INFO] sztp_redirect_urls not found in DHCP4Config ", dhcpPath) } } - return sztpRedirectURLs, fmt.Errorf("sztp_redirect_urls not found in any active connection") + if len(sztpRedirectURLs) == 0 { + return nil, fmt.Errorf("sztp_redirect_urls not found in any active connection") + } + return sztpRedirectURLs, nil } diff --git a/sztp-agent/pkg/secureagent/agent.go b/sztp-agent/pkg/secureagent/agent.go index f3a38c8a..42cd6a0f 100644 --- a/sztp-agent/pkg/secureagent/agent.go +++ b/sztp-agent/pkg/secureagent/agent.go @@ -84,9 +84,9 @@ type Agent struct { } -func NewAgent(bootstrapURL []string, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert string) *Agent { +func NewAgent(bootstrapURL string, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert string) *Agent { return &Agent{ - BootstrapURL: bootstrapURL, + BootstrapURL: []string{bootstrapURL}, SerialNumber: GetSerialNumber(serialNumber), DevicePassword: devicePassword, DevicePrivateKey: devicePrivateKey, diff --git a/sztp-agent/pkg/secureagent/agent_test.go b/sztp-agent/pkg/secureagent/agent_test.go index 95ae286b..fd33a4c9 100644 --- a/sztp-agent/pkg/secureagent/agent_test.go +++ b/sztp-agent/pkg/secureagent/agent_test.go @@ -821,7 +821,7 @@ func TestAgent_SetSerialNumber(t *testing.T) { func TestNewAgent(t *testing.T) { type args struct { - bootstrapURL []string + bootstrapURL string serialNumber string dhcpLeaseFile string devicePassword string @@ -837,7 +837,7 @@ func TestNewAgent(t *testing.T) { { name: "Test Constructor", args: args{ - bootstrapURL: []string{"TestBootstrap"}, + bootstrapURL: "TestBootstrap", serialNumber: "TestSerialNumber", dhcpLeaseFile: "TestDhcpLeaseFile", devicePassword: "TestDevicePassword", diff --git a/sztp-agent/pkg/secureagent/daemon.go b/sztp-agent/pkg/secureagent/daemon.go index f39716f3..7ed14170 100644 --- a/sztp-agent/pkg/secureagent/daemon.go +++ b/sztp-agent/pkg/secureagent/daemon.go @@ -57,16 +57,15 @@ func (a *Agent) RunCommandDaemon() error { func (a *Agent) performBootstrapSequence() error { var err error - // check if empty + // check if the bootstrap URL is already set if len(a.GetBootstrapURL()) == 1 && a.GetBootstrapURL()[0] == "" { - log.Println("lmao") err = a.getBootstrapURL() if err != nil { return err } } - log.Println("Bootstrap URL: ", a.GetBootstrapURL()) bootstrapURLs := a.GetBootstrapURL() + log.Println("Bootstrap URL: ", bootstrapURLs) for _, bootstrapURL := range bootstrapURLs { bootstrapURLCopy := bootstrapURL err = a.doRequestBootstrapServerOnboardingInfo(&bootstrapURLCopy) @@ -106,16 +105,21 @@ func (a *Agent) performBootstrapSequence() error { } func (a *Agent) getBootstrapURL() error { - log.Println("[INFO] Get the Bootstrap URL from DHCP client") - - sztpRedirectUrls, err := dhcp.GetBootstrapURL(a.DhcpLeaseFile) + if a.DhcpLeaseFile != "" { + log.Println("[INFO] Get the Bootstrap URL from DHCP client") + bootstrapURL, err := dhcp.GetBootstrapURLViaLeaseFile(a.DhcpLeaseFile) + if err != nil { + return err + } + a.SetBootstrapURL([]string{bootstrapURL}) + return nil + } + log.Println("[INFO] Get the Bootstrap URL Via Network Manager") + bootstrapURLs, err := dhcp.GetBootstrapURLViaNetworkManager() if err != nil { - log.Println("[ERROR] ", err.Error()) return err } - a.SetBootstrapURL(sztpRedirectUrls) - // log.Println("[INFO] Bootstrap URL retrieved successfully: " + a.GetBootstrapURL()) - log.Println("[INFO] Bootstrap URL retrieved successfully") + a.SetBootstrapURL(bootstrapURLs) return nil }