Skip to content

Commit

Permalink
feat: make bootStrapURL as array and refactor code
Browse files Browse the repository at this point in the history
Signed-off-by: Bhoopesh <[email protected]>
  • Loading branch information
bhoopesh369 committed Jul 3, 2024
1 parent facc163 commit 9af1b66
Show file tree
Hide file tree
Showing 22 changed files with 302 additions and 169 deletions.
1 change: 1 addition & 0 deletions scripts/run_agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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
4 changes: 2 additions & 2 deletions sztp-agent/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ RUN go build -v -o /opi-sztp-agent && CGO_ENABLED=0 go test -v ./...
# second stage to reduce image size
FROM alpine:3.20

RUN apk add --no-cache --no-check-certificate curl dbus networkmanager && rm -rf /var/cache/apk/*
RUN apk add --no-cache --no-check-certificate curl && rm -rf /var/cache/apk/*

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

CMD ["sh", "-c", "dbus-daemon --system && /opi-sztp-agent"]
CMD ["/opi-sztp-agent"]
2 changes: 1 addition & 1 deletion sztp-agent/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func Daemon() *cobra.Command {
return fmt.Errorf("must not be folder: %q", filePath)
}
}
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
a := secureagent.NewAgent([]string{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(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
a := secureagent.NewAgent([]string{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(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
a := secureagent.NewAgent([]string{bootstrapURL}, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
return a.RunCommandEnable()
},
}
Expand Down
2 changes: 1 addition & 1 deletion sztp-agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func Run() *cobra.Command {
return fmt.Errorf("must not be folder: %q", filePath)
}
}
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
a := secureagent.NewAgent([]string{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(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
a := secureagent.NewAgent([]string{bootstrapURL}, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
return a.RunCommandStatus()
},
}
Expand Down
25 changes: 25 additions & 0 deletions sztp-agent/pkg/dhcp/bootstrap_url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
SPDX-License-Identifier: Apache-2.0
Copyright (C) 2022-2023 Intel Corporation
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (C) 2022 Red Hat.
*/

// Package dhcp implements the DHCP client
package dhcp

import "log"

// GetBootstrapURL returns the bootstrap URL
func GetBootstrapURL(dhcpLeaseFile string) ([]string, error) {
url, err := getBootstrapURLViaLeaseFile(dhcpLeaseFile)
if err == nil {
return []string{url}, nil
}
log.Println("[INFO] Trying to get the URL from NetworkManager")
urls, err := getBootstrapURLViaNetworkManager()
if err == nil {
return urls, nil
}
return nil, err
}
15 changes: 15 additions & 0 deletions sztp-agent/pkg/dhcp/bootstrap_url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
SPDX-License-Identifier: Apache-2.0
Copyright (C) 2022-2023 Intel Corporation
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (C) 2022 Red Hat.
*/

// Package dhcp implements the DHCP client
package dhcp

import "testing"

func TestGetBootstrapURL(_ *testing.T) {
// TODO: Implement the test
}
33 changes: 33 additions & 0 deletions sztp-agent/pkg/dhcp/dhcp_lease.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
SPDX-License-Identifier: Apache-2.0
Copyright (C) 2022-2023 Intel Corporation
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (C) 2022 Red Hat.
*/

// Package dhcp implements the DHCP client
package dhcp

import (
"errors"
"log"
"os"
)

const sztpRedirectUrls = "sztp-redirect-urls"

// 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)
if line != "" {
break
}
}
return ExtractfromLine(line, `(?m)[^"]*`, 1), nil
}
log.Println("[Error] File " + dhcpLeaseFile + " does not exist")
return "", errors.New("File " + dhcpLeaseFile + " does not exist")
}
53 changes: 53 additions & 0 deletions sztp-agent/pkg/dhcp/dhcp_lease_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
SPDX-License-Identifier: Apache-2.0
Copyright (C) 2022-2023 Intel Corporation
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (C) 2022 Red Hat.
*/

// Package dhcp implements the DHCP client
package dhcp

import "testing"

func TestGetBootstrapURLViaLeaseFile(t *testing.T) {
dhcpTestFileOK := "/tmp/test.dhcp"
CreateTempTestFile(dhcpTestFileOK, DHCPTestContent, true)

type fields struct {
DhcpLeaseFile string
}
tests := []struct {
name string
fields fields
want string
wantErr bool
}{
{
name: "Test OK Case file exists and get the URL",
fields: fields{
DhcpLeaseFile: dhcpTestFileOK,
},
want: "http://mymock/test",
wantErr: false,
},
{
name: "Test KO Case file does not exist",
fields: fields{
DhcpLeaseFile: "/kk/kk",
},
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
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 {
t.Errorf("GetBootstrapURLViaLeaseFile() = %v, want %v", got, tt.want)
}
})
}
}
20 changes: 11 additions & 9 deletions sztp-agent/pkg/dhcp/network_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,26 @@ 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 "", fmt.Errorf("failed to connect to system bus: %v", err)
return nil, fmt.Errorf("failed to connect to system bus: %v", err)
}

nm := conn.Object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")

var activeConnections []dbus.ObjectPath
err = nm.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.NetworkManager", "ActiveConnections").Store(&activeConnections)
if err != nil {
return "", fmt.Errorf("failed to get ActiveConnections property: %v", err)
return nil, fmt.Errorf("failed to get ActiveConnections property: %v", err)
}

if len(activeConnections) == 0 {
return "", fmt.Errorf("no active connections found")
return nil, fmt.Errorf("no active connections found")
}

var sztpRedirectURLs []string
for _, activeConnPath := range activeConnections {
connActive := conn.Object("org.freedesktop.NetworkManager", activeConnPath)

Expand All @@ -54,14 +55,15 @@ func GetBootstrapURLViaNetworkManager() (string, error) {

if variant, ok := options["sztp_redirect_urls"]; ok {
if variant.Signature().String() == "s" {
sztpRedirectURLs := variant.Value().(string)
log.Println("[SUCCESS] sztp_redirect_urls: ", sztpRedirectURLs)
return sztpRedirectURLs, nil
sztpRedirectURL := variant.Value().(string)
log.Println("[INFO] sztp_redirect_url found: ", sztpRedirectURLs)
sztpRedirectURLs = append(sztpRedirectURLs, sztpRedirectURL)
continue
}
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)
}
}
return "", fmt.Errorf("sztp_redirect_urls not found in any active connection")
return sztpRedirectURLs, fmt.Errorf("sztp_redirect_urls not found in any active connection")
}
11 changes: 5 additions & 6 deletions sztp-agent/pkg/secureagent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package secureagent
const (
CONTENT_TYPE_YANG = "application/yang-data+json"
OS_RELEASE_FILE = "/etc/os-release"
SZTP_REDIRECT_URL = "sztp-redirect-urls"
ARTIFACTS_PATH = "/tmp/"
)

Expand Down Expand Up @@ -70,7 +69,7 @@ type BootstrapServerErrorOutput struct {

// Agent is the basic structure to define an agent instance
type Agent struct {
BootstrapURL string // Bootstrap complete URL
BootstrapURL []string // Bootstrap complete URL
SerialNumber string // Device's Serial Number
DevicePassword string // Device's Password
DevicePrivateKey string // Device's private key
Expand All @@ -85,7 +84,7 @@ type Agent struct {

}

func NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert string) *Agent {
func NewAgent(bootstrapURL []string, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert string) *Agent {
return &Agent{
BootstrapURL: bootstrapURL,
SerialNumber: GetSerialNumber(serialNumber),
Expand All @@ -102,7 +101,7 @@ func NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, deviceP
}
}

func (a *Agent) GetBootstrapURL() string {
func (a *Agent) GetBootstrapURL() []string {
return a.BootstrapURL
}

Expand Down Expand Up @@ -138,8 +137,8 @@ func (a *Agent) GetProgressJSON() ProgressJSON {
return a.ProgressJSON
}

func (a *Agent) SetBootstrapURL(url string) {
a.BootstrapURL = url
func (a *Agent) SetBootstrapURL(urls []string) {
a.BootstrapURL = urls
}

func (a *Agent) SetSerialNumber(serialNumber string) {
Expand Down
Loading

0 comments on commit 9af1b66

Please sign in to comment.