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: put logic in discoverBootstrapURLs #438

Merged
merged 1 commit into from
Jul 22, 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
4 changes: 3 additions & 1 deletion sztp-agent/pkg/secureagent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion sztp-agent/pkg/secureagent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,8 @@ func TestNewAgent(t *testing.T) {
bootstrapTrustAnchorCert: "TestBootstrapTrustCert",
},
want: &Agent{
BootstrapURL: "TestBootstrap",
InputBootstrapURL: "TestBootstrap",
BootstrapURL: "",
SerialNumber: "TestSerialNumber",
DevicePassword: "TestDevicePassword",
DevicePrivateKey: "TestDevicePrivateKey",
Expand Down
42 changes: 31 additions & 11 deletions sztp-agent/pkg/secureagent/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
40 changes: 35 additions & 5 deletions sztp-agent/pkg/secureagent/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: "",
Expand All @@ -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: "",
Expand All @@ -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,
Expand Down
Loading