Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Remove numerous unnecessary code stanzas. * Don't check `isRelease` in `Environment.==`, it will always be equal because the value is effectively a compile-time constant. Also improve the use of MARK comments so Xcode renders the intended divisions nicely. * Add warning to `Environment.isRelease` explaining the seemingly unusual behavior of its value. * Redo the documentation comments for `Environment.secret()` (both versions). Significantly simplify the implementation of the path-taking version. IMPORTANT NOTE: This is an interim step of cleanup while a much more complete revamping of this API is worked on. * Correctly sanitize the excess arguments Xcode passes to test invocations for the testing environment. I forgot to mention in a previous commit that support was added for `VAPOR_ENV` too... * Add sanitization of raw SwiftPM's invocation of the xctest runner binary. This is necessarily a little specific to the version of SwiftPM and Xcode involved, but should at least be specific enough a check to not interfere with normal operations if the call sequence changes. * There is no need to hardcode all the logger levels for `LosslessStringConvertible` conformance. `Logger.Level` is already `RawRepresentable` as `String` and that conformance can be used transparently. (And it has `CaseIterable` for good measure, which provides yet another way to scale this particular elevation.)
- Loading branch information
Showing
4 changed files
with
128 additions
and
97 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,61 @@ | ||
extension Environment { | ||
/// Reads a file's content for a secret. The secret key represents the name of the environment variable that holds the path for the file containing the secret | ||
/// Reads a file's content for a secret. The secret key is the name of the environment variable that is expected to | ||
/// specify the path of the file containing the secret. | ||
/// | ||
/// - Parameters: | ||
/// - key: Environment name for the path to the file containing the secret | ||
/// - fileIO: FileIO handler provided by NIO | ||
/// - on: EventLoop to operate on while opening the file | ||
/// - Throws: Error.environmentVariableNotFound if the environment variable with the key name does not exist | ||
/// - key: The environment variable name | ||
/// - fileIO: `NonBlockingFileIO` handler provided by NIO | ||
/// - eventLoop: `EventLoop` for NIO to use for working with the file | ||
/// | ||
/// Example usage: | ||
/// | ||
/// ```` | ||
/// func configure(_ app: Application) { | ||
/// // ... | ||
/// | ||
/// let databasePassword = try Environment.secret( | ||
/// key: "DATABASE_PASSWORD_FILE", | ||
/// fileIO: app.fileio, | ||
/// on: app.eventLoopGroup.next() | ||
/// ).wait() | ||
/// | ||
/// ```` | ||
/// | ||
/// - Important: Do _not_ use `.wait()` if loading a secret at any time after the app has booted, such as while | ||
/// handling a `Request`. Chain the result as you would any other future instead. | ||
public static func secret(key: String, fileIO: NonBlockingFileIO, on eventLoop: EventLoop) -> EventLoopFuture<String?> { | ||
guard let filePath = self.get(key) else { return eventLoop.future(nil) } | ||
guard let filePath = self.get(key) else { | ||
return eventLoop.future(nil) | ||
} | ||
return self.secret(path: filePath, fileIO: fileIO, on: eventLoop) | ||
} | ||
|
||
|
||
/// Reads a file's content for a secret. The path is a file path to the file that contains the secret in plain text | ||
/// Load the content of a file at a given path as a secret. | ||
/// | ||
/// - Parameters: | ||
/// - path: Path to the file that contains the secret | ||
/// - fileIO: FileIO handler provided by NIO | ||
/// - on: EventLoop to operate on while opening the file | ||
/// - Throws: Error.environmentVariableNotFound if the environment variable with the key name does not exist | ||
/// - path: Path to the file containing the secret | ||
/// - fileIO: `NonBlockingFileIO` handler provided by NIO | ||
/// - eventLoop: `EventLoop` for NIO to use for working with the file | ||
/// | ||
/// - Returns: | ||
/// - On success, a succeeded future with the loaded content of the file. | ||
/// - On any kind of error, a succeeded future with a value of `nil`. It is not currently possible to get error details. | ||
public static func secret(path: String, fileIO: NonBlockingFileIO, on eventLoop: EventLoop) -> EventLoopFuture<String?> { | ||
return fileIO | ||
.openFile(path: path, eventLoop: eventLoop) | ||
.flatMap({ (arg) -> EventLoopFuture<ByteBuffer> in | ||
.flatMap { handle, region in | ||
return fileIO | ||
.read(fileRegion: arg.1, allocator: .init(), eventLoop: eventLoop) | ||
.flatMapThrowing({ (buffer) -> ByteBuffer in | ||
try arg.0.close() | ||
return buffer | ||
}) | ||
}) | ||
.map({ (buffer) -> (String) in | ||
var buffer = buffer | ||
return buffer.readString(length: buffer.writerIndex) ?? "" | ||
}) | ||
.map({ (secret) -> (String) in | ||
secret.trimmingCharacters(in: .whitespacesAndNewlines) | ||
}) | ||
.recover ({ (_) -> String? in | ||
.read(fileRegion: region, allocator: .init(), eventLoop: eventLoop) | ||
.always { _ in try? handle.close() } | ||
} | ||
.map { buffer -> String in | ||
return buffer | ||
.getString(at: buffer.readerIndex, length: buffer.readableBytes)! | ||
.trimmingCharacters(in: .whitespacesAndNewlines) | ||
} | ||
.recover { _ -> String? in | ||
nil | ||
}) | ||
} | ||
} | ||
} | ||
|
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