summaryrefslogtreecommitdiffstats
path: root/extensions/Push/lib/Connector.disabled/AMQP.pm
blob: dda73dade28a29829dffde2d9e8dd77cc7eafd19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

package Bugzilla::Extension::Push::Connector::AMQP;

use 5.10.1;
use strict;
use warnings;

use base 'Bugzilla::Extension::Push::Connector::Base';

use Bugzilla::Constants;
use Bugzilla::Extension::Push::Constants;
use Bugzilla::Extension::Push::Util;
use Bugzilla::Util qw(generate_random_password);
use DateTime;

sub init {
  my ($self) = @_;
  $self->{mq}      = 0;
  $self->{channel} = 1;

  if ($self->config->{queue}) {
    $self->{queue_name} = $self->config->{queue};
  }
  else {
    my $queue_name = Bugzilla->localconfig->{'urlbase'};
    $queue_name =~ s#^https?://##;
    $queue_name =~ s#/$#|#;
    $queue_name .= generate_random_password(16);
    $self->{queue_name} = $queue_name;
  }
}

sub options {
  return (
    {
      name     => 'host',
      label    => 'AMQP Hostname',
      type     => 'string',
      default  => 'localhost',
      required => 1,
    },
    {
      name     => 'port',
      label    => 'AMQP Port',
      type     => 'string',
      default  => '5672',
      required => 1,
      validate => sub {
        $_[0] =~ /\D/ && die "Invalid port (must be numeric)\n";
      },
    },
    {
      name     => 'username',
      label    => 'Username',
      type     => 'string',
      default  => 'guest',
      required => 1,
    },
    {
      name     => 'password',
      label    => 'Password',
      type     => 'password',
      default  => 'guest',
      required => 1,
    },
    {
      name     => 'vhost',
      label    => 'Virtual Host',
      type     => 'string',
      default  => '/',
      required => 1,
    },
    {
      name     => 'exchange',
      label    => 'Exchange',
      type     => 'string',
      default  => '',
      required => 1,
    },
    {name => 'queue', label => 'Queue', type => 'string',},
  );
}

sub stop {
  my ($self) = @_;
  if ($self->{mq}) {
    Bugzilla->push_ext->logger->debug('AMQP: disconnecting');
    $self->{mq}->disconnect();
    $self->{mq} = 0;
  }
}

sub _connect {
  my ($self) = @_;
  my $logger = Bugzilla->push_ext->logger;
  my $config = $self->config;

  $self->stop();

  $logger->debug(
    'AMQP: Connecting to RabbitMQ ' . $config->{host} . ':' . $config->{port});
  require Net::RabbitMQ;
  my $mq = Net::RabbitMQ->new();
  $mq->connect(
    $config->{host},
    {
      port     => $config->{port},
      user     => $config->{username},
      password => $config->{password},
    }
  );
  $self->{mq} = $mq;

  $logger->debug('AMQP: Opening channel ' . $self->{channel});
  $self->{mq}->channel_open($self->{channel});

  $logger->debug('AMQP: Declaring queue ' . $self->{queue_name});
  $self->{mq}->queue_declare($self->{channel}, $self->{queue_name},
    {passive => 0, durable => 1, exclusive => 0, auto_delete => 0,},
  );
}

sub _bind {
  my ($self, $message) = @_;
  my $logger = Bugzilla->push_ext->logger;
  my $config = $self->config;

  # bind to queue (also acts to verify the connection is still valid)
  if ($self->{mq}) {
    eval {
      $logger->debug('AMQP: binding queue('
          . $self->{queue_name}
          . ') with exchange('
          . $config->{exchange}
          . ')');
      $self->{mq}->queue_bind(
        $self->{channel},    $self->{queue_name},
        $config->{exchange}, $message->routing_key,
      );
    };
    if ($@) {
      $logger->debug('AMQP: ' . clean_error($@));
      $self->{mq} = 0;
    }
  }

}

sub should_send {
  my ($self, $message) = @_;
  my $logger = Bugzilla->push_ext->logger;

  my $payload    = $message->payload_decoded();
  my $target     = $payload->{event}->{target};
  my $is_private = $payload->{$target}->{is_private} ? 1 : 0;
  if (!$is_private && exists $payload->{$target}->{bug}) {
    $is_private = $payload->{$target}->{bug}->{is_private} ? 1 : 0;
  }

  if ($is_private) {

    # we only want to push the is_private message from the change_set, as
    # this is guaranteed to contain public information only
    if ($message->routing_key !~ /\.modify:is_private$/) {
      $logger->debug('AMQP: Ignoring private message');
      return 0;
    }
    $logger->debug('AMQP: Sending change of message to is_private');
  }
  return 1;
}

sub send {
  my ($self, $message) = @_;
  my $logger = Bugzilla->push_ext->logger;
  my $config = $self->config;

  # don't push comments to pulse
  if ($message->routing_key =~ /^comment\./) {
    $logger->debug('AMQP: Ignoring comment');
    return PUSH_RESULT_IGNORED;
  }

  # don't push private data
  $self->should_push($message) || return PUSH_RESULT_IGNORED;

  $self->_bind($message);

  eval {
    # reconnect if required
    if (!$self->{mq}) {
      $self->_connect();
    }

    # send message
    $logger->debug('AMQP: Publishing message');
    $self->{mq}->publish(
      $self->{channel}, $message->routing_key, $message->payload,
      {exchange     => $config->{exchange},},
      {content_type => 'text/plain', content_encoding => '8bit',},
    );
  };
  if ($@) {
    return (PUSH_RESULT_TRANSIENT, clean_error($@));
  }

  return PUSH_RESULT_OK;
}

1;