Coverage Report

Created: 2024-09-19 09:45

/proc/self/cwd/source/common/version/version.cc
Line
Count
Source
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
4.51k
const std::string& VersionInfo::revision() {
24
4.51k
  CONSTRUCT_ON_FIRST_USE(std::string, build_scm_revision);
25
4.51k
}
26
27
145
const std::string& VersionInfo::revisionStatus() {
28
145
  CONSTRUCT_ON_FIRST_USE(std::string, build_scm_status);
29
145
}
30
31
132
const std::string& VersionInfo::version() {
32
132
  CONSTRUCT_ON_FIRST_USE(std::string,
33
132
                         fmt::format("{}/{}/{}/{}/{}", revision(), BUILD_VERSION_NUMBER,
34
132
                                     revisionStatus(), buildType(), sslVersion()));
35
132
}
36
37
8.62k
const envoy::config::core::v3::BuildVersion& VersionInfo::buildVersion() {
38
8.62k
  static const auto* result =
39
8.62k
      new envoy::config::core::v3::BuildVersion(makeBuildVersion(BUILD_VERSION_NUMBER));
40
8.62k
  return *result;
41
8.62k
}
42
43
4.47k
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
4.47k
  return false;
49
4.47k
#endif
50
4.47k
}
51
52
145
const std::string& VersionInfo::buildType() {
53
#ifdef NDEBUG
54
  static const std::string release_type = "RELEASE";
55
#else
56
145
  static const std::string release_type = "DEBUG";
57
145
#endif
58
145
  return release_type;
59
145
}
60
61
145
const std::string& VersionInfo::sslVersion() {
62
145
#ifdef ENVOY_SSL_VERSION
63
145
  static const std::string ssl_version = ENVOY_SSL_VERSION;
64
#else
65
  static const std::string ssl_version = "no-ssl";
66
#endif
67
145
  return ssl_version;
68
145
}
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