-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathx509_workload_identity.rb
More file actions
executable file
·81 lines (69 loc) · 2.82 KB
/
Copy pathx509_workload_identity.rb
File metadata and controls
executable file
·81 lines (69 loc) · 2.82 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env ruby
# frozen_string_literal: true
# This enrolled-credential smoke test performs both a real X.509 token exchange
# and an actual OpenAI mTLS API request. It does not use or require an API key.
# Required: OPENAI_CLIENT_CERTIFICATE_CHAIN, OPENAI_CLIENT_KEY,
# IDENTITY_PROVIDER_ID, SERVICE_ACCOUNT_ID, and an organization enabled for
# certificate-authenticated workload identity. The optional key passphrase is
# OPENAI_CLIENT_KEY_PASSPHRASE. Set OPENAI_X509_PROXY_MODE=http_connect only for
# a caller-configured HTTP CONNECT proxy that keeps proxy credentials isolated.
require_relative "../lib/openai"
native_http_client = nil
failure = nil
begin
chain = OpenSSL::X509::Certificate.load(
File.binread(ENV.fetch("OPENAI_CLIENT_CERTIFICATE_CHAIN"))
)
raise ArgumentError, "Expected an enrolled client certificate" if chain.empty?
leaf, *intermediates = chain
key = OpenSSL::PKey.read(
File.binread(ENV.fetch("OPENAI_CLIENT_KEY")),
ENV["OPENAI_CLIENT_KEY_PASSPHRASE"]
)
unless leaf.check_private_key(key)
raise ArgumentError, "The enrolled certificate and private key do not match"
end
now = Time.now
raise ArgumentError, "The enrolled certificate is not yet valid" if now < leaf.not_before
raise ArgumentError, "The enrolled certificate has expired" if now > leaf.not_after
api_origin = ENV.fetch("OPENAI_X509_API_ORIGIN", "https://mtls.api.openai.com")
api_host = URI(api_origin).host&.downcase
approved_hosts = ["mtls.auth.openai.com", api_host].freeze
native_http_client = OpenAI::NetHTTPClient.new do |connection|
unless connection.use_ssl? && connection.port == 443 && approved_hosts.include?(connection.address.downcase)
raise ArgumentError, "Refusing to present the enrolled certificate to an unexpected destination"
end
connection.cert = leaf
connection.extra_chain_cert = intermediates
connection.key = key
end
identity = OpenAI::Auth::X509WorkloadIdentity.new(
identity_provider_id: ENV.fetch("IDENTITY_PROVIDER_ID"),
service_account_id: ENV.fetch("SERVICE_ACCOUNT_ID"),
http_client: native_http_client,
proxy: ENV.fetch("OPENAI_X509_PROXY_MODE", "direct").to_sym,
api_origin: api_origin
)
client = OpenAI::Client.new(
api_key: nil,
workload_identity: identity,
log_level: :off
)
model = client.models.list.data.first
raise "The enrolled service account cannot access any models" if model.nil?
rescue StandardError => error
failure = error
ensure
begin
native_http_client&.close
rescue StandardError => error
failure ||= error
end
end
if failure
status = failure.respond_to?(:status) ? failure.status : nil
status_message = status.is_a?(Integer) ? " (HTTP #{status})" : ""
warn("[x509] #{failure.class}#{status_message}")
exit(1)
end
puts("[x509] real issuer exchange and mTLS API request succeeded")