Skip to content

Commit 64bd48b

Browse files
authored
Reject conflicting DNS flags (apple#1559)
Rejects conflicting DNS configuration when `--no-dns` is supplied together with explicit DNS flags. Fixes apple#1536.
1 parent 5317536 commit 64bd48b

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

Sources/Services/ContainerAPIService/Client/Flags.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,21 @@ public struct Flags {
344344

345345
@Option(name: [.customLong("volume"), .short], help: "Bind mount a volume into the container")
346346
public var volumes: [String] = []
347+
348+
public func validate() throws {
349+
if dnsDisabled {
350+
let hasDNSConfig =
351+
!dns.nameservers.isEmpty
352+
|| dns.domain != nil
353+
|| !dns.options.isEmpty
354+
|| !dns.searchDomains.isEmpty
355+
if hasDNSConfig {
356+
throw ValidationError(
357+
"`--no-dns` cannot be used with DNS configuration flags (`--dns`, `--dns-domain`, `--dns-option`, `--dns-search`)"
358+
)
359+
}
360+
}
361+
}
347362
}
348363

349364
public struct Progress: ParsableArguments {

Tests/ContainerAPIClientTests/ParserTest.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,4 +1296,44 @@ struct ParserTest {
12961296
#expect(result.cpus == 2)
12971297
#expect(result.memoryInBytes == 2048.mib())
12981298
}
1299+
1300+
// MARK: - DNS Flag Validation Tests
1301+
1302+
@Test
1303+
func testManagementFlagsRejectsNoDNSWithDNS() throws {
1304+
#expect(throws: (any Error).self) {
1305+
_ = try Flags.Management.parse(["--dns", "1.1.1.1", "--no-dns"])
1306+
}
1307+
}
1308+
1309+
@Test
1310+
func testManagementFlagsRejectsNoDNSWithDNSDomain() throws {
1311+
#expect(throws: (any Error).self) {
1312+
_ = try Flags.Management.parse(["--dns-domain", "example.com", "--no-dns"])
1313+
}
1314+
}
1315+
1316+
@Test
1317+
func testManagementFlagsRejectsNoDNSWithDNSSearch() throws {
1318+
#expect(throws: (any Error).self) {
1319+
_ = try Flags.Management.parse(["--dns-search", "example.com", "--no-dns"])
1320+
}
1321+
}
1322+
1323+
@Test
1324+
func testManagementFlagsRejectsNoDNSWithDNSOption() throws {
1325+
#expect(throws: (any Error).self) {
1326+
_ = try Flags.Management.parse(["--dns-option", "debug", "--no-dns"])
1327+
}
1328+
}
1329+
1330+
@Test
1331+
func testManagementFlagsAcceptsDNSAlone() throws {
1332+
_ = try Flags.Management.parse(["--dns", "1.1.1.1"])
1333+
}
1334+
1335+
@Test
1336+
func testManagementFlagsAcceptsNoDNSAlone() throws {
1337+
_ = try Flags.Management.parse(["--no-dns"])
1338+
}
12991339
}

0 commit comments

Comments
 (0)