Module: PG::AzureWorkloadIdentity::Connection

Included in:
PG::AzureWorkloadIdentity
Defined in:
lib/pg/azure_workload_identity/connection.rb

Overview

Patch prepended to PG::Connection.singleton_class that teaches libpq's connection-parameter machinery about the non-standard azure_workload_identity option and injects a freshly generated auth token as the password when that option is set.

The three overrides cooperate:

  • #conndefaults adds azure_workload_identity to the list of known parameters so it is recognized rather than rejected as unknown.
  • #conninfo_parse parses a connection string while preserving the value of azure_workload_identity in the returned parameter list (libpq would otherwise drop it).
  • #parse_connect_args runs the final connection string through AuthTokenInjector so the password is replaced with a generated token whenever azure_workload_identity is set.

Instance Method Summary collapse

Instance Method Details

#conndefaultsArray<Hash>

Lists libpq's connection parameter defaults, with the azure_workload_identity option appended.

Returns:

  • (Array<Hash>)

    the connection parameter defaults.



27
28
29
# File 'lib/pg/azure_workload_identity/connection.rb', line 27

def conndefaults
  super + [conndefault_azure_workload_identity]
end

#conninfo_parse(connection_string) ⇒ Array<Hash>

Parses a connection string into the libpq parameter array, preserving the azure_workload_identity option.

The input is first round-tripped through PG::AzureWorkloadIdentity::ConnectionInfo so the custom option is stripped before being handed to libpq's parser, and then re-appended to the parsed result with its original value.

Parameters:

  • connection_string (String)

    either a URI-style or key=value connection string.

Returns:

  • (Array<Hash>)

    the parsed parameter array, including the azure_workload_identity entry when set.



42
43
44
45
46
47
48
49
50
# File 'lib/pg/azure_workload_identity/connection.rb', line 42

def conninfo_parse(connection_string)
  connection_info = ConnectionInfo.from_connection_string(connection_string)

  super(connection_info.to_s).tap do |result|
    if connection_info.azure_workload_identity
      result << conndefault_azure_workload_identity.merge(val: connection_info.azure_workload_identity)
    end
  end
end

#parse_connect_argsString

Normalizes connection arguments and injects an Azure Workload Identity auth token as the password when azure_workload_identity is set.

Parameters:

  • args (Array)

    the arguments passed to PG::Connection.new / PG.connect (forwarded to super).

Returns:

  • (String)

    the connection string with the auth token injected if applicable, otherwise unchanged.



59
60
61
# File 'lib/pg/azure_workload_identity/connection.rb', line 59

def parse_connect_args(...)
  AuthTokenInjector.new.inject_into_connection_string(super)
end