diff --git a/sztp-agent/cmd/run.go b/sztp-agent/cmd/run.go index 8d2792ca..f3b02c1f 100644 --- a/sztp-agent/cmd/run.go +++ b/sztp-agent/cmd/run.go @@ -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" ) @@ -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() }, @@ -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 }