From af365177d0983d65ddb8fe84218e397a772bc804 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Mon, 10 Jun 2019 12:46:37 -0400 Subject: [PATCH] PR feedback and a manual test --- src/lib/md/relay/QueryServer.cpp | 6 +- src/lib/utils/AuthSessionManager.cpp | 19 +++--- src/lib/utils/AuthToken.cpp | 1 - src/lib/utils/crypto/iostreambio.cpp | 97 +++++++++++++--------------- testing/manual/keycodes/keycode.txt | 40 ++++++++++++ 5 files changed, 97 insertions(+), 66 deletions(-) create mode 100644 testing/manual/keycodes/keycode.txt diff --git a/src/lib/md/relay/QueryServer.cpp b/src/lib/md/relay/QueryServer.cpp index 84f15b4c9..f0ede96a5 100644 --- a/src/lib/md/relay/QueryServer.cpp +++ b/src/lib/md/relay/QueryServer.cpp @@ -246,8 +246,8 @@ public: pupd->mutable_security()->CopyFrom(handle.get_normalized_security()); snapshot = true; } - if (!entitled) { - if(chan.entitled()) snapshot = true; + //if we are not entitled AND this is the first request since we lost our entitlement, zero out books + if (!entitled && chan.entitled() ) { if (HASFLAG(flags, MDRequest::BBO) || HASFLAG(flags, MDRequest::DIRECT)) { auto* p = pupd->mutable_direct(); for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { @@ -263,7 +263,7 @@ public: } } } - else { + if(entitled) { // If we are entitled now but this is the first request where we are ensure we send all values if(!chan.entitled()) snapshot = true; diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp index 012d180ab..c51f019fe 100644 --- a/src/lib/utils/AuthSessionManager.cpp +++ b/src/lib/utils/AuthSessionManager.cpp @@ -42,12 +42,7 @@ AuthSessionManager::AuthSessionManager() : _map(), _nusers(), _expiration() bts_assert_msg(valid, "Your BTS Keycode is invalid"); _nusers = kc.nusers(); - _expiration = kc.expiration(); - - auto current = datetime::ll_diff_in_us(datetime::ll_now(), _expiration) > 0; - if(!current) - LOG(Gen,Error, "Your BTS Keycode is expired"); - bts_assert_msg(current, "Your BTS Keycode is EXPIRED"); + _expiration = kc.expiration(); } @@ -56,6 +51,11 @@ bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& t { if(tok.wheel()) return true; + if(datetime::ll_diff_in_us(datetime::ll_now(), _expiration) < 0) + { + LOG(Gen,Error, "Your BTS Keycode is expired!"); + return false; + } auto it = _map.find(tok.id()); if(it != _map.end()) { @@ -72,11 +72,10 @@ bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& t bool AuthSessionManager::check_session(const api::user::TokenPayload& tok) { - auto now = datetime::ll_now(); - if(datetime::ll_diff_in_us(now, _expiration) > 0) + if(tok.wheel()) + return true; + if(datetime::ll_diff_in_us(datetime::ll_now(), _expiration) > 0) { - if(tok.wheel()) - return true; auto it = _map.find(tok.id()); if(it != _map.end()) { diff --git a/src/lib/utils/AuthToken.cpp b/src/lib/utils/AuthToken.cpp index 1a89bf903..5b4be4dbd 100644 --- a/src/lib/utils/AuthToken.cpp +++ b/src/lib/utils/AuthToken.cpp @@ -72,7 +72,6 @@ AuthToken generate_wheel_token() payload.set_date(datetime::ll_now()); payload.set_id(99999); payload.set_wheel(true); - session_manager().add_or_replace_session(payload); auto token = encrypt(payload.SerializeAsString()); AuthToken result; result.set_token(token); diff --git a/src/lib/utils/crypto/iostreambio.cpp b/src/lib/utils/crypto/iostreambio.cpp index 65aa218c2..9026a0c4c 100644 --- a/src/lib/utils/crypto/iostreambio.cpp +++ b/src/lib/utils/crypto/iostreambio.cpp @@ -56,7 +56,7 @@ static BIO_METHOD iostream_method = bwrite, bread, NULL,//bputs - bgets,//bgets + bgets, ctrl, create, NULL,//destroy, @@ -108,40 +108,38 @@ static long ctrl(BIO *bio, int cmd, long num, void *ptr) long ret; iostream *stream = reinterpret_cast(BIO_get_data(bio)); - //LOG(Gen, Error) << "iostreambio: ctrl called, cmd: " << cmd << ", num: " << num << "." ; + LOG(Gen, Warn, "iostreambio: ctrl called, cmd: {}, num: {}", cmd, num); switch(cmd){ case BIO_CTRL_PUSH: case BIO_CTRL_POP: - ret = 0; - break; - // case BIO_CTRL_GET_CLOSE: - // ret = bio->shutdown; - // break; - // case BIO_CTRL_SET_CLOSE: - // bio->shutdown = (int)num; - // ret = 1; - // break; + ret = 0; + break; +// case BIO_CTRL_GET_CLOSE: +// ret = bio->shutdown; +// break; +// case BIO_CTRL_SET_CLOSE: +// bio->shutdown = (int)num; +// ret = 1; +// break; case BIO_CTRL_FLUSH: - /* try to flush the underlying stream */ - ret = 1; - try{ - stream->flush(); - } catch(ios_base::failure &){ - /* ignore, the badbit is checked instead */ - } - // catch(ConnectionClosedException &ex){ - // throw; - // } - if(stream->bad()){ - //LOG(Gen, Error) << "iostreambio: underlying Stream went bad while flushing." ; - ret = 0; - } - break; - default: - //LOG(Gen, Error) << "iostreambio: ctrl called with unhandled cmd: " << cmd << "." ; + /* try to flush the underlying stream */ + ret = 1; + try + { + stream->flush(); + } + catch(ios_base::failure &){/* ignore, the badbit is checked instead */} + if(stream->bad()) + { + LOG(Gen, Error, "iostreambio: underlying Stream went bad while flushing."); ret = 0; - break; + } + break; + default: + LOG(Gen, Error, "iostreambio: ctrl called with unhandled cmd: {}", cmd); + ret = 0; + break; } return ret; @@ -215,33 +213,28 @@ static int bwrite(BIO *bio, const char *buf, int len) iostream *stream = reinterpret_cast(BIO_get_data(bio)); /* write the data */ - try{ - stream->write(buf, len); - } catch(ios_base::failure &){ - /* ignore, the badbit is checked instead */ - } - // catch(ConnectionClosedException &ex){ - // throw; - // } - if(stream->bad()){ - //LOG(Gen, Error) << "iostreambio: underlying Stream went bad while writing." ; - return 0; + try + { + stream->write(buf, len); + } + catch(ios_base::failure &){/* ignore, the badbit is checked instead */} + if(stream->bad()) + { + LOG(Gen, Error, "iostreambio: underlying Stream went bad while writing."); + return 0; } /* flush the underlying stream */ - try{ - stream->flush(); - } catch(ios_base::failure &){ - /* ignore, the badbit is checked instead */ + try + { + stream->flush(); + } + catch(ios_base::failure &) {/* ignore, the badbit is checked instead */} + if(stream->bad()) + { + LOG(Gen, Error, "iostreambio: underlying Stream went bad while flushing (bwrite)."); + return 0; } - // catch(ConnectionClosedException &ex){ - // throw; - // } - if(stream->bad()){ - //LOG(Gen, Error) << "iostreambio: underlying Stream went bad while flushing (bwrite)." ; - return 0; - } - return len; } diff --git a/testing/manual/keycodes/keycode.txt b/testing/manual/keycodes/keycode.txt new file mode 100644 index 000000000..8c955d293 --- /dev/null +++ b/testing/manual/keycodes/keycode.txt @@ -0,0 +1,40 @@ +Test Author: Kevin +If it breaks: Kevin + +This will test our ability to generate and enforce keycodes. + +1) Find spark_private.pem and spark_public.pem in bts\spark\src\lib\utils\crypto copy them to your sparkd binary location + +2) Now generate a keycode to a file: + +./sparkd keycode encode -keyfile:spark_private.pem -pass:BT5u53r -users:1 -expiration:2020-05-18 > test.pem + +3) Now decode the keycode + +./sparkd keycode decode -keyfile:spark_public.pem -source:test.pem + +You should see keycode details printed to screen + +4) Now test concurrent logins. Start a sparkd. + a) Start a spark -multiple login as bts and go to where you would see some market data + b) Start another spark -multiple but log in as bts_2 and display some data + c) Start a third spark -multiple, log in as bts_2 and display some data. + + -You should see the data zero out on the first instance logged in as bts_2 and data ticking on the bts instance as well as + the second bts_2 instance + + d)Kill your sparkd + + +5) Test expiration. Generate a keycode in the past: + +./sparkd keycode encode -keyfile:spark_private.pem -pass:BT5u53r -users:1 -expiration:2018-05-18 > ~/sparkdata/conf/keycode.conf + + a) start sparkd again. + b) Start a spark -multiple login as bts_2 and go to where you would see some market data. You should NOT see data + c) Start a spark -multiple login as bts, you should see data. + d) remove the expired keycode you just generated by: + rm ~/sparkdata/conf/keycode.conf + e) restart sparkd, you should see data as normal in bts_2 + +