Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issues with golint. Tested locally with golint tool #87

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions sztp-agent/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ Copyright (C) 2022 Intel Corporation
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (C) 2022 Red Hat.
*/

// Package cmd implements the CLI commands
package cmd

import (
"github.com/opiproject/sztp/sztp-agent/pkg/secureAgent"
"github.com/spf13/cobra"
)

// NewDaemonCommand returns the daemon command
func NewDaemonCommand() *cobra.Command {
var (
bootstrapURL string
Expand All @@ -32,8 +33,8 @@ func NewDaemonCommand() *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
// 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(&devicePassword, "device-password", "my-secret", "Device's password")
flags.StringVar(&devicePrivateKey, "device-private-key", "/private_key.pem", "Device's private key")
Expand Down
7 changes: 4 additions & 3 deletions sztp-agent/cmd/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ Copyright (C) 2022 Intel Corporation
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (C) 2022 Red Hat.
*/

// Package cmd implements the CLI commands
package cmd

import (
"github.com/opiproject/sztp/sztp-agent/pkg/secureAgent"
"github.com/spf13/cobra"
)

// NewDisableCommand returns the disable command
func NewDisableCommand() *cobra.Command {
var (
bootstrapURL string
Expand All @@ -32,8 +33,8 @@ func NewDisableCommand() *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
// 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(&devicePassword, "device-password", "", "Device's password")
Expand Down
7 changes: 4 additions & 3 deletions sztp-agent/cmd/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ Copyright (C) 2022 Intel Corporation
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (C) 2022 Red Hat.
*/

// Package cmd implements the CLI commands
package cmd

import (
"github.com/opiproject/sztp/sztp-agent/pkg/secureAgent"
"github.com/spf13/cobra"
)

// NewEnableCommand returns the enable command
func NewEnableCommand() *cobra.Command {
var (
bootstrapURL string
Expand All @@ -32,8 +33,8 @@ func NewEnableCommand() *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
// 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(&devicePassword, "device-password", "", "Device's password")
Expand Down
7 changes: 4 additions & 3 deletions sztp-agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ Copyright (C) 2022 Intel Corporation
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (C) 2022 Red Hat.
*/

// Package cmd implements the CLI commands
package cmd

import (
"github.com/opiproject/sztp/sztp-agent/pkg/secureAgent"
"github.com/spf13/cobra"
)

// NewRunCommand returns the run command
func NewRunCommand() *cobra.Command {
var (
bootstrapURL string
Expand All @@ -32,8 +33,8 @@ func NewRunCommand() *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
// 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(&devicePassword, "device-password", "", "Device's password")
Expand Down
7 changes: 4 additions & 3 deletions sztp-agent/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ Copyright (C) 2022 Intel Corporation
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (C) 2022 Red Hat.
*/

// Package cmd implements the CLI commands
package cmd

import (
"github.com/opiproject/sztp/sztp-agent/pkg/secureAgent"
"github.com/spf13/cobra"
)

// NewStatusCommand returns the status command
func NewStatusCommand() *cobra.Command {
var (
bootstrapURL string
Expand All @@ -32,8 +33,8 @@ func NewStatusCommand() *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
// 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(&devicePassword, "device-password", "", "Device's password")
Expand Down
4 changes: 1 addition & 3 deletions sztp-agent/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ go 1.19

require (
github.com/TwiN/go-color v1.2.0
github.com/github/smimesign v0.2.0
github.com/spf13/cobra v1.6.0
gopkg.in/h2non/gock.v1 v1.1.2
)

require (
github.com/github/smimesign v0.2.0 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
7 changes: 1 addition & 6 deletions sztp-agent/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/github/smimesign v0.2.0 h1:Hho4YcX5N1I9XNqhq0fNx0Sts8MhLonHd+HRXVGNjvk=
github.com/github/smimesign v0.2.0/go.mod h1:iZiiwNT4HbtGRVqCQu7uJPEZCuEE5sfSSttcnePkDl4=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/pborman/getopt v0.0.0-20180811024354-2b5b3bfb099b/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -23,13 +19,12 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734 h1:p/H982KKEjUnLJkM3tt/LemDnOc1GiZL5FCVlORJ5zo=
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 5 additions & 1 deletion sztp-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ func newCommand() *cobra.Command {
Use: "opi-sztp-agent",
Short: "opi-sztp-agent is the agent command line interface to work with the sztp workflow",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
err := cmd.Help()
if err != nil {
log.Fatalf(color.InRed("[ERROR]")+"%s", err.Error())
os.Exit(1)
}
os.Exit(1)
},
}
Expand Down
4 changes: 2 additions & 2 deletions sztp-agent/pkg/secureAgent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ Copyright (C) 2022 Intel Corporation
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (C) 2022 Red Hat.
*/

// Package secureAgent implements the secure agent
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
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/"
)
Expand Down
7 changes: 7 additions & 0 deletions sztp-agent/pkg/secureAgent/agent_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
SPDX-License-Identifier: Apache-2.0
Copyright (C) 2022 Intel Corporation
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (C) 2022 Red Hat.
*/
// Package secureAgent implements the secure agent
package secureAgent

import (
Expand Down
47 changes: 39 additions & 8 deletions sztp-agent/pkg/secureAgent/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright (C) 2022 Intel Corporation
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (C) 2022 Red Hat.
*/

// Package secureAgent implements the secure agent
package secureAgent

import (
Expand Down Expand Up @@ -32,6 +32,7 @@ const (
POST = "post"
)

// RunCommandDaemon runs the command in the background
func (a *Agent) RunCommandDaemon() error {
err := a.getBootstrapURL()
if err != nil {
Expand Down Expand Up @@ -146,7 +147,7 @@ func (a *Agent) downloadAndValidateImage() error {
a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference = fmt.Sprintf("%8d", time.Now().Unix())
for i, item := range a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.BootImage.DownloadURI {
log.Printf("[INFO] Downloading Image %v", item)
//Create a empty file
// Create a empty file
file, err := os.Create(ARTIFACTS_PATH + a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference + filepath.Base(item))
if err != nil {
return err
Expand Down Expand Up @@ -175,8 +176,20 @@ func (a *Agent) downloadAndValidateImage() error {
if err != nil {
return err
}
defer file.Close()
defer response.Body.Close()
defer func() error {
err = file.Close()
if err != nil {
return err
}
return nil
}()
defer func() error {
err = response.Body.Close()
if err != nil {
return err
}
return nil
}()

log.Printf("[INFO] Downloaded file: %s with size: %d", ARTIFACTS_PATH+a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference+filepath.Base(item), size)
log.Println("[INFO] Verify the file checksum: ", ARTIFACTS_PATH+a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference+filepath.Base(item))
Expand All @@ -187,7 +200,13 @@ func (a *Agent) downloadAndValidateImage() error {
log.Fatal(err)
return err
}
defer f.Close()
defer func() error {
err = f.Close()
if err != nil {
return err
}
return nil
}()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return err
Expand Down Expand Up @@ -216,7 +235,13 @@ func (a *Agent) copyConfigurationFile() error {
log.Println("[ERROR] creating the configuration file", err.Error())
return err
}
defer file.Close()
defer func() error {
err = file.Close()
if err != nil {
return err
}
return nil
}()
plainTest, err := base64.StdEncoding.DecodeString(a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.Configuration)
_, err = file.WriteString(string(plainTest))
if err != nil {
Expand Down Expand Up @@ -249,7 +274,13 @@ func (a *Agent) launchScriptsConfiguration(typeOf string) error {
log.Println("[ERROR] creating the "+scriptName+"-configuration script", err.Error())
return err
}
defer file.Close()
defer func() error {
err = file.Close()
if err != nil {
return err
}
return nil
}()
plainTest, err := base64.StdEncoding.DecodeString(script)
_, err = file.WriteString(string(plainTest))
if err != nil {
Expand All @@ -268,7 +299,7 @@ func (a *Agent) launchScriptsConfiguration(typeOf string) error {
log.Println("[ERROR] running the "+scriptName+"-configuration script", err.Error())
return err
}
log.Println(string(out)) //remove it
log.Println(string(out)) // remove it
log.Println("[INFO] " + scriptName + "-Configuration script executed successfully")
return nil
}
3 changes: 2 additions & 1 deletion sztp-agent/pkg/secureAgent/daemon_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package secureAgent implements the secure agent
package secureAgent

import (
Expand Down Expand Up @@ -157,7 +158,7 @@ func deleteTempTestFile(file string) {
}
}

func TestAgent_doRequestBootstrapServer(t *testing.T) {
func TestAgent_doReqBootstrap(t *testing.T) {
var output []byte
expected := BootstrapServerPostOutput{
IetfSztpBootstrapServerOutput: struct {
Expand Down
4 changes: 2 additions & 2 deletions sztp-agent/pkg/secureAgent/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Copyright (C) 2022 Intel Corporation
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (C) 2022 Red Hat.
*/

// Package secureAgent implements the secure agent
package secureAgent

// RunCommandDisable runs the command in the background
func (a *Agent) RunCommandDisable() error {

err := a.prepareEnvDisable()
err = a.configureDisable()
err = a.runDisable()
Expand Down
3 changes: 2 additions & 1 deletion sztp-agent/pkg/secureAgent/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ Copyright (C) 2022 Intel Corporation
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (C) 2022 Red Hat.
*/

// Package secureAgent implements the secure agent
package secureAgent

// RunCommandEnable runs the command in the background
func (a *Agent) RunCommandEnable() error {

err := a.prepareEnvEnable()
Expand Down
3 changes: 2 additions & 1 deletion sztp-agent/pkg/secureAgent/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ Copyright (C) 2022 Intel Corporation
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (C) 2022 Red Hat.
*/

// Package secureAgent implements the secure agent
package secureAgent

// RunCommand runs the command in the background
func (a *Agent) RunCommand() error {

err := a.prepareEnv()
Expand Down
3 changes: 2 additions & 1 deletion sztp-agent/pkg/secureAgent/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ Copyright (C) 2022 Intel Corporation
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (C) 2022 Red Hat.
*/

// Package secureAgent implements the secure agent
package secureAgent

// RunCommandStatus runs the command in the background
func (a *Agent) RunCommandStatus() error {

err := a.prepareEnvStatus()
Expand Down
Loading