2

For Vapor script that runs on Linux I need to make a request that corresponds to following cURL but as of now, I was unable to find the best solution for this.

curl -X GET \
 https://endpoint \
  --cert ./sandbox.pem --key ./sandbox.key

PEM is of SHA256

Can you please navigate me to the best way to handle this?

I first tried implementing URLSessionDelegate's urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge) async -> (URLSession.AuthChallengeDisposition, URLCredential?) but I had no idea what way of parsing the key I should choose.

I was trying using HTTPClient, but that failed and said I should use MultiThreadedEventLoopGroup

let tlsConfig = TLSConfiguration.makeServerConfiguration(certificateChain: [.file(pemPath)], privateKey:  .file(keyPath))
    
let client = HTTPClient(eventLoopGroupProvider: .createNew, configuration: .init(tlsConfiguration: tlsConfig))

defer {
    client.shutdown()
}

let url = URL(string: "https:/endpoint")!
let request = try HTTPClient.Request(url: url, method: .GET)
let response = try await client.execute(request: request).get()

I did use it as:

let client = HTTPClient(eventLoopGroupProvider: .shared(MultiThreadedEventLoopGroup(numberOfThreads: 2)), configuration: .init(tlsConfiguration: tlsConfig))

But then I got an error

NIOPosix.NIOConnectionError(host: "endpoint", port: 443, dnsAError: nil, dnsAAAAError: nil, connectionErrors: [NIOPosix.SingleConnectionFailure(target: [IPv4] endpoint/%IP%, error: NIOSSL.NIOSSLError.failedToLoadPrivateKey)])

Thank you for any help/point at the right direction.

cluelessCoder
  • 948
  • 6
  • 19
  • It's failing to load the private key: `NIOSSL.NIOSSLError.failedToLoadPrivateKey`. What format is `sandbox.key` in? Also, you might have better luck asking in Related Projects > SwiftNIO on forums.swift.org . – Johannes Weiss Jul 03 '23 at 22:07
  • It is RSA private key. I'm asking here because I believe there could also be an URLSession answer how to handle that. But at this point, I don't care how to make it work :-)) – cluelessCoder Jul 03 '23 at 22:29
  • I meant if it's a PEM file or p12 or so. NIO says the loading failed – Johannes Weiss Jul 05 '23 at 06:03
  • Where are you storing the files? – Nick Jul 21 '23 at 10:08

0 Answers0