Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Name UseKeychain in an IgnoreUnknown line before it

Țugui Dragoșc90114fb82e9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a38f1a02..bb0a577e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -140,6 +140,8 @@ along a removal is, and says within seconds when its helper isn't answering.
- **Tweaks:** The screenshot folder can no longer be the Trash, and a folder screenshots can't be saved in says why.
- **Terminal:** The Git tab leaves a setting to a file your settings include, so Peel's value never wins over yours.
+ Keep key passphrases in your keychain no longer stops an ssh installed from elsewhere, such as Homebrew's, which
+ doesn't know `UseKeychain`.
- **History:** Put Back brings a file a Homebrew cask or an installer receipt named back under the name the disk
gives it, registers an app it brings back with macOS, and judges with your exclusions as `peel restore` does. Undo
now stays off, as Put Back already did, while History or the exclusions can't be read.
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/SSHFile.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/SSHFile.swift
index 28fc121a..b6e5b84c 100644
--- a/Packages/PeelCore/Sources/PeelCore/Terminal/SSHFile.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/SSHFile.swift
@@ -12,8 +12,31 @@ public enum SSHFile {
folder.appending(path: "Terminal/ssh_config", directoryHint: .notDirectory)
}
+ /// The file for `settings`. The `IgnoreUnknown` line goes first and is no setting's own line, so a file written
+ /// without it still reads as the settings in it.
public static func contents(of settings: Set<SSHSetting>) -> String {
- ([header] + SSHSetting.allCases.filter(settings.contains).flatMap(\.lines)).joined(separator: "\n") + "\n"
+ let chosen = SSHSetting.allCases.filter(settings.contains)
+ let onlyApples = chosen.flatMap(\.onlyApplesSSHKnows)
+ let ignored = onlyApples.isEmpty ? [] : ["IgnoreUnknown " + onlyApples.joined(separator: ",")]
+ return ([header] + ignored + chosen.flatMap(\.lines)).joined(separator: "\n") + "\n"
+ }
+
+ /// The file as this version writes it, when `contents` is exactly what an earlier version wrote, which had no
+ /// `IgnoreUnknown` line. A file changed by hand is nil, and so is one already as this version writes it.
+ public static func update(of contents: String) -> String? {
+ let settings = settings(in: contents)
+ let earlier = ([header] + SSHSetting.allCases.filter(settings.contains).flatMap(\.lines))
+ .joined(separator: "\n") + "\n"
+ let now = self.contents(of: settings)
+ return contents == earlier && contents != now ? now : nil
+ }
+
+ /// Writes the file at `url` again as this version writes it, when an earlier version wrote it.
+ public static func bringUpToDate(_ url: URL) {
+ guard let data = BoundedRead.data(at: url, maximum: maximumBytes),
+ let updated = update(of: String(decoding: data, as: UTF8.self))
+ else { return }
+ try? Data(updated.utf8).write(to: url, options: .atomic)
}
public static func settings(in contents: String) -> Set<SSHSetting> {
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/SSHSetting.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/SSHSetting.swift
index 9d4694b8..3f642f70 100644
--- a/Packages/PeelCore/Sources/PeelCore/Terminal/SSHSetting.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/SSHSetting.swift
@@ -10,4 +10,13 @@ public enum SSHSetting: CaseIterable, Sendable {
case .reuseConnections: ["ControlMaster auto", "ControlPath ~/.ssh/cm-%C", "ControlPersist 10m"]
}
}
+
+ /// The options only Apple's ssh knows. Another ssh, such as Homebrew's, stops at an option it doesn't know
+ /// unless an `IgnoreUnknown` before it names the option (`man ssh_config`).
+ var onlyApplesSSHKnows: [String] {
+ switch self {
+ case .keychain: ["UseKeychain"]
+ case .keepAlive, .reuseConnections: []
+ }
+ }
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SSHFileTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SSHFileTests.swift
index dd84772b..48636b1d 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SSHFileTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SSHFileTests.swift
@@ -66,6 +66,41 @@ struct SSHFileTests {
#expect(await SSHFile.accepted() == Set(SSHSetting.allCases))
}
+ /// Another ssh, such as Homebrew's, doesn't know `UseKeychain`: renamed to an option the ssh on this Mac doesn't
+ /// know either, Peel's file has to be read all the same.
+ @Test func anSSHThatDoesNotKnowAnOptionStillReadsTheFile() async throws {
+ let ssh = try SSHHome()
+ let unknown = SSHFile.contents(of: Set(SSHSetting.allCases)).replacing("UseKeychain", with: "PeelUnknownOption")
+ try FileManager.default.createDirectory(
+ at: ssh.peelFile.deletingLastPathComponent(),
+ withIntermediateDirectories: true
+ )
+ try unknown.write(to: ssh.peelFile, atomically: true, encoding: .utf8)
+ #expect(try await ssh.options(for: "example.org")["serveraliveinterval"] == "60")
+ }
+
+ @Test func aFileWrittenBeforeTheIgnoreUnknownLineReadsAsBefore() {
+ let written = [SSHFile.header, "UseKeychain yes", "AddKeysToAgent yes", "ServerAliveInterval 60"]
+ #expect(SSHFile.settings(in: written.joined(separator: "\n") + "\n") == [.keychain, .keepAlive])
+ }
+
+ @Test func aFileAnEarlierVersionWroteIsBroughtUpToDateAndOneChangedByHandIsNot() throws {
+ let earlier = [SSHFile.header, "UseKeychain yes", "AddKeysToAgent yes"].joined(separator: "\n") + "\n"
+ #expect(SSHFile.update(of: earlier) == SSHFile.contents(of: [.keychain]))
+ #expect(SSHFile.update(of: SSHFile.contents(of: [.keychain])) == nil)
+ #expect(SSHFile.update(of: earlier + "Compression yes\n") == nil)
+ #expect(SSHFile.update(of: SSHFile.contents(of: [.keepAlive])) == nil)
+
+ let ssh = try SSHHome()
+ try FileManager.default.createDirectory(
+ at: ssh.peelFile.deletingLastPathComponent(),
+ withIntermediateDirectories: true
+ )
+ try earlier.write(to: ssh.peelFile, atomically: true, encoding: .utf8)
+ SSHFile.bringUpToDate(ssh.peelFile)
+ #expect(try String(contentsOf: ssh.peelFile, encoding: .utf8) == SSHFile.contents(of: [.keychain]))
+ }
+
@Test func theLinesNameTheFileFromTheHomeOrByItsWholePath() {
let home = URL(filePath: "/Users/example", directoryHint: .isDirectory)
let file = SSHFile.url(in: home.appending(path: "Library/Application Support/Peel", directoryHint: .isDirectory))
diff --git a/Peel/PeelApp.swift b/Peel/PeelApp.swift
index c2a720c0..a463f749 100644
--- a/Peel/PeelApp.swift
+++ b/Peel/PeelApp.swift
@@ -286,6 +286,8 @@ struct PeelApp: App {
private func launch() async {
history.stats = stats
history.checkReadability()
+ // On the main actor, so it never runs between the SSH tab's own read and write.
+ SSHFile.bringUpToDate(ssh.file)
Task { await stats.takeInEarlierTotals() }
// Read beside the rest and behind nothing: until the exclusions are read, nothing moves.
async let exclusionsRead: Void = Marks.interval("Exclusions") { await exclusions.load() }
diff --git a/TERMINAL.md b/TERMINAL.md
index 60ac15f6..0e6f251e 100644
--- a/TERMINAL.md
+++ b/TERMINAL.md
@@ -417,7 +417,7 @@ Match all
| Setting | What it does |
| --- | --- |
-| [Keep key passphrases in your keychain](https://developer.apple.com/library/archive/technotes/tn2449/_index.html) | ssh asks for a key's passphrase once, saves it in your keychain, and adds the key to the agent. `UseKeychain` is macOS's own. |
+| [Keep key passphrases in your keychain](https://developer.apple.com/library/archive/technotes/tn2449/_index.html) | ssh asks for a key's passphrase once, saves it in your keychain, and adds the key to the agent. `UseKeychain` is macOS's own, so Peel's file names it in an `IgnoreUnknown` line first, as Apple's note shows, and an ssh installed from elsewhere, such as Homebrew's, passes over it. An `IgnoreUnknown` your own `~/.ssh/config` sets earlier wins, since ssh keeps the first value it finds. |
| [Keep connections alive](https://man.openbsd.org/ssh_config#ServerAliveInterval) | On a quiet connection, ssh checks on the server every minute, so a router doesn't drop it. |
| [Reuse connections to a server](https://man.openbsd.org/ssh_config#ControlMaster) | A new connection to a server you're already connected to opens at once through the first, which stays open 10 minutes after the last one ends. Leave it off if you use two accounts on one server, such as two GitHub accounts: ssh would send the second through the first one's connection. |
@@ -426,6 +426,7 @@ Without Peel, these lines can go at the end of `~/.ssh/config`, in a `Match all`
```
Match all
# Keep key passphrases in your keychain
+ IgnoreUnknown UseKeychain
UseKeychain yes
AddKeysToAgent yes