Skip to content

Commit

Permalink
Store the root certificates as a multi-line string literal. (#372)
Browse files Browse the repository at this point in the history
This makes reviews easier and avoids expensive memory allocations and Base64 conversions at runtime.

The signature of `roots_pem` changes from `String`-returning function to `String` constant; however, that method has only been made public a few days ago, so no users should be affected.
  • Loading branch information
MrMage authored and rebello95 committed Feb 25, 2019
1 parent b899a30 commit ac3e175
Show file tree
Hide file tree
Showing 3 changed files with 4,448 additions and 14 deletions.
10 changes: 3 additions & 7 deletions Sources/RootsEncoder/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,9 @@ if CommandLine.arguments.contains("test") {
s += "import Foundation\n"
s += "\n"

s += "public func roots_pem() -> String {\n"
s += "public let kRootCertificates: String = \"\"\"\n"
let fileURL = URL(fileURLWithPath: "Assets/roots.pem")
let filedata = try Data(contentsOf: fileURL)
let encoding = filedata.base64EncodedString()
s += " let roots = \n"
s += " \"" + encoding + "\"\n"
s += " return String(data: Data(base64Encoded: roots, options: [])!, encoding: .utf8)!\n"
s += "}\n"
s += try String(contentsOf: fileURL)
s += "\"\"\""
}
print(s)
4 changes: 2 additions & 2 deletions Sources/SwiftGRPC/Core/Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class Channel {
underlyingChannel = withExtendedLifetime(argumentWrappers) {
var argumentValues = argumentWrappers.map { $0.wrapped }
if secure {
return cgrpc_channel_create_secure(address, roots_pem(), nil, nil, &argumentValues, Int32(arguments.count))
return cgrpc_channel_create_secure(address, kRootCertificates, nil, nil, &argumentValues, Int32(arguments.count))
} else {
return cgrpc_channel_create(address, &argumentValues, Int32(arguments.count))
}
Expand Down Expand Up @@ -83,7 +83,7 @@ public class Channel {
/// - Parameter clientCertificates: a PEM representation of the client certificates to use
/// - Parameter clientKey: a PEM representation of the client key to use
/// - Parameter arguments: list of channel configuration options
public init(address: String, certificates: String = roots_pem(), clientCertificates: String? = nil, clientKey: String? = nil, arguments: [Argument] = []) {
public init(address: String, certificates: String = kRootCertificates, clientCertificates: String? = nil, clientKey: String? = nil, arguments: [Argument] = []) {
gRPC.initialize()
host = address
let argumentWrappers = arguments.map { $0.toCArg() }
Expand Down
4,448 changes: 4,443 additions & 5 deletions Sources/SwiftGRPC/Core/Roots.swift

Large diffs are not rendered by default.

0 comments on commit ac3e175

Please sign in to comment.