Skip to content

Commit

Permalink
feat: status command
Browse files Browse the repository at this point in the history
Signed-off-by: Bhoopesh <[email protected]>
  • Loading branch information
bhoopesh369 committed Oct 14, 2024
1 parent ddfeab2 commit 951c0a4
Show file tree
Hide file tree
Showing 17 changed files with 271 additions and 138 deletions.
28 changes: 23 additions & 5 deletions docker-compose.dpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ 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',
'--dhcp-lease-file', '/var/lib/dhclient/dhclient.leases',
'--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']
'--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']

agent2:
<<: *agent
Expand All @@ -59,7 +65,10 @@ 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']
'--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']

agent1:
<<: *agent
Expand All @@ -68,7 +77,10 @@ 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']
'--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']

agent4:
<<: *agent
Expand All @@ -77,7 +89,10 @@ 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']
'--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']

agent5:
<<: *agent
Expand All @@ -86,7 +101,10 @@ 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']
'--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']

volumes:
client-certs:
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
19 changes: 17 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}
if bootstrapURL != "" && dhcpLeaseFile != "" {
return fmt.Errorf("'--bootstrap-url' and '--dhcp-lease-file' are mutualy exclusive")
}
Expand All @@ -52,14 +55,23 @@ 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)
if info.IsDir() {
return fmt.Errorf("must not be folder: %q", filePath)
}
}
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, statusFilePath, resultFilePath, symLinkDir)
return a.RunCommandDaemon()
},
}
Expand All @@ -74,6 +86,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")

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,13 +28,16 @@ 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 {
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, statusFilePath, resultFilePath, symLinkDir)
return a.RunCommandDisable()
},
}
Expand All @@ -49,5 +52,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")

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,13 +28,16 @@ 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 {
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, statusFilePath, resultFilePath, symLinkDir)
return a.RunCommandEnable()
},
}
Expand All @@ -49,6 +52,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")

return cmd
}
19 changes: 17 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 @@ -52,14 +55,23 @@ 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)
if info.IsDir() {
return fmt.Errorf("must not be folder: %q", filePath)
}
}
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, statusFilePath, resultFilePath, symLinkDir)
return a.RunCommand()
},
}
Expand All @@ -74,6 +86,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", "", "Sym Link Directory")

return cmd
}
10 changes: 8 additions & 2 deletions sztp-agent/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ 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 {
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert)
a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, statusFilePath, resultFilePath, symLinkDir)
return a.RunCommandStatus()
},
}
Expand All @@ -45,10 +48,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", "", "Device's password")
flags.StringVar(&devicePassword, "device-password", "", "Dehomevice'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")

return cmd
}
33 changes: 31 additions & 2 deletions sztp-agent/pkg/secureagent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ type Agent struct {
ProgressJSON ProgressJSON // ProgressJson structure
BootstrapServerOnboardingInfo BootstrapServerOnboardingInfo // BootstrapServerOnboardingInfo structure
BootstrapServerRedirectInfo BootstrapServerRedirectInfo // BootstrapServerRedirectInfo structure

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) *Agent {
func NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert, statusFilePath, resultFilePath, symLinkDir string) *Agent {
return &Agent{
InputBootstrapURL: bootstrapURL,
BootstrapURL: "",
Expand All @@ -101,6 +103,9 @@ func NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, deviceP
ProgressJSON: ProgressJSON{},
BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{},
BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{},
StatusFilePath: statusFilePath,
ResultFilePath: resultFilePath,
SymLinkDir: symLinkDir,
}
}

Expand Down Expand Up @@ -140,6 +145,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 @@ -171,3 +188,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 @@ -828,6 +828,9 @@ func TestNewAgent(t *testing.T) {
devicePrivateKey string
deviceEndEntityCert string
bootstrapTrustAnchorCert string
statusFilePath string
resultFilePath string
symLinkDir string
}
tests := []struct {
name string
Expand All @@ -844,6 +847,9 @@ func TestNewAgent(t *testing.T) {
devicePrivateKey: "TestDevicePrivateKey",
deviceEndEntityCert: "TestDeviceEndEntityCert",
bootstrapTrustAnchorCert: "TestBootstrapTrustCert",
statusFilePath: "TestStatusFilePath",
resultFilePath: "TestResultFilePath",
symLinkDir: "TestSymLinkDir",
},
want: &Agent{
InputBootstrapURL: "TestBootstrap",
Expand All @@ -856,12 +862,15 @@ func TestNewAgent(t *testing.T) {
ContentTypeReq: "application/yang-data+json",
InputJSONContent: generateInputJSONContent(),
DhcpLeaseFile: "TestDhcpLeaseFile",
StatusFilePath: "TestStatusFilePath",
ResultFilePath: "TestResultFilePath",
SymLinkDir: "TestSymLinkDir",
},
},
}
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); !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); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewAgent() = %v, want %v", got, tt.want)
}
})
Expand Down
Loading

0 comments on commit 951c0a4

Please sign in to comment.