Skip to content

Commit

Permalink
fix: agent func arg type, minor fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Bhoopesh <[email protected]>
  • Loading branch information
bhoopesh369 committed Jul 20, 2024
1 parent d5b16fa commit 69d5484
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 78 deletions.
1 change: 0 additions & 1 deletion scripts/run_agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ docker run --rm -it --network=host \
--mount type=bind,source=/etc/ssh,target=/etc/ssh,readonly \
--mount type=bind,source=/etc/os-release,target=/etc/os-release,readonly \
--mount type=bind,source=/var/lib/NetworkManager,target=/var/lib/NetworkManager,readonly \
--mount type=bind,source=/var/run/NetworkManager,target=/var/run/NetworkManager,readonly \
--mount type=bind,source=/var/run/dbus,target=/var/run/dbus,readonly \
--privileged \
${DOCKER_SZTP_IMAGE} \
Expand Down
2 changes: 1 addition & 1 deletion sztp-agent/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ RUN apk add --no-cache --no-check-certificate curl && rm -rf /var/cache/apk/*

COPY --from=builder /opi-sztp-agent /

CMD ["/opi-sztp-agent"]
CMD [ "/opi-sztp-agent" ]
9 changes: 9 additions & 0 deletions sztp-agent/NetworkManager.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[main]
plugins=keyfile
dhcp=dhclient

[ifupdown]
managed=false

[device]
wifi.scan-rand-mac-address=no
4 changes: 2 additions & 2 deletions sztp-agent/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Daemon() *cobra.Command {
return fmt.Errorf("'--bootstrap-url' and '--dhcp-lease-file' are mutualy exclusive")
}
if bootstrapURL == "" && dhcpLeaseFile == "" {
fmt.Println("both '--bootstrap-url' and '--dhcp-lease-file' were not provided, trying to get the bootstrap URL via NetworkManager")
fmt.Println("both '--bootstrap-url' and '--dhcp-lease-file' were not provided, will try to get the bootstrap URL via NetworkManager")

Check warning on line 46 in sztp-agent/cmd/daemon.go

View check run for this annotation

Codecov / codecov/patch

sztp-agent/cmd/daemon.go#L46

Added line #L46 was not covered by tests
}
if dhcpLeaseFile != "" {
arrayChecker = append(arrayChecker, dhcpLeaseFile)
Expand All @@ -59,7 +59,7 @@ func Daemon() *cobra.Command {
return fmt.Errorf("must not be folder: %q", filePath)
}
}
a := secureagent.NewAgent([]string{bootstrapURL}, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
return a.RunCommandDaemon()
},
}
Expand Down
2 changes: 1 addition & 1 deletion sztp-agent/cmd/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Disable() *cobra.Command {
Use: "disable",
Short: "Run the disable command",
RunE: func(_ *cobra.Command, _ []string) error {
a := secureagent.NewAgent([]string{bootstrapURL}, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
return a.RunCommandDisable()
},
}
Expand Down
2 changes: 1 addition & 1 deletion sztp-agent/cmd/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Enable() *cobra.Command {
Use: "enable",
Short: "Run the enable command",
RunE: func(_ *cobra.Command, _ []string) error {
a := secureagent.NewAgent([]string{bootstrapURL}, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
return a.RunCommandEnable()
},
}
Expand Down
4 changes: 2 additions & 2 deletions sztp-agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Run() *cobra.Command {
return fmt.Errorf("'--bootstrap-url' and '--dhcp-lease-file' are mutualy exclusive")
}
if bootstrapURL == "" && dhcpLeaseFile == "" {
fmt.Println("both '--bootstrap-url' and '--dhcp-lease-file' were not provided, trying to get the bootstrap URL via NetworkManager")
fmt.Println("both '--bootstrap-url' and '--dhcp-lease-file' were not provided, will try to get the bootstrap URL via NetworkManager")

Check warning on line 46 in sztp-agent/cmd/run.go

View check run for this annotation

Codecov / codecov/patch

sztp-agent/cmd/run.go#L46

Added line #L46 was not covered by tests
}
if dhcpLeaseFile != "" {
arrayChecker = append(arrayChecker, dhcpLeaseFile)
Expand All @@ -59,7 +59,7 @@ func Run() *cobra.Command {
return fmt.Errorf("must not be folder: %q", filePath)
}
}
a := secureagent.NewAgent([]string{bootstrapURL}, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
return a.RunCommand()
},
}
Expand Down
2 changes: 1 addition & 1 deletion sztp-agent/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Status() *cobra.Command {
Use: "status",
Short: "Run the status command",
RunE: func(_ *cobra.Command, _ []string) error {
a := secureagent.NewAgent([]string{bootstrapURL}, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
return a.RunCommandStatus()
},
}
Expand Down
25 changes: 0 additions & 25 deletions sztp-agent/pkg/dhcp/bootstrap_url.go

This file was deleted.

15 changes: 0 additions & 15 deletions sztp-agent/pkg/dhcp/bootstrap_url_test.go

This file was deleted.

8 changes: 4 additions & 4 deletions sztp-agent/pkg/dhcp/dhcp_lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
"os"
)

const sztpRedirectUrls = "sztp-redirect-urls"
const sztpRedirectURL = "sztp-redirect-urls"

// getBootstrapURLViaLeaseFile returns the sztp redirect URL via DHCP lease file
func getBootstrapURLViaLeaseFile(dhcpLeaseFile string) (string, error) {
// GetBootstrapURLViaLeaseFile returns the sztp redirect URL via DHCP lease file
func GetBootstrapURLViaLeaseFile(dhcpLeaseFile string) (string, error) {
var line string
if _, err := os.Stat(dhcpLeaseFile); err == nil {
for {
line = LinesInFileContains(dhcpLeaseFile, sztpRedirectUrls)
line = LinesInFileContains(dhcpLeaseFile, sztpRedirectURL)
if line != "" {
break
}
Expand Down
2 changes: 1 addition & 1 deletion sztp-agent/pkg/dhcp/dhcp_lease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestGetBootstrapURLViaLeaseFile(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getBootstrapURLViaLeaseFile(tt.fields.DhcpLeaseFile)
got, err := GetBootstrapURLViaLeaseFile(tt.fields.DhcpLeaseFile)
if (err != nil) != tt.wantErr {
t.Errorf("GetBootstrapURLViaLeaseFile() error = %v, wantErr %v", err, tt.wantErr)
} else if got != tt.want {
Expand Down
24 changes: 14 additions & 10 deletions sztp-agent/pkg/dhcp/network_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/godbus/dbus/v5"
)

// getBootstrapURLViaNetworkManager returns the sztp redirect URL via NetworkManager
func getBootstrapURLViaNetworkManager() ([]string, error) {
// GetBootstrapURLViaNetworkManager returns the sztp redirect URL via NetworkManager
func GetBootstrapURLViaNetworkManager() ([]string, error) {
conn, err := dbus.SystemBus()
if err != nil {
return nil, fmt.Errorf("failed to connect to system bus: %v", err)
Expand Down Expand Up @@ -45,25 +45,29 @@ func getBootstrapURLViaNetworkManager() ([]string, error) {
continue

Check warning on line 45 in sztp-agent/pkg/dhcp/network_manager.go

View check run for this annotation

Codecov / codecov/patch

sztp-agent/pkg/dhcp/network_manager.go#L37-L45

Added lines #L37 - L45 were not covered by tests
}

dhcp := conn.Object("org.freedesktop.NetworkManager", dhcpPath)
connDhcp := conn.Object("org.freedesktop.NetworkManager", dhcpPath)

var options map[string]dbus.Variant
err = dhcp.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.NetworkManager.DHCP4Config", "Options").Store(&options)
err = connDhcp.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.NetworkManager.DHCP4Config", "Options").Store(&options)
if err != nil {
log.Println("[INFO] failed to get Options property in DHCP4Config ", dhcpPath, ": ", err)
log.Println("[INFO] failed to get Options property: ", err)
continue

Check warning on line 54 in sztp-agent/pkg/dhcp/network_manager.go

View check run for this annotation

Codecov / codecov/patch

sztp-agent/pkg/dhcp/network_manager.go#L48-L54

Added lines #L48 - L54 were not covered by tests
}

if variant, ok := options["sztp_redirect_urls"]; ok {
if variant, ok := options[sztpRedirectURL]; ok {
if variant.Signature().String() == "s" {
sztpRedirectURL := variant.Value().(string)
log.Println("[INFO] sztp_redirect_url found: ", sztpRedirectURLs)
sztpRedirectURLs = append(sztpRedirectURLs, sztpRedirectURL)
url := variant.Value().(string)
log.Println("[INFO] sztp_redirect_url found: ", url)
sztpRedirectURLs = append(sztpRedirectURLs, url)
continue

Check warning on line 62 in sztp-agent/pkg/dhcp/network_manager.go

View check run for this annotation

Codecov / codecov/patch

sztp-agent/pkg/dhcp/network_manager.go#L57-L62

Added lines #L57 - L62 were not covered by tests
}
log.Println("[INFO] sztp_redirect_urls is not a string in DHCP4Config ", dhcpPath)
} else {
log.Println("[INFO] sztp_redirect_urls not found in DHCP4Config ", dhcpPath)
}

Check warning on line 67 in sztp-agent/pkg/dhcp/network_manager.go

View check run for this annotation

Codecov / codecov/patch

sztp-agent/pkg/dhcp/network_manager.go#L64-L67

Added lines #L64 - L67 were not covered by tests
}
return sztpRedirectURLs, fmt.Errorf("sztp_redirect_urls not found in any active connection")
if len(sztpRedirectURLs) == 0 {
return nil, fmt.Errorf("sztp_redirect_urls not found in any active connection")
}
return sztpRedirectURLs, nil

Check warning on line 72 in sztp-agent/pkg/dhcp/network_manager.go

View check run for this annotation

Codecov / codecov/patch

sztp-agent/pkg/dhcp/network_manager.go#L69-L72

Added lines #L69 - L72 were not covered by tests
}
4 changes: 2 additions & 2 deletions sztp-agent/pkg/secureagent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ type Agent struct {

}

func NewAgent(bootstrapURL []string, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert string) *Agent {
func NewAgent(bootstrapURL string, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert string) *Agent {
return &Agent{
BootstrapURL: bootstrapURL,
BootstrapURL: []string{bootstrapURL},
SerialNumber: GetSerialNumber(serialNumber),
DevicePassword: devicePassword,
DevicePrivateKey: devicePrivateKey,
Expand Down
4 changes: 2 additions & 2 deletions sztp-agent/pkg/secureagent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ func TestAgent_SetSerialNumber(t *testing.T) {

func TestNewAgent(t *testing.T) {
type args struct {
bootstrapURL []string
bootstrapURL string
serialNumber string
dhcpLeaseFile string
devicePassword string
Expand All @@ -837,7 +837,7 @@ func TestNewAgent(t *testing.T) {
{
name: "Test Constructor",
args: args{
bootstrapURL: []string{"TestBootstrap"},
bootstrapURL: "TestBootstrap",
serialNumber: "TestSerialNumber",
dhcpLeaseFile: "TestDhcpLeaseFile",
devicePassword: "TestDevicePassword",
Expand Down
24 changes: 14 additions & 10 deletions sztp-agent/pkg/secureagent/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,15 @@ func (a *Agent) RunCommandDaemon() error {

func (a *Agent) performBootstrapSequence() error {
var err error
// check if empty
// check if the bootstrap URL is already set
if len(a.GetBootstrapURL()) == 1 && a.GetBootstrapURL()[0] == "" {

Check warning on line 61 in sztp-agent/pkg/secureagent/daemon.go

View check run for this annotation

Codecov / codecov/patch

sztp-agent/pkg/secureagent/daemon.go#L60-L61

Added lines #L60 - L61 were not covered by tests
log.Println("lmao")
err = a.getBootstrapURL()
if err != nil {
return err
}
}
log.Println("Bootstrap URL: ", a.GetBootstrapURL())
bootstrapURLs := a.GetBootstrapURL()
log.Println("Bootstrap URL: ", bootstrapURLs)
for _, bootstrapURL := range bootstrapURLs {
bootstrapURLCopy := bootstrapURL
err = a.doRequestBootstrapServerOnboardingInfo(&bootstrapURLCopy)
Expand Down Expand Up @@ -106,16 +105,21 @@ func (a *Agent) performBootstrapSequence() error {
}

func (a *Agent) getBootstrapURL() error {
log.Println("[INFO] Get the Bootstrap URL from DHCP client")

sztpRedirectUrls, err := dhcp.GetBootstrapURL(a.DhcpLeaseFile)
if a.DhcpLeaseFile != "" {
log.Println("[INFO] Get the Bootstrap URL from DHCP client")
bootstrapURL, err := dhcp.GetBootstrapURLViaLeaseFile(a.DhcpLeaseFile)
if err != nil {
return err
}
a.SetBootstrapURL([]string{bootstrapURL})
return nil
}
log.Println("[INFO] Get the Bootstrap URL Via Network Manager")
bootstrapURLs, err := dhcp.GetBootstrapURLViaNetworkManager()
if err != nil {
log.Println("[ERROR] ", err.Error())
return err

Check warning on line 120 in sztp-agent/pkg/secureagent/daemon.go

View check run for this annotation

Codecov / codecov/patch

sztp-agent/pkg/secureagent/daemon.go#L117-L120

Added lines #L117 - L120 were not covered by tests
}
a.SetBootstrapURL(sztpRedirectUrls)
// log.Println("[INFO] Bootstrap URL retrieved successfully: " + a.GetBootstrapURL())
log.Println("[INFO] Bootstrap URL retrieved successfully")
a.SetBootstrapURL(bootstrapURLs)

Check warning on line 122 in sztp-agent/pkg/secureagent/daemon.go

View check run for this annotation

Codecov / codecov/patch

sztp-agent/pkg/secureagent/daemon.go#L122

Added line #L122 was not covered by tests
return nil
}

Expand Down

0 comments on commit 69d5484

Please sign in to comment.