Skip to content

Commit

Permalink
Refactor: fix potential panic & refactor some code (#1123)
Browse files Browse the repository at this point in the history
* Fix potential panic
Signed-off-by: Engin Acikgoz <[email protected]>

* Add test
Signed-off-by: Engin Acikgoz <[email protected]>

* Simplify error generators
Signed-off-by: Engin Acikgoz <[email protected]>

* No need to nil check, length check is enough
Signed-off-by: Engin Acikgoz <[email protected]>

* Remove unreachable code
Signed-off-by: Engin Acikgoz <[email protected]>

* Revert "Remove unreachable code"

This reverts commit d0b1eaf.
Signed-off-by: Engin Acikgoz <[email protected]>
  • Loading branch information
canack authored Aug 27, 2024
1 parent e9f1aac commit 7f9a241
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/sentinel/internal/oidc/engine/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func HandleCommandSecrets(
return
}

if req.Namespaces == nil || len(req.Namespaces) == 0 {
if len(req.Namespaces) == 0 {
req.Namespaces = []string{"default"}
}

Expand Down
2 changes: 1 addition & 1 deletion app/sentinel/internal/oidc/safe/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Get(
}
}(source)
if !proceed {
return "", fmt.Errorf("could not proceed")
return "", errors.New("could not proceed")
}

authorizer := tlsconfig.AdaptMatcher(func(id spiffeid.ID) error {
Expand Down
2 changes: 1 addition & 1 deletion app/sentinel/internal/oidc/safe/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func newSecretUpsertRequest(

func respond(cid *string, r *http.Response) (string, error) {
if r == nil {
return "", fmt.Errorf("post: Response is null")
return "", errors.New("post: Response is null")
}

defer func(b io.ReadCloser) {
Expand Down
2 changes: 1 addition & 1 deletion app/sentinel/internal/oidc/safe/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func Post(
log.Println(cid,
"Post: I cannot execute command because I cannot talk to SPIRE.")
return "",
fmt.Errorf(
errors.New(
"post: I cannot execute command because I cannot talk to SPIRE")
}

Expand Down
4 changes: 1 addition & 3 deletions ci/test/assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ func WorkloadIsRunning() error {
}

if podCount != 1 {
return errors.New(
fmt.Sprintf("Expected 1 running pod for workload, found %d", podCount),
)
return fmt.Errorf("Expected 1 running pod for workload, found %d", podCount)
}

return nil
Expand Down
7 changes: 1 addition & 6 deletions core/crypto/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,7 @@ func EncryptToWriterAge(out io.Writer, data string) error {
defer func(w io.WriteCloser) {
err := w.Close()
if err != nil {
fmt.Println(
fmt.Sprintf(
"encryptToWriterAge: problem closing stream: %s",
err.Error(),
),
)
fmt.Printf("encryptToWriterAge: problem closing stream: %s\n", err.Error())
}
}(wrappedWriter)

Expand Down
2 changes: 1 addition & 1 deletion core/entity/v1/data/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func convertValueToMap(values []string) map[string][]byte {
// otherwise, it returns a map with a single entry, "VALUE", containing the
// original 'value' as []byte.
func convertValueNoTemplate(values []string) map[string][]byte {
var data map[string][]byte
var data = make(map[string][]byte)
var jsonData map[string]string

val := ""
Expand Down
26 changes: 26 additions & 0 deletions core/entity/v1/data/convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package data

import (
"reflect"
"testing"
)

func TestConvertValueNoTemplate(t *testing.T) {
t.Run("Success", func(t *testing.T) {
values := []string{"{\"key\":\"value\"}"}
expected := map[string][]byte{"key": []byte("value")}
actual := convertValueNoTemplate(values)
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Expected %v but got %v", expected, actual)
}
})

t.Run("Failure", func(t *testing.T) {
values := []string{"key:value"}
expected := map[string][]byte{"VALUE": []byte("key:value")}
actual := convertValueNoTemplate(values)
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Expected %v but got %v", expected, actual)
}
})
}

0 comments on commit 7f9a241

Please sign in to comment.