2021-12-16 19:43:10 +00:00
|
|
|
package boltdb
|
|
|
|
|
|
|
|
import (
|
2022-01-17 03:40:02 +00:00
|
|
|
"crypto/sha256"
|
2021-12-16 19:43:10 +00:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gofrs/uuid"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-01-17 03:40:02 +00:00
|
|
|
const (
|
2022-06-03 04:00:13 +00:00
|
|
|
jsonobject = `{"LogoURL":"","BlackListedLabels":[],"AuthenticationMethod":1,"InternalAuthSettings": {"RequiredPasswordLength": 12}"LDAPSettings":{"AnonymousMode":true,"ReaderDN":"","URL":"","TLSConfig":{"TLS":false,"TLSSkipVerify":false},"StartTLS":false,"SearchSettings":[{"BaseDN":"","Filter":"","UserNameAttribute":""}],"GroupSearchSettings":[{"GroupBaseDN":"","GroupFilter":"","GroupAttribute":""}],"AutoCreateUsers":true},"OAuthSettings":{"ClientID":"","AccessTokenURI":"","AuthorizationURI":"","ResourceURI":"","RedirectURI":"","UserIdentifier":"","Scopes":"","OAuthAutoCreateUsers":false,"DefaultTeamID":0,"SSO":true,"LogoutURI":"","KubeSecretKey":"j0zLVtY/lAWBk62ByyF0uP80SOXaitsABP0TTJX8MhI="},"OpenAMTConfiguration":{"Enabled":false,"MPSServer":"","MPSUser":"","MPSPassword":"","MPSToken":"","CertFileContent":"","CertFileName":"","CertFilePassword":"","DomainName":""},"FeatureFlagSettings":{},"SnapshotInterval":"5m","TemplatesURL":"https://raw.githubusercontent.com/portainer/templates/master/templates-2.0.json","EdgeAgentCheckinInterval":5,"EnableEdgeComputeFeatures":false,"UserSessionTimeout":"8h","KubeconfigExpiry":"0","EnableTelemetry":true,"HelmRepositoryURL":"https://charts.bitnami.com/bitnami","KubectlShellImage":"portainer/kubectl-shell","DisplayDonationHeader":false,"DisplayExternalContributors":false,"EnableHostManagementFeatures":false,"AllowVolumeBrowserForRegularUsers":false,"AllowBindMountsForRegularUsers":false,"AllowPrivilegedModeForRegularUsers":false,"AllowHostNamespaceForRegularUsers":false,"AllowStackManagementForRegularUsers":false,"AllowDeviceMappingForRegularUsers":false,"AllowContainerCapabilitiesForRegularUsers":false}`
|
2022-01-17 03:40:02 +00:00
|
|
|
passphrase = "my secret key"
|
|
|
|
)
|
|
|
|
|
|
|
|
func secretToEncryptionKey(passphrase string) []byte {
|
|
|
|
hash := sha256.Sum256([]byte(passphrase))
|
|
|
|
return hash[:]
|
|
|
|
}
|
2021-12-16 19:43:10 +00:00
|
|
|
|
2022-01-17 03:40:02 +00:00
|
|
|
func Test_MarshalObjectUnencrypted(t *testing.T) {
|
2021-12-16 19:43:10 +00:00
|
|
|
is := assert.New(t)
|
|
|
|
|
|
|
|
uuid := uuid.Must(uuid.NewV4())
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
object interface{}
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
object: nil,
|
|
|
|
expected: `null`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: true,
|
|
|
|
expected: `true`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: false,
|
|
|
|
expected: `false`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: 123,
|
|
|
|
expected: `123`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: "456",
|
|
|
|
expected: "456",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: uuid,
|
|
|
|
expected: "\"" + uuid.String() + "\"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: uuid.String(),
|
|
|
|
expected: uuid.String(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: map[string]interface{}{"key": "value"},
|
|
|
|
expected: `{"key":"value"}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: []bool{true, false},
|
|
|
|
expected: `[true,false]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: []int{1, 2, 3},
|
|
|
|
expected: `[1,2,3]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: []string{"1", "2", "3"},
|
|
|
|
expected: `["1","2","3"]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: []map[string]interface{}{{"key1": "value1"}, {"key2": "value2"}},
|
|
|
|
expected: `[{"key1":"value1"},{"key2":"value2"}]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: []interface{}{1, "2", false, map[string]interface{}{"key1": "value1"}},
|
|
|
|
expected: `[1,"2",false,{"key1":"value1"}]`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-01-17 03:40:02 +00:00
|
|
|
conn := DbConnection{}
|
|
|
|
|
2021-12-16 19:43:10 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%s -> %s", test.object, test.expected), func(t *testing.T) {
|
2022-01-17 03:40:02 +00:00
|
|
|
data, err := conn.MarshalObject(test.object)
|
2021-12-16 19:43:10 +00:00
|
|
|
is.NoError(err)
|
|
|
|
is.Equal(test.expected, string(data))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-17 03:40:02 +00:00
|
|
|
func Test_UnMarshalObjectUnencrypted(t *testing.T) {
|
2021-12-16 19:43:10 +00:00
|
|
|
is := assert.New(t)
|
|
|
|
|
|
|
|
// Based on actual data entering and what we expect out of the function
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
object []byte
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
object: []byte(""),
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: []byte("35"),
|
|
|
|
expected: "35",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// An unmarshalled byte string should return the same without error
|
|
|
|
object: []byte("9ca4a1dd-a439-4593-b386-a7dfdc2e9fc6"),
|
|
|
|
expected: "9ca4a1dd-a439-4593-b386-a7dfdc2e9fc6",
|
|
|
|
},
|
|
|
|
{
|
2022-01-17 03:40:02 +00:00
|
|
|
// An un-marshalled json object string should return the same as a string without error also
|
2021-12-16 19:43:10 +00:00
|
|
|
object: []byte(jsonobject),
|
|
|
|
expected: jsonobject,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-01-17 03:40:02 +00:00
|
|
|
conn := DbConnection{}
|
|
|
|
|
2021-12-16 19:43:10 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%s -> %s", test.object, test.expected), func(t *testing.T) {
|
|
|
|
var object string
|
2022-01-17 03:40:02 +00:00
|
|
|
err := conn.UnmarshalObject(test.object, &object)
|
2021-12-16 19:43:10 +00:00
|
|
|
is.NoError(err)
|
|
|
|
is.Equal(test.expected, string(object))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-01-17 03:40:02 +00:00
|
|
|
|
|
|
|
func Test_ObjectMarshallingEncrypted(t *testing.T) {
|
|
|
|
is := assert.New(t)
|
|
|
|
|
|
|
|
// Based on actual data entering and what we expect out of the function
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
object []byte
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
object: []byte(""),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: []byte("35"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// An unmarshalled byte string should return the same without error
|
|
|
|
object: []byte("9ca4a1dd-a439-4593-b386-a7dfdc2e9fc6"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// An un-marshalled json object string should return the same as a string without error also
|
|
|
|
object: []byte(jsonobject),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
key := secretToEncryptionKey(passphrase)
|
|
|
|
conn := DbConnection{EncryptionKey: key}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%s -> %s", test.object, test.expected), func(t *testing.T) {
|
|
|
|
|
|
|
|
data, err := conn.MarshalObject(test.object)
|
|
|
|
is.NoError(err)
|
|
|
|
|
|
|
|
var object []byte
|
|
|
|
err = conn.UnmarshalObject(data, &object)
|
|
|
|
|
|
|
|
is.NoError(err)
|
|
|
|
is.Equal(test.object, object)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|