diff --git a/sztp-agent/cmd/daemon.go b/sztp-agent/cmd/daemon.go index 082e76dc..56d45238 100644 --- a/sztp-agent/cmd/daemon.go +++ b/sztp-agent/cmd/daemon.go @@ -17,6 +17,7 @@ func NewDaemonCommand() *cobra.Command { var ( bootstrapURL string serialNumber string + dhcpLeaseFile string devicePassword string devicePrivateKey string deviceEndEntityCert string @@ -27,7 +28,7 @@ func NewDaemonCommand() *cobra.Command { Use: "daemon", Short: "Run the daemon command", RunE: func(cmd *cobra.Command, args []string) error { - a := secureagent.NewAgent(bootstrapURL, serialNumber, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) + a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) return a.RunCommandDaemon() }, } @@ -36,6 +37,7 @@ func NewDaemonCommand() *cobra.Command { // 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(&serialNumber, "serial-number", "my-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", "my-secret", "Device's password") flags.StringVar(&devicePrivateKey, "device-private-key", "/private_key.pem", "Device's private key") flags.StringVar(&deviceEndEntityCert, "device-end-entity-cert", "/my_cert.pem", "Device's End Entity cert") diff --git a/sztp-agent/cmd/disable.go b/sztp-agent/cmd/disable.go index aa440a38..ae4e090b 100644 --- a/sztp-agent/cmd/disable.go +++ b/sztp-agent/cmd/disable.go @@ -17,6 +17,7 @@ func NewDisableCommand() *cobra.Command { var ( bootstrapURL string serialNumber string + dhcpLeaseFile string devicePassword string devicePrivateKey string deviceEndEntityCert string @@ -27,7 +28,7 @@ func NewDisableCommand() *cobra.Command { Use: "disable", Short: "Run the disable command", RunE: func(cmd *cobra.Command, args []string) error { - a := secureagent.NewAgent(bootstrapURL, serialNumber, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) + a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) return a.RunCommandDisable() }, } @@ -37,6 +38,7 @@ func NewDisableCommand() *cobra.Command { // 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") diff --git a/sztp-agent/cmd/enable.go b/sztp-agent/cmd/enable.go index bdbe4bcf..a5df8b20 100644 --- a/sztp-agent/cmd/enable.go +++ b/sztp-agent/cmd/enable.go @@ -17,6 +17,7 @@ func NewEnableCommand() *cobra.Command { var ( bootstrapURL string serialNumber string + dhcpLeaseFile string devicePassword string devicePrivateKey string deviceEndEntityCert string @@ -27,7 +28,7 @@ func NewEnableCommand() *cobra.Command { Use: "enable", Short: "Run the enable command", RunE: func(cmd *cobra.Command, args []string) error { - a := secureagent.NewAgent(bootstrapURL, serialNumber, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) + a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) return a.RunCommandEnable() }, } @@ -37,6 +38,7 @@ func NewEnableCommand() *cobra.Command { // 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") diff --git a/sztp-agent/cmd/run.go b/sztp-agent/cmd/run.go index 0bde4eb7..405ca4a1 100644 --- a/sztp-agent/cmd/run.go +++ b/sztp-agent/cmd/run.go @@ -17,6 +17,7 @@ func NewRunCommand() *cobra.Command { var ( bootstrapURL string serialNumber string + dhcpLeaseFile string devicePassword string devicePrivateKey string deviceEndEntityCert string @@ -27,7 +28,7 @@ func NewRunCommand() *cobra.Command { Use: "run", Short: "Exec the run command", RunE: func(cmd *cobra.Command, args []string) error { - a := secureagent.NewAgent(bootstrapURL, serialNumber, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) + a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) return a.RunCommand() }, } @@ -37,6 +38,7 @@ func NewRunCommand() *cobra.Command { // 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") diff --git a/sztp-agent/cmd/status.go b/sztp-agent/cmd/status.go index 55fd0397..74837f76 100644 --- a/sztp-agent/cmd/status.go +++ b/sztp-agent/cmd/status.go @@ -17,6 +17,7 @@ func NewStatusCommand() *cobra.Command { var ( bootstrapURL string serialNumber string + dhcpLeaseFile string devicePassword string devicePrivateKey string deviceEndEntityCert string @@ -27,7 +28,7 @@ func NewStatusCommand() *cobra.Command { Use: "status", Short: "Run the status command", RunE: func(cmd *cobra.Command, args []string) error { - a := secureagent.NewAgent(bootstrapURL, serialNumber, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) + a := secureagent.NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert) return a.RunCommandStatus() }, } @@ -37,6 +38,7 @@ func NewStatusCommand() *cobra.Command { // 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") diff --git a/sztp-agent/pkg/secureagent/agent.go b/sztp-agent/pkg/secureagent/agent.go index 834c0a88..82d43d26 100644 --- a/sztp-agent/pkg/secureagent/agent.go +++ b/sztp-agent/pkg/secureagent/agent.go @@ -8,11 +8,10 @@ Copyright (C) 2022 Red Hat. package secureagent const ( - CONTENT_TYPE_YANG = "application/yang-data+json" - OS_RELEASE_FILE = "/etc/os-release" - DHCLIENT_LEASE_FILE = "/var/lib/dhclient/dhclient.leases" // By now default, but could be passed by params to get from os - SZTP_REDIRECT_URL = "sztp-redirect-urls" - ARTIFACTS_PATH = "/tmp/" + CONTENT_TYPE_YANG = "application/yang-data+json" + OS_RELEASE_FILE = "/etc/os-release" + SZTP_REDIRECT_URL = "sztp-redirect-urls" + ARTIFACTS_PATH = "/tmp/" ) type ProgressType int64 @@ -174,7 +173,7 @@ type Agent struct { } -func NewAgent(bootstrapURL, serialNumber, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert string) *Agent { +func NewAgent(bootstrapURL, serialNumber, dhcpLeaseFile, devicePassword, devicePrivateKey, deviceEndEntityCert, bootstrapTrustAnchorCert string) *Agent { return &Agent{ BootstrapURL: bootstrapURL, SerialNumber: serialNumber, @@ -184,7 +183,7 @@ func NewAgent(bootstrapURL, serialNumber, devicePassword, devicePrivateKey, devi BootstrapTrustAnchorCert: bootstrapTrustAnchorCert, ContentTypeReq: CONTENT_TYPE_YANG, InputJSONContent: generateInputJSONContent(), - DhcpLeaseFile: DHCLIENT_LEASE_FILE, + DhcpLeaseFile: dhcpLeaseFile, ProgressJSON: ProgressJSON{}, BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, diff --git a/sztp-agent/pkg/secureagent/agent_test.go b/sztp-agent/pkg/secureagent/agent_test.go index de254f7a..398e0fb4 100644 --- a/sztp-agent/pkg/secureagent/agent_test.go +++ b/sztp-agent/pkg/secureagent/agent_test.go @@ -808,6 +808,7 @@ func TestNewAgent(t *testing.T) { type args struct { bootstrapURL string serialNumber string + dhcpLeaseFile string devicePassword string devicePrivateKey string deviceEndEntityCert string @@ -823,6 +824,7 @@ func TestNewAgent(t *testing.T) { args: args{ bootstrapURL: "TestBootstrap", serialNumber: "TestSerialNumber", + dhcpLeaseFile: "TestDhcpLeaseFile", devicePassword: "TestDevicePassword", devicePrivateKey: "TestDevicePrivateKey", deviceEndEntityCert: "TestDeviceEndEntityCert", @@ -837,13 +839,13 @@ func TestNewAgent(t *testing.T) { BootstrapTrustAnchorCert: "TestBootstrapTrustCert", ContentTypeReq: "application/yang-data+json", InputJSONContent: generateInputJSONContent(), - DhcpLeaseFile: DHCLIENT_LEASE_FILE, + DhcpLeaseFile: "TestDhcpLeaseFile", }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := NewAgent(tt.args.bootstrapURL, tt.args.serialNumber, 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); !reflect.DeepEqual(got, tt.want) { t.Errorf("NewAgent() = %v, want %v", got, tt.want) } }) diff --git a/sztp-agent/pkg/secureagent/disable_test.go b/sztp-agent/pkg/secureagent/disable_test.go index 531db4c6..a0e27793 100644 --- a/sztp-agent/pkg/secureagent/disable_test.go +++ b/sztp-agent/pkg/secureagent/disable_test.go @@ -35,7 +35,7 @@ func TestAgent_RunCommandDisable(t *testing.T) { BootstrapTrustAnchorCert: "trustAnchorCert", ContentTypeReq: "application/json", InputJSONContent: generateInputJSONContent(), - DhcpLeaseFile: DHCLIENT_LEASE_FILE, + DhcpLeaseFile: "DHCPLEASEFILE", ProgressJSON: ProgressJSON{}, BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, diff --git a/sztp-agent/pkg/secureagent/enable_test.go b/sztp-agent/pkg/secureagent/enable_test.go index 00aeee7d..b0441ade 100644 --- a/sztp-agent/pkg/secureagent/enable_test.go +++ b/sztp-agent/pkg/secureagent/enable_test.go @@ -35,7 +35,7 @@ func TestAgent_RunCommandEnable(t *testing.T) { BootstrapTrustAnchorCert: "trustAnchorCert", ContentTypeReq: "application/json", InputJSONContent: generateInputJSONContent(), - DhcpLeaseFile: DHCLIENT_LEASE_FILE, + DhcpLeaseFile: "DHCPLEASEFILE", ProgressJSON: ProgressJSON{}, BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, diff --git a/sztp-agent/pkg/secureagent/run_test.go b/sztp-agent/pkg/secureagent/run_test.go index 03cbead3..209ed7ce 100644 --- a/sztp-agent/pkg/secureagent/run_test.go +++ b/sztp-agent/pkg/secureagent/run_test.go @@ -35,7 +35,7 @@ func TestAgent_RunCommand(t *testing.T) { BootstrapTrustAnchorCert: "trustAnchorCert", ContentTypeReq: "application/json", InputJSONContent: generateInputJSONContent(), - DhcpLeaseFile: DHCLIENT_LEASE_FILE, + DhcpLeaseFile: "DHCPLEASEFILE", ProgressJSON: ProgressJSON{}, BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, diff --git a/sztp-agent/pkg/secureagent/status_test.go b/sztp-agent/pkg/secureagent/status_test.go index 71a65fee..afc6cb63 100644 --- a/sztp-agent/pkg/secureagent/status_test.go +++ b/sztp-agent/pkg/secureagent/status_test.go @@ -35,7 +35,7 @@ func TestAgent_RunCommandStatus(t *testing.T) { BootstrapTrustAnchorCert: "trustAnchorCert", ContentTypeReq: "application/json", InputJSONContent: generateInputJSONContent(), - DhcpLeaseFile: DHCLIENT_LEASE_FILE, + DhcpLeaseFile: "DHCPLEASEFILE", ProgressJSON: ProgressJSON{}, BootstrapServerRedirectInfo: BootstrapServerRedirectInfo{}, BootstrapServerOnboardingInfo: BootstrapServerOnboardingInfo{}, diff --git a/sztp-agent/pkg/secureagent/utils.go b/sztp-agent/pkg/secureagent/utils.go index d7ad7d25..da879060 100644 --- a/sztp-agent/pkg/secureagent/utils.go +++ b/sztp-agent/pkg/secureagent/utils.go @@ -13,13 +13,13 @@ import ( "crypto/x509" "encoding/json" "errors" + "github.com/jaypipes/ghw" "log" "net/http" "os" "regexp" "strconv" "strings" - "github.com/jaypipes/ghw" ) // Auxiliar function to get lines from file matching with the substr