Skip to content

Commit 3dd67fb

Browse files
authored
[Ldap] Improve parse hosts (#6864)
1 parent 1e6b6cb commit 3dd67fb

File tree

6 files changed

+305
-192
lines changed

6 files changed

+305
-192
lines changed

ydb/core/security/ldap_auth_provider/ldap_auth_provider.cpp

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <ydb/library/actors/core/log.h>
33
#include <ydb/core/base/ticket_parser.h>
44
#include <ydb/core/security/ticket_parser_log.h>
5+
#include <ydb/core/util/address_classifier.h>
56
#include <queue>
67
#include "ldap_auth_provider.h"
78
#include "ldap_utils.h"
@@ -70,6 +71,7 @@ class TLdapAuthProvider : public NActors::TActorBootstrapped<TLdapAuthProvider>
7071
TLdapAuthProvider(const NKikimrProto::TLdapAuthentication& settings)
7172
: Settings(settings)
7273
, FilterCreator(Settings)
74+
, UrisCreator(Settings, Settings.GetPort() != 0 ? Settings.GetPort() : NKikimrLdap::GetPort(Settings.GetScheme()))
7375
{
7476
const TString& requestedGroupAttribute = Settings.GetRequestedGroupAttribute();
7577
RequestedAttributes[0] = const_cast<char*>(requestedGroupAttribute.empty() ? "memberOf" : requestedGroupAttribute.c_str());
@@ -186,7 +188,7 @@ class TLdapAuthProvider : public NActors::TActorBootstrapped<TLdapAuthProvider>
186188
result = NKikimrLdap::Bind(*ld, Settings.GetBindDn(), Settings.GetBindPassword());
187189
if (!NKikimrLdap::IsSuccess(result)) {
188190
TEvLdapAuthProvider::TError error {
189-
.Message = "Could not perform initial LDAP bind for dn " + Settings.GetBindDn() + " on server " + UrisList + "\n"
191+
.Message = "Could not perform initial LDAP bind for dn " + Settings.GetBindDn() + " on server " + UrisCreator.GetUris() + "\n"
190192
+ NKikimrLdap::ErrorToString(result),
191193
.Retryable = NKikimrLdap::IsRetryableError(result)
192194
};
@@ -215,12 +217,10 @@ class TLdapAuthProvider : public NActors::TActorBootstrapped<TLdapAuthProvider>
215217
}
216218
}
217219

218-
const ui32 port = Settings.GetPort() != 0 ? Settings.GetPort() : NKikimrLdap::GetPort(Settings.GetScheme());
219-
UrisList = GetUris(port);
220-
result = NKikimrLdap::Init(ld, Settings.GetScheme(), UrisList, port);
220+
result = NKikimrLdap::Init(ld, Settings.GetScheme(), UrisCreator.GetUris(), UrisCreator.GetConfiguredPort());
221221
if (!NKikimrLdap::IsSuccess(result)) {
222222
return {{TEvLdapAuthProvider::EStatus::UNAVAILABLE,
223-
{.Message = "Could not initialize LDAP connection for uris: " + UrisList + ". " + NKikimrLdap::LdapError(*ld),
223+
{.Message = "Could not initialize LDAP connection for uris: " + UrisCreator.GetUris() + ". " + NKikimrLdap::LdapError(*ld),
224224
.Retryable = false}}};
225225
}
226226

@@ -250,14 +250,14 @@ class TLdapAuthProvider : public NActors::TActorBootstrapped<TLdapAuthProvider>
250250
char* dn = NKikimrLdap::GetDn(*request.Ld, request.Entry);
251251
if (dn == nullptr) {
252252
return {{TEvLdapAuthProvider::EStatus::UNAUTHORIZED,
253-
{.Message = "Could not get dn for the first entry matching " + FilterCreator.GetFilter(request.Login) + " on server " + UrisList + "\n"
253+
{.Message = "Could not get dn for the first entry matching " + FilterCreator.GetFilter(request.Login) + " on server " + UrisCreator.GetUris() + "\n"
254254
+ NKikimrLdap::LdapError(*request.Ld),
255255
.Retryable = false}}};
256256
}
257257
TEvLdapAuthProvider::TError error;
258258
int result = NKikimrLdap::Bind(*request.Ld, dn, request.Password);
259259
if (!NKikimrLdap::IsSuccess(result)) {
260-
error.Message = "LDAP login failed for user " + TString(dn) + " on server " + UrisList + "\n"
260+
error.Message = "LDAP login failed for user " + TString(dn) + " on server " + UrisCreator.GetUris() + "\n"
261261
+ NKikimrLdap::ErrorToString((result));
262262
error.Retryable = NKikimrLdap::IsRetryableError(result);
263263
}
@@ -279,7 +279,7 @@ class TLdapAuthProvider : public NActors::TActorBootstrapped<TLdapAuthProvider>
279279
TSearchUserResponse response;
280280
if (!NKikimrLdap::IsSuccess(result)) {
281281
response.Status = NKikimrLdap::ErrorToStatus(result);
282-
response.Error = {.Message = "Could not search for filter " + searchFilter + " on server " + UrisList + "\n"
282+
response.Error = {.Message = "Could not search for filter " + searchFilter + " on server " + UrisCreator.GetUris() + "\n"
283283
+ NKikimrLdap::ErrorToString(result),
284284
.Retryable = NKikimrLdap::IsRetryableError(result)};
285285
return response;
@@ -288,11 +288,11 @@ class TLdapAuthProvider : public NActors::TActorBootstrapped<TLdapAuthProvider>
288288
if (countEntries != 1) {
289289
if (countEntries == 0) {
290290
response.Error = {.Message = "LDAP user " + request.User + " does not exist. "
291-
"LDAP search for filter " + searchFilter + " on server " + UrisList + " return no entries",
291+
"LDAP search for filter " + searchFilter + " on server " + UrisCreator.GetUris() + " return no entries",
292292
.Retryable = false};
293293
} else {
294294
response.Error = {.Message = "LDAP user " + request.User + " is not unique. "
295-
"LDAP search for filter " + searchFilter + " on server " + UrisList + " return " + countEntries + " entries",
295+
"LDAP search for filter " + searchFilter + " on server " + UrisCreator.GetUris() + " return " + countEntries + " entries",
296296
.Retryable = false};
297297
}
298298
response.Status = TEvLdapAuthProvider::EStatus::UNAUTHORIZED;
@@ -392,42 +392,11 @@ class TLdapAuthProvider : public NActors::TActorBootstrapped<TLdapAuthProvider>
392392
return {TEvLdapAuthProvider::EStatus::SUCCESS, {}};
393393
}
394394

395-
TString GetUris(ui32 port) const {
396-
TStringBuilder uris;
397-
if (Settings.HostsSize() > 0) {
398-
for (const auto& host : Settings.GetHosts()) {
399-
uris << CreateUri(host, port) << " ";
400-
}
401-
uris.remove(uris.size() - 1);
402-
} else {
403-
uris << CreateUri(Settings.GetHost(), port);
404-
}
405-
return uris;
406-
}
407-
408-
TString CreateUri(const TString& endpoint, ui32 port) const {
409-
TStringBuilder uri;
410-
uri << Settings.GetScheme() << "://" << endpoint;
411-
if (!HasEndpointPort(endpoint)) {
412-
uri << ':' << port;
413-
}
414-
return uri;
415-
}
416-
417-
static bool HasEndpointPort(const TString& endpoint) {
418-
size_t colonPos = endpoint.rfind(':');
419-
if (colonPos == TString::npos) {
420-
return false;
421-
}
422-
++colonPos;
423-
return (endpoint.size() - colonPos) > 0;
424-
}
425-
426395
private:
427396
const NKikimrProto::TLdapAuthentication Settings;
428397
const TSearchFilterCreator FilterCreator;
398+
const TLdapUrisCreator UrisCreator;
429399
char* RequestedAttributes[2];
430-
TString UrisList;
431400
};
432401

433402
IActor* CreateLdapAuthProvider(const NKikimrProto::TLdapAuthentication& settings) {

0 commit comments

Comments
 (0)