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 <[email protected]>
  • Loading branch information
glimchb committed Jul 22, 2024
1 parent 28c2e14 commit 2b323ff
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 18 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
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

0 comments on commit 2b323ff

Please sign in to comment.