Skip to content

Commit ad304b2

Browse files
YDB-1453 added CORS headers to healthcheck handler (#2174)
1 parent c652119 commit ad304b2

File tree

8 files changed

+51
-23
lines changed

8 files changed

+51
-23
lines changed

ydb/core/viewer/counters_hosts.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ using namespace NActors;
1616
using namespace NNodeWhiteboard;
1717

1818
class TCountersHostsList : public TActorBootstrapped<TCountersHostsList> {
19+
IViewer* Viewer;
1920
NMon::TEvHttpInfo::TPtr Event;
2021
THolder<TEvInterconnect::TEvNodesInfo> NodesInfo;
2122
TMap<TNodeId, THolder<TEvWhiteboard::TEvSystemStateResponse>> NodesResponses;
@@ -29,8 +30,9 @@ class TCountersHostsList : public TActorBootstrapped<TCountersHostsList> {
2930
return NKikimrServices::TActivity::VIEWER_HANDLER;
3031
}
3132

32-
TCountersHostsList(IViewer*, NMon::TEvHttpInfo::TPtr& ev)
33-
: Event(ev)
33+
TCountersHostsList(IViewer* viewer, NMon::TEvHttpInfo::TPtr& ev)
34+
: Viewer(viewer)
35+
, Event(ev)
3436
{}
3537

3638
void Bootstrap(const TActorContext& ctx) {
@@ -145,7 +147,7 @@ class TCountersHostsList : public TActorBootstrapped<TCountersHostsList> {
145147
}
146148
}
147149
}
148-
ctx.Send(Event->Sender, new NMon::TEvHttpInfoRes(HTTPOKTEXT + text.Str(), 0, NMon::IEvHttpInfoRes::EContentType::Custom));
150+
ctx.Send(Event->Sender, new NMon::TEvHttpInfoRes(Viewer->GetHTTPOKTEXT(Event->Get()) + text.Str(), 0, NMon::IEvHttpInfoRes::EContentType::Custom));
149151
Die(ctx);
150152
}
151153

ydb/core/viewer/json_acl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class TJsonACL : public TViewerPipeClient<TJsonACL> {
155155

156156
switch (DescribeResult->GetRecord().GetStatus()) {
157157
case NKikimrScheme::StatusAccessDenied:
158-
headers = HTTPFORBIDDENJSON;
158+
headers = Viewer->GetHTTPFORBIDDEN(Event->Get());
159159
break;
160160
default:
161161
break;

ydb/core/viewer/json_describe.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ class TJsonDescribe : public TViewerPipeClient<TJsonDescribe> {
265265
const auto *descriptor = NKikimrScheme::EStatus_descriptor();
266266
auto accessDeniedStatus = descriptor->FindValueByNumber(NKikimrScheme::StatusAccessDenied)->name();
267267
if (DescribeResult->GetStatus() == accessDeniedStatus) {
268-
headers = HTTPFORBIDDENJSON;
268+
headers = Viewer->GetHTTPFORBIDDEN(Event->Get());
269269
}
270270
TProtoToJson::ProtoToJson(json, *DescribeResult, JsonSettings);
271271
} else {

ydb/core/viewer/json_healthcheck.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ enum HealthCheckResponseFormat {
2424
};
2525

2626
class TJsonHealthCheck : public TActorBootstrapped<TJsonHealthCheck> {
27+
IViewer* Viewer;
2728
static const bool WithRetry = false;
2829
NMon::TEvHttpInfo::TPtr Event;
2930
TJsonSettings JsonSettings;
@@ -36,8 +37,9 @@ class TJsonHealthCheck : public TActorBootstrapped<TJsonHealthCheck> {
3637
return NKikimrServices::TActivity::VIEWER_HANDLER;
3738
}
3839

39-
TJsonHealthCheck(IViewer*, NMon::TEvHttpInfo::TPtr& ev)
40-
: Event(ev)
40+
TJsonHealthCheck(IViewer* viewer, NMon::TEvHttpInfo::TPtr& ev)
41+
: Viewer(viewer)
42+
, Event(ev)
4143
{}
4244

4345
void Bootstrap(const TActorContext& ctx) {
@@ -78,7 +80,7 @@ class TJsonHealthCheck : public TActorBootstrapped<TJsonHealthCheck> {
7880
if (Ydb::Monitoring::StatusFlag_Status_Parse(params.Get("min_status"), &minStatus)) {
7981
request->Request.set_minimum_status(minStatus);
8082
} else {
81-
Send(Event->Sender, new NMon::TEvHttpInfoRes(HTTPBADREQUEST, 0, NMon::IEvHttpInfoRes::EContentType::Custom));
83+
Send(Event->Sender, new NMon::TEvHttpInfoRes(Viewer->GetHTTPBADREQUEST(Event->Get()), 0, NMon::IEvHttpInfoRes::EContentType::Custom));
8284
return PassAway();
8385
}
8486
}
@@ -104,7 +106,7 @@ class TJsonHealthCheck : public TActorBootstrapped<TJsonHealthCheck> {
104106
THashMap<TMetricRecord, ui32> recordCounters;
105107
for (auto& log : ev->Get()->Result.issue_log()) {
106108
TMetricRecord record {
107-
.Database = log.location().database().name(),
109+
.Database = log.location().database().name(),
108110
.Message = log.message(),
109111
.Status = descriptor->FindValueByNumber(log.status())->name(),
110112
.Type = log.type()
@@ -124,7 +126,7 @@ class TJsonHealthCheck : public TActorBootstrapped<TJsonHealthCheck> {
124126
void HandleJSON(NHealthCheck::TEvSelfCheckResult::TPtr& ev, const TActorContext &ctx) {
125127
TStringStream json;
126128
TProtoToJson::ProtoToJson(json, ev->Get()->Result, JsonSettings);
127-
ctx.Send(Event->Sender, new NMon::TEvHttpInfoRes(HTTPOKJSON + json.Str(), 0, NMon::IEvHttpInfoRes::EContentType::Custom));
129+
ctx.Send(Event->Sender, new NMon::TEvHttpInfoRes(Viewer->GetHTTPOKJSON(Event->Get()) + json.Str(), 0, NMon::IEvHttpInfoRes::EContentType::Custom));
128130
Die(ctx);
129131
}
130132

@@ -134,7 +136,7 @@ class TJsonHealthCheck : public TActorBootstrapped<TJsonHealthCheck> {
134136
TStringStream ss;
135137
IMetricEncoderPtr encoder = EncoderPrometheus(&ss);
136138
IMetricEncoder* e = encoder.Get();
137-
139+
138140
TIntrusivePtr<TDomainsInfo> domains = AppData()->DomainsInfo;
139141
TIntrusivePtr<TDomainsInfo::TDomain> domain = domains->Domains.begin()->second;
140142
auto filterDatabase = Database ? Database : "/" + domain->Name;
@@ -173,7 +175,7 @@ class TJsonHealthCheck : public TActorBootstrapped<TJsonHealthCheck> {
173175
e->OnMetricEnd();
174176
e->OnStreamEnd();
175177

176-
ctx.Send(Event->Sender, new NMon::TEvHttpInfoRes(HTTPOKTEXT + ss.Str(), 0, NMon::IEvHttpInfoRes::EContentType::Custom));
178+
ctx.Send(Event->Sender, new NMon::TEvHttpInfoRes(Viewer->GetHTTPOKTEXT(Event->Get()) + ss.Str(), 0, NMon::IEvHttpInfoRes::EContentType::Custom));
177179
Die(ctx);
178180
}
179181

@@ -186,7 +188,7 @@ class TJsonHealthCheck : public TActorBootstrapped<TJsonHealthCheck> {
186188
}
187189

188190
void HandleTimeout(const TActorContext &ctx) {
189-
Send(Event->Sender, new NMon::TEvHttpInfoRes(HTTPGATEWAYTIMEOUT, 0, NMon::IEvHttpInfoRes::EContentType::Custom));
191+
Send(Event->Sender, new NMon::TEvHttpInfoRes(Viewer->GetHTTPGATEWAYTIMEOUT(Event->Get()), 0, NMon::IEvHttpInfoRes::EContentType::Custom));
190192
Die(ctx);
191193
}
192194
};

ydb/core/viewer/json_hotkeys.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class TJsonHotkeys : public TViewerPipeClient<TJsonHotkeys> {
143143
if (DescribeResult != nullptr) {
144144
switch (DescribeResult->GetRecord().GetStatus()) {
145145
case NKikimrScheme::StatusAccessDenied:
146-
headers = HTTPFORBIDDENJSON;
146+
headers = Viewer->GetHTTPFORBIDDEN(Event->Get());
147147
break;
148148
default:
149149
break;

ydb/core/viewer/json_local_rpc.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class TJsonLocalRpc : public TActorBootstrapped<TJsonLocalRpc<TProtoRequest, TPr
131131
NProtobufJson::Json2Proto(postData, request, json2ProtoConfig);
132132
}
133133
catch (const yexception& e) {
134-
Send(Event->Sender, new NMon::TEvHttpInfoRes(HTTPBADREQUEST, 0, NMon::IEvHttpInfoRes::EContentType::Custom));
134+
Send(Event->Sender, new NMon::TEvHttpInfoRes(Viewer->GetHTTPBADREQUEST(Event->Get(), {}, "Bad Request"), 0, NMon::IEvHttpInfoRes::EContentType::Custom));
135135
PassAway();
136136
}
137137
} else {
@@ -195,9 +195,9 @@ class TJsonLocalRpc : public TActorBootstrapped<TJsonLocalRpc<TProtoRequest, TPr
195195
TString headers = Viewer->GetHTTPOKJSON(Event->Get());
196196
if (DescribeResult) {
197197
if (!DescribeResult->Status->IsSuccess()) {
198-
headers = HTTPBADREQUEST;
198+
headers = Viewer->GetHTTPBADREQUEST(Event->Get(), {}, "Bad Request");
199199
if (DescribeResult->Status->GetStatus() == NYdb::EStatus::UNAUTHORIZED) {
200-
headers = HTTPFORBIDDENJSON;
200+
headers = Viewer->GetHTTPFORBIDDEN(Event->Get());
201201
}
202202
} else {
203203
TProtoToJson::ProtoToJson(json, *(DescribeResult->Message), JsonSettings);

ydb/core/viewer/viewer.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,11 @@ class TViewer : public TActorBootstrapped<TViewer>, public IViewer {
152152

153153
TString GetCORS(const NMon::TEvHttpInfo* request) override;
154154
TString GetHTTPOKJSON(const NMon::TEvHttpInfo* request, TString response) override;
155+
TString GetHTTPOKTEXT(const NMon::TEvHttpInfo* request, TString response) override;
155156
TString GetHTTPOK(const NMon::TEvHttpInfo* request, TString type, TString response) override;
156157
TString GetHTTPGATEWAYTIMEOUT(const NMon::TEvHttpInfo* request) override;
157158
TString GetHTTPBADREQUEST(const NMon::TEvHttpInfo* request, TString type, TString response) override;
159+
TString GetHTTPFORBIDDEN(const NMon::TEvHttpInfo* request) override;
158160

159161
void RegisterVirtualHandler(
160162
NKikimrViewer::EObjectType parentObjectType,
@@ -467,6 +469,22 @@ TString TViewer::GetHTTPOKJSON(const NMon::TEvHttpInfo* request, TString respons
467469
return res;
468470
}
469471

472+
TString TViewer::GetHTTPOKTEXT(const NMon::TEvHttpInfo* request, TString response) {
473+
TStringBuilder res;
474+
res << "HTTP/1.1 200 Ok\r\n"
475+
<< "Content-Type: text/plain; charset=utf-8\r\n"
476+
<< "X-Worker-Name: " << CurrentWorkerName << "\r\n";
477+
res << GetCORS(request);
478+
if (response) {
479+
res << "Content-Length: " << response.size() << "\r\n";
480+
}
481+
res << "\r\n";
482+
if (response) {
483+
res << response;
484+
}
485+
return res;
486+
}
487+
470488
TString TViewer::GetHTTPGATEWAYTIMEOUT(const NMon::TEvHttpInfo* request) {
471489
TStringBuilder res;
472490
res << "HTTP/1.1 504 Gateway Time-out\r\n"
@@ -492,6 +510,16 @@ TString TViewer::GetHTTPBADREQUEST(const NMon::TEvHttpInfo* request, TString con
492510
return res;
493511
}
494512

513+
TString TViewer::GetHTTPFORBIDDEN(const NMon::TEvHttpInfo* request) {
514+
TStringBuilder res;
515+
res << "HTTP/1.1 403 Forbidden\r\n"
516+
<< "Content-Type: application/json; charset=utf-8\r\n"
517+
<< "Connection: Close\r\n";
518+
res << GetCORS(request);
519+
res << "\r\n";
520+
return res;
521+
}
522+
495523
TString TViewer::GetHTTPOK(const NMon::TEvHttpInfo* request, TString contentType = {}, TString response = {}) {
496524
TStringBuilder res;
497525
res << "HTTP/1.1 200 Ok\r\n"

ydb/core/viewer/viewer.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,10 @@ class IViewer {
156156
virtual TString GetCORS(const NMon::TEvHttpInfo* request) = 0;
157157
virtual TString GetHTTPOK(const NMon::TEvHttpInfo* request, TString contentType = {}, TString response = {}) = 0;
158158
virtual TString GetHTTPOKJSON(const NMon::TEvHttpInfo* request, TString response = {}) = 0;
159+
virtual TString GetHTTPOKTEXT(const NMon::TEvHttpInfo* request, TString response = {}) = 0;
159160
virtual TString GetHTTPGATEWAYTIMEOUT(const NMon::TEvHttpInfo* request) = 0;
160161
virtual TString GetHTTPBADREQUEST(const NMon::TEvHttpInfo* request, TString contentType = {}, TString response = {}) = 0;
162+
virtual TString GetHTTPFORBIDDEN(const NMon::TEvHttpInfo* request) = 0;
161163
};
162164

163165
void SetupPQVirtualHandlers(IViewer* viewer);
@@ -184,12 +186,6 @@ struct TJsonRequestParameters {
184186
static TString GetParameters() { return TString(); }
185187
};
186188

187-
static const char HTTPOKJSON[] = "HTTP/1.1 200 Ok\r\nAccess-Control-Allow-Origin: *\r\nContent-Type: application/json; charset=utf-8\r\nConnection: Close\r\n\r\n";
188-
static const char HTTPOKTEXT[] = "HTTP/1.1 200 Ok\r\nAccess-Control-Allow-Origin: *\r\nContent-Type: text/plain; charset=utf-8\r\nConnection: Close\r\n\r\n";
189-
static const char HTTPFORBIDDENJSON[] = "HTTP/1.1 403 Forbidden\r\nAccess-Control-Allow-Origin: *\r\nContent-Type: application/json; charset=utf-8\r\nConnection: Close\r\n\r\n";
190-
static const char HTTPGATEWAYTIMEOUT[] = "HTTP/1.1 504 Gateway Time-out\r\nConnection: Close\r\n\r\nGateway Time-out\r\n";
191-
static const char HTTPBADREQUEST[] = "HTTP/1.1 400 Bad Request\r\nConnection: Close\r\n\r\nBad Request\r\n";
192-
193189
template <typename ValueType, typename OutputIteratorType>
194190
void GenericSplitIds(TStringBuf source, char delim, OutputIteratorType it) {
195191
for (TStringBuf value = source.NextTok(delim); !value.empty(); value = source.NextTok(delim)) {

0 commit comments

Comments
 (0)