Class: AzIdentity::RedisCredentialsProvider

Inherits:
Object
  • Object
show all
Includes:
AzureEnvironment
Defined in:
lib/az_identity/redis_credentials_provider.rb

Overview

Adapts Azure identity credential acquisition to the credentials-provider interface expected by redis-client / redis-rb:

provider.call        # => [username, password]
provider.expires_at  # => Time or nil

The password is a Microsoft Entra access token for the Azure Cache for Redis scope. The username is the object (principal) ID of the managed identity or service principal, taken from the token's oid claim unless an explicit :username is supplied.

Instance Method Summary collapse

Methods included from AzureEnvironment

#authority_url, #redis_resource, #storage_resource

Constructor Details

#initialize(options = {}) ⇒ RedisCredentialsProvider

Returns a new instance of RedisCredentialsProvider.



22
23
24
25
26
# File 'lib/az_identity/redis_credentials_provider.rb', line 22

def initialize(options = {})
  @username = options[:username]
  @credential_client = options[:credential_client] || build_default_client(options)
  @mutex = Mutex.new
end

Instance Method Details

#callObject

Returns fresh [username, access_token]. Acquires or refreshes the token as needed. Called by the Redis client on every (re)connect and before a command whenever the token is close to expiring.



31
32
33
34
35
36
37
38
39
# File 'lib/az_identity/redis_credentials_provider.rb', line 31

def call
  credentials = @mutex.synchronize do
    @credentials = @credential_client.fetch_credentials_if_needed
  end

  raise Identity::BaseClient::FetchCredentialsError, 'no Azure credentials available for Redis' unless credentials

  [username(credentials.token), credentials.token]
end

#expires_atObject

The Time at which the current token expires, so the Redis client can re-authenticate long-lived connections before the token is rejected.



43
44
45
# File 'lib/az_identity/redis_credentials_provider.rb', line 43

def expires_at
  @mutex.synchronize { @credentials&.expires_at }
end