-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathmtls_custom_http_client.rb
More file actions
executable file
·61 lines (52 loc) · 2.17 KB
/
Copy pathmtls_custom_http_client.rb
File metadata and controls
executable file
·61 lines (52 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env ruby
# frozen_string_literal: true
# This example sends an API-key request over mutual TLS by configuring the
# SDK's pooled Net::HTTP connections with Ruby's native OpenSSL support. Set
# OPENAI_CLIENT_CERTIFICATE_CHAIN to a PEM file containing the leaf certificate
# followed by any required intermediates, OPENAI_CLIENT_KEY to the matching
# private key, and OPENAI_MODEL to a model available to the project.
# OPENAI_CLIENT_KEY_PASSPHRASE is optional.
require_relative "../lib/openai"
# Set this to the mTLS endpoint for your data residency. EU Data Residency uses
# https://mtls-eu.api.openai.com/v1. For enrollment and current endpoint
# guidance, see:
# https://help.openai.com/en/articles/10876024-openai-mutual-tls-beta-program
mtls_endpoint = URI("https://mtls.api.openai.com/v1")
certificates = OpenSSL::X509::Certificate.load(
File.binread(ENV.fetch("OPENAI_CLIENT_CERTIFICATE_CHAIN"))
)
raise ArgumentError, "Expected a client certificate" if certificates.empty?
leaf_certificate, *intermediates = certificates
private_key = OpenSSL::PKey.read(
File.binread(ENV.fetch("OPENAI_CLIENT_KEY")),
ENV["OPENAI_CLIENT_KEY_PASSPHRASE"]
)
unless leaf_certificate.check_private_key(private_key)
raise ArgumentError, "The client certificate and private key do not match"
end
now = Time.now
raise ArgumentError, "The client certificate is not yet valid" if now < leaf_certificate.not_before
raise ArgumentError, "The client certificate has expired" if now > leaf_certificate.not_after
mtls_destination = [mtls_endpoint.host, mtls_endpoint.port]
http_client = OpenAI::NetHTTPClient.new do |http|
unless http.use_ssl? && mtls_destination == [http.address, http.port]
raise ArgumentError, "Refusing to present the client certificate to an unexpected origin"
end
http.cert = leaf_certificate
http.extra_chain_cert = intermediates
http.key = private_key
end
client = OpenAI::Client.new(
api_key: ENV.fetch("OPENAI_API_KEY"),
base_url: mtls_endpoint.to_s,
http_client: http_client
)
begin
response = client.responses.create(
model: ENV.fetch("OPENAI_MODEL"),
input: "Reply with: mTLS transport configured"
)
puts(response.output_text)
ensure
http_client.close
end