Line data Source code
1 : #include "source/common/protobuf/message_validator_impl.h" 2 : 3 : #include "envoy/common/exception.h" 4 : 5 : #include "source/common/common/assert.h" 6 : #include "source/common/common/hash.h" 7 : #include "source/common/common/macros.h" 8 : 9 : #include "absl/strings/str_cat.h" 10 : 11 : namespace Envoy { 12 : namespace ProtobufMessage { 13 : 14 : namespace { 15 : const char deprecation_error[] = " If continued use of this field is absolutely necessary, " 16 : "see " ENVOY_DOC_URL_RUNTIME_OVERRIDE_DEPRECATED " for " 17 : "how to apply a temporary and highly discouraged override."; 18 : 19 908 : void onDeprecatedFieldCommon(absl::string_view description, bool soft_deprecation) { 20 908 : if (soft_deprecation) { 21 908 : ENVOY_LOG_MISC(warn, "Deprecated field: {}", absl::StrCat(description, deprecation_error)); 22 908 : } else { 23 0 : throwExceptionOrPanic(DeprecatedProtoFieldException, 24 0 : absl::StrCat(description, deprecation_error)); 25 0 : } 26 908 : } 27 : } // namespace 28 : 29 402 : void WipCounterBase::setWipCounter(Stats::Counter& wip_counter) { 30 402 : ASSERT(wip_counter_ == nullptr); 31 402 : wip_counter_ = &wip_counter; 32 402 : wip_counter.add(prestats_wip_count_); 33 402 : } 34 : 35 192 : void WipCounterBase::onWorkInProgressCommon(absl::string_view description) { 36 192 : ENVOY_LOG_MISC(warn, "{}", description); 37 192 : if (wip_counter_ != nullptr) { 38 0 : wip_counter_->inc(); 39 192 : } else { 40 192 : prestats_wip_count_++; 41 192 : } 42 192 : } 43 : 44 : void WarningValidationVisitorImpl::setCounters(Stats::Counter& unknown_counter, 45 268 : Stats::Counter& wip_counter) { 46 268 : setWipCounter(wip_counter); 47 268 : ASSERT(unknown_counter_ == nullptr); 48 268 : unknown_counter_ = &unknown_counter; 49 268 : unknown_counter.add(prestats_unknown_count_); 50 268 : } 51 : 52 0 : void WarningValidationVisitorImpl::onUnknownField(absl::string_view description) { 53 0 : const uint64_t hash = HashUtil::xxHash64(description); 54 0 : auto it = descriptions_.insert(hash); 55 : // If we've seen this before, skip. 56 0 : if (!it.second) { 57 0 : return; 58 0 : } 59 : 60 : // It's a new field, log and bump stat. 61 0 : ENVOY_LOG(warn, "Unknown field: {}", description); 62 0 : if (unknown_counter_ == nullptr) { 63 0 : ++prestats_unknown_count_; 64 0 : } else { 65 0 : unknown_counter_->inc(); 66 0 : } 67 0 : } 68 : 69 : void WarningValidationVisitorImpl::onDeprecatedField(absl::string_view description, 70 0 : bool soft_deprecation) { 71 0 : onDeprecatedFieldCommon(description, soft_deprecation); 72 0 : } 73 : 74 0 : void WarningValidationVisitorImpl::onWorkInProgress(absl::string_view description) { 75 0 : onWorkInProgressCommon(description); 76 0 : } 77 : 78 22 : void StrictValidationVisitorImpl::onUnknownField(absl::string_view description) { 79 22 : throwExceptionOrPanic(UnknownProtoFieldException, 80 22 : absl::StrCat("Protobuf message (", description, ") has unknown fields")); 81 22 : } 82 : 83 : void StrictValidationVisitorImpl::onDeprecatedField(absl::string_view description, 84 908 : bool soft_deprecation) { 85 908 : onDeprecatedFieldCommon(description, soft_deprecation); 86 908 : } 87 : 88 192 : void StrictValidationVisitorImpl::onWorkInProgress(absl::string_view description) { 89 192 : onWorkInProgressCommon(description); 90 192 : } 91 : 92 2562 : ValidationVisitor& getNullValidationVisitor() { 93 2562 : MUTABLE_CONSTRUCT_ON_FIRST_USE(NullValidationVisitorImpl); 94 2562 : } 95 : 96 10113 : ValidationVisitor& getStrictValidationVisitor() { 97 10113 : MUTABLE_CONSTRUCT_ON_FIRST_USE(StrictValidationVisitorImpl); 98 10113 : } 99 : 100 : } // namespace ProtobufMessage 101 : } // namespace Envoy