From d5660898c0a65e4ddfef2c378d549bddb3f629d8 Mon Sep 17 00:00:00 2001 From: Boris Glimcher Date: Fri, 2 Aug 2024 19:40:09 +0300 Subject: [PATCH] refactor: move GetBootstrapURLsViaLeaseFile to dhcp package Fixes #441 Signed-off-by: Boris Glimcher --- sztp-agent/pkg/dhcp/leases.go | 45 +++++++++++++++ sztp-agent/pkg/secureagent/daemon.go | 22 +------- sztp-agent/pkg/secureagent/utils.go | 26 --------- sztp-agent/pkg/secureagent/utils_test.go | 71 ------------------------ 4 files changed, 48 insertions(+), 116 deletions(-) diff --git a/sztp-agent/pkg/dhcp/leases.go b/sztp-agent/pkg/dhcp/leases.go index 32a182b2..5e4a32f9 100644 --- a/sztp-agent/pkg/dhcp/leases.go +++ b/sztp-agent/pkg/dhcp/leases.go @@ -7,3 +7,48 @@ Copyright (C) 2022 Red Hat. // Package dhcp implements the DHCP client package dhcp + +import ( + "bufio" + "log" + "os" + "regexp" + "strings" +) + +// GetBootstrapURLsViaLeaseFile retrieves the Bootstrap URL from a DHCP lease file. +// +// Parameters: +// - leaseFile: the path to the DHCP lease file. +// - key: the key used to retrieve the Bootstrap URL. +// +// Returns: +// - []string: a slice of Bootstrap URLs. +// - error: an error if the file cannot be read or the key is not found. +func GetBootstrapURLsViaLeaseFile(leaseFile, key string) ([]string, error) { + file, err := os.Open(leaseFile) + if err != nil { + return nil, err + } + defer func() { + if err := file.Close(); err != nil { + log.Println("[ERROR] Error when closing:", err) + } + }() + + var sztpRedirectURLs []string + scanner := bufio.NewScanner(file) + re := regexp.MustCompile(`(?m)[^"]*`) + for scanner.Scan() { + line := scanner.Text() + if strings.Contains(line, key) { + url := re.FindAllString(line, -1) + if len(url) == 1 { + continue + } + sztpRedirectURLs = append(sztpRedirectURLs, url[0]) + } + } + + return sztpRedirectURLs, nil +} diff --git a/sztp-agent/pkg/secureagent/daemon.go b/sztp-agent/pkg/secureagent/daemon.go index 4835af90..e6465929 100644 --- a/sztp-agent/pkg/secureagent/daemon.go +++ b/sztp-agent/pkg/secureagent/daemon.go @@ -31,6 +31,7 @@ import ( "time" "github.com/github/smimesign/ietf-cms/protocol" + "github.com/opiproject/sztp/sztp-agent/pkg/dhcp" ) const ( @@ -98,11 +99,11 @@ func (a *Agent) discoverBootstrapURLs() error { } if a.DhcpLeaseFile != "" { log.Println("[INFO] User gave us the DHCP Lease File: " + a.DhcpLeaseFile) - url, err := a.getBootstrapURLsViaLeaseFile() + urls, err := dhcp.GetBootstrapURLsViaLeaseFile(a.DhcpLeaseFile, SZTP_REDIRECT_URL) if err != nil { return err } - a.SetBootstrapURL(url) + a.SetBootstrapURL(urls[0]) log.Println("[INFO] Bootstrap URL retrieved successfully: " + a.GetBootstrapURL()) return nil } @@ -112,23 +113,6 @@ func (a *Agent) discoverBootstrapURLs() error { 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 { - for { - line = linesInFileContains(a.DhcpLeaseFile, SZTP_REDIRECT_URL) - if line != "" { - break - } - } - return extractfromLine(line, `(?m)[^"]*`, 1), nil - } - log.Println("[Error] File " + a.DhcpLeaseFile + " does not exist") - return "", errors.New("File " + a.DhcpLeaseFile + " does not exist") -} - func (a *Agent) doHandleBootstrapRedirect() error { if reflect.ValueOf(a.BootstrapServerRedirectInfo).IsZero() { return nil diff --git a/sztp-agent/pkg/secureagent/utils.go b/sztp-agent/pkg/secureagent/utils.go index daf097a1..b8cf22c9 100644 --- a/sztp-agent/pkg/secureagent/utils.go +++ b/sztp-agent/pkg/secureagent/utils.go @@ -9,40 +9,14 @@ Copyright (C) 2022 Red Hat. package secureagent import ( - "bufio" "encoding/json" "log" - "os" - "regexp" "strings" "github.com/go-ini/ini" "github.com/jaypipes/ghw" ) -// Auxiliar function to get lines from file matching with the substr -func linesInFileContains(file string, substr string) string { - // nolint:gosec - f, _ := os.Open(file) - scanner := bufio.NewScanner(f) - for scanner.Scan() { - line := scanner.Text() - if strings.Contains(line, substr) { - return line - } - } - return "" -} - -func extractfromLine(line, regex string, index int) string { - re := regexp.MustCompile(regex) - res := re.FindAllString(line, -1) - if len(res) == 1 { - return "" - } - return re.FindAllString(line, -1)[index] -} - // GetSerialNumber returns the serial number of the device func GetSerialNumber(givenSerialNumber string) string { if givenSerialNumber != "" { diff --git a/sztp-agent/pkg/secureagent/utils_test.go b/sztp-agent/pkg/secureagent/utils_test.go index c484b6aa..a3cd990d 100644 --- a/sztp-agent/pkg/secureagent/utils_test.go +++ b/sztp-agent/pkg/secureagent/utils_test.go @@ -5,80 +5,9 @@ package secureagent import ( - "strings" "testing" ) -func Test_extractfromLine(t *testing.T) { - type args struct { - line string - regex string - index int - } - tests := []struct { - name string - args args - want string - }{ - { - name: "Test OK all fields aligned", - args: args{ - line: " option sztp-redirect-urls \"https://bootstrap:9090/restconf/operations/ietf-sztp-bootstrap-server:get-bootstrapping-data\";", - regex: `(?m)[^"]*`, - index: 1, - }, - want: "https://bootstrap:9090/restconf/operations/ietf-sztp-bootstrap-server:get-bootstrapping-data", - }, - { - name: "Test KO no match reg", - args: args{ - line: "ANYTHING", - regex: `(?m)[^"]*`, - index: 1, - }, - want: "", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := extractfromLine(tt.args.line, tt.args.regex, tt.args.index); got != tt.want { - t.Errorf("extractfromLine() = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_linesInFileContains(t *testing.T) { - dhcpTestFileOK := "/tmp/test.dhcp" - createTempTestFile(dhcpTestFileOK, DHCPTestContent, true) - type args struct { - file string - substr string - } - tests := []struct { - name string - args args - want string - }{ - { - name: "Test OK line in files", - args: args{ - file: dhcpTestFileOK, - substr: "sztp-redirect-urls", - }, - want: "option sztp-redirect-urls \"http://mymock/test\";", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := strings.TrimSpace(linesInFileContains(tt.args.file, tt.args.substr)); got != tt.want { - t.Errorf("linesInFileContains() = %v, want %v", got, tt.want) - } - }) - } - deleteTempTestFile(dhcpTestFileOK) -} - func Test_replaceQuotes(t *testing.T) { type args struct { input string