From 0f6b623ef7406627e6a523e2f6e96f009db6985b Mon Sep 17 00:00:00 2001 From: henryzhx8 Date: Thu, 12 Dec 2024 07:07:32 +0000 Subject: [PATCH 1/3] support project anonymous write --- core/plugin/flusher/sls/FlusherSLS.cpp | 7 ++++ core/sdk/Client.cpp | 57 ++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/core/plugin/flusher/sls/FlusherSLS.cpp b/core/plugin/flusher/sls/FlusherSLS.cpp index 47977e47a6..67a87aa1ad 100644 --- a/core/plugin/flusher/sls/FlusherSLS.cpp +++ b/core/plugin/flusher/sls/FlusherSLS.cpp @@ -40,6 +40,9 @@ // TODO: temporarily used here #include "pipeline/PipelineManager.h" #include "plugin/flusher/sls/DiskBufferWriter.h" +#ifdef __ENTERPRISE__ +#include "plugin/flusher/sls/EnterpriseSLSClientManager.h" +#endif using namespace std; @@ -895,6 +898,10 @@ void FlusherSLS::OnSendDone(const HttpResponse& response, SenderQueueItem* item) } } SLSClientManager::GetInstance()->UpdateAccessKeyStatus(mAliuid, !hasAuthError); +#ifdef __ENTERPRISE__ + static auto manager = static_cast(SLSClientManager::GetInstance()); + manager->UpdateProjectAnonymousWriteStatus(mProject, !hasAuthError); +#endif } bool FlusherSLS::Send(string&& data, const string& shardHashKey, const string& logstore) { diff --git a/core/sdk/Client.cpp b/core/sdk/Client.cpp index c99d4dd71f..7b1d4e46dc 100644 --- a/core/sdk/Client.cpp +++ b/core/sdk/Client.cpp @@ -18,10 +18,16 @@ #include "CurlImp.h" #include "Exception.h" #include "Result.h" -#include "logger/Logger.h" -#include "plugin/flusher/sls/SLSClientManager.h" #include "app_config/AppConfig.h" +#include "common/Flags.h" +#include "logger/Logger.h" #include "monitor/Monitor.h" +#include "plugin/flusher/sls/SLSClientManager.h" +#ifdef __ENTERPRISE__ +#include "plugin/flusher/sls/EnterpriseSLSClientManager.h" +#endif + +DECLARE_FLAG_STRING(default_aliuid); namespace logtail { namespace sdk { @@ -214,7 +220,17 @@ namespace sdk { SLSClientManager::AuthType type; string accessKeyId, accessKeySecret; if (!SLSClientManager::GetInstance()->GetAccessKey(mAliuid, type, accessKeyId, accessKeySecret)) { - throw LOGException(LOGE_UNAUTHORIZED, ""); +#ifdef __ENTERPRISE__ + static auto manager = static_cast(SLSClientManager::GetInstance()); + auto status = manager->GetProjectAnonymousWriteStatus(project); + if (status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::INVALID + || status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::PENDDING) { + return throw LOGException(LOGE_UNAUTHORIZED, ""); + ; + } + SLSClientManager::GetInstance()->GetAccessKey( + STRING_FLAG(default_aliuid), type, accessKeyId, accessKeySecret); +#endif } if (type == SLSClientManager::AuthType::ANONYMOUS) { header[X_LOG_KEYPROVIDER] = MD5_SHA1_SALT_KEYPROVIDER; @@ -232,8 +248,17 @@ namespace sdk { if (mPort == 80 && mUsingHTTPS) { port = 443; } - mClient->Send( - httpMethod, host, port, url, queryString, header, body, mTimeout, httpMessage, AppConfig::GetInstance()->GetBindInterface(), mUsingHTTPS); + mClient->Send(httpMethod, + host, + port, + url, + queryString, + header, + body, + mTimeout, + httpMessage, + AppConfig::GetInstance()->GetBindInterface(), + mUsingHTTPS); if (httpMessage.statusCode != 200) { if (realIpPtr != NULL) { @@ -252,7 +277,16 @@ namespace sdk { SLSClientManager::AuthType type; string accessKeyId, accessKeySecret; if (!SLSClientManager::GetInstance()->GetAccessKey(mAliuid, type, accessKeyId, accessKeySecret)) { - return nullptr; +#ifdef __ENTERPRISE__ + static auto manager = static_cast(SLSClientManager::GetInstance()); + auto status = manager->GetProjectAnonymousWriteStatus(project); + if (status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::INVALID + || status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::PENDDING) { + return nullptr; + } + SLSClientManager::GetInstance()->GetAccessKey( + STRING_FLAG(default_aliuid), type, accessKeyId, accessKeySecret); +#endif } if (type == SLSClientManager::AuthType::ANONYMOUS) { httpHeader[X_LOG_KEYPROVIDER] = MD5_SHA1_SALT_KEYPROVIDER; @@ -280,7 +314,16 @@ namespace sdk { SLSClientManager::AuthType type; string accessKeyId, accessKeySecret; if (!SLSClientManager::GetInstance()->GetAccessKey(mAliuid, type, accessKeyId, accessKeySecret)) { - return nullptr; +#ifdef __ENTERPRISE__ + static auto manager = static_cast(SLSClientManager::GetInstance()); + auto status = manager->GetProjectAnonymousWriteStatus(project); + if (status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::INVALID + || status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::PENDDING) { + return nullptr; + } + SLSClientManager::GetInstance()->GetAccessKey( + STRING_FLAG(default_aliuid), type, accessKeyId, accessKeySecret); +#endif } if (type == SLSClientManager::AuthType::ANONYMOUS) { httpHeader[X_LOG_KEYPROVIDER] = MD5_SHA1_SALT_KEYPROVIDER; From 109d5c947bdab36a36f327fb371e4037f8b06896 Mon Sep 17 00:00:00 2001 From: henryzhx8 Date: Thu, 12 Dec 2024 07:22:04 +0000 Subject: [PATCH 2/3] polish --- core/sdk/Client.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/sdk/Client.cpp b/core/sdk/Client.cpp index 7b1d4e46dc..f439f141b4 100644 --- a/core/sdk/Client.cpp +++ b/core/sdk/Client.cpp @@ -224,7 +224,7 @@ namespace sdk { static auto manager = static_cast(SLSClientManager::GetInstance()); auto status = manager->GetProjectAnonymousWriteStatus(project); if (status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::INVALID - || status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::PENDDING) { + || status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::PENDING) { return throw LOGException(LOGE_UNAUTHORIZED, ""); ; } @@ -281,7 +281,7 @@ namespace sdk { static auto manager = static_cast(SLSClientManager::GetInstance()); auto status = manager->GetProjectAnonymousWriteStatus(project); if (status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::INVALID - || status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::PENDDING) { + || status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::PENDING) { return nullptr; } SLSClientManager::GetInstance()->GetAccessKey( @@ -318,7 +318,7 @@ namespace sdk { static auto manager = static_cast(SLSClientManager::GetInstance()); auto status = manager->GetProjectAnonymousWriteStatus(project); if (status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::INVALID - || status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::PENDDING) { + || status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::PENDING) { return nullptr; } SLSClientManager::GetInstance()->GetAccessKey( From 0fa2cd2799a1ecc52417a18c4417d9d073559f2f Mon Sep 17 00:00:00 2001 From: henryzhx8 Date: Fri, 13 Dec 2024 08:02:31 +0000 Subject: [PATCH 3/3] polish --- core/sdk/Client.cpp | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/core/sdk/Client.cpp b/core/sdk/Client.cpp index f439f141b4..acfc49f95c 100644 --- a/core/sdk/Client.cpp +++ b/core/sdk/Client.cpp @@ -27,8 +27,6 @@ #include "plugin/flusher/sls/EnterpriseSLSClientManager.h" #endif -DECLARE_FLAG_STRING(default_aliuid); - namespace logtail { namespace sdk { @@ -221,15 +219,10 @@ namespace sdk { string accessKeyId, accessKeySecret; if (!SLSClientManager::GetInstance()->GetAccessKey(mAliuid, type, accessKeyId, accessKeySecret)) { #ifdef __ENTERPRISE__ - static auto manager = static_cast(SLSClientManager::GetInstance()); - auto status = manager->GetProjectAnonymousWriteStatus(project); - if (status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::INVALID - || status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::PENDING) { - return throw LOGException(LOGE_UNAUTHORIZED, ""); - ; + static auto* manager = static_cast(SLSClientManager::GetInstance()); + if (!manager->GetAccessKeyIfProjectSupportsAnonymousWrite(project, type, accessKeyId, accessKeySecret)) { + throw LOGException(LOGE_UNAUTHORIZED, ""); } - SLSClientManager::GetInstance()->GetAccessKey( - STRING_FLAG(default_aliuid), type, accessKeyId, accessKeySecret); #endif } if (type == SLSClientManager::AuthType::ANONYMOUS) { @@ -278,14 +271,10 @@ namespace sdk { string accessKeyId, accessKeySecret; if (!SLSClientManager::GetInstance()->GetAccessKey(mAliuid, type, accessKeyId, accessKeySecret)) { #ifdef __ENTERPRISE__ - static auto manager = static_cast(SLSClientManager::GetInstance()); - auto status = manager->GetProjectAnonymousWriteStatus(project); - if (status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::INVALID - || status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::PENDING) { + static auto* manager = static_cast(SLSClientManager::GetInstance()); + if (!manager->GetAccessKeyIfProjectSupportsAnonymousWrite(project, type, accessKeyId, accessKeySecret)) { return nullptr; } - SLSClientManager::GetInstance()->GetAccessKey( - STRING_FLAG(default_aliuid), type, accessKeyId, accessKeySecret); #endif } if (type == SLSClientManager::AuthType::ANONYMOUS) { @@ -315,14 +304,10 @@ namespace sdk { string accessKeyId, accessKeySecret; if (!SLSClientManager::GetInstance()->GetAccessKey(mAliuid, type, accessKeyId, accessKeySecret)) { #ifdef __ENTERPRISE__ - static auto manager = static_cast(SLSClientManager::GetInstance()); - auto status = manager->GetProjectAnonymousWriteStatus(project); - if (status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::INVALID - || status == EnterpriseSLSClientManager::ProjectAnonymousWriteStatus::PENDING) { + static auto* manager = static_cast(SLSClientManager::GetInstance()); + if (!manager->GetAccessKeyIfProjectSupportsAnonymousWrite(project, type, accessKeyId, accessKeySecret)) { return nullptr; } - SLSClientManager::GetInstance()->GetAccessKey( - STRING_FLAG(default_aliuid), type, accessKeyId, accessKeySecret); #endif } if (type == SLSClientManager::AuthType::ANONYMOUS) {