Class: Anthropic::Credentials::IdentityTokenFile

Inherits:
Object
  • Object
show all
Defined in:
lib/anthropic/credentials/identity_token_file.rb

Overview

An identity token provider that reads a JWT from a file on every call.

Kubernetes projected service-account tokens (and similar) are rotated in place, so the file MUST be re-read on every invocation rather than cached. This provider is used as the identity_token_provider argument to WorkloadIdentity.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ IdentityTokenFile

Returns a new instance of IdentityTokenFile.

Parameters:

  • path (String, nil) (defaults to: nil)

    explicit path to the token file; if nil, falls back to ANTHROPIC_IDENTITY_TOKEN_FILE environment variable

Raises:



19
20
21
22
23
24
25
26
# File 'lib/anthropic/credentials/identity_token_file.rb', line 19

def initialize(path = nil)
  resolved = resolve_path(path)
  if resolved.nil?
    raise Anthropic::Errors::ConfigurationError,
          "No identity token file path given. Pass path or set #{Credentials::ENV_IDENTITY_TOKEN_FILE}"
  end
  @path = Pathname.new(resolved)
end

Instance Attribute Details

#pathPathname (readonly)

Returns the resolved path to the identity token file.

Returns:

  • (Pathname)

    the resolved path to the identity token file



13
14
15
# File 'lib/anthropic/credentials/identity_token_file.rb', line 13

def path
  @path
end

Instance Method Details

#callString

Reads and returns the identity token from the file.

The file is re-read on every call to handle token rotation.

Returns:

  • (String)

    the identity token (JWT) with leading/trailing whitespace stripped

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/anthropic/credentials/identity_token_file.rb', line 35

def call
  content = @path.read.strip
  if content.empty?
    raise Anthropic::Errors::ConfigurationError,
          "Identity token file at #{@path} is empty"
  end
  content
rescue Errno::ENOENT
  raise Anthropic::Errors::ConfigurationError,
        "Identity token file not found at #{@path}"
rescue Errno::EACCES => e
  raise Anthropic::Errors::ConfigurationError,
        "Identity token file at #{@path} is not readable: #{e.message}"
rescue Errno::EISDIR
  raise Anthropic::Errors::ConfigurationError,
        "Identity token path #{@path} is a directory, not a file"
end