-
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Project public key files for use in configs (#264)
- Loading branch information
1 parent
5894bbc
commit 05c5aca
Showing
4 changed files
with
51 additions
and
9 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
Sources/Packages/Sources/SecretKit/PublicKeyStandinFileController.swift
This file contains 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,41 @@ | ||
import Foundation | ||
import OSLog | ||
|
||
/// Controller responsible for writing public keys to disk, so that they're easily accessible by scripts. | ||
public class PublicKeyFileStoreController { | ||
|
||
private let logger = Logger() | ||
private let directory: String | ||
|
||
/// Initializes a PublicKeyFileStoreController. | ||
public init(homeDirectory: String) { | ||
directory = homeDirectory.appending("/PublicKeys") | ||
} | ||
|
||
/// Writes out the keys specified to disk. | ||
/// - Parameter secrets: The Secrets to generate keys for. | ||
/// - Parameter clear: Whether or not the directory should be erased before writing keys. | ||
public func generatePublicKeys(for secrets: [AnySecret], clear: Bool = false) throws { | ||
logger.log("Writing public keys to disk") | ||
if clear { | ||
try? FileManager.default.removeItem(at: URL(fileURLWithPath: directory)) | ||
} | ||
try? FileManager.default.createDirectory(at: URL(fileURLWithPath: directory), withIntermediateDirectories: false, attributes: nil) | ||
let keyWriter = OpenSSHKeyWriter() | ||
for secret in secrets { | ||
let path = path(for: secret) | ||
guard let data = keyWriter.openSSHString(secret: secret).data(using: .utf8) else { continue } | ||
FileManager.default.createFile(atPath: path, contents: data, attributes: nil) | ||
} | ||
logger.log("Finished writing public keys") | ||
} | ||
|
||
/// The path for a Secret's public key. | ||
/// - Parameter secret: The Secret to return the path for. | ||
/// - Returns: The path to the Secret's public key. | ||
/// - Warning: This method returning a path does not imply that a key has been written to disk already. This method only describes where it will be written to. | ||
public func path<SecretType: Secret>(for secret: SecretType) -> String { | ||
directory.appending("/").appending("\(secret.name.replacingOccurrences(of: " ", with: "-")).pub") | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
This file contains 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