Skip to content

Commit

Permalink
Merge branch 'main' into cli-fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Node <jordansavell1198@gmail.com>
  • Loading branch information
Xeckt authored Jun 26, 2024
2 parents ab2dacd + e61f053 commit 939b25e
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions sztp-agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Copyright (C) 2022 Red Hat.
package cmd

import (
"fmt"
"net/url"
"os"

"github.com/opiproject/sztp/sztp-agent/pkg/secureagent"
"github.com/spf13/cobra"
)
Expand All @@ -34,6 +38,27 @@ func Run() *cobra.Command {
Use: "run",
Short: "Exec the run command",
RunE: func(_ *cobra.Command, _ []string) error {
arrayChecker := []string{devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert}
if bootstrapURL != "" && dhcpLeaseFile != "" {
return fmt.Errorf("'--bootstrap-url' and '--dhcp-lease-file' are mutualy exclusive")
}
if bootstrapURL == "" && dhcpLeaseFile == "" {
return fmt.Errorf("'--bootstrap-url' or '--dhcp-lease-file' is required")
}
if dhcpLeaseFile != "" {
arrayChecker = append(arrayChecker, dhcpLeaseFile)
}
if bootstrapURL != "" {
_, err := url.ParseRequestURI(bootstrapURL)
cobra.CheckErr(err)
}
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)
return a.RunCommand()
},
Expand All @@ -42,13 +67,13 @@ func Run() *cobra.Command {
flags := cmd.Flags()
// TODO this options should be retrieved automatically instead of requests in the agent
// Opened discussion to define the procedure: https://github.com/opiproject/sztp/issues/2
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(&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(&bootstrapURL, "bootstrap-url", "", "Bootstrap server URL. Mutually exclusive with '--dhcp-lease-file'")
flags.StringVar(&serialNumber, "serial-number", "", "Device's serial number. If empty, discover via SMBIOS")
flags.StringVar(&dhcpLeaseFile, "dhcp-lease-file", "", "Device's dhclient leases file. Mutually exclusive with '--bootstrap-url'")
flags.StringVar(&devicePassword, "device-password", "my-secret", "Device's password")
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")

return cmd
}

0 comments on commit 939b25e

Please sign in to comment.