Skip to content

Commit

Permalink
MFW 4883 fixing db settings (#391)
Browse files Browse the repository at this point in the history
* util: move DecodeAttribute into it's own function, add it to util_test as well, testing the error that broke main branch devices

* database settings: move settings over to golang-shared, add basic unmarshal testers

* MFW-4883 Fixing unit tests for decode string util function

* MFW-4883 Adding interface settings

* MFW-4883 Added an extra password attribute

* MFW-4883 Fixing database password attr name

* MFW-4883 Fixing interfaces settings

* Added encrypted password attributed for pppoe

* Added attributes for interfaces

* MFW-4883 Removing interface structs

---------

Co-authored-by: John Sommerville <jsommerville@arista.com>

version: bug
  • Loading branch information
manuwelakanade authored Apr 30, 2024
1 parent 876a74b commit 4867269
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"name":"untangle-node-captiveportal","allowedState":0},{"name":"untangle-node-threat-prevention","allowedState":1},{"name":"untangle-node-sitefilter","allowedState":0},{"name":"untangle-node-geoip","allowedState":0},{"name":"untangle-node-discovery","allowedState":0},{"name":"untangle-node-classd","allowedState":1},{"name":"untangle-node-dynamic-lists","allowedState":0}]
[{"name":"untangle-node-discovery","allowedState":0},{"name":"untangle-node-classd","allowedState":1},{"name":"untangle-node-dynamic-lists","allowedState":0},{"name":"untangle-node-captiveportal","allowedState":0},{"name":"untangle-node-threat-prevention","allowedState":1},{"name":"untangle-node-sitefilter","allowedState":0},{"name":"untangle-node-geoip","allowedState":0}]
17 changes: 17 additions & 0 deletions services/settings/database_settings/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package database_settings

type Database struct {
Enabled bool `json:"enabled"`
ID string `json:"id"`
Database string `json:"db_name"`
UserName string `json:"db_username"`
Password string `json:"db_password"`
PasswordEncrypted string `json:"db_password_encrypted"`
Server string `json:"db_server"`
Port int `json:"db_port"`
Description string `json:"description"`
Name string `json:"name"`
Type string `json:"type"`
ConnectionString string `json:"db_connection_string"`
Default bool `json:"default"`
}
123 changes: 123 additions & 0 deletions services/settings/database_settings/database_settings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package database_settings

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

// unnmarshalTest stolen from policy tests - we can probably generalize this to be reused across areas better
type unmarshalTest struct {
name string
json string
expectedErr bool
expected Databases
}

// runUnmarshalTest runs the unmarshal test.
func runUnmarshalTest(t *testing.T, tests []unmarshalTest) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var actual Databases
if !tt.expectedErr {
assert.NoError(t, json.Unmarshal([]byte(tt.json), &actual))
assert.EqualValues(t, actual, tt.expected)
} else {
assert.Error(t, json.Unmarshal([]byte(tt.json), &actual))
}
})
}
}

// TestDatabaseUnmarshal tests unmarshalling the database settings
func TestDatabaseUnmarshal(t *testing.T) {
tests := []unmarshalTest{
{
name: "Generic database settings unmarshal test",
json: `{"databases": [{"enabled": true,
"db_name": "testingdb",
"db_username": "test_user",
"db_password": "test_pw",
"db_server": "test_server",
"db_port": 5,
"description": "Some desc",
"name": "New DB Source",
"type": "DB Type 5",
"default": false,
"db_connection_string": "asdfasdfasdf"
}]}`,
expectedErr: false,
expected: Databases{
[]Database{
{
Enabled: true,
Database: "testingdb",
UserName: "test_user",
Password: "test_pw",
Server: "test_server",
Port: 5,
Description: "Some desc",
Name: "New DB Source",
Type: "DB Type 5",
Default: false,
ConnectionString: "asdfasdfasdf",
},
},
},
},
{
name: "Default sqlite DB test",
json: `{"databases": [
{
"db_connection_string": "sqlite:///tmp/reports.db",
"db_name": "reports.db",
"db_username": "",
"db_password": "",
"db_server": "",
"db_port": 0,
"description": "Local reports database",
"enabled": true,
"id": "66a6bc90-2f5e-4dc3-8180-a7cf4133daf2",
"name": "Local DB",
"type": "sqlite",
"default": true
}]}`,
expectedErr: false,
expected: Databases{
[]Database{
{
Enabled: true,
ConnectionString: "sqlite:///tmp/reports.db",
Database: "reports.db",
UserName: "",
Password: "",
Server: "",
Port: 0,
Description: "Local reports database",
Name: "Local DB",
Type: "sqlite",
Default: true,
ID: "66a6bc90-2f5e-4dc3-8180-a7cf4133daf2",
},
},
}},
{
name: "bad rule object type",
json: `{"name": "Geo Rule Tester",
"id": "c2428365-65be-4901-bfc0-bde2b310fedf",
"type": "asdfasdf",
"description": "Whatever",
"conditions": ["1458dc12-a9c2-4d0c-8203-1340c61c2c3b"],
"action": {
"type": "SET_CONFIGURATION",
"configuration_id": "1202b42e-2f21-49e9-b42c-5614e04d0031",
"key": "GeoipRuleObject"
}
}`,
expectedErr: false,
expected: Databases{},
},
}
runUnmarshalTest(t, tests)
}
5 changes: 5 additions & 0 deletions services/settings/database_settings/databases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package database_settings

type Databases struct {
Databases []Database `json:"databases"`
}
20 changes: 20 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/base64"
"fmt"
"io"
"math/rand"
"strings"
Expand Down Expand Up @@ -102,3 +104,21 @@ func ExtractFilesFromTar(b []byte, isGzip bool, fileName ...string) (map[string]

return foundFiles, nil
}

// DecodeAttribute is used to decode strings into base64 encoded strings
func DecodeAttribute(value string) (string, error) {
const num = 2
// check for empty interface value
if value == "" {
return "", nil
}

stringValue := fmt.Sprintf("%v", value)
rightStart := stringValue[len(stringValue)-num:]
rightEnd := stringValue[0 : len(stringValue)-num]
decodedBytes, err := base64.StdEncoding.DecodeString(rightStart + rightEnd)
if err != nil {
return "", err
}
return string(decodedBytes), nil
}
42 changes: 42 additions & 0 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,45 @@ func TestStringArrayToDB(t *testing.T) {
})
}
}

// Test DecodeAttribute functions
func TestDecodeAttribute(t *testing.T) {
tests := []struct {
name string
input string
expected string
errorExpected bool
}{
{
name: "Null string input",
input: "",
expected: "",
errorExpected: false,
},
{
name: "String value test",
input: "Testingval",
expected: "",
errorExpected: true,
},
{
name: "b64 encoded string value test",
input: "VzdGluZ3ZhbA==VG",
expected: "Testingval",
errorExpected: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if !test.errorExpected {
actual, err := DecodeAttribute(test.input)
assert.Equal(t, test.expected, actual)
assert.NoError(t, err)
} else {
_, err := DecodeAttribute(test.input)
assert.Error(t, err)
}
})
}
}

0 comments on commit 4867269

Please sign in to comment.