-
Notifications
You must be signed in to change notification settings - Fork 694
oidc impersonation #11449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
StekPerepolnen
merged 7 commits into
ydb-platform:main
from
StekPerepolnen:oidc-impersonation
Dec 2, 2024
Merged
oidc impersonation #11449
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
34dcd14
impersonate
StekPerepolnen 2f97361
impersonate
StekPerepolnen 62ab8ab
remove unnecessary
StekPerepolnen 767932d
rename variables
StekPerepolnen 18ba292
escape only values
StekPerepolnen 5d15b43
remove namespace
StekPerepolnen 7d3af5d
return const
StekPerepolnen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
ydb/mvp/oidc_proxy/oidc_impersonate_start_page_nebius.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
#include <library/cpp/string_utils/base64/base64.h> | ||
#include <ydb/library/actors/http/http.h> | ||
#include <ydb/library/security/util.h> | ||
#include <ydb/mvp/core/mvp_log.h> | ||
#include <ydb/mvp/core/mvp_tokens.h> | ||
#include "openid_connect.h" | ||
#include "oidc_session_create.h" | ||
#include "oidc_impersonate_start_page_nebius.h" | ||
|
||
namespace NMVP::NOIDC { | ||
|
||
THandlerImpersonateStart::THandlerImpersonateStart(const NActors::TActorId& sender, | ||
const NHttp::THttpIncomingRequestPtr& request, | ||
const NActors::TActorId& httpProxyId, | ||
const TOpenIdConnectSettings& settings) | ||
: Sender(sender) | ||
, Request(request) | ||
, HttpProxyId(httpProxyId) | ||
, Settings(settings) | ||
{} | ||
|
||
void THandlerImpersonateStart::Bootstrap() { | ||
BLOG_D("Start impersonation process"); | ||
|
||
NHttp::TUrlParameters urlParameters(Request->URL); | ||
TString serviceAccountId = urlParameters["service_account_id"]; | ||
|
||
NHttp::THeaders headers(Request->Headers); | ||
NHttp::TCookies cookies(headers.Get("Cookie")); | ||
|
||
TStringBuf sessionCookieValue = GetCookie(cookies, CreateNameSessionCookie(Settings.ClientId)); | ||
TString sessionToken = DecodeToken(sessionCookieValue); | ||
TStringBuf impersonatedCookieValue = GetCookie(cookies, CreateNameImpersonatedCookie(Settings.ClientId)); | ||
|
||
if (sessionToken.empty()) { | ||
return ReplyBadRequestAndPassAway("Wrong impersonate parameter: session cookie not found"); | ||
} | ||
if (!impersonatedCookieValue.empty()) { | ||
return ReplyBadRequestAndPassAway("Wrong impersonate parameter: impersonated cookie already exists"); | ||
} | ||
if (serviceAccountId.empty()) { | ||
return ReplyBadRequestAndPassAway("Wrong impersonate parameter: service_account_id not found"); | ||
} | ||
|
||
RequestImpersonatedToken(sessionToken, serviceAccountId); | ||
} | ||
|
||
void THandlerImpersonateStart::RequestImpersonatedToken(TString& sessionToken, TString& serviceAccountId) { | ||
BLOG_D("Request impersonated token"); | ||
NHttp::THttpOutgoingRequestPtr httpRequest = NHttp::THttpOutgoingRequest::CreateRequestPost(Settings.GetImpersonateEndpointURL()); | ||
httpRequest->Set<&NHttp::THttpRequest::ContentType>("application/x-www-form-urlencoded"); | ||
|
||
TMvpTokenator* tokenator = MVPAppData()->Tokenator; | ||
TString token = ""; | ||
if (tokenator) { | ||
token = tokenator->GetToken(Settings.SessionServiceTokenName); | ||
} | ||
httpRequest->Set("Authorization", token); // Bearer included | ||
|
||
TCgiParameters params; | ||
params.emplace("session", sessionToken); | ||
params.emplace("service_account_id", serviceAccountId); | ||
httpRequest->Set<&NHttp::THttpRequest::Body>(params()); | ||
|
||
Send(HttpProxyId, new NHttp::TEvHttpProxy::TEvHttpOutgoingRequest(httpRequest)); | ||
Become(&THandlerImpersonateStart::StateWork); | ||
} | ||
|
||
void THandlerImpersonateStart::ProcessImpersonatedToken(const TString& impersonatedToken) { | ||
TString impersonatedCookieName = CreateNameImpersonatedCookie(Settings.ClientId); | ||
TString impersonatedCookieValue = Base64Encode(impersonatedToken); | ||
BLOG_D("Set impersonated cookie: (" << impersonatedCookieName << ": " << NKikimr::MaskTicket(impersonatedCookieValue) << ")"); | ||
|
||
NHttp::THeadersBuilder responseHeaders; | ||
responseHeaders.Set("Set-Cookie", CreateSecureCookie(impersonatedCookieName, impersonatedCookieValue)); | ||
SetCORS(Request, &responseHeaders); | ||
ReplyAndPassAway(Request->CreateResponse("200", "OK", responseHeaders)); | ||
} | ||
|
||
void THandlerImpersonateStart::Handle(NHttp::TEvHttpProxy::TEvHttpIncomingResponse::TPtr event) { | ||
if (event->Get()->Error.empty() && event->Get()->Response) { | ||
NHttp::THttpIncomingResponsePtr response = std::move(event->Get()->Response); | ||
BLOG_D("Incoming response from authorization server: " << response->Status); | ||
if (response->Status == "200") { | ||
TStringBuf errorMessage; | ||
NJson::TJsonValue jsonValue; | ||
NJson::TJsonReaderConfig jsonConfig; | ||
if (NJson::ReadJsonTree(response->Body, &jsonConfig, &jsonValue)) { | ||
const NJson::TJsonValue* jsonImpersonatedToken; | ||
if (jsonValue.GetValuePointer("impersonation", &jsonImpersonatedToken)) { | ||
TString impersonatedToken = jsonImpersonatedToken->GetStringRobust(); | ||
ProcessImpersonatedToken(impersonatedToken); | ||
return; | ||
} else { | ||
errorMessage = "Wrong OIDC provider response: impersonated token not found"; | ||
} | ||
} else { | ||
errorMessage = "Wrong OIDC response"; | ||
} | ||
NHttp::THeadersBuilder responseHeaders; | ||
responseHeaders.Set("Content-Type", "text/plain"); | ||
SetCORS(Request, &responseHeaders); | ||
return ReplyAndPassAway(Request->CreateResponse("400", "Bad Request", responseHeaders, errorMessage)); | ||
} else { | ||
NHttp::THeadersBuilder responseHeaders; | ||
NHttp::THeaders headers(response->Headers); | ||
if (headers.Has("Content-Type")) { | ||
responseHeaders.Set("Content-Type", headers.Get("Content-Type")); | ||
} | ||
SetCORS(Request, &responseHeaders); | ||
return ReplyAndPassAway(Request->CreateResponse(response->Status, response->Message, responseHeaders, response->Body)); | ||
} | ||
} else { | ||
NHttp::THeadersBuilder responseHeaders; | ||
responseHeaders.Set("Content-Type", "text/plain"); | ||
SetCORS(Request, &responseHeaders); | ||
return ReplyAndPassAway(Request->CreateResponse("400", "Bad Request", responseHeaders, event->Get()->Error)); | ||
} | ||
} | ||
|
||
void THandlerImpersonateStart::ReplyAndPassAway(NHttp::THttpOutgoingResponsePtr httpResponse) { | ||
Send(Sender, new NHttp::TEvHttpProxy::TEvHttpOutgoingResponse(std::move(httpResponse))); | ||
PassAway(); | ||
} | ||
|
||
void THandlerImpersonateStart::ReplyBadRequestAndPassAway(const TString& errorMessage) { | ||
NHttp::THeadersBuilder responseHeaders; | ||
responseHeaders.Set("Content-Type", "text/plain"); | ||
SetCORS(Request, &responseHeaders); | ||
ReplyAndPassAway(Request->CreateResponse("400", "Bad Request", responseHeaders, errorMessage)); | ||
} | ||
|
||
TImpersonateStartPageHandler::TImpersonateStartPageHandler(const NActors::TActorId& httpProxyId, const TOpenIdConnectSettings& settings) | ||
: TBase(&TImpersonateStartPageHandler::StateWork) | ||
, HttpProxyId(httpProxyId) | ||
, Settings(settings) | ||
{} | ||
|
||
void TImpersonateStartPageHandler::Handle(NHttp::TEvHttpProxy::TEvHttpIncomingRequest::TPtr event) { | ||
Register(new THandlerImpersonateStart(event->Sender, event->Get()->Request, HttpProxyId, Settings)); | ||
} | ||
|
||
} // NMVP::NOIDC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#pragma once | ||
|
||
#include "oidc_settings.h" | ||
#include "context.h" | ||
#include <ydb/library/actors/core/events.h> | ||
|
||
namespace NMVP::NOIDC { | ||
|
||
class THandlerImpersonateStart : public NActors::TActorBootstrapped<THandlerImpersonateStart> { | ||
private: | ||
using TBase = NActors::TActorBootstrapped<THandlerImpersonateStart>; | ||
|
||
protected: | ||
const NActors::TActorId Sender; | ||
const NHttp::THttpIncomingRequestPtr Request; | ||
const NActors::TActorId HttpProxyId; | ||
const TOpenIdConnectSettings Settings; | ||
|
||
public: | ||
THandlerImpersonateStart(const NActors::TActorId& sender, | ||
const NHttp::THttpIncomingRequestPtr& request, | ||
const NActors::TActorId& httpProxyId, | ||
const TOpenIdConnectSettings& settings); | ||
void Bootstrap(); | ||
void RequestImpersonatedToken(TString&, TString&); | ||
void ProcessImpersonatedToken(const TString& impersonatedToken); | ||
void Handle(NHttp::TEvHttpProxy::TEvHttpIncomingResponse::TPtr event); | ||
void ReplyAndPassAway(NHttp::THttpOutgoingResponsePtr httpResponse); | ||
void ReplyBadRequestAndPassAway(const TString& errorMessage); | ||
|
||
STFUNC(StateWork) { | ||
switch (ev->GetTypeRewrite()) { | ||
hFunc(NHttp::TEvHttpProxy::TEvHttpIncomingResponse, Handle); | ||
} | ||
} | ||
}; | ||
|
||
class TImpersonateStartPageHandler : public NActors::TActor<TImpersonateStartPageHandler> { | ||
StekPerepolnen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
using TBase = NActors::TActor<TImpersonateStartPageHandler>; | ||
|
||
const NActors::TActorId HttpProxyId; | ||
const TOpenIdConnectSettings Settings; | ||
|
||
public: | ||
TImpersonateStartPageHandler(const NActors::TActorId& httpProxyId, const TOpenIdConnectSettings& settings); | ||
void Handle(NHttp::TEvHttpProxy::TEvHttpIncomingRequest::TPtr event); | ||
|
||
STFUNC(StateWork) { | ||
switch (ev->GetTypeRewrite()) { | ||
hFunc(NHttp::TEvHttpProxy::TEvHttpIncomingRequest, Handle); | ||
cFunc(NActors::TEvents::TEvPoisonPill::EventType, PassAway); | ||
} | ||
} | ||
}; | ||
|
||
} // NMVP::NOIDC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include "openid_connect.h" | ||
#include "oidc_session_create.h" | ||
#include "oidc_impersonate_stop_page_nebius.h" | ||
|
||
namespace NMVP::NOIDC { | ||
|
||
THandlerImpersonateStop::THandlerImpersonateStop(const NActors::TActorId& sender, | ||
const NHttp::THttpIncomingRequestPtr& request, | ||
const NActors::TActorId& httpProxyId, | ||
const TOpenIdConnectSettings& settings) | ||
: Sender(sender) | ||
, Request(request) | ||
, HttpProxyId(httpProxyId) | ||
, Settings(settings) | ||
{} | ||
|
||
void THandlerImpersonateStop::Bootstrap() { | ||
TString impersonatedCookieName = CreateNameImpersonatedCookie(Settings.ClientId); | ||
BLOG_D("Clear impersonated cookie: (" << impersonatedCookieName << ")"); | ||
|
||
NHttp::THeadersBuilder responseHeaders; | ||
responseHeaders.Set("Set-Cookie", ClearSecureCookie(impersonatedCookieName)); | ||
SetCORS(Request, &responseHeaders); | ||
|
||
ReplyAndPassAway(Request->CreateResponse("200", "OK", responseHeaders)); | ||
} | ||
|
||
void THandlerImpersonateStop::ReplyAndPassAway(NHttp::THttpOutgoingResponsePtr httpResponse) { | ||
Send(Sender, new NHttp::TEvHttpProxy::TEvHttpOutgoingResponse(std::move(httpResponse))); | ||
PassAway(); | ||
} | ||
|
||
TImpersonateStopPageHandler::TImpersonateStopPageHandler(const NActors::TActorId& httpProxyId, const TOpenIdConnectSettings& settings) | ||
: TBase(&TImpersonateStopPageHandler::StateWork) | ||
, HttpProxyId(httpProxyId) | ||
, Settings(settings) | ||
{} | ||
|
||
void TImpersonateStopPageHandler::Handle(NHttp::TEvHttpProxy::TEvHttpIncomingRequest::TPtr event) { | ||
Register(new THandlerImpersonateStop(event->Sender, event->Get()->Request, HttpProxyId, Settings)); | ||
} | ||
|
||
} // NMVP::NOIDC |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.