Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/source/server/config_validation/server.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include <iostream>
4
5
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
6
#include "envoy/config/core/v3/config_source.pb.h"
7
#include "envoy/config/listener/v3/listener.pb.h"
8
#include "envoy/config/listener/v3/listener_components.pb.h"
9
#include "envoy/event/timer.h"
10
#include "envoy/server/drain_manager.h"
11
#include "envoy/server/instance.h"
12
#include "envoy/ssl/context_manager.h"
13
#include "envoy/tracing/tracer.h"
14
15
#include "source/common/access_log/access_log_manager_impl.h"
16
#include "source/common/common/assert.h"
17
#include "source/common/common/random_generator.h"
18
#include "source/common/grpc/common.h"
19
#include "source/common/network/dns_resolver/dns_factory_util.h"
20
#include "source/common/protobuf/message_validator_impl.h"
21
#include "source/common/quic/quic_stat_names.h"
22
#include "source/common/router/context_impl.h"
23
#include "source/common/router/rds_impl.h"
24
#include "source/common/runtime/runtime_impl.h"
25
#include "source/common/secret/secret_manager_impl.h"
26
#include "source/common/thread_local/thread_local_impl.h"
27
#include "source/server/config_validation/admin.h"
28
#include "source/server/config_validation/api.h"
29
#include "source/server/config_validation/cluster_manager.h"
30
#include "source/server/config_validation/dns.h"
31
#include "source/server/hot_restart_nop_impl.h"
32
#include "source/server/server.h"
33
34
#include "absl/types/optional.h"
35
36
namespace Envoy {
37
namespace Server {
38
39
/**
40
 * validateConfig() takes over from main() for a config-validation run of Envoy. It returns true if
41
 * the config is valid, false if invalid.
42
 */
43
bool validateConfig(const Options& options,
44
                    const Network::Address::InstanceConstSharedPtr& local_address,
45
                    ComponentFactory& component_factory, Thread::ThreadFactory& thread_factory,
46
                    Filesystem::Instance& file_system,
47
                    const ProcessContextOptRef& process_context = absl::nullopt);
48
49
/**
50
 * ValidationInstance does the bulk of the work for config-validation runs of Envoy. It implements
51
 * Server::Instance, but some functionality not needed until serving time, such as updating
52
 * health-check status, is not implemented. Everything else is written in terms of other
53
 * validation-specific interface implementations, with the end result that we can load and
54
 * initialize a configuration, skipping any steps that affect the outside world (such as
55
 * hot-restarting or connecting to upstream clusters) but otherwise exercising the entire startup
56
 * flow.
57
 *
58
 * If we finish initialization, and reach the point where an ordinary Envoy run would begin serving
59
 * requests, the validation is considered successful.
60
 */
61
class ValidationInstance final : Logger::Loggable<Logger::Id::main>,
62
                                 public Instance,
63
                                 public ServerLifecycleNotifier,
64
                                 public WorkerFactory {
65
public:
66
  ValidationInstance(const Options& options, Event::TimeSystem& time_system,
67
                     const Network::Address::InstanceConstSharedPtr& local_address,
68
                     Stats::IsolatedStoreImpl& store, Thread::BasicLockable& access_log_lock,
69
                     ComponentFactory& component_factory, Thread::ThreadFactory& thread_factory,
70
                     Filesystem::Instance& file_system,
71
                     const ProcessContextOptRef& process_context = absl::nullopt);
72
73
  // Server::Instance
74
0
  void run() override { PANIC("not implemented"); }
75
0
  OptRef<Admin> admin() override {
76
0
    return makeOptRefFromPtr(static_cast<Envoy::Server::Admin*>(admin_.get()));
77
0
  }
78
22.1k
  Api::Api& api() override { return *api_; }
79
0
  Upstream::ClusterManager& clusterManager() override { return *config_.clusterManager(); }
80
0
  const Upstream::ClusterManager& clusterManager() const override {
81
0
    return *config_.clusterManager();
82
0
  }
83
0
  Ssl::ContextManager& sslContextManager() override { return *ssl_context_manager_; }
84
5.21k
  Event::Dispatcher& dispatcher() override { return *dispatcher_; }
85
0
  Network::DnsResolverSharedPtr dnsResolver() override {
86
0
    envoy::config::core::v3::TypedExtensionConfig typed_dns_resolver_config;
87
0
    Network::DnsResolverFactory& dns_resolver_factory =
88
0
        Network::createDefaultDnsResolverFactory(typed_dns_resolver_config);
89
0
    return dns_resolver_factory.createDnsResolver(dispatcher(), api(), typed_dns_resolver_config);
90
0
  }
91
0
  void drainListeners(OptRef<const Network::ExtraShutdownListenerOptions>) override {}
92
0
  DrainManager& drainManager() override { return *drain_manager_; }
93
153
  AccessLog::AccessLogManager& accessLogManager() override { return access_log_manager_; }
94
0
  void failHealthcheck(bool) override {}
95
0
  HotRestart& hotRestart() override { return nop_hot_restart_; }
96
22.1k
  Init::Manager& initManager() override { return init_manager_; }
97
0
  ServerLifecycleNotifier& lifecycleNotifier() override { return *this; }
98
0
  ListenerManager& listenerManager() override { return *listener_manager_; }
99
0
  Secret::SecretManager& secretManager() override { return *secret_manager_; }
100
22.1k
  Runtime::Loader& runtime() override { return *runtime_; }
101
  void shutdown() override;
102
0
  bool isShutdown() override { return false; }
103
0
  void shutdownAdmin() override {}
104
0
  Singleton::Manager& singletonManager() override { return *singleton_manager_; }
105
0
  OverloadManager& overloadManager() override { return *overload_manager_; }
106
0
  bool healthCheckFailed() override { return false; }
107
0
  const Options& options() override { return options_; }
108
0
  time_t startTimeCurrentEpoch() override { PANIC("not implemented"); }
109
0
  time_t startTimeFirstEpoch() override { PANIC("not implemented"); }
110
17.1k
  Stats::Store& stats() override { return stats_store_; }
111
0
  Grpc::Context& grpcContext() override { return grpc_context_; }
112
0
  Http::Context& httpContext() override { return http_context_; }
113
0
  Router::Context& routerContext() override { return router_context_; }
114
0
  ProcessContextOptRef processContext() override { return api_->processContext(); }
115
5.21k
  ThreadLocal::Instance& threadLocal() override { return thread_local_; }
116
0
  LocalInfo::LocalInfo& localInfo() const override { return *local_info_; }
117
0
  TimeSource& timeSource() override { return api_->timeSource(); }
118
0
  Envoy::MutexTracer* mutexTracer() override { return mutex_tracer_; }
119
0
  void flushStats() override {}
120
40.0k
  ProtobufMessage::ValidationContext& messageValidationContext() override {
121
40.0k
    return validation_context_;
122
40.0k
  }
123
0
  bool enableReusePortDefault() override { return true; }
124
125
0
  Configuration::StatsConfig& statsConfig() override { return config_.statsConfig(); }
126
0
  envoy::config::bootstrap::v3::Bootstrap& bootstrap() override { return bootstrap_; }
127
7.46k
  Configuration::ServerFactoryContext& serverFactoryContext() override { return server_contexts_; }
128
0
  Configuration::TransportSocketFactoryContext& transportSocketFactoryContext() override {
129
0
    return server_contexts_;
130
0
  }
131
0
  void setDefaultTracingConfig(const envoy::config::trace::v3::Tracing& tracing_config) override {
132
0
    http_context_.setDefaultTracingConfig(tracing_config);
133
0
  }
134
0
  void setSinkPredicates(std::unique_ptr<Stats::SinkPredicates>&&) override {}
135
136
  // Server::WorkerFactory
137
0
  WorkerPtr createWorker(uint32_t, OverloadManager&, const std::string&) override {
138
    // Returned workers are not currently used so we can return nothing here safely vs. a
139
    // validation mock.
140
0
    return nullptr;
141
0
  }
142
143
  // ServerLifecycleNotifier
144
0
  ServerLifecycleNotifier::HandlePtr registerCallback(Stage, StageCallback) override {
145
0
    return nullptr;
146
0
  }
147
0
  ServerLifecycleNotifier::HandlePtr registerCallback(Stage, StageCallbackWithCompletion) override {
148
0
    return nullptr;
149
0
  }
150
151
private:
152
  void initialize(const Options& options,
153
                  const Network::Address::InstanceConstSharedPtr& local_address,
154
                  ComponentFactory& component_factory);
155
156
  // init_manager_ must come before any member that participates in initialization, and destructed
157
  // only after referencing members are gone, since initialization continuation can potentially
158
  // occur at any point during member lifetime.
159
  Init::ManagerImpl init_manager_{"Validation server"};
160
0
  Init::WatcherImpl init_watcher_{"(no-op)", []() {}};
161
  // secret_manager_ must come before listener_manager_, config_ and dispatcher_, and destructed
162
  // only after these members can no longer reference it, since:
163
  // - There may be active filter chains referencing it in listener_manager_.
164
  // - There may be active clusters referencing it in config_.cluster_manager_.
165
  // - There may be active connections referencing it.
166
  std::unique_ptr<Secret::SecretManager> secret_manager_;
167
  const Options& options_;
168
  ProtobufMessage::ProdValidationContextImpl validation_context_;
169
  Stats::IsolatedStoreImpl& stats_store_;
170
  ThreadLocal::InstanceImpl thread_local_;
171
  envoy::config::bootstrap::v3::Bootstrap bootstrap_;
172
  Api::ApiPtr api_;
173
  // ssl_context_manager_ must come before dispatcher_, since ClusterInfo
174
  // references SslSocketFactory and is deleted on the main thread via the dispatcher.
175
  std::unique_ptr<Ssl::ContextManager> ssl_context_manager_;
176
  Event::DispatcherPtr dispatcher_;
177
  std::unique_ptr<Server::ValidationAdmin> admin_;
178
  Singleton::ManagerPtr singleton_manager_;
179
  std::unique_ptr<Runtime::Loader> runtime_;
180
  Random::RandomGeneratorImpl random_generator_;
181
  Configuration::MainImpl config_;
182
  LocalInfo::LocalInfoPtr local_info_;
183
  AccessLog::AccessLogManagerImpl access_log_manager_;
184
  std::unique_ptr<Upstream::ValidationClusterManagerFactory> cluster_manager_factory_;
185
  std::unique_ptr<ListenerManager> listener_manager_;
186
  std::unique_ptr<OverloadManager> overload_manager_;
187
  MutexTracer* mutex_tracer_{nullptr};
188
  Grpc::ContextImpl grpc_context_;
189
  Http::ContextImpl http_context_;
190
  Router::ContextImpl router_context_;
191
  Event::TimeSystem& time_system_;
192
  ServerFactoryContextImpl server_contexts_;
193
  Quic::QuicStatNames quic_stat_names_;
194
  Filter::TcpListenerFilterConfigProviderManagerImpl tcp_listener_config_provider_manager_;
195
  Server::DrainManagerPtr drain_manager_;
196
  HotRestartNopImpl nop_hot_restart_;
197
};
198
199
} // namespace Server
200
} // namespace Envoy