Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
Practical-Cryptdb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Zhaozhen
Practical-Cryptdb
Commits
4621b5dc
Commit
4621b5dc
authored
Nov 08, 2017
by
Casualet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add
parent
b8b07031
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
81 additions
and
30 deletions
+81
-30
Analysis.cc
main/Analysis.cc
+1
-2
Analysis.hh
main/Analysis.hh
+9
-7
rewrite_main.cc
main/rewrite_main.cc
+7
-8
ConnectWrapper.cc
mysqlproxy/ConnectWrapper.cc
+40
-12
Makefrag
mysqlproxy/Makefrag
+1
-1
lua_related.cc
mysqlproxy/lua_related.cc
+23
-0
No files found.
main/Analysis.cc
View file @
4621b5dc
...
@@ -892,8 +892,7 @@ std::string Analysis::getAnonIndexName(const TableMeta &tm,
...
@@ -892,8 +892,7 @@ std::string Analysis::getAnonIndexName(const TableMeta &tm,
}
}
bool
Analysis
::
isAlias
(
const
std
::
string
&
db
,
bool
Analysis
::
isAlias
(
const
std
::
string
&
db
,
const
std
::
string
&
table
)
const
const
std
::
string
&
table
)
const
{
{
auto
db_alias_pair
=
table_aliases
.
find
(
db
);
auto
db_alias_pair
=
table_aliases
.
find
(
db
);
if
(
table_aliases
.
end
()
==
db_alias_pair
)
{
if
(
table_aliases
.
end
()
==
db_alias_pair
)
{
return
false
;
return
false
;
...
...
main/Analysis.hh
View file @
4621b5dc
...
@@ -257,12 +257,15 @@ private:
...
@@ -257,12 +257,15 @@ private:
class
RewritePlan
;
class
RewritePlan
;
/*
*1.A set of functions for manipulating metadata at high level.
*/
class
Analysis
{
class
Analysis
{
Analysis
()
=
delete
;
Analysis
()
=
delete
;
Analysis
(
Analysis
&&
a
)
=
delete
;
Analysis
(
Analysis
&&
a
)
=
delete
;
Analysis
&
operator
=
(
const
Analysis
&
a
)
=
delete
;
Analysis
&
operator
=
(
const
Analysis
&
a
)
=
delete
;
Analysis
&
operator
=
(
Analysis
&&
a
)
=
delete
;
Analysis
&
operator
=
(
Analysis
&&
a
)
=
delete
;
public
:
public
:
Analysis
(
const
std
::
string
&
default_db
,
const
SchemaInfo
&
schema
,
Analysis
(
const
std
::
string
&
default_db
,
const
SchemaInfo
&
schema
,
const
std
::
unique_ptr
<
AES_KEY
>
&
master_key
,
const
std
::
unique_ptr
<
AES_KEY
>
&
master_key
,
...
@@ -276,14 +279,13 @@ public:
...
@@ -276,14 +279,13 @@ public:
master_key
(
analysis
.
getMasterKey
()),
master_key
(
analysis
.
getMasterKey
()),
default_sec_rating
(
analysis
.
getDefaultSecurityRating
())
{}
default_sec_rating
(
analysis
.
getDefaultSecurityRating
())
{}
unsigned
int
pos
;
// a counter indicating how many projection
/*a counter indicating how many projection fields have been analyzed so far*/
// fields have been analyzed so far
unsigned
int
pos
;
/
/each field may or may not has a salt field
/
*each field may or may not has a salt field*/
std
::
map
<
const
FieldMeta
*
,
const
salt_type
>
salts
;
std
::
map
<
const
FieldMeta
*
,
const
salt_type
>
salts
;
/
/each Item has a rewrite plain, which lists possible way to encrypt this item, that is OLKs!
/
*each Item has a rewrite plain, which lists possible way to encrypt this item, that is OLKs!*/
std
::
map
<
const
Item
*
,
std
::
unique_ptr
<
RewritePlan
>
>
rewritePlans
;
std
::
map
<
const
Item
*
,
std
::
unique_ptr
<
RewritePlan
>
>
rewritePlans
;
std
::
map
<
std
::
string
,
std
::
map
<
const
std
::
string
,
const
std
::
string
>>
std
::
map
<
std
::
string
,
std
::
map
<
const
std
::
string
,
const
std
::
string
>>
table_aliases
;
table_aliases
;
std
::
map
<
const
Item_field
*
,
std
::
pair
<
Item_field
*
,
OLK
>>
item_cache
;
std
::
map
<
const
Item_field
*
,
std
::
pair
<
Item_field
*
,
OLK
>>
item_cache
;
// information for decrypting results
// information for decrypting results
...
...
main/rewrite_main.cc
View file @
4621b5dc
...
@@ -1288,6 +1288,10 @@ static std::string serilize_OnionAdjustExcept(OnionAdjustExcept &e){
...
@@ -1288,6 +1288,10 @@ static std::string serilize_OnionAdjustExcept(OnionAdjustExcept &e){
// NOTE : This will probably choke on multidatabase queries.
// NOTE : This will probably choke on multidatabase queries.
/*
*parse the query, rewrite the query using handlers and then return an executor.
*possibly trigger an onion adjustment.
*/
AbstractQueryExecutor
*
AbstractQueryExecutor
*
Rewriter
::
dispatchOnLex
(
Analysis
&
a
,
const
std
::
string
&
query
)
Rewriter
::
dispatchOnLex
(
Analysis
&
a
,
const
std
::
string
&
query
)
{
{
...
@@ -1345,7 +1349,6 @@ Rewriter::dispatchOnLex(Analysis &a, const std::string &query)
...
@@ -1345,7 +1349,6 @@ Rewriter::dispatchOnLex(Analysis &a, const std::string &query)
out_data
=
adjustOnion
(
a
,
e
.
o
,
e
.
tm
,
e
.
fm
,
e
.
tolevel
);
out_data
=
adjustOnion
(
a
,
e
.
o
,
e
.
tm
,
e
.
fm
,
e
.
tolevel
);
std
::
string
resadjust
=
serilize_OnionAdjustExcept
(
e
);
std
::
string
resadjust
=
serilize_OnionAdjustExcept
(
e
);
std
::
cout
<<
"###################################################**************************************"
<<
resadjust
<<
std
::
endl
;
std
::
vector
<
std
::
unique_ptr
<
Delta
>
>
&
deltas
=
out_data
.
first
;
std
::
vector
<
std
::
unique_ptr
<
Delta
>
>
&
deltas
=
out_data
.
first
;
const
std
::
list
<
std
::
string
>
&
adjust_queries
=
out_data
.
second
;
const
std
::
list
<
std
::
string
>
&
adjust_queries
=
out_data
.
second
;
return
new
OnionAdjustmentExecutor
(
std
::
move
(
deltas
),
return
new
OnionAdjustmentExecutor
(
std
::
move
(
deltas
),
...
@@ -1360,16 +1363,12 @@ Rewriter::dispatchOnLex(Analysis &a, const std::string &query)
...
@@ -1360,16 +1363,12 @@ Rewriter::dispatchOnLex(Analysis &a, const std::string &query)
QueryRewrite
QueryRewrite
Rewriter
::
rewrite
(
const
std
::
string
&
q
,
const
SchemaInfo
&
schema
,
Rewriter
::
rewrite
(
const
std
::
string
&
q
,
const
SchemaInfo
&
schema
,
const
std
::
string
&
default_db
,
const
ProxyState
&
ps
)
const
std
::
string
&
default_db
,
const
ProxyState
&
ps
){
{
//LOG(cdb_v) << "q " << q;
LOG
(
cdb_v
)
<<
"q "
<<
q
;
assert
(
0
==
mysql_thread_init
());
assert
(
0
==
mysql_thread_init
());
Analysis
analysis
(
default_db
,
schema
,
ps
.
getMasterKey
(),
Analysis
analysis
(
default_db
,
schema
,
ps
.
getMasterKey
(),
ps
.
defaultSecurityRating
());
ps
.
defaultSecurityRating
());
// NOTE: Care what data you try to read from Analysis
// at this height.
AbstractQueryExecutor
*
const
executor
=
AbstractQueryExecutor
*
const
executor
=
Rewriter
::
dispatchOnLex
(
analysis
,
q
);
Rewriter
::
dispatchOnLex
(
analysis
,
q
);
if
(
!
executor
)
{
if
(
!
executor
)
{
...
@@ -1385,9 +1384,9 @@ std::string ReturnField::stringify() {
...
@@ -1385,9 +1384,9 @@ std::string ReturnField::stringify() {
res
<<
" is_salt: "
<<
is_salt
<<
" filed_called "
<<
field_called
;
res
<<
" is_salt: "
<<
is_salt
<<
" filed_called "
<<
field_called
;
res
<<
" fm "
<<
olk
.
key
<<
" onion "
<<
olk
.
o
;
res
<<
" fm "
<<
olk
.
key
<<
" onion "
<<
olk
.
o
;
res
<<
" salt_pos "
<<
salt_pos
;
res
<<
" salt_pos "
<<
salt_pos
;
return
res
.
str
();
return
res
.
str
();
}
}
std
::
string
ReturnMeta
::
stringify
()
{
std
::
string
ReturnMeta
::
stringify
()
{
std
::
stringstream
res
;
std
::
stringstream
res
;
res
<<
"rmeta contains "
<<
rfmeta
.
size
()
<<
" elements:
\n
"
;
res
<<
"rmeta contains "
<<
rfmeta
.
size
()
<<
" elements:
\n
"
;
...
...
mysqlproxy/ConnectWrapper.cc
View file @
4621b5dc
...
@@ -149,6 +149,22 @@ disconnect(lua_State *const L) {
...
@@ -149,6 +149,22 @@ disconnect(lua_State *const L) {
return
0
;
return
0
;
}
}
/*
*Input: client name, query, thread_id
*client name is used for fetching the proxystate, plan query for rewritting, and thread_id for
*fetching the default db
*Functionality: rewrite the query and get the executor.
There are different types of query:
*SELECT ...:
*INSERT INTO ...:
*DELETE ...:
*UPDATE ...:
*SET ...:
*
*Output: status code. Store QueryRewrite in the wrapperstate
*/
static
int
static
int
rewrite
(
lua_State
*
const
L
)
{
rewrite
(
lua_State
*
const
L
)
{
// ANON_REGION(__func__, &perf_cg);
// ANON_REGION(__func__, &perf_cg);
...
@@ -169,7 +185,7 @@ rewrite(lua_State *const L) {
...
@@ -169,7 +185,7 @@ rewrite(lua_State *const L) {
const
std
::
string
&
query
=
xlua_tolstring
(
L
,
2
);
const
std
::
string
&
query
=
xlua_tolstring
(
L
,
2
);
const
unsigned
long
long
_thread_id
=
const
unsigned
long
long
_thread_id
=
strtoull
(
xlua_tolstring
(
L
,
3
).
c_str
(),
NULL
,
10
);
strtoull
(
xlua_tolstring
(
L
,
3
).
c_str
(),
NULL
,
10
);
//this is not used??
c_wrapper
->
last_query
=
query
;
c_wrapper
->
last_query
=
query
;
if
(
EXECUTE_QUERIES
)
{
if
(
EXECUTE_QUERIES
)
{
try
{
try
{
...
@@ -177,7 +193,7 @@ rewrite(lua_State *const L) {
...
@@ -177,7 +193,7 @@ rewrite(lua_State *const L) {
&
c_wrapper
->
default_db
),
&
c_wrapper
->
default_db
),
"proxy failed to retrieve default database!"
);
"proxy failed to retrieve default database!"
);
// save a reference so a second thread won't eat objects
// save a reference so a second thread won't eat objects
// that DeltaOuput wants later
// that DeltaOuput wants later
(a shared_ptr for the schemaInfo)
const
std
::
shared_ptr
<
const
SchemaInfo
>
&
schema
=
const
std
::
shared_ptr
<
const
SchemaInfo
>
&
schema
=
ps
->
getSchemaInfo
();
ps
->
getSchemaInfo
();
c_wrapper
->
schema_info_refs
.
push_back
(
schema
);
c_wrapper
->
schema_info_refs
.
push_back
(
schema
);
...
@@ -185,8 +201,10 @@ rewrite(lua_State *const L) {
...
@@ -185,8 +201,10 @@ rewrite(lua_State *const L) {
//parse, rewrite, delta, adjust, returnMeta,
//parse, rewrite, delta, adjust, returnMeta,
std
::
unique_ptr
<
QueryRewrite
>
qr
=
std
::
unique_ptr
<
QueryRewrite
>
qr
=
std
::
unique_ptr
<
QueryRewrite
>
(
new
QueryRewrite
(
std
::
unique_ptr
<
QueryRewrite
>
(
new
QueryRewrite
(
Rewriter
::
rewrite
(
query
,
*
schema
.
get
(),
Rewriter
::
rewrite
(
query
,
c_wrapper
->
default_db
,
*
ps
)));
*
schema
.
get
(),
c_wrapper
->
default_db
,
*
ps
)));
assert
(
qr
);
assert
(
qr
);
c_wrapper
->
setQueryRewrite
(
std
::
move
(
qr
));
c_wrapper
->
setQueryRewrite
(
std
::
move
(
qr
));
}
catch
(
const
AbstractException
&
e
)
{
}
catch
(
const
AbstractException
&
e
)
{
...
@@ -285,8 +303,6 @@ getResTypeFromLuaTable(lua_State *const L, int fields_index,
...
@@ -285,8 +303,6 @@ getResTypeFromLuaTable(lua_State *const L, int fields_index,
lua_pop
(
L
,
1
);
lua_pop
(
L
,
1
);
}
}
//printrawReturnValue(myRawFromLua);
return
ResType
(
status
,
lua_tointeger
(
L
,
affected_rows_index
),
return
ResType
(
status
,
lua_tointeger
(
L
,
affected_rows_index
),
lua_tointeger
(
L
,
insert_id_index
),
std
::
move
(
names
),
lua_tointeger
(
L
,
insert_id_index
),
std
::
move
(
names
),
std
::
move
(
types
),
std
::
move
(
rows
));
std
::
move
(
types
),
std
::
move
(
rows
));
...
@@ -307,21 +323,26 @@ nilBuffer(lua_State *const L, size_t count)
...
@@ -307,21 +323,26 @@ nilBuffer(lua_State *const L, size_t count)
* */
* */
static
void
static
void
parseReturnMeta
(
const
ReturnMeta
&
rtm
){
parseReturnMeta
(
const
ReturnMeta
&
rtm
){
}
}
/*
*Input: client
*/
static
int
static
int
next
(
lua_State
*
const
L
)
{
next
(
lua_State
*
const
L
)
{
/*only one client can call next at a time*/
scoped_lock
l
(
&
big_lock
);
scoped_lock
l
(
&
big_lock
);
assert
(
0
==
mysql_thread_init
());
assert
(
0
==
mysql_thread_init
());
//查找client
const
std
::
string
client
=
xlua_tolstring
(
L
,
1
);
const
std
::
string
client
=
xlua_tolstring
(
L
,
1
);
if
(
clients
.
find
(
client
)
==
clients
.
end
())
{
if
(
clients
.
find
(
client
)
==
clients
.
end
())
{
xlua_pushlstring
(
L
,
"error"
);
xlua_pushlstring
(
L
,
"error"
);
xlua_pushlstring
(
L
,
"unknown client"
);
xlua_pushlstring
(
L
,
"unknown client"
);
lua_pushinteger
(
L
,
100
);
lua_pushinteger
(
L
,
100
);
xlua_pushlstring
(
L
,
"12345"
);
xlua_pushlstring
(
L
,
"12345"
);
nilBuffer
(
L
,
1
);
nilBuffer
(
L
,
1
);
return
5
;
return
5
;
}
}
...
@@ -332,13 +353,17 @@ next(lua_State *const L) {
...
@@ -332,13 +353,17 @@ next(lua_State *const L) {
ProxyState
*
const
ps
=
thread_ps
=
c_wrapper
->
ps
.
get
();
ProxyState
*
const
ps
=
thread_ps
=
c_wrapper
->
ps
.
get
();
assert
(
ps
);
assert
(
ps
);
/*???*/
ps
->
safeCreateEmbeddedTHD
();
ps
->
safeCreateEmbeddedTHD
();
const
ResType
&
res
=
getResTypeFromLuaTable
(
L
,
2
,
3
,
4
,
5
,
6
);
const
ResType
&
res
=
getResTypeFromLuaTable
(
L
,
2
,
3
,
4
,
5
,
6
);
const
std
::
unique_ptr
<
QueryRewrite
>
&
qr
=
c_wrapper
->
getQueryRewrite
();
const
std
::
unique_ptr
<
QueryRewrite
>
&
qr
=
c_wrapper
->
getQueryRewrite
();
parseReturnMeta
(
qr
->
rmeta
);
parseReturnMeta
(
qr
->
rmeta
);
try
{
try
{
NextParams
nparams
(
*
ps
,
c_wrapper
->
default_db
,
c_wrapper
->
last_query
);
NextParams
nparams
(
*
ps
,
c_wrapper
->
default_db
,
c_wrapper
->
last_query
);
...
@@ -352,7 +377,7 @@ next(lua_State *const L) {
...
@@ -352,7 +377,7 @@ next(lua_State *const L) {
// > a given killzone will only apply to the next query translation
// > a given killzone will only apply to the next query translation
c_wrapper
->
setKillZone
(
qr
->
kill_zone
);
c_wrapper
->
setKillZone
(
qr
->
kill_zone
);
}
}
switch
(
result_type
)
{
switch
(
result_type
){
case
AbstractQueryExecutor
:
:
ResultType
::
QUERY_COME_AGAIN
:
{
case
AbstractQueryExecutor
:
:
ResultType
::
QUERY_COME_AGAIN
:
{
// more to do before we have the client's results
// more to do before we have the client's results
xlua_pushlstring
(
L
,
"again"
);
xlua_pushlstring
(
L
,
"again"
);
...
@@ -389,14 +414,14 @@ next(lua_State *const L) {
...
@@ -389,14 +414,14 @@ next(lua_State *const L) {
// lua_pop(L, lua_gettop(L));
// lua_pop(L, lua_gettop(L));
xlua_pushlstring
(
L
,
"error"
);
xlua_pushlstring
(
L
,
"error"
);
xlua_pushlstring
(
L
,
e
.
getMessage
());
xlua_pushlstring
(
L
,
e
.
getMessage
());
lua_pushinteger
(
L
,
e
.
getErrorCode
());
lua_pushinteger
(
L
,
e
.
getErrorCode
());
xlua_pushlstring
(
L
,
e
.
getSQLState
());
xlua_pushlstring
(
L
,
e
.
getSQLState
());
nilBuffer
(
L
,
1
);
nilBuffer
(
L
,
1
);
return
5
;
return
5
;
}
}
}
}
static
void
static
void
returnResultSet
(
lua_State
*
const
L
,
const
ResType
&
rd
)
{
returnResultSet
(
lua_State
*
const
L
,
const
ResType
&
rd
)
{
TEST_GenericPacketException
(
true
==
rd
.
ok
,
"something bad happened"
);
TEST_GenericPacketException
(
true
==
rd
.
ok
,
"something bad happened"
);
...
@@ -441,6 +466,9 @@ returnResultSet(lua_State *const L, const ResType &rd) {
...
@@ -441,6 +466,9 @@ returnResultSet(lua_State *const L, const ResType &rd) {
return
;
return
;
}
}
/*Init the lua library*/
static
const
struct
luaL_reg
static
const
struct
luaL_reg
cryptdb_lib
[]
=
{
cryptdb_lib
[]
=
{
#define F(n) { #n, n }
#define F(n) { #n, n }
...
...
mysqlproxy/Makefrag
View file @
4621b5dc
OBJDIRS += mysqlproxy
OBJDIRS += mysqlproxy
PROXY_SRCS := ConnectWrapper.cc
PROXY_SRCS := ConnectWrapper.cc
lua_related.cc
PROXY_OBJS := $(patsubst %.cc,$(OBJDIR)/mysqlproxy/%.o,$(PROXY_SRCS))
PROXY_OBJS := $(patsubst %.cc,$(OBJDIR)/mysqlproxy/%.o,$(PROXY_SRCS))
all: $(OBJDIR)/libexecute.so
all: $(OBJDIR)/libexecute.so
...
...
mysqlproxy/lua_related.cc
0 → 100644
View file @
4621b5dc
#include <lua5.1/lua.hpp>
#include <string>
std
::
string
xlua_tolstring
(
lua_State
*
const
,
int
);
void
xlua_pushlstring
(
lua_State
*
const
,
const
std
::
string
&
);
std
::
string
xlua_tolstring
(
lua_State
*
const
l
,
int
index
){
size_t
len
;
char
const
*
const
s
=
lua_tolstring
(
l
,
index
,
&
len
);
return
std
::
string
(
s
,
len
);
}
void
xlua_pushlstring
(
lua_State
*
const
l
,
const
std
::
string
&
s
){
lua_pushlstring
(
l
,
s
.
data
(),
s
.
length
());
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment