-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Resolve trusted proxy host names to all available A/AAAA records #43188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
geoand
merged 2 commits into
quarkusio:main
from
ahus1:is-42782-fix-dns-lookup-trusted-proxies
Feb 18, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
.../deployment/src/test/java/io/quarkus/vertx/http/proxy/TrustedForwarderDnsResolveTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package io.quarkus.vertx.http.proxy; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.vertx.http.ForwardedHandlerInitializer; | ||
import io.quarkus.vertx.http.proxy.fakedns.DnameRecordEncoder; | ||
import io.quarkus.vertx.http.proxy.fakedns.DnsMessageEncoder; | ||
import io.quarkus.vertx.http.proxy.fakedns.FakeDNSServer; | ||
import io.restassured.RestAssured; | ||
|
||
public class TrustedForwarderDnsResolveTest { | ||
|
||
private FakeDNSServer dnsServer; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(ForwardedHandlerInitializer.class, DnameRecordEncoder.class, DnsMessageEncoder.class, | ||
FakeDNSServer.class) | ||
.addAsResource(new StringAsset("quarkus.http.proxy.proxy-address-forwarding=true\n" + | ||
"quarkus.http.proxy.allow-forwarded=true\n" + | ||
"quarkus.http.proxy.enable-forwarded-host=true\n" + | ||
"quarkus.http.proxy.enable-forwarded-prefix=true\n" + | ||
"quarkus.vertx.resolver.servers=127.0.0.1:53530\n" + | ||
"quarkus.http.proxy.trusted-proxies=trusted.example.com"), | ||
"application.properties")); | ||
|
||
@BeforeEach | ||
public void setUp() throws Exception { | ||
dnsServer = new FakeDNSServer(); | ||
dnsServer.start(); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() { | ||
dnsServer.stop(); | ||
} | ||
|
||
@Test | ||
public void testTrustedProxyResolved() { | ||
dnsServer.addRecordsToStore("trusted.example.com", "127.0.0.3", "127.0.0.2", "127.0.0.1"); | ||
RestAssured.given() | ||
.header("Forwarded", "proto=http;for=backend2:5555;host=somehost2") | ||
.get("/path") | ||
.then() | ||
.body(Matchers.equalTo("http|somehost2|backend2:5555|/path|http://somehost2/path")); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...http/deployment/src/test/java/io/quarkus/vertx/http/proxy/fakedns/DnameRecordEncoder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.quarkus.vertx.http.proxy.fakedns; | ||
|
||
import org.apache.directory.server.dns.io.encoder.ResourceRecordEncoder; | ||
import org.apache.directory.server.dns.messages.ResourceRecord; | ||
import org.apache.directory.server.dns.store.DnsAttribute; | ||
import org.apache.mina.core.buffer.IoBuffer; | ||
|
||
public class DnameRecordEncoder extends ResourceRecordEncoder { | ||
|
||
protected void putResourceRecordData(IoBuffer byteBuffer, ResourceRecord record) { | ||
String domainName = record.get(DnsAttribute.DOMAIN_NAME); | ||
putDomainName(byteBuffer, domainName); | ||
} | ||
} |
198 changes: 198 additions & 0 deletions
198
...-http/deployment/src/test/java/io/quarkus/vertx/http/proxy/fakedns/DnsMessageEncoder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
package io.quarkus.vertx.http.proxy.fakedns; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF 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 | ||
* | ||
* http://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. | ||
* | ||
*/ | ||
import org.apache.directory.server.dns.io.encoder.*; | ||
import org.apache.directory.server.dns.messages.DnsMessage; | ||
import org.apache.directory.server.dns.messages.MessageType; | ||
import org.apache.directory.server.dns.messages.OpCode; | ||
import org.apache.directory.server.dns.messages.QuestionRecord; | ||
import org.apache.directory.server.dns.messages.RecordType; | ||
import org.apache.directory.server.dns.messages.ResourceRecord; | ||
import org.apache.directory.server.dns.messages.ResponseCode; | ||
import org.apache.directory.server.i18n.I18n; | ||
import org.apache.mina.core.buffer.IoBuffer; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* An encoder for DNS messages. The primary usage of the DnsMessageEncoder is | ||
* to call the <code>encode(ByteBuffer, DnsMessage)</code> method which will | ||
* write the message to the outgoing ByteBuffer according to the DnsMessage | ||
* encoding in RFC-1035. | ||
* | ||
* @author <a href="mailto:[email protected]">Apache Directory Project</a> | ||
* @version $Rev$, $Date$ | ||
*/ | ||
public class DnsMessageEncoder { | ||
/** the log for this class */ | ||
private static final Logger log = LoggerFactory.getLogger(DnsMessageEncoder.class); | ||
|
||
/** | ||
* A Hashed Adapter mapping record types to their encoders. | ||
*/ | ||
private static final Map<RecordType, RecordEncoder> DEFAULT_ENCODERS; | ||
|
||
static { | ||
Map<RecordType, RecordEncoder> map = new HashMap<RecordType, RecordEncoder>(); | ||
|
||
map.put(RecordType.SOA, new StartOfAuthorityRecordEncoder()); | ||
map.put(RecordType.A, new AddressRecordEncoder()); | ||
map.put(RecordType.NS, new NameServerRecordEncoder()); | ||
map.put(RecordType.CNAME, new CanonicalNameRecordEncoder()); | ||
map.put(RecordType.PTR, new PointerRecordEncoder()); | ||
map.put(RecordType.MX, new MailExchangeRecordEncoder()); | ||
map.put(RecordType.SRV, new ServerSelectionRecordEncoder()); | ||
map.put(RecordType.TXT, new TextRecordEncoder()); | ||
map.put(RecordType.DNAME, new DnameRecordEncoder()); | ||
|
||
DEFAULT_ENCODERS = Collections.unmodifiableMap(map); | ||
} | ||
|
||
/** | ||
* Encodes the {@link DnsMessage} into the {@link IoBuffer}. | ||
* | ||
* @param byteBuffer | ||
* @param message | ||
*/ | ||
public void encode(IoBuffer byteBuffer, DnsMessage message) { | ||
byteBuffer.putShort((short) message.getTransactionId()); | ||
|
||
byte header = (byte) 0x00; | ||
header |= encodeMessageType(message.getMessageType()); | ||
header |= encodeOpCode(message.getOpCode()); | ||
header |= encodeAuthoritativeAnswer(message.isAuthoritativeAnswer()); | ||
header |= encodeTruncated(message.isTruncated()); | ||
header |= encodeRecursionDesired(message.isRecursionDesired()); | ||
byteBuffer.put(header); | ||
|
||
header = (byte) 0x00; | ||
header |= encodeRecursionAvailable(message.isRecursionAvailable()); | ||
header |= encodeResponseCode(message.getResponseCode()); | ||
byteBuffer.put(header); | ||
|
||
byteBuffer | ||
.putShort((short) (message.getQuestionRecords() != null ? message.getQuestionRecords().size() : 0)); | ||
byteBuffer.putShort((short) (message.getAnswerRecords() != null ? message.getAnswerRecords().size() : 0)); | ||
byteBuffer.putShort((short) (message.getAuthorityRecords() != null ? message.getAuthorityRecords().size() | ||
: 0)); | ||
byteBuffer.putShort((short) (message.getAdditionalRecords() != null ? message.getAdditionalRecords().size() | ||
: 0)); | ||
|
||
putQuestionRecords(byteBuffer, message.getQuestionRecords()); | ||
putResourceRecords(byteBuffer, message.getAnswerRecords()); | ||
putResourceRecords(byteBuffer, message.getAuthorityRecords()); | ||
putResourceRecords(byteBuffer, message.getAdditionalRecords()); | ||
} | ||
|
||
private void putQuestionRecords(IoBuffer byteBuffer, List<QuestionRecord> questions) { | ||
if (questions == null) { | ||
return; | ||
} | ||
|
||
QuestionRecordEncoder encoder = new QuestionRecordEncoder(); | ||
|
||
Iterator<QuestionRecord> it = questions.iterator(); | ||
|
||
while (it.hasNext()) { | ||
QuestionRecord question = it.next(); | ||
encoder.put(byteBuffer, question); | ||
} | ||
} | ||
|
||
private void putResourceRecords(IoBuffer byteBuffer, List<ResourceRecord> records) { | ||
if (records == null) { | ||
return; | ||
} | ||
|
||
Iterator<ResourceRecord> it = records.iterator(); | ||
|
||
while (it.hasNext()) { | ||
ResourceRecord record = it.next(); | ||
|
||
try { | ||
put(byteBuffer, record); | ||
} catch (IOException ioe) { | ||
log.error(ioe.getLocalizedMessage(), ioe); | ||
} | ||
} | ||
} | ||
|
||
private void put(IoBuffer byteBuffer, ResourceRecord record) throws IOException { | ||
RecordType type = record.getRecordType(); | ||
|
||
RecordEncoder encoder = DEFAULT_ENCODERS.get(type); | ||
|
||
if (encoder == null) { | ||
throw new IOException(I18n.err(I18n.ERR_597, type)); | ||
} | ||
|
||
encoder.put(byteBuffer, record); | ||
} | ||
|
||
private byte encodeMessageType(MessageType messageType) { | ||
byte oneBit = (byte) (messageType.convert() & 0x01); | ||
return (byte) (oneBit << 7); | ||
} | ||
|
||
private byte encodeOpCode(OpCode opCode) { | ||
byte fourBits = (byte) (opCode.convert() & 0x0F); | ||
return (byte) (fourBits << 3); | ||
} | ||
|
||
private byte encodeAuthoritativeAnswer(boolean authoritative) { | ||
if (authoritative) { | ||
return (byte) ((byte) 0x01 << 2); | ||
} | ||
return (byte) 0; | ||
} | ||
|
||
private byte encodeTruncated(boolean truncated) { | ||
if (truncated) { | ||
return (byte) ((byte) 0x01 << 1); | ||
} | ||
return 0; | ||
} | ||
|
||
private byte encodeRecursionDesired(boolean recursionDesired) { | ||
if (recursionDesired) { | ||
return (byte) 0x01; | ||
} | ||
return 0; | ||
} | ||
|
||
private byte encodeRecursionAvailable(boolean recursionAvailable) { | ||
if (recursionAvailable) { | ||
return (byte) ((byte) 0x01 << 7); | ||
} | ||
return 0; | ||
} | ||
|
||
private byte encodeResponseCode(ResponseCode responseCode) { | ||
return (byte) (responseCode.convert() & 0x0F); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be declared in the BOM or in the parent.