LCOV - code coverage report
Current view: top level - source/common/version - version.cc (source / functions) Hit Total Coverage
Test: coverage.dat Lines: 61 61 100.0 %
Date: 2024-01-05 06:35:25 Functions: 8 8 100.0 %

          Line data    Source code
       1             : #include "source/common/version/version.h"
       2             : 
       3             : #include <map>
       4             : #include <regex>
       5             : #include <string>
       6             : 
       7             : #include "source/common/common/fmt.h"
       8             : #include "source/common/common/macros.h"
       9             : #include "source/common/protobuf/utility.h"
      10             : 
      11             : #include "absl/strings/numbers.h"
      12             : #include "absl/strings/str_split.h"
      13             : #include "absl/strings/string_view.h"
      14             : 
      15             : #ifdef ENVOY_SSL_FIPS
      16             : #include "openssl/crypto.h"
      17             : #endif
      18             : 
      19             : extern const char build_scm_revision[];
      20             : extern const char build_scm_status[];
      21             : 
      22             : namespace Envoy {
      23         216 : const std::string& VersionInfo::revision() {
      24         216 :   CONSTRUCT_ON_FIRST_USE(std::string, build_scm_revision);
      25         216 : }
      26             : 
      27          82 : const std::string& VersionInfo::revisionStatus() {
      28          82 :   CONSTRUCT_ON_FIRST_USE(std::string, build_scm_status);
      29          82 : }
      30             : 
      31          69 : const std::string& VersionInfo::version() {
      32          69 :   CONSTRUCT_ON_FIRST_USE(std::string,
      33          69 :                          fmt::format("{}/{}/{}/{}/{}", revision(), BUILD_VERSION_NUMBER,
      34          69 :                                      revisionStatus(), buildType(), sslVersion()));
      35          69 : }
      36             : 
      37         248 : const envoy::config::core::v3::BuildVersion& VersionInfo::buildVersion() {
      38         248 :   static const auto* result =
      39         248 :       new envoy::config::core::v3::BuildVersion(makeBuildVersion(BUILD_VERSION_NUMBER));
      40         248 :   return *result;
      41         248 : }
      42             : 
      43         134 : bool VersionInfo::sslFipsCompliant() {
      44             : #ifdef ENVOY_SSL_FIPS
      45             :   RELEASE_ASSERT(FIPS_mode() == 1, "FIPS mode must be enabled in Envoy FIPS configuration.");
      46             :   return true;
      47             : #else
      48         134 :   return false;
      49         134 : #endif
      50         134 : }
      51             : 
      52          82 : const std::string& VersionInfo::buildType() {
      53          82 : #ifdef NDEBUG
      54          82 :   static const std::string release_type = "RELEASE";
      55             : #else
      56             :   static const std::string release_type = "DEBUG";
      57             : #endif
      58          82 :   return release_type;
      59          82 : }
      60             : 
      61          82 : const std::string& VersionInfo::sslVersion() {
      62          82 : #ifdef ENVOY_SSL_VERSION
      63          82 :   static const std::string ssl_version = ENVOY_SSL_VERSION;
      64             : #else
      65             :   static const std::string ssl_version = "no-ssl";
      66             : #endif
      67          82 :   return ssl_version;
      68          82 : }
      69             : 
      70          13 : envoy::config::core::v3::BuildVersion VersionInfo::makeBuildVersion(const char* version) {
      71          13 :   envoy::config::core::v3::BuildVersion result;
      72             :   // Split BUILD_VERSION_NUMBER into version and an optional build label after the '-'
      73          13 :   std::regex ver_regex("([\\d]+)\\.([\\d]+)\\.([\\d]+)(-(.*))?");
      74             :   // Match indexes, given the regex above
      75          13 :   constexpr std::cmatch::size_type major = 1;
      76          13 :   constexpr std::cmatch::size_type minor = 2;
      77          13 :   constexpr std::cmatch::size_type patch = 3;
      78          13 :   constexpr std::cmatch::size_type label = 5;
      79          13 :   std::cmatch match;
      80          13 :   if (std::regex_match(version, match, ver_regex)) {
      81          13 :     int value = 0;
      82          13 :     if (absl::SimpleAtoi(match.str(major), &value)) {
      83          13 :       result.mutable_version()->set_major_number(value);
      84          13 :     }
      85          13 :     if (absl::SimpleAtoi(match.str(minor), &value)) {
      86          13 :       result.mutable_version()->set_minor_number(value);
      87          13 :     }
      88          13 :     if (absl::SimpleAtoi(match.str(patch), &value)) {
      89          13 :       result.mutable_version()->set_patch(value);
      90          13 :     }
      91          13 :   }
      92          13 :   std::map<std::string, std::string> fields;
      93          13 :   if (!match.str(label).empty()) {
      94          13 :     fields[BuildVersionMetadataKeys::get().BuildLabel] = match.str(label);
      95          13 :   }
      96          13 :   fields[BuildVersionMetadataKeys::get().BuildType] = buildType();
      97          13 :   fields[BuildVersionMetadataKeys::get().SslVersion] = sslVersion();
      98          13 :   fields[BuildVersionMetadataKeys::get().RevisionSHA] = revision();
      99          13 :   fields[BuildVersionMetadataKeys::get().RevisionStatus] = revisionStatus();
     100          13 :   *result.mutable_metadata() = MessageUtil::keyValueStruct(fields);
     101          13 :   return result;
     102          13 : }
     103             : 
     104             : } // namespace Envoy

Generated by: LCOV version 1.15