Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
MKCLOS
Core Development Platform
corepkgv2
Commits
fb06db03
Commit
fb06db03
authored
6 years ago
by
Roshan Patil
Browse files
Options
Downloads
Patches
Plain Diff
dalhelper completed only security is disabled
parent
efd18c1a
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!23
Devbranch to Master
,
!16
Savedata dalmdl
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
dalmdl/fdb/fdb.go
+164
-29
dalmdl/fdb/fdb.go
dalmdl/fdb/fdb_test.go
+101
-1
dalmdl/fdb/fdb_test.go
with
265 additions
and
30 deletions
dalmdl/fdb/fdb.go
+
164
−
29
View file @
fb06db03
...
...
@@ -5,7 +5,13 @@
package
fdb
import
(
"io/ioutil"
"net/http"
"strings"
"sync"
"time"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/securitymdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/cachemdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/constantmdl"
...
...
@@ -16,6 +22,16 @@ import (
"github.com/tidwall/gjson"
)
// securityRequired for securing fdb data
var
securityRequired
=
false
// downloadFromInternet flag enables filedownload from internet
var
downloadFromInternet
=
false
var
hostAddressForDataFiles
=
""
var
splitString
=
""
var
defaultSecurityKey
=
[]
byte
{}
var
mutex
=
&
sync
.
Mutex
{}
var
(
// NumberOfReads Collects Number of Reads
...
...
@@ -24,6 +40,10 @@ var (
NumberOfWrites
=
0
// Fastcache Holds Cache for queries
Fastcache
cachemdl
.
FastCacheHelper
// NumberOfReadsWithoutSecurity Collects Number of Reads without Security
NumberOfReadsWithoutSecurity
=
0
// NumberOfWritesWithoutSecurity Collects Number of Writes without Security
NumberOfWritesWithoutSecurity
=
0
)
// GetDataDAO will return query data
...
...
@@ -71,18 +91,20 @@ func getDataFromFDB(filePath string) ([]byte, error) {
if
errormdl
.
CheckErr
(
err
)
!=
nil
{
loggermdl
.
LogError
(
err
)
}
// TODO: Compression is not commited
// if securityRequired {
// // decryptedData, decryptionError := securityhelper.AESDecryptDefault(data)
// keyBytes := GetKeyWithFileNameAndDefaultKey(filePath)
// decryptedData, decryptionError := securityhelper.AESDecrypt(data, keyBytes)
// if decryptionError != nil {
// return nil, decryptionError
// // // decryptedData, decryptionError := securityhelper.AESDecryptDefault(data)
// keyBytes, hashError := GetKeyWithFileNameAndDefaultKey(filePath)
// if errormdl.CheckErr1(hashError) != nil {
// return nil, errormdl.CheckErr1(hashError)
// }
// decompressedData, decompressError := securityhelper.Decompress(decryptedData)
// if decompressError != nil {
// return nil, decompressError
// decryptedData, decryptionError := securitymdl.AESDecrypt(data, keyBytes)
// if errormdl.CheckErr2(decryptionError) != nil {
// return nil, errormdl.CheckErr2(decryptionError)
// }
// decompressedData, decompressError := securitymdl.Decompress(decryptedData)
// if errormdl.CheckErr3(decompressError) != nil {
// return nil, errormdl.CheckErr3(decompressError)
// }
// return decompressedData, nil
// }
...
...
@@ -95,27 +117,29 @@ func SaveDataToFDB(filePath string, data []byte, makeDir, createBackup bool) err
NumberOfWrites
=
NumberOfWrites
+
1
mutex
.
Unlock
()
// TODO: Compression is not commited
// if securityRequired {
// // encryptedData, encryptionError := securityhelper.AESEncryptDefault(data)
// keyBytes := GetKeyWithFileNameAndDefaultKey(filePath)
// compressedText, compressionError := securityhelper.Compress(data)
// if compressionError != nil {
// logginghelper.LogError("Error while compressing data of ", filePath, " : ", compressionError)
// return compressionError
// }
// encryptedData, encryptionError := securityhelper.AESEncrypt(compressedText, keyBytes)
// if encryptionError != nil {
// logginghelper.LogError("Error while encrypting data of ", filePath, " : ", encryptionError)
// return encryptionError
// }
// fileWriteError := filehelper.WriteFile(filePath, encryptedData)
// if fileWriteError != nil {
// logginghelper.LogError("Error while writing data to ", filePath, " : ", fileWriteError)
// }
// return fileWriteError
// // // encryptedData, encryptionError := securityhelper.AESEncryptDefault(data)
// keyBytes, hashError := GetKeyWithFileNameAndDefaultKey(filePath)
// if errormdl.CheckErr(hashError) != nil {
// return errormdl.CheckErr(hashError)
// }
// // compressedText, compressionError := securityhelper.Compress(data)
// // if compressionError != nil {
// // logginghelper.LogError("Error while compressing data of ", filePath, " : ", compressionError)
// // return compressionError
// // }
// encryptedData, encryptionError := securitymdl.AESEncrypt(compressedText, keyBytes)
// if errormdl.CheckErr1(encryptionError) != nil {
// return errormdl.CheckErr1(encryptionError)
// }
// saveError := filemdl.GetInstance().Save(filePath, encryptedData, makeDir, createBackup)
// if errormdl.CheckErr(saveError) != nil {
// return errormdl.CheckErr(saveError)
// }
// return saveError
// }
saveError
:=
filemdl
.
GetInstance
()
.
Save
(
filePath
,
data
,
makeDir
,
createBackup
)
if
errormdl
.
CheckErr
(
saveError
)
!=
nil
{
...
...
@@ -124,3 +148,114 @@ func SaveDataToFDB(filePath string, data []byte, makeDir, createBackup bool) err
// TODO: Delete file related entries from cacahe entries
return
nil
}
//EnableSecurity Set remote path for receiving files if not found at local
func
EnableSecurity
(
key
[]
byte
,
initializationVector
string
)
{
securitymdl
.
SetSecurityConfig
(
key
,
initializationVector
)
defaultSecurityKey
=
key
securityRequired
=
true
}
//DisableSecurity disables security
func
DisableSecurity
()
{
securityRequired
=
false
}
//GetSecurityStatus get status of security flag
func
GetSecurityStatus
()
bool
{
return
securityRequired
}
// // GetKeyWithFileNameAndDefaultKey generates key using file name + Default key
// func GetKeyWithFileNameAndDefaultKey(filePath string) ([]byte, error) {
// fileName := filepath.Base(filePath)
// fileNameBytes := []byte(fileName)
// fileNameBytes = append(fileNameBytes, defaultSecurityKey...)
// keyBytes, getHashError := hashmdl.Get128BitHash(fileNameBytes)
// if errormdl.CheckErr(getHashError) != nil {
// return nil, errormdl.CheckErr(getHashError)
// }
// return keyBytes[:], nil
// }
//AppendDataToFDB apppends data to FDB
func
AppendDataToFDB
(
filePath
string
,
data
[]
byte
,
createBackup
bool
)
error
{
saveError
:=
filemdl
.
GetInstance
()
.
Save
(
filePath
,
data
,
false
,
createBackup
)
return
saveError
}
// SoftDeleteFileFromFDB rename file as per timestamp
func
SoftDeleteFileFromFDB
(
filePath
string
)
error
{
t
:=
time
.
Now
()
newFilePath
:=
filePath
+
"_deleted_"
+
t
.
Format
(
"20060102150405"
)
err
:=
filemdl
.
RenameFile
(
filePath
,
newFilePath
)
return
err
}
// HardDeleteFileFromFDB permanantly delete file from fdb
func
HardDeleteFileFromFDB
(
filePath
string
)
error
{
err
:=
filemdl
.
DeleteFile
(
filePath
)
return
err
}
//GetDataFromFDBWithoutSecurity gets data from FDB
func
GetDataFromFDBWithoutSecurity
(
filePath
string
)
([]
byte
,
error
)
{
//Following checking will be done for the provided file path, which will let us know
// whether the file is Read only/Write only/Update only
mutex
.
Lock
()
NumberOfReadsWithoutSecurity
=
NumberOfReadsWithoutSecurity
+
1
mutex
.
Unlock
()
data
,
err
:=
filemdl
.
ReadFile
(
filePath
)
if
errormdl
.
CheckErr
(
err
)
!=
nil
{
if
!
downloadFromInternet
{
return
nil
,
errormdl
.
CheckErr
(
err
)
}
if
hostAddressForDataFiles
==
""
{
loggermdl
.
LogError
(
"Call dalhelper.SetRemoteHostPath(remoteHost) to set remote file path "
)
return
nil
,
errormdl
.
CheckErr
(
err
)
}
relativeFilePath
:=
strings
.
SplitAfter
(
filePath
,
splitString
)
remotePath
:=
hostAddressForDataFiles
+
relativeFilePath
[
1
]
resp
,
httpError
:=
http
.
Get
(
remotePath
)
if
httpError
!=
nil
||
(
resp
!=
nil
&&
resp
.
StatusCode
==
404
)
{
return
nil
,
httpError
}
data
,
readError
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
errormdl
.
CheckErr1
(
readError
)
!=
nil
{
loggermdl
.
LogError
(
"Error while reading data from response body "
,
readError
)
return
nil
,
errormdl
.
CheckErr1
(
readError
)
}
defer
resp
.
Body
.
Close
()
saveError
:=
SaveDataToFDB
(
filePath
,
data
,
true
,
false
)
return
data
,
saveError
}
return
data
,
err
}
//SaveDataToFDBWithoutSecurity saves data to FDB
func
SaveDataToFDBWithoutSecurity
(
filePath
string
,
data
[]
byte
,
makeDir
,
createBackup
bool
)
error
{
mutex
.
Lock
()
NumberOfWritesWithoutSecurity
=
NumberOfWritesWithoutSecurity
+
1
mutex
.
Unlock
()
saveError
:=
filemdl
.
GetInstance
()
.
Save
(
filePath
,
data
,
makeDir
,
createBackup
)
return
saveError
}
// EnableRemoteFileDownload Set remote address and enable file download from internet
func
EnableRemoteFileDownload
(
remoteHostAddress
,
pathSplitString
string
)
{
hostAddressForDataFiles
=
remoteHostAddress
splitString
=
pathSplitString
downloadFromInternet
=
true
}
// DisableRemoteFileDownload disables file download from internet
func
DisableRemoteFileDownload
()
{
hostAddressForDataFiles
=
""
splitString
=
""
downloadFromInternet
=
false
}
// GetRemoteFileDownloadStatus get status of file download from internet
func
GetRemoteFileDownloadStatus
()
bool
{
return
downloadFromInternet
}
This diff is collapsed.
Click to expand it.
dalmdl/fdb/fdb_test.go
+
101
−
1
View file @
fb06db03
...
...
@@ -5,6 +5,7 @@
package
fdb
import
(
"fmt"
"testing"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/coreospackage/dalhelper"
...
...
@@ -50,6 +51,7 @@ func TestGetDataDAO_WithCacheGetErrAndFileRead(t *testing.T) {
errormdl
.
IsTestingNegetiveCaseOnCheckBool
=
true
_
,
err
:=
GetDataDAO
(
"../../testingdata/users.json"
,
"#"
,
true
,
gjson
.
Result
{})
assert
.
NoError
(
t
,
err
,
"This should not return error"
)
errormdl
.
IsTestingNegetiveCaseOnCheckBool
=
false
}
func
TestSaveDataToFDB
(
t
*
testing
.
T
)
{
...
...
@@ -64,6 +66,104 @@ func Test1SaveDataToFDB(t *testing.T) {
errormdl
.
IsTestingNegetiveCaseOn
=
true
ba
,
_
:=
dalhelper
.
GetDataFromFDB
(
"../../testingdata/users.json"
)
err
:=
SaveDataToFDB
(
"../../testingdata/users.json"
,
ba
,
false
,
true
)
errormdl
.
IsTestingNegetiveCaseOn
=
tru
e
errormdl
.
IsTestingNegetiveCaseOn
=
fals
e
assert
.
Error
(
t
,
err
,
"This should return error"
)
}
func
Test2SaveDataToFDB
(
t
*
testing
.
T
)
{
Fastcache
.
Purge
()
ba
,
_
:=
getDataFromFDB
(
"../../testingdata/users.json"
)
EnableSecurity
([]
byte
(
"MKCLSecurity$#@!"
),
"AAAAAAAAAAAAAAAA"
)
err
:=
SaveDataToFDB
(
"../../testingdata/users_enc.json"
,
ba
,
true
,
false
)
DisableSecurity
()
fmt
.
Println
(
GetSecurityStatus
())
assert
.
NoError
(
t
,
err
,
"No Error Expected"
)
}
func
Test3SaveDataToFDB
(
t
*
testing
.
T
)
{
Fastcache
.
Purge
()
ba
,
_
:=
getDataFromFDB
(
"../../testingdata/users.json"
)
EnableSecurity
([]
byte
(
"MKCLSecurity$#@!"
),
"AAAAAAAAAAAAAAAA"
)
err
:=
SaveDataToFDB
(
"../../testingdata/users_enc1.json"
,
ba
,
true
,
false
)
DisableSecurity
()
fmt
.
Println
(
GetSecurityStatus
())
assert
.
NoError
(
t
,
err
,
"No Error Expected"
)
}
func
TestAppendDataToFDB
(
t
*
testing
.
T
)
{
Fastcache
.
Purge
()
ba
,
_
:=
getDataFromFDB
(
"../../testingdata/users.json"
)
err
:=
AppendDataToFDB
(
"../../testingdata/users_enc.json"
,
ba
,
false
)
assert
.
NoError
(
t
,
err
,
"No Error Expected"
)
}
func
TestSoftDeleteFileFromFDB
(
t
*
testing
.
T
)
{
Fastcache
.
Purge
()
err
:=
SoftDeleteFileFromFDB
(
"../../testingdata/users_enc.json"
)
assert
.
NoError
(
t
,
err
,
"No Error Expected"
)
}
func
TestHardDeleteFileFromFDB
(
t
*
testing
.
T
)
{
Fastcache
.
Purge
()
err
:=
HardDeleteFileFromFDB
(
"../../testingdata/users_enc1.json"
)
assert
.
NoError
(
t
,
err
,
"No Error Expected"
)
}
func
TestGetDataFromFDBWithoutSecurity
(
t
*
testing
.
T
)
{
Fastcache
.
Purge
()
_
,
err
:=
GetDataFromFDBWithoutSecurity
(
"../../testingdata/users.json"
)
assert
.
NoError
(
t
,
err
,
"No Error Expected"
)
}
func
Test1GetDataFromFDBWithoutSecurity
(
t
*
testing
.
T
)
{
Fastcache
.
Purge
()
errormdl
.
IsTestingNegetiveCaseOn
=
true
_
,
err
:=
GetDataFromFDBWithoutSecurity
(
"../../testingdata/users.json"
)
errormdl
.
IsTestingNegetiveCaseOn
=
false
assert
.
Error
(
t
,
err
,
"No Error Expected"
)
}
func
Test2GetDataFromFDBWithoutSecurity
(
t
*
testing
.
T
)
{
Fastcache
.
Purge
()
errormdl
.
IsTestingNegetiveCaseOn
=
true
EnableRemoteFileDownload
(
""
,
""
)
_
,
err
:=
GetDataFromFDBWithoutSecurity
(
"../../testingdata/users.json"
)
errormdl
.
IsTestingNegetiveCaseOn
=
false
assert
.
Error
(
t
,
err
,
"No Error Expected"
)
}
func
Test3GetDataFromFDBWithoutSecurity
(
t
*
testing
.
T
)
{
Fastcache
.
Purge
()
EnableRemoteFileDownload
(
"http://cdpcdn.mkcl.org/"
,
"testingdata"
)
_
,
err
:=
GetDataFromFDBWithoutSecurity
(
"../../testingdata/92/4d/f1/924df182524b2428080069888694a3a2.mp3"
)
DisableRemoteFileDownload
()
fmt
.
Println
(
GetRemoteFileDownloadStatus
())
assert
.
NoError
(
t
,
err
,
"No Error Expected"
)
}
func
Test4GetDataFromFDBWithoutSecurity
(
t
*
testing
.
T
)
{
Fastcache
.
Purge
()
EnableRemoteFileDownload
(
"http://cdpcdn1.mkcl.org/"
,
"testingdata"
)
_
,
err
:=
GetDataFromFDBWithoutSecurity
(
"../../testingdata/3b/23/e4/3b23e4f39eb4bd69b53cfefbe0c606fc.mp4"
)
DisableRemoteFileDownload
()
fmt
.
Println
(
GetRemoteFileDownloadStatus
())
assert
.
Error
(
t
,
err
,
"No Error Expected"
)
}
func
Test5GetDataFromFDBWithoutSecurity
(
t
*
testing
.
T
)
{
Fastcache
.
Purge
()
errormdl
.
IsTestingNegetiveCaseOn1
=
true
EnableRemoteFileDownload
(
"http://cdpcdn.mkcl.org/"
,
"testingdata"
)
_
,
err
:=
GetDataFromFDBWithoutSecurity
(
"../../testingdata/3b/23/e4/3b23e4f39eb4bd69b53cfefbe0c606fc.mp4"
)
DisableRemoteFileDownload
()
fmt
.
Println
(
GetRemoteFileDownloadStatus
())
errormdl
.
IsTestingNegetiveCaseOn1
=
false
assert
.
Error
(
t
,
err
,
"No Error Expected"
)
}
func
TestSaveDataToFDBWithoutSecurity
(
t
*
testing
.
T
)
{
Fastcache
.
Purge
()
ba
,
_
:=
getDataFromFDB
(
"../../testingdata/users.json"
)
err
:=
SaveDataToFDBWithoutSecurity
(
"../../testingdata/users.json"
,
ba
,
false
,
false
)
errormdl
.
IsTestingNegetiveCaseOn1
=
false
assert
.
NoError
(
t
,
err
,
"No Error Expected"
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets