Skip to content

Commit

Permalink
feat: expose dhclient.leases as argument
Browse files Browse the repository at this point in the history
Signed-off-by: Boris Glimcher <Boris.Glimcher@emc.com>
  • Loading branch information
glimchb committed Oct 12, 2023
1 parent 1d1cd6c commit 00a901d
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 19 deletions.
4 changes: 3 additions & 1 deletion sztp-agent/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func NewDaemonCommand() *cobra.Command {
var (
bootstrapURL string
serialNumber string
dhcpLeaseFile string
devicePassword string
devicePrivateKey string
deviceEndEntityCert string
Expand All @@ -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()
},
}
Expand All @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion sztp-agent/cmd/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func NewDisableCommand() *cobra.Command {
var (
bootstrapURL string
serialNumber string
dhcpLeaseFile string
devicePassword string
devicePrivateKey string
deviceEndEntityCert string
Expand All @@ -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()
},
}
Expand All @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion sztp-agent/cmd/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func NewEnableCommand() *cobra.Command {
var (
bootstrapURL string
serialNumber string
dhcpLeaseFile string
devicePassword string
devicePrivateKey string
deviceEndEntityCert string
Expand All @@ -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()
},
}
Expand All @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion sztp-agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func NewRunCommand() *cobra.Command {
var (
bootstrapURL string
serialNumber string
dhcpLeaseFile string
devicePassword string
devicePrivateKey string
deviceEndEntityCert string
Expand All @@ -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()
},
}
Expand All @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion sztp-agent/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func NewStatusCommand() *cobra.Command {
var (
bootstrapURL string
serialNumber string
dhcpLeaseFile string
devicePassword string
devicePrivateKey string
deviceEndEntityCert string
Expand All @@ -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()
},
}
Expand All @@ -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")
Expand Down
13 changes: 6 additions & 7 deletions sztp-agent/pkg/secureagent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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{},
Expand Down
6 changes: 4 additions & 2 deletions sztp-agent/pkg/secureagent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ func TestNewAgent(t *testing.T) {
type args struct {
bootstrapURL string
serialNumber string
dhcpLeaseFile string
devicePassword string
devicePrivateKey string
deviceEndEntityCert string
Expand All @@ -823,6 +824,7 @@ func TestNewAgent(t *testing.T) {
args: args{
bootstrapURL: "TestBootstrap",
serialNumber: "TestSerialNumber",
dhcpLeaseFile: "TestDhcpLeaseFile",
devicePassword: "TestDevicePassword",
devicePrivateKey: "TestDevicePrivateKey",
deviceEndEntityCert: "TestDeviceEndEntityCert",
Expand All @@ -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)
}
})
Expand Down
2 changes: 1 addition & 1 deletion sztp-agent/pkg/secureagent/disable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
2 changes: 1 addition & 1 deletion sztp-agent/pkg/secureagent/enable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
2 changes: 1 addition & 1 deletion sztp-agent/pkg/secureagent/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
2 changes: 1 addition & 1 deletion sztp-agent/pkg/secureagent/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
2 changes: 1 addition & 1 deletion sztp-agent/pkg/secureagent/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 00a901d

Please sign in to comment.