Skip to content

Commit 5a15362

Browse files
authored
Detect which condition didn't match and log the cause when configuring DPS as default DNS (mageddo#585)
* adjusting dev * refactoring file edition conditions to rule pattern this way I can detect which condition didn't match and log the cause * [Gradle Release Plugin] - new version commit: '3.30.2-snapshot'. * release notes
1 parent c78f1cf commit 5a15362

File tree

7 files changed

+72
-16
lines changed

7 files changed

+72
-16
lines changed

RELEASE-NOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.30.2
2+
* Detect which condition didn't match and log the cause when configuring DPS as default DNS #580
3+
* Fixed backend develop docker-compose.yml
4+
15
## 3.30.1
26
* Implemented but not exposed Canary Rate Threshold Circuit Breaker Strategy. #533
37

docker-compose-dev.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
version: '3'
22
services:
33
backend:
4-
image: openjdk:19-jdk
4+
image: openjdk:21-jdk
55
environment:
6+
MG_RESOLVCONF: /host/run/stub-resolv.conf
67
MG_DPS_NETWORK: true
78
MG_DPS_NETWORK_AUTO_CONNECT: true
8-
MG_NO_REMOTE_SERVERS: true
9+
MG_NO_REMOTE_SERVERS: false
910
MG_LOG_LEVEL: DEBUG
1011
# MG_LOG_LEVEL: DEBUG
1112
# MG_DPS_NETWORK: true
@@ -18,7 +19,7 @@ services:
1819
volumes:
1920
- ./build/libs:/app
2021
- /var/run/docker.sock:/var/run/docker.sock
21-
# - /etc/resolv.conf:/etc/resolv.conf
22+
# - /run/systemd/resolve/:/host/run/
2223
# - /etc/systemd/:/host/etc/systemd/
2324
working_dir: /app
2425
command: java -jar dns-proxy-server-3.16.1-snapshot-all.jar

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=3.30.1-snapshot
1+
version=3.30.2-snapshot

src/main/java/com/mageddo/dnsproxyserver/dnsconfigurator/linux/DnsConfiguratorLinux.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,25 @@
55
import com.mageddo.dnsproxyserver.dnsconfigurator.DnsConfigurator;
66
import com.mageddo.dnsproxyserver.dnsconfigurator.linux.ResolvFile.Type;
77
import com.mageddo.dnsproxyserver.systemd.ResolvedService;
8+
import com.mageddo.io.path.predicate.PathExistsPredicate;
9+
import com.mageddo.io.path.predicate.PathIsFilePredicate;
10+
import com.mageddo.io.path.predicate.PathIsWritablePredicate;
811
import com.mageddo.net.IpAddr;
912
import com.mageddo.utils.Tests;
1013
import lombok.RequiredArgsConstructor;
1114
import lombok.extern.slf4j.Slf4j;
15+
import org.apache.commons.lang3.ClassUtils;
1216

1317
import javax.enterprise.inject.Default;
1418
import javax.inject.Inject;
1519
import javax.inject.Singleton;
16-
import java.nio.file.Files;
1720
import java.nio.file.Path;
1821
import java.util.Collections;
1922
import java.util.List;
23+
import java.util.Optional;
2024
import java.util.concurrent.atomic.AtomicBoolean;
2125
import java.util.concurrent.atomic.AtomicReference;
26+
import java.util.function.Predicate;
2227

2328
import static com.mageddo.dnsproxyserver.utils.Splits.splitToPaths;
2429
import static org.apache.commons.lang3.ObjectUtils.firstNonNull;
@@ -32,6 +37,11 @@ public class DnsConfiguratorLinux implements DnsConfigurator {
3237
private final AtomicBoolean resolvedConfigured = new AtomicBoolean();
3338

3439
private volatile AtomicReference<ResolvFile> confFile;
40+
private final List<Predicate<Path>> pathPredicates = List.of(
41+
new PathExistsPredicate(),
42+
new PathIsFilePredicate(),
43+
new PathIsWritablePredicate()
44+
);
3545

3646
@Override
3747
public void configure(IpAddr addr) {
@@ -46,8 +56,7 @@ public void configure(IpAddr addr) {
4656
if (confFile.isResolvconf()) {
4757
final var overrideNameServers = Configs
4858
.getInstance()
49-
.getResolvConfOverrideNameServers()
50-
;
59+
.getResolvConfOverrideNameServers();
5160
ResolvconfConfigurator.process(confFile.getPath(), addr, overrideNameServers);
5261
} else if (confFile.isResolved()) {
5362
this.configureResolved(addr, confFile);
@@ -81,16 +90,14 @@ ResolvFile getConfFile() {
8190
}
8291

8392
ResolvFile findBestConfFile() {
84-
return buildConfPaths()
93+
return this.buildConfPaths()
8594
.stream()
8695
.filter(it -> {
87-
final var valid = Files.exists(it)
88-
&& !Files.isDirectory(it)
89-
&& Files.isWritable(it);
90-
if (!valid) {
91-
log.info("status=noValidConfFile, file={}", it);
92-
}
93-
return valid;
96+
final var unmetPredicate = this.findUnmetPredicate(it);
97+
unmetPredicate.ifPresent(pathPredicate -> {
98+
log.info("status=noValidConfFile, file={}, condition={}", it, ClassUtils.getSimpleName(pathPredicate));
99+
});
100+
return unmetPredicate.isEmpty();
94101
}
95102
)
96103
.map(this::toResolvFile)
@@ -99,6 +106,13 @@ ResolvFile findBestConfFile() {
99106
;
100107
}
101108

109+
private Optional<Predicate<Path>> findUnmetPredicate(Path it) {
110+
return this.pathPredicates
111+
.stream()
112+
.filter(p -> !p.test(it))
113+
.findFirst();
114+
}
115+
102116
List<Path> buildConfPaths() {
103117
return Objects.firstNonNull(
104118
splitToPaths(getConfigResolvPaths()),
@@ -144,7 +158,7 @@ static void tryRestartResolved() {
144158
} catch (Throwable e) {
145159
log.warn(
146160
"status=can't restart resolved service, please run: "
147-
+ "'service systemd-resolved restart' to apply DPS as default DNS.\n{}",
161+
+ "'service systemd-resolved restart' to apply DPS as default DNS.\n{}",
148162
e.getMessage()
149163
);
150164
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.mageddo.io.path.predicate;
2+
3+
import java.nio.file.Files;
4+
import java.nio.file.Path;
5+
import java.util.function.Predicate;
6+
7+
public class PathExistsPredicate implements Predicate<Path> {
8+
@Override
9+
public boolean test(Path p) {
10+
return Files.exists(p);
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.mageddo.io.path.predicate;
2+
3+
import java.nio.file.Files;
4+
import java.nio.file.Path;
5+
import java.util.function.Predicate;
6+
7+
public class PathIsFilePredicate implements Predicate<Path> {
8+
@Override
9+
public boolean test(Path path) {
10+
return !Files.isDirectory(path);
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.mageddo.io.path.predicate;
2+
3+
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.util.function.Predicate;
7+
8+
public class PathIsWritablePredicate implements Predicate<Path> {
9+
@Override
10+
public boolean test(Path path) {
11+
return Files.isWritable(path);
12+
}
13+
}

0 commit comments

Comments
 (0)