Skip to content

Commit

Permalink
fix: result and status
Browse files Browse the repository at this point in the history
Signed-off-by: Bhoopesh <[email protected]>
  • Loading branch information
bhoopesh369 committed Oct 25, 2024
1 parent 5b861ce commit 501e683
Show file tree
Hide file tree
Showing 16 changed files with 360 additions and 253 deletions.
25 changes: 5 additions & 20 deletions docker-compose.dpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ services:
'--bootstrap-trust-anchor-cert', '/certs/opi.pem',
'--device-end-entity-cert', '/certs/third_my_cert.pem',
'--device-private-key', '/certs/third_private_key.pem',
'--serial-number', 'third-serial-number',
'--status-file-path', '/var/lib/sztp/status.json',
'--result-file-path', '/var/lib/sztp/result.json',
'--sym-link-dir', '/run/sztp']
'--serial-number', 'third-serial-number']

agent2:
<<: *agent
Expand All @@ -65,10 +62,7 @@ services:
'--bootstrap-trust-anchor-cert', '/certs/opi.pem',
'--device-end-entity-cert', '/certs/second_my_cert.pem',
'--device-private-key', '/certs/second_private_key.pem',
'--serial-number', 'second-serial-number',
'--status-file-path', '/var/lib/sztp/status.json',
'--result-file-path', '/var/lib/sztp/result.json',
'--sym-link-dir', '/run/sztp']
'--serial-number', 'second-serial-number']

agent1:
<<: *agent
Expand All @@ -77,10 +71,7 @@ services:
'--bootstrap-trust-anchor-cert', '/certs/opi.pem',
'--device-end-entity-cert', '/certs/first_my_cert.pem',
'--device-private-key', '/certs/first_private_key.pem',
'--serial-number', 'first-serial-number',
'--status-file-path', '/var/lib/sztp/status.json',
'--result-file-path', '/var/lib/sztp/result.json',
'--sym-link-dir', '/run/sztp']
'--serial-number', 'first-serial-number']

agent4:
<<: *agent
Expand All @@ -89,10 +80,7 @@ services:
'--bootstrap-trust-anchor-cert', '/certs/opi.pem',
'--device-end-entity-cert', '/certs/first_my_cert.pem',
'--device-private-key', '/certs/first_private_key.pem',
'--serial-number', 'first-serial-number',
'--status-file-path', '/var/lib/sztp/status.json',
'--result-file-path', '/var/lib/sztp/result.json',
'--sym-link-dir', '/run/sztp']
'--serial-number', 'first-serial-number']

agent5:
<<: *agent
Expand All @@ -101,10 +89,7 @@ services:
'--bootstrap-trust-anchor-cert', '/certs/opi.pem',
'--device-end-entity-cert', '/certs/first_my_cert.pem',
'--device-private-key', '/certs/first_private_key.pem',
'--serial-number', 'first-serial-number',
'--status-file-path', '/var/lib/sztp/status.json',
'--result-file-path', '/var/lib/sztp/result.json',
'--sym-link-dir', '/run/sztp']
'--serial-number', 'first-serial-number']

volumes:
client-certs:
Expand Down
21 changes: 6 additions & 15 deletions sztp-agent/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ func Daemon() *cobra.Command {
deviceEndEntityCert string
bootstrapTrustAnchorCert string
statusFilePath string
resultFilePath string
symLinkDir 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, statusFilePath}
arrayChecker := []string{devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, statusFilePath, resultFilePath}
if bootstrapURL != "" && dhcpLeaseFile != "" {
return fmt.Errorf("'--bootstrap-url' and '--dhcp-lease-file' are mutualy exclusive")
}
Expand All @@ -55,15 +55,6 @@ func Daemon() *cobra.Command {
_, err := url.ParseRequestURI(bootstrapURL)
cobra.CheckErr(err)
}
if statusFilePath == "" {
return fmt.Errorf("'--status-file-path' is required")
}
if resultFilePath == "" {
return fmt.Errorf("'--result-file-path' is required")
}
if symLinkDir == "" {
return fmt.Errorf("'--symlink-dir' is required")
}
for _, filePath := range arrayChecker {
info, err := os.Stat(filePath)
cobra.CheckErr(err)
Expand All @@ -87,9 +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", "Path to the status file")
flags.StringVar(&resultFilePath, "result-file-path", "/var/lib/sztp/result.json", "Path to the result file")
flags.StringVar(&symLinkDir, "sym-link-dir", "/run/sztp", "Path to the symlink directory")
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: 5 additions & 5 deletions sztp-agent/cmd/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func Disable() *cobra.Command {
deviceEndEntityCert string
bootstrapTrustAnchorCert string
statusFilePath string
resultFilePath string
symLinkDir string
resultFilePath string
symLinkDir string
)

cmd := &cobra.Command{
Expand All @@ -53,9 +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", "", "Status file path")
flags.StringVar(&resultFilePath, "result-file-path", "", "Result file path")
flags.StringVar(&symLinkDir, "sym-link-dir", "", "Sym Link Directory")
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: 5 additions & 5 deletions sztp-agent/cmd/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func Enable() *cobra.Command {
deviceEndEntityCert string
bootstrapTrustAnchorCert string
statusFilePath string
resultFilePath string
symLinkDir string
resultFilePath string
symLinkDir string
)

cmd := &cobra.Command{
Expand All @@ -53,9 +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", "", "Status file path")
flags.StringVar(&resultFilePath, "result-file-path", "", "Result file path")
flags.StringVar(&symLinkDir, "sym-link-dir", "", "Sym Link Directory")
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
}
15 changes: 3 additions & 12 deletions sztp-agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func Run() *cobra.Command {
deviceEndEntityCert string
bootstrapTrustAnchorCert string
statusFilePath string
resultFilePath string
symLinkDir string
resultFilePath string
symLinkDir string
)

cmd := &cobra.Command{
Expand All @@ -55,15 +55,6 @@ func Run() *cobra.Command {
_, err := url.ParseRequestURI(bootstrapURL)
cobra.CheckErr(err)
}
if statusFilePath == "" {
return fmt.Errorf("'--status-file-path' is required")
}
if resultFilePath == "" {
return fmt.Errorf("'--result-file-path' is required")
}
if symLinkDir == "" {
return fmt.Errorf("'--symlink-dir' is required")
}
for _, filePath := range arrayChecker {
info, err := os.Stat(filePath)
cobra.CheckErr(err)
Expand All @@ -89,7 +80,7 @@ func Run() *cobra.Command {
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", "", "Sym Link Directory")
flags.StringVar(&symLinkDir, "sym-link-dir", "/run/sztp", "Sym Link Directory")

return cmd
}
12 changes: 6 additions & 6 deletions sztp-agent/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func Status() *cobra.Command {
deviceEndEntityCert string
bootstrapTrustAnchorCert string
statusFilePath string
resultFilePath string
symLinkDir string
resultFilePath string
symLinkDir string
)

cmd := &cobra.Command{
Expand All @@ -49,13 +49,13 @@ func Status() *cobra.Command {
flags.StringVar(&bootstrapURL, "bootstrap-url", "", "Bootstrap server URL")
flags.StringVar(&serialNumber, "serial-number", "", "Device's serial number")
flags.StringVar(&dhcpLeaseFile, "dhcp-lease-file", "/var/lib/dhclient/dhclient.leases", "Device's dhclient leases file")
flags.StringVar(&devicePassword, "device-password", "", "Dehomevice's password")
flags.StringVar(&devicePassword, "device-password", "", "Device's password")
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", "", "Status file path")
flags.StringVar(&resultFilePath, "result-file-path", "", "Result file path")
flags.StringVar(&symLinkDir, "sym-link-dir", "", "Sym Link Directory")
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: 4 additions & 4 deletions sztp-agent/pkg/secureagent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ 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
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, statusFilePath, resultFilePath, symLinkDir string, httpClient HttpClient) *Agent {
Expand All @@ -116,7 +116,7 @@ func NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, deviceP
HttpClient: httpClient,
StatusFilePath: statusFilePath,
ResultFilePath: resultFilePath,
SymLinkDir: symLinkDir,
SymLinkDir: symLinkDir,
}
}

Expand Down
10 changes: 5 additions & 5 deletions sztp-agent/pkg/secureagent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,9 +829,9 @@ func TestNewAgent(t *testing.T) {
devicePrivateKey string
deviceEndEntityCert string
bootstrapTrustAnchorCert string
statusFilePath string
resultFilePath string
symLinkDir string
statusFilePath string
resultFilePath string
symLinkDir string
}
client := http.Client{}
tests := []struct {
Expand All @@ -851,7 +851,7 @@ func TestNewAgent(t *testing.T) {
bootstrapTrustAnchorCert: "TestBootstrapTrustCert",
statusFilePath: "TestStatusFilePath",
resultFilePath: "TestResultFilePath",
symLinkDir: "TestSymLinkDir",
symLinkDir: "TestSymLinkDir",
},
want: &Agent{
InputBootstrapURL: "TestBootstrap",
Expand All @@ -866,7 +866,7 @@ func TestNewAgent(t *testing.T) {
DhcpLeaseFile: "TestDhcpLeaseFile",
StatusFilePath: "TestStatusFilePath",
ResultFilePath: "TestResultFilePath",
SymLinkDir: "TestSymLinkDir",
SymLinkDir: "TestSymLinkDir",
HttpClient: &client,
},
},
Expand Down
22 changes: 15 additions & 7 deletions sztp-agent/pkg/secureagent/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func (a *Agent) copyConfigurationFile() error {
log.Println("[INFO] Starting the Copy Configuration.")
_ = a.doReportProgress(ProgressTypeConfigInitiated, "Configuration Initiated")
_ = a.updateAndSaveStatus("config", true, "")
_ = 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 @@ -37,28 +37,32 @@ func (a *Agent) copyConfigurationFile() error {
}
log.Println("[INFO] Configuration file copied successfully")
_ = a.doReportProgress(ProgressTypeConfigComplete, "Configuration Complete")
_ = a.updateAndSaveStatus("config", false, "")
_ = 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")
_ = a.updateAndSaveStatus(scriptName+"-script", true, "")
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 @@ -92,7 +96,11 @@ func (a *Agent) launchScriptsConfiguration(typeOf string) error {
}
log.Println(string(out)) // remove it
_ = a.doReportProgress(reportEnd, "Report end")
_ = a.updateAndSaveStatus(scriptName+"-script", false, "")
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

0 comments on commit 501e683

Please sign in to comment.