Class: PG::AzureWorkloadIdentity::ConnectionInfo::URI

Inherits:
Object
  • Object
show all
Defined in:
lib/pg/azure_workload_identity/connection_info/uri.rb

Overview

Parses and manipulates a PostgreSQL connection string in URI format (e.g. postgres://user@host:5432/db?param=value).

Connection parameters may be provided either as URI components (userinfo, host, port) or as query-string parameters. Accessors prefer the URI component and fall back to the query string when absent.

The non-standard azure_workload_identity query parameter is recognized and extracted on construction so the value can drive Azure Workload Identity authentication while keeping the rest of the connection string valid for libpq.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_string) ⇒ URI

Parses the given connection string and extracts the azure_workload_identity query parameter (if any) so it is not forwarded to libpq.

Parameters:

  • connection_string (String)

    a PostgreSQL connection URI.



39
40
41
42
43
# File 'lib/pg/azure_workload_identity/connection_info/uri.rb', line 39

def initialize(connection_string)
  @uri = ::URI.parse(connection_string)
  @query = @uri.query ? ::URI.decode_www_form(@uri.query).to_h : {} # : Hash[String, String]
  @azure_workload_identity = @query.delete("azure_workload_identity")
end

Instance Attribute Details

#azure_workload_identityString? (readonly)

Returns the value of the azure_workload_identity query parameter that was present in the original connection string, or nil if the parameter was not set.

Returns:

  • (String, nil)

    the value of the azure_workload_identity query parameter that was present in the original connection string, or nil if the parameter was not set.



32
33
34
# File 'lib/pg/azure_workload_identity/connection_info/uri.rb', line 32

def azure_workload_identity
  @azure_workload_identity
end

Class Method Details

.matches?(connection_string) ⇒ Boolean

Tests whether a connection string is in URI format.

Parameters:

  • connection_string (String)

    the connection string to test.

Returns:

  • (Boolean)

    true if the string matches the RFC 2396 absolute URI reference grammar, false otherwise.



25
26
27
# File 'lib/pg/azure_workload_identity/connection_info/uri.rb', line 25

def self.matches?(connection_string)
  /\A#{::URI::RFC2396_PARSER.regexp[:ABS_URI_REF]}\z/.match?(connection_string)
end

Instance Method Details

#hostString?

Returns the host from the URI authority, falling back to the host query parameter when the URI host is missing or empty.

Returns:

  • (String, nil)

    the host from the URI authority, falling back to the host query parameter when the URI host is missing or empty.



53
54
55
56
57
# File 'lib/pg/azure_workload_identity/connection_info/uri.rb', line 53

def host
  return @query["host"] if @uri.host.nil? || @uri.host.empty?

  @uri.host
end

#password=(value) ⇒ String

Sets the password, storing it as a password query parameter and clearing any password embedded in the URI userinfo.

Parameters:

  • value (String)

    the password to set.

Returns:

  • (String)

    the value that was set.



70
71
72
73
# File 'lib/pg/azure_workload_identity/connection_info/uri.rb', line 70

def password=(value)
  @uri.password = nil
  @query["password"] = value
end

#portInteger, ...

Returns the port from the URI authority, falling back to the port query parameter.

Returns:

  • (Integer, String, nil)

    the port from the URI authority, falling back to the port query parameter.



61
62
63
# File 'lib/pg/azure_workload_identity/connection_info/uri.rb', line 61

def port
  @uri.port || @query["port"]
end

#to_sString

Serializes the connection info back to a URI string.

The query string is rebuilt from the current parameters (so the azure_workload_identity flag is omitted) and the scheme is normalized to always include the "//" authority separator, ensuring the result is a valid libpq connection URI.

Returns:

  • (String)

    the connection URI.



83
84
85
86
87
# File 'lib/pg/azure_workload_identity/connection_info/uri.rb', line 83

def to_s
  @uri.query = ::URI.encode_www_form(@query)

  @uri.to_s.sub(%r{^#{@uri.scheme}:(?!//)}, "#{@uri.scheme}://")
end

#userString?

Returns the username from the URI userinfo, falling back to the user query parameter.

Returns:

  • (String, nil)

    the username from the URI userinfo, falling back to the user query parameter.



47
48
49
# File 'lib/pg/azure_workload_identity/connection_info/uri.rb', line 47

def user
  @uri.user || @query["user"]
end