Skip to content

Commit

Permalink
refactor: move GetBootstrapURLsViaLeaseFile to dhcp package
Browse files Browse the repository at this point in the history
Fixes #441

Signed-off-by: Boris Glimcher <[email protected]>
  • Loading branch information
glimchb committed Aug 2, 2024
1 parent b8503d0 commit d566089
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 116 deletions.
45 changes: 45 additions & 0 deletions sztp-agent/pkg/dhcp/leases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
22 changes: 3 additions & 19 deletions sztp-agent/pkg/secureagent/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"time"

"github.com/github/smimesign/ietf-cms/protocol"
"github.com/opiproject/sztp/sztp-agent/pkg/dhcp"
)

const (
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
26 changes: 0 additions & 26 deletions sztp-agent/pkg/secureagent/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
71 changes: 0 additions & 71 deletions sztp-agent/pkg/secureagent/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d566089

Please sign in to comment.