1
#pragma once
2

            
3
#include <string>
4
#include <vector>
5

            
6
#include "envoy/common/optref.h"
7
#include "envoy/config/subscription.h"
8
#include "envoy/server/instance.h"
9

            
10
#include "source/common/protobuf/protobuf.h"
11

            
12
namespace Envoy {
13
namespace Config {
14

            
15
/**
16
 * The configuration validator interface. One can implement such validator
17
 * to add custom constraints to the fetched config, and reject a config
18
 * which violates these constraints.
19
 * The validators will be extensions that can be dynamically configured.
20
 *
21
 * A validator example: a validator that prevents removing all Clusters
22
 * from an Envoy (which may be caused by a bug in the config plane, and not
23
 * intentional change).
24
 */
25
class ConfigValidator {
26
public:
27
15
  virtual ~ConfigValidator() = default;
28

            
29
  /**
30
   * Validates a given set of resources matching a State-of-the-World update.
31
   * @param server A server instance to fetch the state before applying the config.
32
   * @param resources List of decoded resources that reflect the new state.
33
   * @throw EnvoyException if the config should be rejected.
34
   */
35
  virtual void validate(const Server::Instance& server,
36
                        const std::vector<DecodedResourcePtr>& resources) PURE;
37

            
38
  /**
39
   * Validates a given set of resources matching an Incremental update.
40
   * @param server A server instance to fetch the state before applying the config.
41
   * @param added_resources A list of decoded resources to add to the current state.
42
   * @param removed_resources A list of resources to remove from the current state.
43
   * @throw EnvoyException if the config should be rejected.
44
   */
45
  virtual void validate(const Server::Instance& server,
46
                        const std::vector<DecodedResourcePtr>& added_resources,
47
                        const Protobuf::RepeatedPtrField<std::string>& removed_resources) PURE;
48
};
49

            
50
using ConfigValidatorPtr = std::unique_ptr<ConfigValidator>;
51

            
52
/**
53
 * A factory abstract class for creating instances of ConfigValidators.
54
 */
55
class ConfigValidatorFactory : public Config::TypedFactory {
56
public:
57
10
  ~ConfigValidatorFactory() override = default;
58

            
59
  /**
60
   * Creates a ConfigValidator using the given config.
61
   */
62
  virtual ConfigValidatorPtr
63
  createConfigValidator(const Protobuf::Any& config,
64
                        ProtobufMessage::ValidationVisitor& validation_visitor) PURE;
65

            
66
108
  std::string category() const override { return "envoy.config.validators"; }
67

            
68
  /**
69
   * Returns the xDS service type url that the config validator expects to receive.
70
   */
71
  virtual std::string typeUrl() const PURE;
72
};
73

            
74
} // namespace Config
75
} // namespace Envoy