-
Notifications
You must be signed in to change notification settings - Fork 49
/
out_aws-elasticsearch-service.rb
233 lines (197 loc) · 7.79 KB
/
out_aws-elasticsearch-service.rb
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# -*- encoding: utf-8 -*-
require 'rubygems'
require 'fluent/plugin/out_elasticsearch'
require 'aws-sdk-core'
require 'faraday_middleware/aws_sigv4'
module Fluent::Plugin
class AwsElasticsearchServiceOutput < ElasticsearchOutput
Fluent::Plugin.register_output('aws-elasticsearch-service', self)
config_section :endpoint do
config_param :region, :string
config_param :url, :string
config_param :access_key_id, :string, :default => ""
config_param :secret_access_key, :string, :default => "", secret: true
config_param :assume_role_arn, :string, :default => nil
config_param :ecs_container_credentials_relative_uri, :string, :default => nil #Set with AWS_CONTAINER_CREDENTIALS_RELATIVE_URI environment variable value
config_param :assume_role_session_name, :string, :default => "fluentd"
config_param :assume_role_web_identity_token_file, :string, :default => nil
config_param :sts_credentials_region, :string, :default => nil
end
# here overrides default value of reload_connections to false because
# AWS Elasticsearch Service doesn't return addresses of nodes and Elasticsearch client
# fails to reload connections properly. This ends up "temporarily failed to flush the buffer"
# error repeating forever. See this discussion for details:
# https://discuss.elastic.co/t/elasitcsearch-ruby-raises-cannot-get-new-connection-from-pool-error/36252
config_set_default :reload_connections, false
#
# @override
#
def get_connection_options(con_host=nil)
raise "`endpoint` require." if @endpoint.empty?
@endpoint.map do |ep|
raise Fluent::ConfigError, "Ensure you don't have a trailing slash on the endpoint URL in your fluentd configuration." if ep[:url].end_with?("/")
end
hosts =
begin
@endpoint.map do |ep|
uri = URI(ep[:url])
host = %w(user password path).inject(host: uri.host, port: uri.port, scheme: uri.scheme) do |hash, key|
hash[key.to_sym] = uri.public_send(key) unless uri.public_send(key).nil? || uri.public_send(key) == ''
hash
end
host[:aws_elasticsearch_service] = {
:credentials => credentials(ep),
:region => ep[:region]
}
host
end
end
{
hosts: hosts
}
end
def write(chunk)
super
end
private
#
# get AWS Credentials
#
def credentials(opts)
calback = lambda do
credentials = nil
unless opts[:access_key_id].empty? or opts[:secret_access_key].empty?
credentials = Aws::Credentials.new opts[:access_key_id], opts[:secret_access_key]
else
if opts[:assume_role_arn].nil?
aws_container_credentials_relative_uri = opts[:ecs_container_credentials_relative_uri] || ENV["AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"]
if aws_container_credentials_relative_uri.nil?
credentials = Aws::SharedCredentials.new({retries: 2}).credentials
credentials ||= Aws::InstanceProfileCredentials.new.credentials
credentials ||= Aws::ECSCredentials.new.credentials
else
credentials = Aws::ECSCredentials.new({
credential_path: aws_container_credentials_relative_uri
}).credentials
end
else
if opts[:assume_role_web_identity_token_file].nil?
credentials = sts_credential_provider({
role_arn: opts[:assume_role_arn],
role_session_name: opts[:assume_role_session_name],
region: sts_credentials_region(opts)
}).credentials
else
credentials = sts_web_identity_credential_provider({
role_arn: opts[:assume_role_arn],
web_identity_token_file: opts[:assume_role_web_identity_token_file],
region: sts_credentials_region(opts)
}).credentials
end
end
end
raise "No valid AWS credentials found." unless credentials.set?
credentials
end
def calback.inspect
credentials = self.call
credentials.credentials.inspect
end
calback
end
def sts_credentials_region(opts)
opts[:sts_credentials_region] || opts[:region]
end
def sts_credential_provider(opts)
# AssumeRoleCredentials is an auto-refreshing credential provider
@sts ||= Aws::AssumeRoleCredentials.new(opts)
end
def sts_web_identity_credential_provider(opts)
# AssumeRoleWebIdentityCredentials is an auto-refreshing credential provider
@sts ||= Aws::AssumeRoleWebIdentityCredentials.new(opts)
end
end
#
# monkey patch
#
class ElasticsearchOutput
module Elasticsearch
module Client
include ::Elasticsearch::Client
extend self
end
module Transport
module Transport
module HTTP
class Faraday < ::Elasticsearch::Transport::Transport::HTTP::Faraday
alias :__build_connections_origin_from_aws_elasticsearch_service_output :__build_connections
# Builds and returns a collection of connections.
#
# @return [Connections::Collection]
# @override
#
def __build_connections
::Elasticsearch::Transport::Transport::Connections::Collection.new(
:connections => hosts.map { |host|
host[:protocol] = host[:scheme] || DEFAULT_PROTOCOL
host[:port] ||= DEFAULT_PORT
url = __full_url(host)
::Elasticsearch::Transport::Transport::Connections::Connection.new(
:host => host,
:connection => ::Faraday::Connection.new(
url,
(options[:transport_options] || {}),
&__aws_elasticsearch_service_setting(host, &@block)
),
:options => host[:connection_options]
)
},
:selector_class => options[:selector_class],
:selector => options[:selector]
)
end
def __aws_elasticsearch_service_setting(host, &block)
lambda do |faraday|
if host[:aws_elasticsearch_service]
faraday.request :aws_sigv4,
credentials: host[:aws_elasticsearch_service][:credentials],
service_name: 'es',
region: host[:aws_elasticsearch_service][:region]
end
block.call faraday
end
end
end
end
end
end
end
end
end
#
# monkey patch
#
class FaradayMiddleware::AwsSigV4
alias :initialize_origin_from_aws_elasticsearch_service_output :initialize
def initialize(app, options = nil)
super(app)
credentials = options.fetch(:credentials)
service_name = options.fetch(:service_name)
region = options.fetch(:region)
@signer =
begin
if credentials.is_a?(Proc)
signer = lambda do
Aws::Sigv4::Signer.new(service: service_name, region: region, credentials: credentials.call)
end
def signer.sign_request(req)
self.call.sign_request(req)
end
signer
else
Aws::Sigv4::Signer.new(service: service_name, region: region, credentials: credentials)
end
end
@net_http = app.is_a?(Faraday::Adapter::NetHttp)
end
end