Skip to content
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

router: add config to send date header with requests #11915

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions api/envoy/extensions/filters/http/router/v3/router.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE;
// Router :ref:`configuration overview <config_http_filters_router>`.
// [#extension: envoy.filters.http.router]

// [#next-free-field: 7]
// [#next-free-field: 8]
message Router {
option (udpa.annotations.versioning).previous_message_type =
"envoy.config.filter.http.router.v2.Router";
Expand All @@ -35,9 +35,9 @@ message Router {
bool start_child_span = 2;

// Configuration for HTTP upstream logs emitted by the router. Upstream logs
// are configured in the same way as access logs, but each log entry represents
// an upstream request. Presuming retries are configured, multiple upstream
// requests may be made for each downstream (inbound) request.
// are configured in the same way as access logs, but each log entry
// represents an upstream request. Presuming retries are configured, multiple
// upstream requests may be made for each downstream (inbound) request.
repeated config.accesslog.v3.AccessLog upstream_log = 3;

// Do not add any additional *x-envoy-* headers to requests or responses. This
Expand Down Expand Up @@ -75,7 +75,11 @@ message Router {
}];

// If not set, ingress Envoy will ignore
// :ref:`config_http_filters_router_x-envoy-expected-rq-timeout-ms` header, populated by egress
// Envoy, when deriving timeout for upstream cluster.
// :ref:`config_http_filters_router_x-envoy-expected-rq-timeout-ms` header,
// populated by egress Envoy, when deriving timeout for upstream cluster.
bool respect_expected_rq_timeout = 6;

// Enable the addition of the `Date` header to outbound requests.
// If `true`, the header will be assigned on each request (and retry).
bool add_request_date_header = 7;
}
16 changes: 10 additions & 6 deletions api/envoy/extensions/filters/http/router/v4alpha/router.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion include/envoy/http/header_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ class HeaderEntry {
* Default O(1) response headers.
*/
#define INLINE_RESP_HEADERS(HEADER_FUNC) \
HEADER_FUNC(Date) \
HEADER_FUNC(EnvoyDegraded) \
HEADER_FUNC(EnvoyImmediateHealthCheckFail) \
HEADER_FUNC(EnvoyRateLimited) \
Expand All @@ -318,6 +317,7 @@ class HeaderEntry {
HEADER_FUNC(Connection) \
HEADER_FUNC(ContentLength) \
HEADER_FUNC(ContentType) \
HEADER_FUNC(Date) \
HEADER_FUNC(EnvoyAttemptCount) \
HEADER_FUNC(EnvoyDecoratorOperation) \
HEADER_FUNC(KeepAlive) \
Expand Down
2 changes: 1 addition & 1 deletion source/common/http/async_client_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ AsyncClientImpl::AsyncClientImpl(Upstream::ClusterInfoConstSharedPtr cluster,
Router::ShadowWriterPtr&& shadow_writer,
Http::Context& http_context)
: cluster_(cluster), config_("http.async-client.", local_info, stats_store, cm, runtime, random,
std::move(shadow_writer), true, false, false, false, {},
std::move(shadow_writer), true, false, false, false, false, {},
dispatcher.timeSource(), http_context),
dispatcher_(dispatcher) {}

Expand Down
6 changes: 3 additions & 3 deletions source/common/http/date_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class DateProvider {
virtual ~DateProvider() = default;

/**
* Set the Date header potentially using a cached value.
* @param headers supplies the headers to fill.
* Set the `Date` header (potentially using a cached value).
* @param headers the headers to populate.
*/
virtual void setDateHeader(ResponseHeaderMap& headers) PURE;
virtual void setDateHeader(RequestOrResponseHeaderMap& headers) PURE;
};

} // namespace Http
Expand Down
4 changes: 2 additions & 2 deletions source/common/http/date_provider_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ void TlsCachingDateProviderImpl::onRefreshDate() {
refresh_timer_->enableTimer(std::chrono::milliseconds(500));
}

void TlsCachingDateProviderImpl::setDateHeader(ResponseHeaderMap& headers) {
void TlsCachingDateProviderImpl::setDateHeader(RequestOrResponseHeaderMap& headers) {
headers.setDate(tls_->getTyped<ThreadLocalCachedDate>().date_string_);
}

void SlowDateProviderImpl::setDateHeader(ResponseHeaderMap& headers) {
void SlowDateProviderImpl::setDateHeader(RequestOrResponseHeaderMap& headers) {
headers.setDate(date_formatter_.now(time_source_));
}

Expand Down
4 changes: 2 additions & 2 deletions source/common/http/date_provider_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TlsCachingDateProviderImpl : public DateProviderImplBase, public Singleton
TlsCachingDateProviderImpl(Event::Dispatcher& dispatcher, ThreadLocal::SlotAllocator& tls);

// Http::DateProvider
void setDateHeader(ResponseHeaderMap& headers) override;
void setDateHeader(RequestOrResponseHeaderMap& headers) override;

private:
struct ThreadLocalCachedDate : public ThreadLocal::ThreadLocalObject {
Expand All @@ -58,7 +58,7 @@ class SlowDateProviderImpl : public DateProviderImplBase {

public:
// Http::DateProvider
void setDateHeader(ResponseHeaderMap& headers) override;
void setDateHeader(RequestOrResponseHeaderMap& headers) override;
};

} // namespace Http
Expand Down
1 change: 1 addition & 0 deletions source/common/router/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ envoy_cc_library(
"//source/common/common:utility_lib",
"//source/common/grpc:common_lib",
"//source/common/http:codes_lib",
"//source/common/http:date_provider_lib",
"//source/common/http:header_map_lib",
"//source/common/http:headers_lib",
"//source/common/http:message_lib",
Expand Down
8 changes: 8 additions & 0 deletions source/common/router/router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ Http::FilterHeadersStatus Filter::decodeHeaders(Http::RequestHeaderMap& headers,
ASSERT(headers.Method());
ASSERT(headers.Host());

if (config_.add_request_date_header_) {
config_.date_provider_.setDateHeader(headers);
}

downstream_headers_ = &headers;

// Extract debug configuration from filter state. This is used further along to determine whether
Expand Down Expand Up @@ -1525,6 +1529,10 @@ void Filter::doRetry() {
downstream_headers_->setEnvoyAttemptCount(attempt_count_);
}

if (config_.add_request_date_header_) {
config_.date_provider_.setDateHeader(*downstream_headers_);
}

UpstreamRequest* upstream_request_tmp = upstream_request.get();
upstream_request->moveIntoList(std::move(upstream_request), upstream_requests_);
upstream_requests_.front()->encodeHeaders(!callbacks_->decodingBuffer() &&
Expand Down
14 changes: 10 additions & 4 deletions source/common/router/router.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "common/common/linked_object.h"
#include "common/common/logger.h"
#include "common/config/well_known_names.h"
#include "common/http/date_provider_impl.h"
#include "common/http/utility.h"
#include "common/router/config_impl.h"
#include "common/router/upstream_request.h"
Expand Down Expand Up @@ -182,14 +183,16 @@ class FilterConfig {
Stats::Scope& scope, Upstream::ClusterManager& cm, Runtime::Loader& runtime,
Runtime::RandomGenerator& random, ShadowWriterPtr&& shadow_writer,
bool emit_dynamic_stats, bool start_child_span, bool suppress_envoy_headers,
bool respect_expected_rq_timeout,
bool respect_expected_rq_timeout, bool add_request_date_header,
const Protobuf::RepeatedPtrField<std::string>& strict_check_headers,
TimeSource& time_source, Http::Context& http_context)
: scope_(scope), local_info_(local_info), cm_(cm), runtime_(runtime),
random_(random), stats_{ALL_ROUTER_STATS(POOL_COUNTER_PREFIX(scope, stat_prefix))},
emit_dynamic_stats_(emit_dynamic_stats), start_child_span_(start_child_span),
suppress_envoy_headers_(suppress_envoy_headers),
respect_expected_rq_timeout_(respect_expected_rq_timeout), http_context_(http_context),
respect_expected_rq_timeout_(respect_expected_rq_timeout),
add_request_date_header_(add_request_date_header), http_context_(http_context),
date_provider_(Http::SlowDateProviderImpl(time_source)),
stat_name_pool_(scope_.symbolTable()), retry_(stat_name_pool_.add("retry")),
zone_name_(stat_name_pool_.add(local_info_.zoneName())),
empty_stat_name_(stat_name_pool_.add("")), shadow_writer_(std::move(shadow_writer)),
Expand All @@ -209,8 +212,9 @@ class FilterConfig {
context.runtime(), context.random(), std::move(shadow_writer),
PROTOBUF_GET_WRAPPED_OR_DEFAULT(config, dynamic_stats, true),
config.start_child_span(), config.suppress_envoy_headers(),
config.respect_expected_rq_timeout(), config.strict_check_headers(),
context.api().timeSource(), context.httpContext()) {
config.respect_expected_rq_timeout(), config.add_request_date_header(),
config.strict_check_headers(), context.api().timeSource(),
context.httpContext()) {
for (const auto& upstream_log : config.upstream_log()) {
upstream_logs_.push_back(AccessLog::AccessLogFactory::fromProto(upstream_log, context));
}
Expand All @@ -231,10 +235,12 @@ class FilterConfig {
const bool start_child_span_;
const bool suppress_envoy_headers_;
const bool respect_expected_rq_timeout_;
const bool add_request_date_header_;
// TODO(xyu-stripe): Make this a bitset to keep cluster memory footprint down.
HeaderVectorPtr strict_check_headers_;
std::list<AccessLog::InstanceSharedPtr> upstream_logs_;
Http::Context& http_context_;
Http::SlowDateProviderImpl date_provider_;
Stats::StatNamePool stat_name_pool_;
Stats::StatName retry_;
Stats::StatName zone_name_;
Expand Down
54 changes: 53 additions & 1 deletion test/common/http/date_provider_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,63 @@
#include "gtest/gtest.h"

using testing::NiceMock;
using testing::Return;

namespace Envoy {
namespace Http {

TEST(DateProviderImplTest, All) {
class MockTimeSource : public TimeSource {
public:
MOCK_METHOD(SystemTime, systemTime, ());
MOCK_METHOD(MonotonicTime, monotonicTime, ());
};

TEST(SlowDateProviderImplTest, RequestHeaders) {
MockTimeSource time_source;
SystemTime time_point{SystemTime::duration(2000000)};
EXPECT_CALL(time_source, systemTime()).WillOnce(Return(time_point));

SlowDateProviderImpl provider(time_source);
TestRequestHeaderMapImpl headers;
EXPECT_EQ(nullptr, headers.Date());

provider.setDateHeader(headers);
EXPECT_EQ(headers.get_("date"), "Thu, 01 Jan 1970 00:00:02 GMT");
}

TEST(SlowDateProviderImplTest, ResponseHeaders) {
MockTimeSource time_source;
SystemTime time_point{SystemTime::duration(2000000)};
EXPECT_CALL(time_source, systemTime()).WillOnce(Return(time_point));

SlowDateProviderImpl provider(time_source);
TestResponseHeaderMapImpl headers;
EXPECT_EQ(nullptr, headers.Date());

provider.setDateHeader(headers);
EXPECT_EQ(headers.Date()->value(), "Thu, 01 Jan 1970 00:00:02 GMT");
}

TEST(TlsCachingDateProviderImplTest, TlsRequestHeaders) {
Event::MockDispatcher dispatcher;
NiceMock<ThreadLocal::MockInstance> tls;
Event::MockTimer* timer = new Event::MockTimer(&dispatcher);
EXPECT_CALL(*timer, enableTimer(std::chrono::milliseconds(500), _));

TlsCachingDateProviderImpl provider(dispatcher, tls);
TestRequestHeaderMapImpl headers;
provider.setDateHeader(headers);
EXPECT_NE(nullptr, headers.Date());

EXPECT_CALL(*timer, enableTimer(std::chrono::milliseconds(500), _));
timer->invokeCallback();

headers.removeDate();
provider.setDateHeader(headers);
EXPECT_NE(nullptr, headers.Date());
}

TEST(TlsCachingDateProviderImplTest, TlsResponseHeaders) {
Event::MockDispatcher dispatcher;
NiceMock<ThreadLocal::MockInstance> tls;
Event::MockTimer* timer = new Event::MockTimer(&dispatcher);
Expand Down
Loading