From 7b59f3cead750d00bafe406ab2150f3abd189acb Mon Sep 17 00:00:00 2001
From: wujing <wujing50@huawei.com>
Date: Tue, 24 Nov 2020 17:09:08 +0800
Subject: [PATCH 06/17] using 64 bit unique token in CRI websockets server
 Request Cache

Signed-off-by: wujing <wujing50@huawei.com>
---
 src/daemon/entry/cri/request_cache.cc | 10 ++++++----
 src/daemon/entry/cri/request_cache.h  |  2 +-
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/daemon/entry/cri/request_cache.cc b/src/daemon/entry/cri/request_cache.cc
index 5209bcaf..a3cb3771 100644
--- a/src/daemon/entry/cri/request_cache.cc
+++ b/src/daemon/entry/cri/request_cache.cc
@@ -86,11 +86,13 @@ std::string RequestCache::UniqueToken()
     std::default_random_engine e1(r());
     std::uniform_int_distribution<int> uniform_dist(1, 254);
     // Number of bytes to be TokenLen when base64 encoded.
-    const int tokenSize { 16 };
-    char rawToken[tokenSize + 1] { 0 };
+    const int tokenSize = ceil(static_cast<double>(TokenLen) * 6 / 8);
+    char rawToken[tokenSize + 1];
+    (void)memset(rawToken, 0, sizeof(rawToken));
     for (int i {}; i < maxTries; ++i) {
-        char buf[40] { 0 };
-        for (size_t j {}; j < tokenSize; ++j) {
+        char buf[TokenLen + 1];
+        (void)memset(buf, 0, sizeof(buf));
+        for (int j {}; j < tokenSize; ++j) {
             rawToken[j] = (char)uniform_dist(e1);
         }
         lws_b64_encode_string(rawToken, (int)strlen(rawToken), buf, (int)sizeof(buf));
diff --git a/src/daemon/entry/cri/request_cache.h b/src/daemon/entry/cri/request_cache.h
index b0b7f491..024f3ba7 100644
--- a/src/daemon/entry/cri/request_cache.h
+++ b/src/daemon/entry/cri/request_cache.h
@@ -52,7 +52,7 @@ private:
     static std::mutex m_mutex;
     static std::atomic<RequestCache *> m_instance;
     const size_t MaxInFlight { 1000 };
-    const size_t TokenLen { 8 };
+    const size_t TokenLen { 64 };
 };
 
 #endif // DAEMON_ENTRY_CRI_REQUEST_CACHE_H
-- 
2.25.1

