1
0
Fork 0
mirror of https://github.com/external-secrets/external-secrets.git synced 2024-12-15 17:51:01 +00:00

Issue 3436 (#3444)

* utiliy for comparing byteslice and string

Signed-off-by: himasagaratluri <himasagar.atluri@gmail.com>

* unit test for utility

Signed-off-by: himasagaratluri <himasagar.atluri@gmail.com>

* add validation for StringType

Signed-off-by: himasagaratluri <himasagar.atluri@gmail.com>

* if clause to consider binary

Signed-off-by: himasagaratluri <himasagar.atluri@gmail.com>

* Test case: if clause to consider binary

Signed-off-by: himasagaratluri <himasagar.atluri@gmail.com>

---------

Signed-off-by: himasagaratluri <himasagar.atluri@gmail.com>
This commit is contained in:
hima 2024-05-03 14:17:36 -05:00 committed by GitHub
parent 6252ad9394
commit f22c53fca0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 78 additions and 1 deletions

View file

@ -588,7 +588,7 @@ func (sm *SecretsManager) putSecretValueWithContext(ctx context.Context, secretI
if !isManagedByESO(data) { if !isManagedByESO(data) {
return fmt.Errorf("secret not managed by external-secrets") return fmt.Errorf("secret not managed by external-secrets")
} }
if awsSecret != nil && bytes.Equal(awsSecret.SecretBinary, value) { if awsSecret != nil && bytes.Equal(awsSecret.SecretBinary, value) || utils.CompareStringAndByteSlices(awsSecret.SecretString, value) {
return nil return nil
} }

View file

@ -507,3 +507,21 @@ func dig[T any](key string, data map[string]any) (t T, _ error) {
return t, errKeyNotFound return t, errKeyNotFound
} }
func CompareStringAndByteSlices(valueString *string, valueByte []byte) bool {
if valueString == nil {
return false
}
stringToByteSlice := []byte(*valueString)
if len(stringToByteSlice) != len(valueByte) {
return false
}
for sb := range valueByte {
if stringToByteSlice[sb] != valueByte[sb] {
return false
}
}
return true
}

View file

@ -20,6 +20,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/aws/aws-sdk-go/aws"
"github.com/oracle/oci-go-sdk/v65/vault" "github.com/oracle/oci-go-sdk/v65/vault"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@ -845,3 +846,61 @@ func TestGetByteValue(t *testing.T) {
}) })
} }
} }
func TestCompareStringAndByteSlices(t *testing.T) {
type args struct {
stringValue *string
byteValueSlice []byte
}
type testCase struct {
name string
args args
want bool
wantErr bool
}
tests := []testCase{
{
name: "same contents",
args: args{
stringValue: aws.String("value"),
byteValueSlice: []byte("value"),
},
want: true,
wantErr: true,
}, {
name: "different contents",
args: args{
stringValue: aws.String("value89"),
byteValueSlice: []byte("value"),
},
want: true,
wantErr: false,
}, {
name: "same contents with random",
args: args{
stringValue: aws.String("value89!3#@212"),
byteValueSlice: []byte("value89!3#@212"),
},
want: true,
wantErr: true,
}, {
name: "check Nil",
args: args{
stringValue: nil,
byteValueSlice: []byte("value89!3#@212"),
},
want: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := CompareStringAndByteSlices(tt.args.stringValue, tt.args.byteValueSlice)
if got != tt.wantErr {
t.Errorf("CompareStringAndByteSlices() got = %v, want = %v", got, tt.wantErr)
return
}
})
}
}