From 2b323ff1a170efda76b50ce6e7bed94e45945267 Mon Sep 17 00:00:00 2001 From: Boris Glimcher Date: Mon, 22 Jul 2024 23:57:24 +0300 Subject: [PATCH] refactor: put logic in discoverBootstrapURLs - if user gave us URL, use it - else if user gave us lease file, use it - else try to fecth from network manager via dbus Signed-off-by: Boris Glimcher --- sztp-agent/pkg/secureagent/agent.go | 4 ++- sztp-agent/pkg/secureagent/agent_test.go | 3 +- sztp-agent/pkg/secureagent/daemon.go | 42 +++++++++++++++++------ sztp-agent/pkg/secureagent/daemon_test.go | 40 ++++++++++++++++++--- 4 files changed, 71 insertions(+), 18 deletions(-) diff --git a/sztp-agent/pkg/secureagent/agent.go b/sztp-agent/pkg/secureagent/agent.go index 774a96a0..e248dc42 100644 --- a/sztp-agent/pkg/secureagent/agent.go +++ b/sztp-agent/pkg/secureagent/agent.go @@ -70,6 +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 SerialNumber string // Device's Serial Number DevicePassword string // Device's Password @@ -87,7 +88,8 @@ type Agent struct { func NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert string) *Agent { return &Agent{ - BootstrapURL: bootstrapURL, + InputBootstrapURL: bootstrapURL, + 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 c3de2aa9..ad2ebb09 100644 --- a/sztp-agent/pkg/secureagent/agent_test.go +++ b/sztp-agent/pkg/secureagent/agent_test.go @@ -846,7 +846,8 @@ func TestNewAgent(t *testing.T) { bootstrapTrustAnchorCert: "TestBootstrapTrustCert", }, want: &Agent{ - BootstrapURL: "TestBootstrap", + InputBootstrapURL: "TestBootstrap", + BootstrapURL: "", SerialNumber: "TestSerialNumber", DevicePassword: "TestDevicePassword", DevicePrivateKey: "TestDevicePrivateKey", diff --git a/sztp-agent/pkg/secureagent/daemon.go b/sztp-agent/pkg/secureagent/daemon.go index 2ca807e0..4835af90 100644 --- a/sztp-agent/pkg/secureagent/daemon.go +++ b/sztp-agent/pkg/secureagent/daemon.go @@ -56,11 +56,9 @@ func (a *Agent) RunCommandDaemon() error { func (a *Agent) performBootstrapSequence() error { var err error - if a.GetBootstrapURL() == "" { - err = a.discoverBootstrapURLs() - if err != nil { - return err - } + err = a.discoverBootstrapURLs() + if err != nil { + return err } err = a.doRequestBootstrapServerOnboardingInfo() if err != nil { @@ -91,6 +89,31 @@ func (a *Agent) performBootstrapSequence() error { } func (a *Agent) discoverBootstrapURLs() error { + log.Println("[INFO] Discovering the Bootstrap URL") + if a.InputBootstrapURL != "" { + log.Println("[INFO] User gave us the Bootstrap URL: " + a.InputBootstrapURL) + a.SetBootstrapURL(a.InputBootstrapURL) + log.Println("[INFO] Bootstrap URL retrieved successfully: " + a.GetBootstrapURL()) + return nil + } + if a.DhcpLeaseFile != "" { + log.Println("[INFO] User gave us the DHCP Lease File: " + a.DhcpLeaseFile) + url, err := a.getBootstrapURLsViaLeaseFile() + if err != nil { + return err + } + a.SetBootstrapURL(url) + log.Println("[INFO] Bootstrap URL retrieved successfully: " + a.GetBootstrapURL()) + return nil + } + log.Println("[INFO] User gave us nothing, discover the Bootstrap URL from Network Manager via dbus") + // TODO: fetch the Bootstrap URL from Network Manager via dbus in the future + log.Println("[INFO] Bootstrap URL retrieved successfully: " + a.GetBootstrapURL()) + return nil +} + +// TODO: move this function into DHCP package folder +func (a *Agent) getBootstrapURLsViaLeaseFile() (string, error) { log.Println("[INFO] Get the Bootstrap URL from DHCP client") var line string if _, err := os.Stat(a.DhcpLeaseFile); err == nil { @@ -100,13 +123,10 @@ func (a *Agent) discoverBootstrapURLs() error { break } } - a.SetBootstrapURL(extractfromLine(line, `(?m)[^"]*`, 1)) - } else { - log.Printf("[ERROR] File " + a.DhcpLeaseFile + " does not exist\n") - return errors.New(" File " + a.DhcpLeaseFile + " does not exist\n") + return extractfromLine(line, `(?m)[^"]*`, 1), nil } - log.Println("[INFO] Bootstrap URL retrieved successfully: " + a.GetBootstrapURL()) - return nil + log.Println("[Error] File " + a.DhcpLeaseFile + " does not exist") + return "", errors.New("File " + a.DhcpLeaseFile + " does not exist") } func (a *Agent) doHandleBootstrapRedirect() error { diff --git a/sztp-agent/pkg/secureagent/daemon_test.go b/sztp-agent/pkg/secureagent/daemon_test.go index 48902397..eb7d968b 100644 --- a/sztp-agent/pkg/secureagent/daemon_test.go +++ b/sztp-agent/pkg/secureagent/daemon_test.go @@ -36,7 +36,7 @@ func TestAgent_discoverBootstrapURLs(t *testing.T) { createTempTestFile(dhcpTestFileOK, DHCPTestContent, true) type fields struct { - BootstrapURL string + InputBootstrapURL string SerialNumber string DevicePassword string DevicePrivateKey string @@ -52,9 +52,39 @@ func TestAgent_discoverBootstrapURLs(t *testing.T) { wantErr bool }{ { - name: "Test OK Case file exists and get url successfully", + name: "Test OK Case dhcp leases file exists and get url successfully", fields: fields{ - BootstrapURL: "http://localhost", + InputBootstrapURL: "", + SerialNumber: "my-serial-number", + DevicePassword: "my-password", + DevicePrivateKey: "", + DeviceEndEntityCert: "", + BootstrapTrustAnchorCert: "", + ContentTypeReq: CONTENT_TYPE_YANG, + InputJSONContent: "", + DhcpLeasesFile: dhcpTestFileOK, + }, + wantErr: false, + }, + { + name: "Test OK Case url given by user while leases file is not", + fields: fields{ + InputBootstrapURL: "http://user/given", + SerialNumber: "my-serial-number", + DevicePassword: "my-password", + DevicePrivateKey: "", + DeviceEndEntityCert: "", + BootstrapTrustAnchorCert: "", + ContentTypeReq: CONTENT_TYPE_YANG, + InputJSONContent: "", + DhcpLeasesFile: "", + }, + wantErr: false, + }, + { + name: "Test OK Case url given by user and leases file given by user as well", + fields: fields{ + InputBootstrapURL: "http://user/given", SerialNumber: "my-serial-number", DevicePassword: "my-password", DevicePrivateKey: "", @@ -69,7 +99,7 @@ func TestAgent_discoverBootstrapURLs(t *testing.T) { { name: "Test KO when not file found", fields: fields{ - BootstrapURL: "http://localhost", + InputBootstrapURL: "", SerialNumber: "my-serial-number", DevicePassword: "my-password", DevicePrivateKey: "", @@ -85,7 +115,7 @@ func TestAgent_discoverBootstrapURLs(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { a := &Agent{ - BootstrapURL: tt.fields.BootstrapURL, + InputBootstrapURL: tt.fields.InputBootstrapURL, SerialNumber: tt.fields.SerialNumber, DevicePassword: tt.fields.DevicePassword, DevicePrivateKey: tt.fields.DevicePrivateKey,