From 935ac32ea20c0447edef37e7962d2308d45cb3b1 Mon Sep 17 00:00:00 2001 From: Max Goedjen Date: Sun, 31 Aug 2025 16:07:35 -0700 Subject: [PATCH] Factor out comment writer. (#651) --- .../Sources/SecretAgentKit/Agent.swift | 2 +- .../OpenSSH/OpenSSHPublicKeyWriter.swift | 26 ++++++++++--------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/Sources/Packages/Sources/SecretAgentKit/Agent.swift b/Sources/Packages/Sources/SecretAgentKit/Agent.swift index d00d963..8851127 100644 --- a/Sources/Packages/Sources/SecretAgentKit/Agent.swift +++ b/Sources/Packages/Sources/SecretAgentKit/Agent.swift @@ -90,7 +90,7 @@ extension Agent { for secret in secrets { let keyBlob = publicKeyWriter.data(secret: secret) keyData.append(keyBlob.lengthAndData) - keyData.append(secret.name.lengthAndData) + keyData.append(publicKeyWriter.comment(secret: secret).lengthAndData) count += 1 if let (certificateData, name) = try? await certificateHandler.keyBlobAndName(for: secret) { diff --git a/Sources/Packages/Sources/SecretKit/OpenSSH/OpenSSHPublicKeyWriter.swift b/Sources/Packages/Sources/SecretKit/OpenSSH/OpenSSHPublicKeyWriter.swift index 2c7f446..99713e0 100644 --- a/Sources/Packages/Sources/SecretKit/OpenSSH/OpenSSHPublicKeyWriter.swift +++ b/Sources/Packages/Sources/SecretKit/OpenSSH/OpenSSHPublicKeyWriter.swift @@ -31,18 +31,7 @@ public struct OpenSSHPublicKeyWriter: Sendable { /// Generates an OpenSSH string representation of the secret. /// - Returns: OpenSSH string representation of the secret. public func openSSHString(secret: SecretType) -> String { - let resolvedComment: String - if let comment = secret.publicKeyAttribution { - resolvedComment = comment - } else { - let dashedKeyName = secret.name.replacingOccurrences(of: " ", with: "-") - let dashedHostName = ["secretive", Host.current().localizedName, "local"] - .compactMap { $0 } - .joined(separator: ".") - .replacingOccurrences(of: " ", with: "-") - resolvedComment = "\(dashedKeyName)@\(dashedHostName)" - } - return [openSSHIdentifier(for: secret.keyType), data(secret: secret).base64EncodedString(), resolvedComment] + return [openSSHIdentifier(for: secret.keyType), data(secret: secret).base64EncodedString(), comment(secret: secret)] .compactMap { $0 } .joined(separator: " ") } @@ -65,6 +54,19 @@ public struct OpenSSHPublicKeyWriter: Sendable { .joined(separator: ":") } + public func comment(secret: SecretType) -> String { + if let comment = secret.publicKeyAttribution { + return comment + } else { + let dashedKeyName = secret.name.replacingOccurrences(of: " ", with: "-") + let dashedHostName = ["secretive", Host.current().localizedName, "local"] + .compactMap { $0 } + .joined(separator: ".") + .replacingOccurrences(of: " ", with: "-") + return "\(dashedKeyName)@\(dashedHostName)" + } + + } } extension OpenSSHPublicKeyWriter {