Class: Google::Cloud::PubSub::Subscription::PushConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/pubsub/subscription/push_config.rb

Overview

Configuration for a push delivery endpoint.

Examples:

Create a push config:

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new
topic = pubsub.topic "my-topic"

push_config = Google::Cloud::PubSub::Subscription::PushConfig.new endpoint: "http://example.net/callback"
push_config.set_oidc_token "service-account@example.net", "audience-header-value"

sub = topic.subscribe "my-subscription", push_config: push_config

Read a push config:

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

sub = pubsub.subscription "my-topic-sub"
sub.push_config.endpoint #=> "http://example.com/callback"
sub.push_config.authentication.email #=> "user@example.com"
sub.push_config.authentication.audience #=> "client-12345"

Update a push config:

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new
sub = pubsub.subscription "my-subscription"

sub.push_config do |pc|
  pc.endpoint = "http://example.net/callback"
  pc.set_oidc_token "user@example.net", "client-67890"
end

Defined Under Namespace

Classes: OidcToken

Instance Method Summary collapse

Constructor Details

#initialize(endpoint: nil, email: nil, audience: nil) ⇒ PushConfig

Creates a new push configuration.

Parameters:

  • endpoint (String) (defaults to: nil)

    A URL locating the endpoint to which messages should be pushed. For example, a Webhook endpoint might use https://example.com/push.

  • email (String) (defaults to: nil)

    The service account email to be used for generating the OIDC token. The caller must have the iam.serviceAccounts.actAs permission for the service account.

  • audience (String) (defaults to: nil)

    The audience to be used when generating OIDC token. The audience claim identifies the recipients that the JWT is intended for. The audience value is a single case-sensitive string. Having multiple values (array) for the audience field is not supported. More info about the OIDC JWT token audience here: https://tools.ietf.org/html/rfc7519#section-4.1.3 Note: if not specified, the endpoint URL will be used.

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
# File 'lib/google/cloud/pubsub/subscription/push_config.rb', line 71

def initialize endpoint: nil, email: nil, audience: nil
  @grpc = Google::Cloud::PubSub::V1::PushConfig.new

  self.endpoint = endpoint unless endpoint.nil?

  raise ArgumentError, "audience provided without email. Authentication is invalid" if audience && !email

  set_oidc_token email, audience if email
end

Instance Method Details

#authenticationOidcToken?

The authentication method used by push endpoints to verify the source of push requests.

Returns:

  • (OidcToken, nil)

    An OIDC JWT token if specified, nil otherwise.



104
105
106
107
108
# File 'lib/google/cloud/pubsub/subscription/push_config.rb', line 104

def authentication
  return nil unless @grpc.authentication_method == :oidc_token

  OidcToken.from_grpc @grpc.oidc_token
end

#authentication=(new_auth) ⇒ Object

Sets the authentication method used by push endpoints to verify the source of push requests.

Parameters:

  • new_auth (OidcToken, nil)

    An authentication value.



114
115
116
117
118
119
120
121
122
# File 'lib/google/cloud/pubsub/subscription/push_config.rb', line 114

def authentication= new_auth
  if new_auth.nil?
    @grpc.oidc_token = nil
  else
    raise ArgumentError unless new_auth.is_a? OidcToken

    @grpc.oidc_token = new_auth.to_grpc
  end
end

#endpointString

A URL locating the endpoint to which messages should be pushed. For example, a Webhook endpoint might use https://example.com/push.

Returns:

  • (String)


86
87
88
# File 'lib/google/cloud/pubsub/subscription/push_config.rb', line 86

def endpoint
  @grpc.push_endpoint
end

#endpoint=(new_endpoint) ⇒ Object

Sets the URL locating the endpoint to which messages should be pushed. For example, a Webhook endpoint might use https://example.com/push.

Parameters:

  • new_endpoint (String, nil)

    New URL value



95
96
97
# File 'lib/google/cloud/pubsub/subscription/push_config.rb', line 95

def endpoint= new_endpoint
  @grpc.push_endpoint = String new_endpoint
end

#oidc_token?Boolean

Checks whether authentication is an OidcToken.

Returns:

  • (Boolean)


128
129
130
# File 'lib/google/cloud/pubsub/subscription/push_config.rb', line 128

def oidc_token?
  authentication.is_a? OidcToken
end

#set_oidc_token(email, audience) ⇒ Object

Sets the authentication method to use an OidcToken.

Parameters:

  • email (String)

    Service account email.

  • audience (String)

    Audience to be used.



137
138
139
140
141
142
143
# File 'lib/google/cloud/pubsub/subscription/push_config.rb', line 137

def set_oidc_token email, audience
  oidc_token = OidcToken.new.tap do |token|
    token.email = email
    token.audience = audience
  end
  self.authentication = oidc_token
end

#versionString

The format of the pushed message. This attribute indicates the version of the data expected by the endpoint. This controls the shape of the pushed message (i.e., its fields and metadata). The endpoint version is based on the version of the Pub/Sub API.

If not present during the Subscription creation, it will default to the version of the API used to make such call.

The possible values for this attribute are:

  • v1beta1: uses the push format defined in the v1beta1 Pub/Sub API.
  • v1 or v1beta2: uses the push format defined in the v1 Pub/Sub API.

Returns:

  • (String)


161
162
163
# File 'lib/google/cloud/pubsub/subscription/push_config.rb', line 161

def version
  @grpc.attributes["x-goog-version"]
end

#version=(new_version) ⇒ Object

Sets the format of the pushed message.

The possible values for this attribute are:

  • v1beta1: uses the push format defined in the v1beta1 Pub/Sub API.
  • v1 or v1beta2: uses the push format defined in the v1 Pub/Sub API.

Parameters:

  • new_version (String, nil)

    The new version value.



176
177
178
179
180
181
182
# File 'lib/google/cloud/pubsub/subscription/push_config.rb', line 176

def version= new_version
  if new_version.nil?
    @grpc.attributes.delete "x-goog-version"
  else
    @grpc.attributes["x-goog-version"] = new_version
  end
end