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

refactor: move GetBootstrapURLsViaLeaseFile to dhcp package #442

Merged
merged 2 commits into from
Aug 2, 2024
Merged
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
6 changes: 3 additions & 3 deletions .github/workflows/sztp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ jobs:
uses: docker/setup-buildx-action@abe89fb761023d1d963c81f6b5e0df54236dc097

- name: Start containers
run: docker-compose up -d
run: docker compose up -d

- name: Run Tests
run: ./scripts/tests.sh

- name: Logs
if: failure()
run: docker-compose logs
run: docker compose logs

- name: Stop containers
if: always()
run: docker-compose down
run: docker compose down
46 changes: 46 additions & 0 deletions sztp-agent/pkg/dhcp/leases.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,49 @@ 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) {
// nolint:gosec
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])
glimchb marked this conversation as resolved.
Show resolved Hide resolved
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
Loading