Repository: envoyproxy/envoy
File: source/extensions/clusters/reverse_connection/reverse_connection.cc (C++)

Code before:
  if (cluster.lb_policy() != envoy::config::cluster::v3::Cluster::CLUSTER_PROVIDED) {
    return absl::InvalidArgumentError(
        fmt::format("cluster: LB policy {} is not valid for Cluster type {}. Only "
                    "'CLUSTER_PROVIDED' is allowed with cluster type 'REVERSE_CONNECTION'",
                    envoy::config::cluster::v3::Cluster::LbPolicy_Name(cluster.lb_policy()),
                    cluster.cluster_type().name()));
  }

  if (cluster.has_load_assignment()) {
    return absl::InvalidArgumentError(
        "Reverse Conn clusters must have no load assignment configured");
  }

  // Validate that the required bootstrap extension is configured using Envoy's standard utility.
  const std::string extension_name = "envoy.bootstrap.reverse_tunnel.upstream_socket_interface";
  auto* factory =
      Config::Utility::getAndCheckFactoryByName<Server::Configuration::BootstrapExtensionFactory>(
          extension_name, /*is_optional=*/true);
  if (factory == nullptr) {
    return absl::InvalidArgumentError(fmt::format(
        "Reverse connection cluster requires the upstream reverse tunnel bootstrap extension '{}' "
        "to be configured. Please add it to bootstrap_extensions in your bootstrap configuration.",
        extension_name));
  }

  // Validate that the factory is a socket interface (it implements both interfaces).
  auto* upstream_socket_interface =
      dynamic_cast<const BootstrapReverseConnection::ReverseTunnelAcceptor*>(factory);
  if (upstream_socket_interface == nullptr) {
    return absl::InvalidArgumentError(
        fmt::format("Bootstrap extension '{}' exists but is not of the expected type "
                    "(ReverseTunnelAcceptor). This indicates a configuration error.",
                    extension_name));
  }

  // Validate the host_id_format early to catch formatter errors.
  auto validation_or_error = Envoy::Formatter::FormatterImpl::create(
      proto_config.host_id_format(), /*omit_empty_values=*/false,
      Envoy::Formatter::BuiltInCommandParserFactoryHelper::commandParsers());
  RETURN_IF_NOT_OK_REF(validation_or_error.status());

  absl::Status creation_status = absl::OkStatus();
  auto new_cluster = std::shared_ptr<RevConCluster>(
      new RevConCluster(cluster, context, creation_status, proto_config));
  RETURN_IF_NOT_OK(creation_status);
  auto lb = std::make_unique<RevConCluster::ThreadAwareLoadBalancer>(new_cluster);
  return std::make_pair(new_cluster, std::move(lb));
}

/**
 * Static registration for the rev-con cluster factory. @see RegisterFactory.

Review comment (suggestion):
```suggestion
  // Validate that the factory is a ReverseTunnelAcceptor.
```

Code after:
  if (cluster.lb_policy() != envoy::config::cluster::v3::Cluster::CLUSTER_PROVIDED) {
    return absl::InvalidArgumentError(
        fmt::format("cluster: LB policy {} is not valid for Cluster type {}. Only "
                    "'CLUSTER_PROVIDED' is allowed with cluster type 'REVERSE_CONNECTION'",
                    envoy::config::cluster::v3::Cluster::LbPolicy_Name(cluster.lb_policy()),
                    cluster.cluster_type().name()));
  }

  if (cluster.has_load_assignment()) {
    return absl::InvalidArgumentError(
        "Reverse Conn clusters must have no load assignment configured");
  }

  // Validate that the required bootstrap extension is configured using Envoy's standard utility.
  const std::string extension_name = "envoy.bootstrap.reverse_tunnel.upstream_socket_interface";
  auto* factory =
      Config::Utility::getAndCheckFactoryByName<Server::Configuration::BootstrapExtensionFactory>(
          extension_name, /*is_optional=*/true);
  if (factory == nullptr) {
    return absl::InvalidArgumentError(fmt::format(
        "Reverse connection cluster requires the upstream reverse tunnel bootstrap extension '{}' "
        "to be configured. Please add it to bootstrap_extensions in your bootstrap configuration.",
        extension_name));
  }

  // Validate that the factory is a ReverseTunnelAcceptor.
  auto* upstream_socket_interface =
      dynamic_cast<const BootstrapReverseConnection::ReverseTunnelAcceptor*>(factory);
  if (upstream_socket_interface == nullptr) {
    return absl::InvalidArgumentError(
        fmt::format("Bootstrap extension '{}' exists but is not of the expected type "
                    "(ReverseTunnelAcceptor). This indicates a configuration error.",
                    extension_name));
  }

  // Validate the host_id_format early to catch formatter errors.
  auto validation_or_error = Envoy::Formatter::FormatterImpl::create(
      proto_config.host_id_format(), /*omit_empty_values=*/false,
      Envoy::Formatter::BuiltInCommandParserFactoryHelper::commandParsers());
  RETURN_IF_NOT_OK_REF(validation_or_error.status());

  absl::Status creation_status = absl::OkStatus();
  auto new_cluster = std::shared_ptr<RevConCluster>(
      new RevConCluster(cluster, context, creation_status, proto_config));
  RETURN_IF_NOT_OK(creation_status);
  auto lb = std::make_unique<RevConCluster::ThreadAwareLoadBalancer>(new_cluster);
  return std::make_pair(new_cluster, std::move(lb));
}

/**
 * Static registration for the rev-con cluster factory. @see RegisterFactory.