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

feat: status command #453

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions docker-compose.dpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ services:
- dhcp-leases-folder:/var/lib/dhclient/
- /etc/os-release:/etc/os-release
- /etc/ssh:/etc/ssh
- /var/lib/sztp:/var/lib/sztp
- /run/sztp:/run/sztp
privileged: true
networks:
- opi
command: ['/opi-sztp-agent', 'daemon',
Expand Down
3 changes: 3 additions & 0 deletions scripts/run_agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ 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/lib/sztp,target=/var/lib/sztp \
--mount type=bind,source=/run/sztp,target=/run/sztp \
--privileged \
${DOCKER_SZTP_IMAGE} \
/opi-sztp-agent daemon \
--dhcp-lease-file /var/lib/NetworkManager/dhclient-eth0.lease \
Expand Down
10 changes: 8 additions & 2 deletions sztp-agent/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ func Daemon() *cobra.Command {
devicePrivateKey string
deviceEndEntityCert string
bootstrapTrustAnchorCert string
statusFilePath string
resultFilePath string
symLinkDir string
)

cmd := &cobra.Command{
Use: "daemon",
Short: "Run the daemon command",
RunE: func(_ *cobra.Command, _ []string) error {
arrayChecker := []string{devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert}
arrayChecker := []string{devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, statusFilePath, resultFilePath}
if bootstrapURL != "" && dhcpLeaseFile != "" {
return fmt.Errorf("'--bootstrap-url' and '--dhcp-lease-file' are mutualy exclusive")
}
Expand All @@ -60,7 +63,7 @@ func Daemon() *cobra.Command {
}
}
client := secureagent.NewHTTPClient(bootstrapTrustAnchorCert, deviceEndEntityCert, devicePrivateKey)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, &client)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, statusFilePath, resultFilePath, symLinkDir, &client)
return a.RunCommandDaemon()
},
}
Expand All @@ -75,6 +78,9 @@ func Daemon() *cobra.Command {
flags.StringVar(&devicePrivateKey, "device-private-key", "/certs/private_key.pem", "Device's private key")
flags.StringVar(&deviceEndEntityCert, "device-end-entity-cert", "/certs/my_cert.pem", "Device's End Entity cert")
flags.StringVar(&bootstrapTrustAnchorCert, "bootstrap-trust-anchor-cert", "/certs/opi.pem", "Bootstrap server trust anchor Cert")
flags.StringVar(&statusFilePath, "status-file-path", "/var/lib/sztp/status.json", "Status file path")
flags.StringVar(&resultFilePath, "result-file-path", "/var/lib/sztp/result.json", "Result file path")
flags.StringVar(&symLinkDir, "sym-link-dir", "/run/sztp", "Sym Link Directory")

return cmd
}
9 changes: 8 additions & 1 deletion sztp-agent/cmd/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ func Disable() *cobra.Command {
devicePrivateKey string
deviceEndEntityCert string
bootstrapTrustAnchorCert string
statusFilePath string
resultFilePath string
symLinkDir string
)

cmd := &cobra.Command{
Use: "disable",
Short: "Run the disable command",
RunE: func(_ *cobra.Command, _ []string) error {
client := secureagent.NewHTTPClient(bootstrapTrustAnchorCert, deviceEndEntityCert, devicePrivateKey)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, &client)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, statusFilePath, resultFilePath, symLinkDir, &client)
return a.RunCommandDisable()
},
}
Expand All @@ -50,5 +53,9 @@ func Disable() *cobra.Command {
flags.StringVar(&devicePrivateKey, "device-private-key", "", "Device's private key")
flags.StringVar(&deviceEndEntityCert, "device-end-entity-cert", "", "Device's End Entity cert")
flags.StringVar(&bootstrapTrustAnchorCert, "bootstrap-trust-anchor-cert", "", "Bootstrap server trust anchor Cert")
flags.StringVar(&statusFilePath, "status-file-path", "/var/lib/sztp/status.json", "Status file path")
flags.StringVar(&resultFilePath, "result-file-path", "/var/lib/sztp/result.json", "Result file path")
flags.StringVar(&symLinkDir, "sym-link-dir", "/run/sztp", "Sym Link Directory")

return cmd
}
8 changes: 7 additions & 1 deletion sztp-agent/cmd/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ func Enable() *cobra.Command {
devicePrivateKey string
deviceEndEntityCert string
bootstrapTrustAnchorCert string
statusFilePath string
resultFilePath string
symLinkDir string
)

cmd := &cobra.Command{
Use: "enable",
Short: "Run the enable command",
RunE: func(_ *cobra.Command, _ []string) error {
client := secureagent.NewHTTPClient(bootstrapTrustAnchorCert, deviceEndEntityCert, devicePrivateKey)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, &client)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, statusFilePath, resultFilePath, symLinkDir, &client)
return a.RunCommandEnable()
},
}
Expand All @@ -50,6 +53,9 @@ func Enable() *cobra.Command {
flags.StringVar(&devicePrivateKey, "device-private-key", "", "Device's private key")
flags.StringVar(&deviceEndEntityCert, "device-end-entity-cert", "", "Device's End Entity cert")
flags.StringVar(&bootstrapTrustAnchorCert, "bootstrap-trust-anchor-cert", "", "Bootstrap server trust anchor Cert")
flags.StringVar(&statusFilePath, "status-file-path", "/var/lib/sztp/status.json", "Status file path")
flags.StringVar(&resultFilePath, "result-file-path", "/var/lib/sztp/result.json", "Result file path")
flags.StringVar(&symLinkDir, "sym-link-dir", "/run/sztp", "Sym Link Directory")

return cmd
}
10 changes: 8 additions & 2 deletions sztp-agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ func Run() *cobra.Command {
devicePrivateKey string
deviceEndEntityCert string
bootstrapTrustAnchorCert string
statusFilePath string
resultFilePath string
symLinkDir string
)

cmd := &cobra.Command{
Use: "run",
Short: "Exec the run command",
RunE: func(_ *cobra.Command, _ []string) error {
arrayChecker := []string{devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert}
arrayChecker := []string{devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, statusFilePath, resultFilePath}
if bootstrapURL != "" && dhcpLeaseFile != "" {
return fmt.Errorf("'--bootstrap-url' and '--dhcp-lease-file' are mutualy exclusive")
}
Expand All @@ -60,7 +63,7 @@ func Run() *cobra.Command {
}
}
client := secureagent.NewHTTPClient(bootstrapTrustAnchorCert, deviceEndEntityCert, devicePrivateKey)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, &client)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, statusFilePath, resultFilePath, symLinkDir, &client)
return a.RunCommand()
},
}
Expand All @@ -75,6 +78,9 @@ func Run() *cobra.Command {
flags.StringVar(&devicePrivateKey, "device-private-key", "/certs/private_key.pem", "Device's private key")
flags.StringVar(&deviceEndEntityCert, "device-end-entity-cert", "/certs/my_cert.pem", "Device's End Entity cert")
flags.StringVar(&bootstrapTrustAnchorCert, "bootstrap-trust-anchor-cert", "/certs/opi.pem", "Bootstrap server trust anchor Cert")
flags.StringVar(&statusFilePath, "status-file-path", "/var/lib/sztp/status.json", "Status file path")
flags.StringVar(&resultFilePath, "result-file-path", "/var/lib/sztp/result.json", "Result file path")
flags.StringVar(&symLinkDir, "sym-link-dir", "/run/sztp", "Sym Link Directory")

return cmd
}
8 changes: 7 additions & 1 deletion sztp-agent/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ func Status() *cobra.Command {
devicePrivateKey string
deviceEndEntityCert string
bootstrapTrustAnchorCert string
statusFilePath string
resultFilePath string
symLinkDir string
)

cmd := &cobra.Command{
Use: "status",
Short: "Run the status command",
RunE: func(_ *cobra.Command, _ []string) error {
client := secureagent.NewHTTPClient(bootstrapTrustAnchorCert, deviceEndEntityCert, devicePrivateKey)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, &client)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, statusFilePath, resultFilePath, symLinkDir, &client)
return a.RunCommandStatus()
},
}
Expand All @@ -50,6 +53,9 @@ func Status() *cobra.Command {
flags.StringVar(&devicePrivateKey, "device-private-key", "", "Device's private key")
flags.StringVar(&deviceEndEntityCert, "device-end-entity-cert", "", "Device's End Entity cert")
flags.StringVar(&bootstrapTrustAnchorCert, "bootstrap-trust-anchor-cert", "", "Bootstrap server trust anchor Cert")
flags.StringVar(&statusFilePath, "status-file-path", "/var/lib/sztp/status.json", "Status file path")
flags.StringVar(&resultFilePath, "result-file-path", "/var/lib/sztp/result.json", "Result file path")
flags.StringVar(&symLinkDir, "sym-link-dir", "/run/sztp", "Sym Link Directory")

return cmd
}
32 changes: 31 additions & 1 deletion sztp-agent/pkg/secureagent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,12 @@ type Agent struct {
BootstrapServerOnboardingInfo BootstrapServerOnboardingInfo // BootstrapServerOnboardingInfo structure
BootstrapServerRedirectInfo BootstrapServerRedirectInfo // BootstrapServerRedirectInfo structure
HttpClient HttpClient
StatusFilePath string // Path to the status file
ResultFilePath string // Path to the result file
SymLinkDir string // Path to the symlink directory for the status file
}

func NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert string, httpClient HttpClient) *Agent {
func NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, statusFilePath, resultFilePath, symLinkDir string, httpClient HttpClient) *Agent {
return &Agent{
InputBootstrapURL: bootstrapURL,
BootstrapURL: "",
Expand All @@ -111,6 +114,9 @@ func NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, deviceP
BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{},
BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{},
HttpClient: httpClient,
StatusFilePath: statusFilePath,
ResultFilePath: resultFilePath,
SymLinkDir: symLinkDir,
}
}

Expand Down Expand Up @@ -150,6 +156,18 @@ func (a *Agent) GetProgressJSON() ProgressJSON {
return a.ProgressJSON
}

func (a *Agent) GetStatusFilePath() string {
return a.StatusFilePath
}

func (a *Agent) GetResultFilePath() string {
return a.ResultFilePath
}

func (a *Agent) GetSymLinkDir() string {
return a.SymLinkDir
}

func (a *Agent) SetBootstrapURL(url string) {
a.BootstrapURL = url
}
Expand Down Expand Up @@ -181,3 +199,15 @@ func (a *Agent) SetContentTypeReq(ct string) {
func (a *Agent) SetProgressJSON(p ProgressJSON) {
a.ProgressJSON = p
}

func (a *Agent) SetStatusFilePath(path string) {
a.StatusFilePath = path
}

func (a *Agent) SetResultFilePath(path string) {
a.ResultFilePath = path
}

func (a *Agent) SetSymLinkDir(path string) {
a.SymLinkDir = path
}
11 changes: 10 additions & 1 deletion sztp-agent/pkg/secureagent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,9 @@ func TestNewAgent(t *testing.T) {
devicePrivateKey string
deviceEndEntityCert string
bootstrapTrustAnchorCert string
statusFilePath string
resultFilePath string
symLinkDir string
}
client := http.Client{}
tests := []struct {
Expand All @@ -846,6 +849,9 @@ func TestNewAgent(t *testing.T) {
devicePrivateKey: "TestDevicePrivateKey",
deviceEndEntityCert: "TestDeviceEndEntityCert",
bootstrapTrustAnchorCert: "TestBootstrapTrustCert",
statusFilePath: "TestStatusFilePath",
resultFilePath: "TestResultFilePath",
symLinkDir: "TestSymLinkDir",
},
want: &Agent{
InputBootstrapURL: "TestBootstrap",
Expand All @@ -858,13 +864,16 @@ func TestNewAgent(t *testing.T) {
ContentTypeReq: "application/yang-data+json",
InputJSONContent: generateInputJSONContent(),
DhcpLeaseFile: "TestDhcpLeaseFile",
StatusFilePath: "TestStatusFilePath",
ResultFilePath: "TestResultFilePath",
SymLinkDir: "TestSymLinkDir",
HttpClient: &client,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewAgent(tt.args.bootstrapURL, tt.args.serialNumber, tt.args.dhcpLeaseFile, tt.args.devicePassword, tt.args.devicePrivateKey, tt.args.deviceEndEntityCert, tt.args.bootstrapTrustAnchorCert, &client); !reflect.DeepEqual(got, tt.want) {
if got := NewAgent(tt.args.bootstrapURL, tt.args.serialNumber, tt.args.dhcpLeaseFile, tt.args.devicePassword, tt.args.devicePrivateKey, tt.args.deviceEndEntityCert, tt.args.bootstrapTrustAnchorCert, tt.args.statusFilePath, tt.args.resultFilePath, tt.args.symLinkDir, &client); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewAgent() = %v, want %v", got, tt.want)
}
})
Expand Down
18 changes: 15 additions & 3 deletions sztp-agent/pkg/secureagent/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
func (a *Agent) copyConfigurationFile() error {
log.Println("[INFO] Starting the Copy Configuration.")
_ = a.doReportProgress(ProgressTypeConfigInitiated, "Configuration Initiated")
_ = a.updateAndSaveStatus(StageTypeConfig, true, "")
// Copy the configuration file to the device
file, err := os.Create(ARTIFACTS_PATH + a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference + "-config")
if err != nil {
Expand All @@ -36,26 +37,32 @@ func (a *Agent) copyConfigurationFile() error {
}
log.Println("[INFO] Configuration file copied successfully")
_ = a.doReportProgress(ProgressTypeConfigComplete, "Configuration Complete")
_ = a.updateAndSaveStatus(StageTypeConfig, false, "")
return nil
}

func (a *Agent) launchScriptsConfiguration(typeOf string) error {
var script, scriptName string
var reportStart, reportEnd ProgressType
switch typeOf {
case "post":
case POST:
script = a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.PostConfigurationScript
scriptName = "post"
scriptName = POST
reportStart = ProgressTypePostScriptInitiated
reportEnd = ProgressTypePostScriptComplete
default: // pre or default
script = a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.PreConfigurationScript
scriptName = "pre"
scriptName = PRE
reportStart = ProgressTypePreScriptInitiated
reportEnd = ProgressTypePreScriptComplete
}
log.Println("[INFO] Starting the " + scriptName + "-configuration.")
_ = a.doReportProgress(reportStart, "Report starting")
if scriptName == PRE {
_ = a.updateAndSaveStatus(StageTypePreScript, true, "")
} else if scriptName == POST {
_ = a.updateAndSaveStatus(StageTypePostScript, true, "")
}
// nolint:gosec
file, err := os.Create(ARTIFACTS_PATH + a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference + scriptName + "configuration.sh")
if err != nil {
Expand Down Expand Up @@ -89,6 +96,11 @@ func (a *Agent) launchScriptsConfiguration(typeOf string) error {
}
log.Println(string(out)) // remove it
_ = a.doReportProgress(reportEnd, "Report end")
if scriptName == PRE {
_ = a.updateAndSaveStatus(StageTypePreScript, false, "")
} else if scriptName == POST {
_ = a.updateAndSaveStatus(StageTypePostScript, false, "")
}
log.Println("[INFO] " + scriptName + "-Configuration script executed successfully")
return nil
}
Loading
Loading