Skip to content

Commit

Permalink
Enable static format validation where possible
Browse files Browse the repository at this point in the history
Signed-off-by: Yan Avlasov <yavlasov@google.com>
  • Loading branch information
yanavlasov committed Mar 30, 2024
1 parent 113e997 commit 364fd8a
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ class HttpFilterLanguageIntegrationTest
}

void initializeConfig(std::string default_language, std::string supported_languages) {
const std::string yaml = R"EOF(
constexpr absl::string_view yaml = R"EOF(
name: envoy.filters.http.language
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.language.v3alpha.Language
default_language: {}
supported_languages: {}
)EOF";
config_helper_.prependFilter(
fmt::format(fmt::runtime(yaml), default_language, supported_languages));
config_helper_.prependFilter(fmt::format(yaml, default_language, supported_languages));
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ class PlatformBridgeFilterTest : public testing::Test {
std::stringstream ss;
filter_->dumpState(ss, 0);

std::string expected_state_template =
constexpr absl::string_view expected_state_template =
R"EOF(PlatformBridgeFilter, filter_name_: {}, error_response_: {}
Request Filter, state_.iteration_state_: {}, state_.on_headers_called_: {}, state_.headers_forwarded_: {}, state_.on_data_called_: {}, state_.data_forwarded_: {}, state_.on_trailers_called_: {}, state_.trailers_forwarded_: {}, state_.on_resume_called_: {}, pending_headers_: {}, buffer: {}, pending_trailers_: {}, state_.stream_complete_: {}
Response Filter, state_.iteration_state_: {}, state_.on_headers_called_: {}, state_.headers_forwarded_: {}, state_.on_data_called_: {}, state_.data_forwarded_: {}, state_.on_trailers_called_: {}, state_.trailers_forwarded_: {}, state_.on_resume_called_: {}, pending_headers_: {}, buffer: {}, pending_trailers_: {}, state_.stream_complete_: {}
)EOF";

std::string expected_state = fmt::format(
fmt::runtime(expected_state_template), name, error_response, request.iteration_state,
expected_state_template, name, error_response, request.iteration_state,
request.on_headers_called, request.headers_forwarded, request.on_data_called,
request.data_forwarded, request.on_trailers_called, request.trailers_forwarded,
request.on_resume_called, request.pending_headers, request.buffer, request.pending_trailers,
Expand Down
2 changes: 1 addition & 1 deletion source/extensions/common/aws/signer_base_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ std::string SignerBaseImpl::createContentHash(Http::RequestMessage& message, boo
std::string
SignerBaseImpl::createAuthorizationCredential(absl::string_view access_key_id,
absl::string_view credential_scope) const {
return fmt::format(fmt::runtime(SignatureConstants::AuthorizationCredentialFormat), access_key_id,
return fmt::format(SignatureConstants::AuthorizationCredentialFormat, access_key_id,
credential_scope);
}

Expand Down
8 changes: 4 additions & 4 deletions source/extensions/common/aws/sigv4_signer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Aws {

std::string SigV4SignerImpl::createCredentialScope(absl::string_view short_date,
absl::string_view override_region) const {
return fmt::format(fmt::runtime(SigV4SignatureConstants::SigV4CredentialScopeFormat), short_date,
return fmt::format(SigV4SignatureConstants::SigV4CredentialScopeFormat, short_date,
override_region.empty() ? region_ : override_region, service_name_);
}

Expand All @@ -31,8 +31,8 @@ std::string SigV4SignerImpl::createStringToSign(absl::string_view canonical_requ
absl::string_view credential_scope) const {
auto& crypto_util = Envoy::Common::Crypto::UtilitySingleton::get();
return fmt::format(
fmt::runtime(SigV4SignatureConstants::SigV4StringToSignFormat),
SigV4SignatureConstants::SigV4Algorithm, long_date, credential_scope,
SigV4SignatureConstants::SigV4StringToSignFormat, SigV4SignatureConstants::SigV4Algorithm,
long_date, credential_scope,
Hex::encode(crypto_util.getSha256Digest(Buffer::OwnedImpl(canonical_request))));
}

Expand All @@ -59,7 +59,7 @@ std::string SigV4SignerImpl::createAuthorizationHeader(
const std::map<std::string, std::string>& canonical_headers,
absl::string_view signature) const {
const auto signed_headers = Utility::joinCanonicalHeaderNames(canonical_headers);
return fmt::format(fmt::runtime(SigV4SignatureConstants::SigV4AuthorizationHeaderFormat),
return fmt::format(SigV4SignatureConstants::SigV4AuthorizationHeaderFormat,
SigV4SignatureConstants::SigV4Algorithm,
createAuthorizationCredential(access_key_id, credential_scope), signed_headers,
signature);
Expand Down
10 changes: 5 additions & 5 deletions source/extensions/common/aws/sigv4a_signer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ std::string SigV4ASignerImpl::createAuthorizationHeader(
const std::map<std::string, std::string>& canonical_headers,
absl::string_view signature) const {
const auto signed_headers = Utility::joinCanonicalHeaderNames(canonical_headers);
return fmt::format(fmt::runtime(SigV4ASignatureConstants::SigV4AAuthorizationHeaderFormat),
return fmt::format(SigV4ASignatureConstants::SigV4AAuthorizationHeaderFormat,
createAuthorizationCredential(access_key_id, credential_scope), signed_headers,
signature);
}

std::string SigV4ASignerImpl::createCredentialScope(
const absl::string_view short_date,
ABSL_ATTRIBUTE_UNUSED const absl::string_view override_region) const {
return fmt::format(fmt::runtime(SigV4ASignatureConstants::SigV4ACredentialScopeFormat),
short_date, service_name_);
return fmt::format(SigV4ASignatureConstants::SigV4ACredentialScopeFormat, short_date,
service_name_);
}

std::string SigV4ASignerImpl::createStringToSign(const absl::string_view canonical_request,
const absl::string_view long_date,
const absl::string_view credential_scope) const {
auto& crypto_util = Envoy::Common::Crypto::UtilitySingleton::get();
return fmt::format(
fmt::runtime(SigV4ASignatureConstants::SigV4AStringToSignFormat), getAlgorithmString(),
long_date, credential_scope,
SigV4ASignatureConstants::SigV4AStringToSignFormat, getAlgorithmString(), long_date,
credential_scope,
Hex::encode(crypto_util.getSha256Digest(Buffer::OwnedImpl(canonical_request))));
}

Expand Down
10 changes: 7 additions & 3 deletions source/extensions/network/dns_resolver/cares/dns_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ absl::optional<std::string> DnsResolverImpl::maybeBuildResolversCsv(
// resolver->asString() is avoided as that format may be modified by custom
// Address::Instance implementations in ways that make the <port> not a simple
// integer. See https://github.com/envoyproxy/envoy/pull/3366.
resolver_addrs.push_back(fmt::format(fmt::runtime(resolver->ip()->ipv6() ? "[{}]:{}" : "{}:{}"),
resolver->ip()->addressAsString(),
resolver->ip()->port()));
if (resolver->ip()->ipv6()) {
resolver_addrs.push_back(
fmt::format("[{}]:{}", resolver->ip()->addressAsString(), resolver->ip()->port()));
} else {
resolver_addrs.push_back(
fmt::format("{}:{}", resolver->ip()->addressAsString(), resolver->ip()->port()));
}
}
return {absl::StrJoin(resolver_addrs, ",")};
}
Expand Down
4 changes: 2 additions & 2 deletions source/server/hot_restarting_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ sockaddr_un RpcStream::createDomainSocketAddress(uint64_t id, const std::string&
id = id % MaxConcurrentProcesses;
sockaddr_un address;
initDomainSocketAddress(&address);
Network::Address::PipeInstance addr(
fmt::format(fmt::runtime(socket_path + "_{}_{}"), role, base_id_ + id), socket_mode, nullptr);
Network::Address::PipeInstance addr(fmt::format("{}_{}_{}", socket_path, role, base_id_ + id),
socket_mode, nullptr);
safeMemcpy(&address, &(addr.getSockAddr()));
fchmod(domain_socket_, socket_mode);

Expand Down
4 changes: 2 additions & 2 deletions test/extensions/common/aws/sigv4a_signer_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ class SigV4ASignerImplTest : public testing::Test {
}

std::string short_date = "20180102";
std::string credential_scope = fmt::format(fmt::runtime("{}/service/aws4_request"), short_date);
std::string credential_scope = fmt::format("{}/service/aws4_request", short_date);
std::string long_date = "20180102T030400Z";
std::string string_to_sign =
fmt::format(fmt::runtime(SigV4ASignatureConstants::SigV4AStringToSignFormat),
fmt::format(SigV4ASignatureConstants::SigV4AStringToSignFormat,
SigV4ASignatureConstants::SigV4AAlgorithm, long_date, credential_scope,
Hex::encode(crypto_util.getSha256Digest(Buffer::OwnedImpl(canonical_request))));
auto hash = crypto_util.getSha256Digest(Buffer::OwnedImpl(string_to_sign));
Expand Down
4 changes: 2 additions & 2 deletions test/extensions/common/tap/admin_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class BufferedAdminHandlerTest : public BaseAdminHandlerTest {
void triggerTimeout() { attachedRequestBuffered()->onTimeout(attachedRequest()); }

std::string makeBufferedAdminYaml(uint64_t max_traces, std::string timeout_s = "0s") {
const std::string buffered_admin_request_yaml_ =
constexpr absl::string_view buffered_admin_request_yaml_ =
R"EOF(
config_id: test_config_id
tap_config:
Expand All @@ -142,7 +142,7 @@ config_id: test_config_id
max_traces: {}
timeout: {}
)EOF";
return fmt::format(fmt::runtime(buffered_admin_request_yaml_), max_traces, timeout_s);
return fmt::format(buffered_admin_request_yaml_, max_traces, timeout_s);
}

// Cannot be moved into individual test cases as expected calls are validated on object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class LocalRateLimiterDescriptorImplTest : public LocalRateLimiterImplTest {
rate_limiter_ = std::make_shared<LocalRateLimiterImpl>(
fill_interval, max_tokens, tokens_per_fill, dispatcher_, descriptors_);
}
const std::string single_descriptor_config_yaml = R"(
static constexpr absl::string_view single_descriptor_config_yaml = R"(
entries:
- key: foo2
value: bar2
Expand Down Expand Up @@ -247,7 +247,7 @@ class LocalRateLimiterDescriptorImplTest : public LocalRateLimiterImplTest {

// Verify descriptor rate limit time interval is multiple of token bucket fill interval.
TEST_F(LocalRateLimiterDescriptorImplTest, DescriptorRateLimitDivisibleByTokenFillInterval) {
TestUtility::loadFromYaml(fmt::format(fmt::runtime(single_descriptor_config_yaml), 10, 10, "60s"),
TestUtility::loadFromYaml(fmt::format(single_descriptor_config_yaml, 10, 10, "60s"),
*descriptors_.Add());

EXPECT_THROW_WITH_MESSAGE(
Expand All @@ -256,9 +256,9 @@ TEST_F(LocalRateLimiterDescriptorImplTest, DescriptorRateLimitDivisibleByTokenFi
}

TEST_F(LocalRateLimiterDescriptorImplTest, DuplicateDescriptor) {
TestUtility::loadFromYaml(fmt::format(fmt::runtime(single_descriptor_config_yaml), 1, 1, "0.1s"),
TestUtility::loadFromYaml(fmt::format(single_descriptor_config_yaml, 1, 1, "0.1s"),
*descriptors_.Add());
TestUtility::loadFromYaml(fmt::format(fmt::runtime(single_descriptor_config_yaml), 1, 1, "0.1s"),
TestUtility::loadFromYaml(fmt::format(single_descriptor_config_yaml, 1, 1, "0.1s"),
*descriptors_.Add());

EXPECT_THROW_WITH_MESSAGE(
Expand All @@ -276,9 +276,8 @@ TEST_F(LocalRateLimiterDescriptorImplTest, DescriptorRateLimitNoExceptionWithout
TEST_F(LocalRateLimiterDescriptorImplTest, CasEdgeCasesDescriptor) {
// This tests the case in which an allowed check races with the fill timer.
{
TestUtility::loadFromYaml(
fmt::format(fmt::runtime(single_descriptor_config_yaml), 1, 1, "0.1s"),
*descriptors_.Add());
TestUtility::loadFromYaml(fmt::format(single_descriptor_config_yaml, 1, 1, "0.1s"),
*descriptors_.Add());
initializeWithDescriptor(std::chrono::milliseconds(50), 1, 1);

dispatcher_.globalTimeSystem().advanceTimeAndRun(std::chrono::milliseconds(50), dispatcher_,
Expand Down Expand Up @@ -331,7 +330,7 @@ TEST_F(LocalRateLimiterDescriptorImplTest, CasEdgeCasesDescriptor) {
}

TEST_F(LocalRateLimiterDescriptorImplTest, TokenBucketDescriptor2) {
TestUtility::loadFromYaml(fmt::format(fmt::runtime(single_descriptor_config_yaml), 1, 1, "0.1s"),
TestUtility::loadFromYaml(fmt::format(single_descriptor_config_yaml, 1, 1, "0.1s"),
*descriptors_.Add());
initializeWithDescriptor(std::chrono::milliseconds(50), 1, 1);

Expand All @@ -344,7 +343,7 @@ TEST_F(LocalRateLimiterDescriptorImplTest, TokenBucketDescriptor2) {

// Verify token bucket functionality with a single token.
TEST_F(LocalRateLimiterDescriptorImplTest, TokenBucketDescriptor) {
TestUtility::loadFromYaml(fmt::format(fmt::runtime(single_descriptor_config_yaml), 1, 1, "0.1s"),
TestUtility::loadFromYaml(fmt::format(single_descriptor_config_yaml, 1, 1, "0.1s"),
*descriptors_.Add());
initializeWithDescriptor(std::chrono::milliseconds(50), 1, 1);

Expand Down Expand Up @@ -387,7 +386,7 @@ TEST_F(LocalRateLimiterDescriptorImplTest, TokenBucketDescriptor) {

// Verify token bucket functionality with request per unit > 1.
TEST_F(LocalRateLimiterDescriptorImplTest, TokenBucketMultipleTokensPerFillDescriptor) {
TestUtility::loadFromYaml(fmt::format(fmt::runtime(single_descriptor_config_yaml), 2, 2, "0.1s"),
TestUtility::loadFromYaml(fmt::format(single_descriptor_config_yaml, 2, 2, "0.1s"),
*descriptors_.Add());
initializeWithDescriptor(std::chrono::milliseconds(50), 2, 2);

Expand Down Expand Up @@ -424,7 +423,7 @@ TEST_F(LocalRateLimiterDescriptorImplTest, TokenBucketMultipleTokensPerFillDescr
// Verify token bucket functionality with multiple descriptors.
TEST_F(LocalRateLimiterDescriptorImplTest, TokenBucketDifferentDescriptorDifferentRateLimits) {
TestUtility::loadFromYaml(multiple_descriptor_config_yaml, *descriptors_.Add());
TestUtility::loadFromYaml(fmt::format(fmt::runtime(single_descriptor_config_yaml), 1, 1, "1000s"),
TestUtility::loadFromYaml(fmt::format(single_descriptor_config_yaml, 1, 1, "1000s"),
*descriptors_.Add());
initializeWithDescriptor(std::chrono::milliseconds(50), 3, 1);

Expand All @@ -450,7 +449,7 @@ TEST_F(LocalRateLimiterDescriptorImplTest, TokenBucketDifferentDescriptorDiffere
TEST_F(LocalRateLimiterDescriptorImplTest,
TokenBucketDifferentDescriptorDifferentRateLimitsSorted) {
TestUtility::loadFromYaml(multiple_descriptor_config_yaml, *descriptors_.Add());
TestUtility::loadFromYaml(fmt::format(fmt::runtime(single_descriptor_config_yaml), 2, 2, "1s"),
TestUtility::loadFromYaml(fmt::format(single_descriptor_config_yaml, 2, 2, "1s"),
*descriptors_.Add());
initializeWithDescriptor(std::chrono::milliseconds(50), 3, 3);
std::vector<RateLimit::LocalDescriptor> descriptors{{{{"hello", "world"}, {"foo", "bar"}}},
Expand All @@ -465,7 +464,7 @@ TEST_F(LocalRateLimiterDescriptorImplTest,

// Verify token bucket status of max tokens, remaining tokens and remaining fill interval.
TEST_F(LocalRateLimiterDescriptorImplTest, TokenBucketDescriptorStatus) {
TestUtility::loadFromYaml(fmt::format(fmt::runtime(single_descriptor_config_yaml), 2, 2, "3s"),
TestUtility::loadFromYaml(fmt::format(single_descriptor_config_yaml, 2, 2, "3s"),
*descriptors_.Add());
initializeWithDescriptor(std::chrono::milliseconds(1000), 2, 2);

Expand Down Expand Up @@ -512,7 +511,7 @@ TEST_F(LocalRateLimiterDescriptorImplTest, TokenBucketDescriptorStatus) {
// multiple descriptors.
TEST_F(LocalRateLimiterDescriptorImplTest, TokenBucketDifferentDescriptorStatus) {
TestUtility::loadFromYaml(multiple_descriptor_config_yaml, *descriptors_.Add());
TestUtility::loadFromYaml(fmt::format(fmt::runtime(single_descriptor_config_yaml), 2, 2, "3s"),
TestUtility::loadFromYaml(fmt::format(single_descriptor_config_yaml, 2, 2, "3s"),
*descriptors_.Add());
initializeWithDescriptor(std::chrono::milliseconds(50), 2, 1);

Expand Down
Loading

0 comments on commit 364fd8a

Please sign in to comment.