將 SPIFFE JWT-SVID 交換為短效的 OpenAI 存取權杖,即可使用 SPIFFE 作為工作負載身分提供者。這讓經由 SPIRE 或其他相容於 SPIFFE 的身分提供者驗證的工作負載,無須儲存長效 API 金鑰就能呼叫 OpenAI API。
若要搭配 Codex 使用,請依照本頁取得並檢查 JWT-SVID。接著設定 Codex 工作負載身分,將該 Token 寫入檔案,並讓 Codex 指向該檔案。本頁的服務帳戶對應與 SDK 範例適用於 OpenAI API。
OpenAI 支援可作為 JWT 主體 Token 驗證的 SPIFFE JWT-SVID,其內容須包含簽發者、對象、到期時間、簽發時間戳記,以及可透過 JWKS 驗證的簽章。OpenAI 不支援將 SPIFFE X.509-SVID 作為工作負載身分聯合的主體 Token。
JWT-SVID 規格要求包含 sub、aud 與 exp 宣告。若要在 OpenAI 使用 JWT-SVID,Token 還必須包含 iss 與 iat 宣告,以及 kid 標頭,讓 OpenAI 能依據工作負載身分提供者的組態驗證 Token。
JWT-SVID 並非 OpenID Connect ID Token。SPIRE OIDC Discovery Provider 提供探索中繼資料與 JWKS 金鑰,讓 OpenAI 能驗證 JWT-SVID;它不會改變 Token 的 SPIFFE 語意,也不需要 OIDC 登入流程。
如需 SPIFFE 術語與 Token 要求的詳細資訊,請參閱 SPIFFE JWT-SVID 規格與 Workload API 規格。
設定 SPIFFE 提供者,為需要呼叫 OpenAI API 的工作負載簽發 JWT-SVID。以下指示使用 SPIRE 術語,但只要相容於 SPIFFE 的提供者所簽發的 JWT-SVID 包含 OpenAI 可驗證的簽發者資訊與 JWKS 簽章金鑰資料,就能使用相同的 OpenAI 組態。
你的 SPIFFE 設定必須提供:
- 工作負載的固定 SPIFFE ID,例如
spiffe://example.org/ns/production/sa/openai-wif。
- 專供存取 OpenAI 使用的單一 JWT-SVID 對象,例如
https://api.openai.com/v1,或你自行選擇的其他不透明值。
- JWT 簽發者 URL,須出現在 JWT-SVID 的
iss 宣告中,供 OpenAI 驗證。
- JWT-SVID 簽章金鑰的公開 JWKS,可透過 OIDC 探索或上傳 JWKS 提供。
- 讓工作負載端能從 SPIFFE Workload API 取得最新 JWT-SVID 的方式。
對象是必須完全相符的識別碼,不一定是接收 JWT-SVID 的端點。你可以使用 https://api.openai.com/v1 或其他服務專屬值,只要 SPIFFE Workload API 請求與 OpenAI 提供者組態中的值相符即可。
可行時,請透過 SPIRE OIDC Discovery Provider 提供 SPIFFE 簽發者資訊。將 SPIRE Server 的 jwt_issuer 與 OIDC Discovery Provider 的 jwt_issuer 設定為同一個 HTTPS 簽發者 URL,並在 OpenAI 中設定相同的 URL。
在 SPIRE Server 組態中:
1234server {
trust_domain = "example.org"
jwt_issuer = "https://spire-oidc.example.org"
}
在獨立的 SPIRE OIDC Discovery Provider 組態中:
123# Relevant issuer fields only
domains = ["spire-oidc.example.org"]
jwt_issuer = "https://spire-oidc.example.org"
OIDC Discovery Provider 組態也需要金鑰資料來源,例如 server_api、workload_api 或 file,以及提供服務的機制,例如 ACME、TLS 憑證或 Unix 通訊端。如需完整的組態選項,請參閱 SPIRE OIDC Discovery Provider 文件。
SPIFFE 信任網域與 JWT 簽發者是不同的概念。在此範例中,JWT-SVID 的主體是 example.org 信任網域中的 SPIFFE ID,而簽發者則是 HTTPS 簽發者 URL:
1234{
"sub": "spiffe://example.org/ns/production/sa/openai-wif",
"iss": "https://spire-oidc.example.org"
}
SPIRE OIDC Discovery Provider 提供 OIDC 探索文件與 JWKS 端點,供 OpenAI 在停用 使用上傳的 JWKS 驗證 Token 時使用。
如果 OpenAI 無法連線至你的簽發者探索端點,請改用上傳 JWKS 模式。在此模式下,OpenAI 仍會比對工作負載身分提供者的簽發者與 JWT-SVID 的 iss 宣告,但會使用你儲存在工作負載身分提供者中的 JWKS JSON 來驗證簽章。
注意: SPIFFE JWT-SVID 規格將 JWT 標頭 kid 列為選用,但 OpenAI 要求 JWT 主體 Token 必須包含 kid 標頭,以便從設定的 JWKS 中選取簽章金鑰。如果你的 SPIFFE 提供者允許省略 kid,請將它設定為在 OpenAI 工作負載身分聯合使用的 Token 中包含此標頭。
若要從能呼叫 SPIFFE Workload API 的工作負載檢查 JWT-SVID,請以你將在 OpenAI 中設定的相同對象請求一個 JWT-SVID。請在與應用程式相同的工作負載上下文中執行此指令,因為 Workload API 的授權取決於呼叫程序的身分。
1234TOKEN=$(spire-agent api fetch jwt \
-socketPath /run/spire/sockets/agent.sock \
-audience "https://api.openai.com/v1" | sed -n '2p')
export TOKEN
如果你的工作負載有多個 SPIFFE ID,請指定要請求的身分:
12345TOKEN=$(spire-agent api fetch jwt \
-socketPath /run/spire/sockets/agent.sock \
-spiffeID "spiffe://example.org/ns/production/sa/openai-wif" \
-audience "https://api.openai.com/v1" | sed -n '2p')
export TOKEN
設定工作負載身分聯合前,請先將 JWT-SVID 匯出為 TOKEN 環境變數,再於本機執行以下任一範例,檢查其標頭與宣告:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25const parts = process.env.TOKEN?.split(".") ?? [];
if (parts.length !== 3) {
throw new Error("Expected a compact JWT with three segments");
}
const decode = (segment) => {
if (!/^[A-Za-z0-9_-]+$/.test(segment) || segment.length % 4 === 1) {
throw new Error("JWT segment is not valid Base64URL");
}
const bytes = Buffer.from(segment, "base64url");
if (bytes.toString("base64url") !== segment) {
throw new Error("JWT segment is not valid Base64URL");
}
const decoded = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
const value = JSON.parse(decoded);
if (value === null || Array.isArray(value) || typeof value !== "object") {
throw new Error("JWT segment is not a JSON object");
}
return decoded;
};
console.log("Header:");
console.log(decode(parts[0]));
console.log("\nPayload:");
console.log(decode(parts[1]));
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
33import base64
import json
import os
import re
def reject_non_json_constant(value):
raise ValueError(f"JWT segment contains non-JSON constant: {value}")
parts = os.environ.get("TOKEN", "").split(".")
if len(parts) != 3:
raise ValueError("Expected a compact JWT with three segments")
def decode(segment):
if re.fullmatch(r"[A-Za-z0-9_-]+", segment) is None or len(segment) % 4 == 1:
raise ValueError("JWT segment is not valid Base64URL")
padded_segment = segment + "=" * (-len(segment) % 4)
decoded = base64.b64decode(padded_segment, altchars=b"-_", validate=True)
if base64.urlsafe_b64encode(decoded).rstrip(b"=").decode("ascii") != segment:
raise ValueError("JWT segment is not valid Base64URL")
decoded_text = decoded.decode("utf-8")
value = json.loads(decoded_text, parse_constant=reject_non_json_constant)
if not isinstance(value, dict):
raise ValueError("JWT segment is not a JSON object")
return decoded_text
print("Header:")
print(decode(parts[0]))
print("\nPayload:")
print(decode(parts[1]))
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
82
83
84package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"strings"
"unicode/utf8"
)
func decodeSegment(segment string) (json.RawMessage, error) {
if !isBase64URLSegment(segment) {
return nil, fmt.Errorf("JWT segment is not valid Base64URL")
}
decoded, err := base64.RawURLEncoding.DecodeString(segment)
if err != nil {
return nil, err
}
if base64.RawURLEncoding.EncodeToString(decoded) != segment {
return nil, fmt.Errorf("JWT segment is not valid Base64URL")
}
if !utf8.Valid(decoded) {
return nil, fmt.Errorf("JWT segment is not valid UTF-8")
}
var value json.RawMessage
if err := json.Unmarshal(decoded, &value); err != nil {
return nil, err
}
if trimmed := bytes.TrimSpace(value); len(trimmed) == 0 || trimmed[0] != '{' {
return nil, fmt.Errorf("JWT segment is not a JSON object")
}
return value, nil
}
func isBase64URLSegment(segment string) bool {
if segment == "" || len(segment)%4 == 1 {
return false
}
for _, character := range segment {
if !('A' <= character && character <= 'Z') &&
!('a' <= character && character <= 'z') &&
!('0' <= character && character <= '9') &&
character != '-' &&
character != '_' {
return false
}
}
return true
}
func printJSON(label string, value json.RawMessage) error {
formatted, err := json.MarshalIndent(value, "", " ")
if err != nil {
return err
}
fmt.Printf("%s:\n%s\n", label, formatted)
return nil
}
func main() {
parts := strings.Split(os.Getenv("TOKEN"), ".")
if len(parts) != 3 {
panic("Expected a compact JWT with three segments")
}
header, err := decodeSegment(parts[0])
if err != nil {
panic(err)
}
payload, err := decodeSegment(parts[1])
if err != nil {
panic(err)
}
if err := printJSON("Header", header); err != nil {
panic(err)
}
fmt.Println()
if err := printJSON("Payload", payload); err != nil {
panic(err)
}
}
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// Add Jackson (com.fasterxml.jackson.core:jackson-databind) to your project.
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public final class DecodeJwtExample {
private static final ObjectMapper JSON =
new ObjectMapper().enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
private DecodeJwtExample() {}
static String decodeUtf8(byte[] bytes) throws IOException {
try {
return StandardCharsets.UTF_8
.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT)
.decode(ByteBuffer.wrap(bytes))
.toString();
} catch (CharacterCodingException exception) {
throw new IOException("JWT segment is not valid UTF-8", exception);
}
}
static String decodeSegment(String segment) throws IOException {
if (!isBase64UrlSegment(segment)) {
throw new IllegalArgumentException("JWT segment is not valid Base64URL");
}
byte[] bytes = Base64.getUrlDecoder().decode(segment);
if (!Base64.getUrlEncoder().withoutPadding().encodeToString(bytes).equals(segment)) {
throw new IllegalArgumentException("JWT segment is not valid Base64URL");
}
String decoded = decodeUtf8(bytes);
JsonNode value = JSON.readTree(decoded);
if (value == null || value.isMissingNode() || !value.isObject()) {
throw new IOException("JWT segment is not a JSON object");
}
return decoded;
}
static boolean isBase64UrlSegment(String segment) {
if (segment.isEmpty() || segment.length() % 4 == 1) {
return false;
}
return segment
.chars()
.allMatch(
character ->
character >= 'A' && character <= 'Z'
|| character >= 'a' && character <= 'z'
|| character >= '0' && character <= '9'
|| character == '-'
|| character == '_');
}
static String[] requireCompactJwt(String token) {
if (token == null) {
throw new IllegalArgumentException("Expected a compact JWT with three segments");
}
String[] parts = token.split("\\.", -1);
if (parts.length != 3) {
throw new IllegalArgumentException("Expected a compact JWT with three segments");
}
return parts;
}
public static void main(String[] args) throws IOException {
String[] parts = requireCompactJwt(System.getenv("TOKEN"));
System.out.println("Header:");
System.out.println(decodeSegment(parts[0]));
System.out.println("\nPayload:");
System.out.println(decodeSegment(parts[1]));
}
}
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
62using System.Text;
using System.Text.Json;
static string DecodeSegment(string segment)
{
if (
segment.Length % 4 == 1 ||
segment.Any(
character =>
!(
character is >= 'A' and <= 'Z' ||
character is >= 'a' and <= 'z' ||
character is >= '0' and <= '9' ||
character is '-' or '_'
)
)
)
{
throw new FormatException("JWT segment is not valid Base64URL");
}
byte[] decoded = Convert.FromBase64String(
segment.Replace('-', '+').Replace('_', '/') +
new string('=', (4 - segment.Length % 4) % 4)
);
string canonicalSegment = Convert
.ToBase64String(decoded)
.TrimEnd('=')
.Replace('+', '-')
.Replace('/', '_');
if (canonicalSegment != segment)
{
throw new FormatException("JWT segment is not valid Base64URL");
}
string decodedJson = new UTF8Encoding(false, true).GetString(decoded);
using JsonDocument document = JsonDocument.Parse(decodedJson);
if (document.RootElement.ValueKind is not JsonValueKind.Object)
{
throw new FormatException("JWT segment is not a JSON object");
}
return decodedJson;
}
string? token = Environment.GetEnvironmentVariable("TOKEN");
if (token is null)
{
throw new InvalidOperationException(
"Expected a compact JWT with three segments"
);
}
string[] parts = token.Split('.');
if (parts.Length != 3)
{
throw new InvalidOperationException(
"Expected a compact JWT with three segments"
);
}
Console.WriteLine("Header:");
Console.WriteLine(DecodeSegment(parts[0]));
Console.WriteLine("\nPayload:");
Console.WriteLine(DecodeSegment(parts[1]));
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
34require "base64"
require "json"
parts = ENV.fetch("TOKEN", "").split(".", -1)
raise "Expected a compact JWT with three segments" unless parts.length == 3
decode = lambda do |segment|
unless segment.match?(/\A[A-Za-z0-9_-]+\z/) && segment.length % 4 != 1
raise "JWT segment is not valid Base64URL"
end
padded = segment.ljust((segment.length + 3) & ~3, "=")
begin
decoded = Base64.urlsafe_decode64(padded)
rescue ArgumentError
raise "JWT segment is not valid Base64URL"
end
unless Base64.urlsafe_encode64(decoded, padding: false) == segment
raise "JWT segment is not valid Base64URL"
end
decoded.force_encoding(Encoding::UTF_8)
raise "JWT segment is not valid UTF-8" unless decoded.valid_encoding?
value = JSON.parse(decoded)
raise "JWT segment is not a JSON object" unless value.is_a?(Hash)
decoded
end
puts("Header:")
puts(decode.call(parts[0]))
puts("\nPayload:")
puts(decode.call(parts[1]))
每個範例都會解碼 JWT,但不會驗證 Token 簽章。請使用本機解碼器處理正式環境的 Token,並避免將正式環境的 Token 貼入第三方工具。
解碼後的 SPIFFE JWT-SVID 內容會類似以下範例:
1234{
"alg": "ES256",
"kid": "jwt-svid-key-1"
}
1234567{
"iss": "https://spire-oidc.example.org",
"aud": ["https://api.openai.com/v1"],
"sub": "spiffe://example.org/ns/production/sa/openai-wif",
"iat": 1716235422,
"exp": 1716235722
}
交換 Token 前,請使用解碼後的內容,比對收到的 Token 與 OpenAI 組態。檢查標頭中的 alg 與 kid,以及酬載中的 iss、aud、sub、iat 與 exp。alg 的實際值取決於你的 SPIRE Server JWT 簽章金鑰組態。
在 OpenAI 中為 SPIFFE JWT-SVID 簽發者建立工作負載身分提供者,再新增與你信任的 SPIFFE ID 相符的服務帳戶對應。
-
建立工作負載身分提供者。 將 名稱 設定為唯一值,例如 spiffe-prod。使用 說明欄位協助管理員識別提供者,例如填入 Production SPIFFE workloads。
-
設定簽發者與對象。 將 OIDC 簽發者 URL 設定為與 JWT-SVID 的 iss 宣告完全相同的值,例如 https://spire-oidc.example.org。將 對象 設定為向 SPIFFE Workload API 發出請求時使用的對象值。在此範例中,該值為 https://api.openai.com/v1。
-
選擇 JWKS 來源。 如果 OpenAI 能連線至你的 SPIRE OIDC Discovery Provider,請讓 使用上傳的 JWKS 驗證 Token 保持停用。OpenAI 會透過 OIDC 探索及探索到的 JWKS 驗證 JWT-SVID 簽章。
如果 OpenAI 無法連線至簽發者,請啟用 使用上傳的 JWKS 驗證 Token,再將 JWKS JSON 設定為 JWT-SVID 簽章金鑰的公開金鑰集。請上傳完整的公開 JWKS 物件,包含包覆金鑰的 keys 陣列。請勿包含私密金鑰資料。
-
僅在需要衍生對應屬性時新增屬性轉換。 直接從 sub 進行對應時,不需要屬性轉換。只有在需要從一或多個 Token 宣告衍生出對應值時,才使用屬性轉換。如需轉換行為的詳細資訊,請參閱工作負載身分聯合主要指南。
-
建立服務帳戶對應。 將 名稱 設定為在該工作負載身分提供者內唯一的值,例如 production-openai-wif。使用 說明欄位說明哪些工作負載可以使用此對應,例如填入 Production SPIFFE workload for OpenAI API access。
-
比對 SPIFFE ID。 將 鍵 設定為 sub,並將 值 設定為工作負載的 SPIFFE ID,例如 spiffe://example.org/ns/production/sa/openai-wif。
對於具特殊權限的工作負載,請優先採用 SPIFFE ID 完全相符的比對方式。只有在該前綴下的每個 SPIFFE ID 都應能取得新簽發的 OpenAI 存取權杖時,才使用結尾萬用字元。例如,spiffe://example.org/ns/production/sa/* 允許任何相符的正式環境服務帳戶路徑。
-
選擇 OpenAI 目標。 將 專案 設定為目標服務帳戶所屬的 OpenAI 專案。將 服務帳戶 設定為 SPIFFE 工作負載可使用的 OpenAI 服務帳戶,例如 spiffe-prod-openai-wif。若想為此對應建立新的服務帳戶,而非重用現有帳戶,請勾選 Create a new service account in this project。
-
視需要縮小 API 權限範圍。 選取適當的 權限 ,例如 api.model.request 與 api.vector_store.read,進一步限制透過此對應簽發的存取權杖。將權限留白,即可避免新增 WIF 專屬的範圍限制;Token 仍會以對應的服務帳戶身分進行授權。
設定 OpenAI SDK 用戶端,將最新的 SPIFFE JWT-SVID 交換為 OpenAI 簽發的存取權杖。
以下 SDK 範例假設你的 SPIFFE 整合會更新 JWT-SVID 並將其寫入 /var/run/spiffe/openai.jwt。請確保只有該工作負載能讀取此檔案。由於 JWT-SVID 的有效期很短,請在 Token 到期前更新檔案。另一種做法是在可行時,於主體 Token 提供者中使用對應程式語言的 SPIFFE 程式庫,直接從 SPIFFE Workload API 取得 JWT-SVID,以免 Token 檔案過時。
在工作負載環境中設定 OPENAI_IDENTITY_PROVIDER_ID 與 OPENAI_SERVICE_ACCOUNT_ID。Token 檔案包含外部主體 Token。OPENAI_IDENTITY_PROVIDER_ID 用來識別 OpenAI 工作負載身分提供者,OPENAI_SERVICE_ACCOUNT_ID 則用來識別目標 OpenAI 服務帳戶。接著,OpenAI 會根據 Token 宣告,尋找該提供者與服務帳戶的相符對應。
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
40import { readFile } from "node:fs/promises";
import OpenAI from "openai";
const tokenPath = "/var/run/spiffe/openai.jwt";
const identityProviderId = process.env.OPENAI_IDENTITY_PROVIDER_ID;
const serviceAccountId = process.env.OPENAI_SERVICE_ACCOUNT_ID;
if (!identityProviderId || !serviceAccountId) {
throw new Error(
"Set OPENAI_IDENTITY_PROVIDER_ID and OPENAI_SERVICE_ACCOUNT_ID"
);
}
function spiffeJwtSvidProvider(path) {
return {
tokenType: "jwt",
getToken: async () => {
const token = (await readFile(path, "utf8")).trim();
if (!token) {
throw new Error("The SPIFFE JWT-SVID file is empty.");
}
return token;
},
};
}
const client = new OpenAI({
workloadIdentity: {
identityProviderId,
serviceAccountId,
provider: spiffeJwtSvidProvider(tokenPath),
},
});
const response = await client.responses.create({
model: "gpt-5.6-terra",
input: "Say hello from SPIFFE workload identity federation.",
});
console.log(response.output_text);
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
33import os
from pathlib import Path
from openai import OpenAI
from openai.auth import SubjectTokenProvider
TOKEN_PATH = "/var/run/spiffe/openai.jwt"
def spiffe_jwt_svid_provider(token_path: str) -> SubjectTokenProvider:
def get_token() -> str:
token = Path(token_path).read_text().strip()
if not token:
raise RuntimeError("The SPIFFE JWT-SVID file is empty.")
return token
return {"token_type": "jwt", "get_token": get_token}
client = OpenAI(
workload_identity={
"identity_provider_id": os.environ["OPENAI_IDENTITY_PROVIDER_ID"],
"service_account_id": os.environ["OPENAI_SERVICE_ACCOUNT_ID"],
"provider": spiffe_jwt_svid_provider(TOKEN_PATH),
},
)
response = client.responses.create(
model="gpt-5.6-terra",
input="Say hello from SPIFFE workload identity federation.",
)
print(response.output_text)
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
69package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/auth"
"github.com/openai/openai-go/v3/option"
"github.com/openai/openai-go/v3/responses"
)
const tokenPath = "/var/run/spiffe/openai.jwt"
type spiffeJWTSVIDProvider struct {
path string
}
func (p spiffeJWTSVIDProvider) TokenType() auth.SubjectTokenType {
return auth.SubjectTokenTypeJWT
}
func (p spiffeJWTSVIDProvider) GetToken(ctx context.Context, _ auth.HTTPDoer) (string, error) {
data, err := os.ReadFile(p.path)
if err != nil {
return "", &auth.SubjectTokenProviderError{
Provider: "spiffe",
Message: "failed to read SPIFFE JWT-SVID",
Cause: err,
}
}
token := strings.TrimSpace(string(data))
if token == "" {
return "", &auth.SubjectTokenProviderError{
Provider: "spiffe",
Message: "SPIFFE JWT-SVID file is empty",
}
}
return token, nil
}
func main() {
client := openai.NewClient(
option.WithWorkloadIdentity(auth.WorkloadIdentity{
IdentityProviderID: os.Getenv("OPENAI_IDENTITY_PROVIDER_ID"),
ServiceAccountID: os.Getenv("OPENAI_SERVICE_ACCOUNT_ID"),
Provider: spiffeJWTSVIDProvider{
path: tokenPath,
},
}),
)
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: openai.ChatModelGPT4_1Mini,
Input: responses.ResponseNewParamsInputUnion{
OfString: openai.String("Say hello from SPIFFE workload identity federation."),
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(response.OutputText())
}
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
75import com.fasterxml.jackson.databind.json.JsonMapper;
import com.openai.auth.SubjectTokenProvider;
import com.openai.auth.SubjectTokenType;
import com.openai.auth.WorkloadIdentity;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.HttpClient;
import com.openai.errors.SubjectTokenProviderException;
import com.openai.models.responses.ResponseCreateParams;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.CompletableFuture;
public final class SpiffeWorkloadIdentityExample {
private static final String TOKEN_PATH = "/var/run/spiffe/openai.jwt";
private SpiffeWorkloadIdentityExample() {}
static final class SpiffeJwtSvidProvider implements SubjectTokenProvider {
private final Path tokenPath;
SpiffeJwtSvidProvider(String tokenPath) {
this.tokenPath = Path.of(tokenPath);
}
@Override
public SubjectTokenType tokenType() {
return SubjectTokenType.JWT;
}
@Override
public String getToken(HttpClient httpClient, JsonMapper jsonMapper) {
String token;
try {
token = Files.readString(tokenPath).trim();
} catch (Exception e) {
throw new SubjectTokenProviderException("spiffe", "failed to read SPIFFE JWT-SVID", e);
}
if (token.isEmpty()) {
throw new SubjectTokenProviderException("spiffe", "SPIFFE JWT-SVID file is empty", null);
}
return token;
}
@Override
public CompletableFuture<String> getTokenAsync(HttpClient httpClient, JsonMapper jsonMapper) {
return CompletableFuture.supplyAsync(() -> getToken(httpClient, jsonMapper));
}
}
public static void main(String[] args) {
WorkloadIdentity workloadIdentity =
WorkloadIdentity.builder()
.identityProviderId(System.getenv("OPENAI_IDENTITY_PROVIDER_ID"))
.serviceAccountId(System.getenv("OPENAI_SERVICE_ACCOUNT_ID"))
.provider(new SpiffeJwtSvidProvider(TOKEN_PATH))
.build();
OpenAIClient client = OpenAIOkHttpClient.builder().workloadIdentity(workloadIdentity).build();
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-5.6-terra")
.input("Say hello from SPIFFE workload identity federation.")
.build();
client.responses().create(params).output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(outputText -> System.out.println(outputText.text()));
}
}
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
49require "openai"
TOKEN_PATH = "/var/run/spiffe/openai.jwt"
class SpiffeJWTSVIDProvider
include OpenAI::Auth::SubjectTokenProvider
def initialize(token_path:)
@token_path = token_path
end
def token_type
OpenAI::Auth::TokenType::JWT
end
def get_token
token = File.read(@token_path).strip
if token.empty?
raise OpenAI::Errors::SubjectTokenProviderError.new(
message: "SPIFFE JWT-SVID file is empty",
provider: "spiffe"
)
end
token
rescue SystemCallError => e
raise OpenAI::Errors::SubjectTokenProviderError.new(
message: "Failed to read SPIFFE JWT-SVID: #{e.message}",
provider: "spiffe",
cause: e
)
end
end
provider = SpiffeJWTSVIDProvider.new(token_path: TOKEN_PATH)
workload_identity = OpenAI::Auth::WorkloadIdentity.new(
identity_provider_id: ENV.fetch("OPENAI_IDENTITY_PROVIDER_ID"),
service_account_id: ENV.fetch("OPENAI_SERVICE_ACCOUNT_ID"),
provider: provider
)
client = OpenAI::Client.new(workload_identity: workload_identity)
response = client.responses.create(
model: "gpt-5.6-terra",
input: "Say hello from SPIFFE workload identity federation."
)
puts(response.output_text)
- 請使用 JWT-SVID 進行 OpenAI 工作負載身分聯合。X.509-SVID 適用於雙向 TLS,但 OpenAI Token 交換端點不接受此類 SVID。
- 請使用單一專用對象來存取 OpenAI。避免使用範圍過大的對象,例如整個信任網域或環境名稱。
- 盡可能採用 SPIFFE ID 完全相符的比對方式。只有在刻意共用信任邊界時,才使用萬用字元對應。
- 將 JWT-SVID 的有效期設短一些,以降低持有者 Token 遭重放的風險。OpenAI 存取權杖的到期時間絕不會晚於交換時使用的外部主體 Token。
- 請謹慎輪替簽章金鑰。在輪替期間,透過 OIDC 探索同時發布新舊公開金鑰;或在簽發使用新
kid 的 JWT-SVID 前,更新已上傳的公開 JWKS。
- 讓 SPIRE Server 與工作負載的時鐘保持同步。時鐘偏差過大,可能導致原本有效的 JWT-SVID 被判定為尚未生效、簽發時間過久或已到期而遭到拒絕。
- 保護 SPIFFE Workload API 通訊端。能取得工作負載 JWT-SVID 的程序,就能嘗試用它交換 OpenAI 存取權。
- 讓 OpenAI 服務帳戶的邊界與應用程式和環境的權限邊界保持一致。不要讓互不相關的 SPIFFE 工作負載共用高權限服務帳戶。
- 監控 Token 交換失敗的情況,檢查是否因簽發者、對象、簽署金鑰或對應不符所致。