Skip to content

Commit

Permalink
refactor: put logic in discoverBootstrapURLs
Browse files Browse the repository at this point in the history
- 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 <Boris.Glimcher@emc.com>
  • Loading branch information
glimchb committed Jul 22, 2024
1 parent 28c2e14 commit c91735e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
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

0 comments on commit c91735e

Please sign in to comment.