Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
/*
* Copyright 2026 LY Corporation
*
* LY Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.armeria.xds.it;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.stream.Stream;

import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import com.linecorp.armeria.client.BlockingWebClient;
import com.linecorp.armeria.client.ClientRequestContextCaptor;
import com.linecorp.armeria.client.Clients;
import com.linecorp.armeria.client.WebClient;
import com.linecorp.armeria.common.AggregatedHttpResponse;
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.HttpStatus;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.server.ServerBuilder;
import com.linecorp.armeria.testing.junit5.server.SelfSignedCertificateExtension;
import com.linecorp.armeria.testing.junit5.server.ServerExtension;
import com.linecorp.armeria.xds.XdsBootstrap;
import com.linecorp.armeria.xds.client.endpoint.XdsHttpPreprocessor;

import io.envoyproxy.envoy.config.bootstrap.v3.Bootstrap;

class HttpProtocolOptionsTest {

@RegisterExtension
@Order(0)
static XdsCertificateExtension cert = new XdsCertificateExtension(new SelfSignedCertificateExtension());

@RegisterExtension
@Order(1)
static ServerExtension server = new ServerExtension() {
@Override
protected void configure(ServerBuilder sb) {
sb.http(0);
sb.https(0);
sb.tls(cert.tlsKeyPair());
sb.service("/", (ctx, req) -> HttpResponse.of(200));
}
};

// language=YAML
private static final String BOOTSTRAP_TEMPLATE =
"""
static_resources:
listeners:
- name: my-listener
api_listener:
api_listener:
"@type": type.googleapis.com/envoy.extensions.filters.network.\
http_connection_manager.v3.HttpConnectionManager
stat_prefix: http
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: [ "*" ]
routes:
- match:
prefix: /
route:
cluster: my-cluster
http_filters:
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.\
http.router.v3.Router
clusters:
- name: my-cluster
type: STATIC
load_assignment:
cluster_name: my-cluster
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: 127.0.0.1
port_value: %d
typed_extension_protocol_options:
envoy.extensions.upstreams.http.v3.HttpProtocolOptions:
"@type": type.googleapis.com/envoy.extensions.upstreams.\
http.v3.HttpProtocolOptions
%s
""";

static Stream<Arguments> sessionProtocolSelection() {
return Stream.of(
Arguments.of("explicit_http_config: {http2_protocol_options: {}}",
SessionProtocol.H2C),
Arguments.of("explicit_http_config: {http_protocol_options: {}}",
SessionProtocol.H1C),
Arguments.of("auto_config: {}", SessionProtocol.HTTP)
);
}

@ParameterizedTest
@MethodSource
void sessionProtocolSelection(String protocolConfig,
SessionProtocol expectedProtocol) {
final String yaml = BOOTSTRAP_TEMPLATE.formatted(
server.httpPort(), protocolConfig);
final Bootstrap bootstrap = XdsResourceReader.fromYaml(yaml, Bootstrap.class);
try (XdsBootstrap xdsBootstrap = XdsBootstrap.of(bootstrap);
XdsHttpPreprocessor preprocessor = XdsHttpPreprocessor.ofListener("my-listener", xdsBootstrap);
ClientRequestContextCaptor captor = Clients.newContextCaptor()) {
final BlockingWebClient client = WebClient.of(preprocessor).blocking();
final AggregatedHttpResponse res = client.get("/");
assertThat(res.status()).isEqualTo(HttpStatus.OK);
assertThat(captor.get().sessionProtocol()).isEqualTo(expectedProtocol);
}
}

// language=YAML
private static final String TLS_BOOTSTRAP_TEMPLATE =
"""
static_resources:
listeners:
- name: my-listener
api_listener:
api_listener:
"@type": type.googleapis.com/envoy.extensions.filters.network.\
http_connection_manager.v3.HttpConnectionManager
stat_prefix: http
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: [ "*" ]
routes:
- match:
prefix: /
route:
cluster: my-cluster
http_filters:
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.\
http.router.v3.Router
clusters:
- name: my-cluster
type: STATIC
load_assignment:
cluster_name: my-cluster
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: 127.0.0.1
port_value: %d
transport_socket:
name: envoy.transport_sockets.tls
typed_config:
"@type": type.googleapis.com/envoy.extensions.transport_sockets.\
tls.v3.UpstreamTlsContext
common_tls_context: {}
typed_extension_protocol_options:
envoy.extensions.upstreams.http.v3.HttpProtocolOptions:
"@type": type.googleapis.com/envoy.extensions.upstreams.\
http.v3.HttpProtocolOptions
%s
""";

static Stream<Arguments> tlsSessionProtocolSelection() {
return Stream.of(
Arguments.of("explicit_http_config: {http2_protocol_options: {}}",
SessionProtocol.H2),
Arguments.of("explicit_http_config: {http_protocol_options: {}}",
SessionProtocol.H1),
Arguments.of("auto_config: {}", SessionProtocol.HTTPS)
);
}

@ParameterizedTest
@MethodSource
void tlsSessionProtocolSelection(String protocolConfig,
SessionProtocol expectedProtocol) {
final String yaml = TLS_BOOTSTRAP_TEMPLATE.formatted(
server.httpsPort(), protocolConfig);
final Bootstrap bootstrap = XdsResourceReader.fromYaml(yaml, Bootstrap.class);
try (XdsBootstrap xdsBootstrap = XdsBootstrap.of(bootstrap);
XdsHttpPreprocessor preprocessor = XdsHttpPreprocessor.ofListener("my-listener", xdsBootstrap);
ClientRequestContextCaptor captor = Clients.newContextCaptor()) {
final BlockingWebClient client = WebClient.of(preprocessor).blocking();
final AggregatedHttpResponse res = client.get("/");
assertThat(res.status()).isEqualTo(HttpStatus.OK);
assertThat(captor.get().sessionProtocol()).isEqualTo(expectedProtocol);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,7 @@ message Cluster {
// specific options.
// [#next-major-version: make this a list of typed extensions.]
// [#extension-category: envoy.upstream_options]
option (armeria.xds.supported.field) = 36;
map<string, google.protobuf.Any> typed_extension_protocol_options = 36;

// If the DNS refresh rate is specified and the cluster type is either
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import "envoy/extensions/filters/network/http_connection_manager/v3/http_connect
import "udpa/annotations/status.proto";
import "validate/validate.proto";

import "armeria/xds/supported.proto";

option java_package = "io.envoyproxy.envoy.extensions.upstreams.http.v3";
option java_outer_classname = "HttpProtocolOptionsProto";
option java_multiple_files = true;
Expand Down Expand Up @@ -69,8 +71,10 @@ message HttpProtocolOptions {
oneof protocol_config {
option (validate.required) = true;

option (armeria.xds.supported.oneof_field) = 1;
config.core.v3.Http1ProtocolOptions http_protocol_options = 1;

option (armeria.xds.supported.oneof_field) = 2;
config.core.v3.Http2ProtocolOptions http2_protocol_options = 2;

// .. warning::
Expand Down Expand Up @@ -149,13 +153,15 @@ message HttpProtocolOptions {
option (validate.required) = true;

// To explicitly configure either HTTP/1 or HTTP/2 (but not both!) use ``explicit_http_config``.
option (armeria.xds.supported.oneof_field) = 3;
ExplicitHttpConfig explicit_http_config = 3;

// This allows switching on protocol based on what protocol the downstream
// connection used.
UseDownstreamHttpConfig use_downstream_protocol_config = 4;

// This allows switching on protocol based on ALPN
option (armeria.xds.supported.oneof_field) = 5;
AutoHttpConfig auto_config = 5;
Comment thread
jrhee17 marked this conversation as resolved.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.util.Set;

import com.google.common.base.MoreObjects;

import com.linecorp.armeria.client.ClientDecoration;
import com.linecorp.armeria.client.ClientRequestContext;
import com.linecorp.armeria.client.ClientTlsSpec;
Expand All @@ -44,6 +46,9 @@
import com.linecorp.armeria.xds.internal.XdsCommonUtil;
import com.linecorp.armeria.xds.internal.XdsEndpoint;

import io.envoyproxy.envoy.extensions.upstreams.http.v3.HttpProtocolOptions;
import io.envoyproxy.envoy.extensions.upstreams.http.v3.HttpProtocolOptions.ExplicitHttpConfig;

/**
* A factory which injects cluster-related filters.
* <pre>{@code
Expand Down Expand Up @@ -71,12 +76,21 @@ final class ClusterFilterFactory {

private final XdsEndpointGroup endpointGroup;
private final SessionProtocol sessionProtocol;
@Nullable
private final HttpProtocolOptions httpProtocolOptions;

ClusterFilterFactory(XdsLoadBalancer loadBalancer,
@Nullable HttpProtocolOptions httpProtocolOptions,
TransportSocketSnapshot transportSocket) {
endpointGroup = XdsEndpointGroup.of(loadBalancer);
sessionProtocol = transportSocket.clientTlsSpec() != null ?
SessionProtocol.HTTPS : SessionProtocol.HTTP;
this.httpProtocolOptions = httpProtocolOptions;
final boolean tls = transportSocket.clientTlsSpec() != null;
sessionProtocol = sessionProtocol(tls, httpProtocolOptions);
}

@Nullable
HttpProtocolOptions httpProtocolOptions() {
return httpProtocolOptions;
}

HttpPreprocessor httpPreprocessor() {
Expand Down Expand Up @@ -149,4 +163,25 @@ private static void applyClusterSettings(ClientRequestContext ctx) {
}
ctx.setClientTlsSpec(clientTlsSpec);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("sessionProtocol", sessionProtocol)
.toString();
}

private static SessionProtocol sessionProtocol(boolean tls,
@Nullable HttpProtocolOptions httpProtocolOptions) {
if (httpProtocolOptions != null && httpProtocolOptions.hasExplicitHttpConfig()) {
final ExplicitHttpConfig explicitConfig = httpProtocolOptions.getExplicitHttpConfig();
if (explicitConfig.hasHttp2ProtocolOptions()) {
return tls ? SessionProtocol.H2 : SessionProtocol.H2C;
}
if (explicitConfig.hasHttpProtocolOptions()) {
return tls ? SessionProtocol.H1 : SessionProtocol.H1C;
}
}
return tls ? SessionProtocol.HTTPS : SessionProtocol.HTTP;
}
}
Loading
Loading