s3fsr 1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,311 @@
1
+ module AWS
2
+ module S3
3
+ # A bucket can be set to log the requests made on it. By default logging is turned off. You can check if a bucket has logging enabled:
4
+ #
5
+ # Bucket.logging_enabled_for? 'jukebox'
6
+ # # => false
7
+ #
8
+ # Enabling it is easy:
9
+ #
10
+ # Bucket.enable_logging_for('jukebox')
11
+ #
12
+ # Unless you specify otherwise, logs will be written to the bucket you want to log. The logs are just like any other object. By default they will start with the prefix 'log-'. You can customize what bucket you want the logs to be delivered to, as well as customize what the log objects' key is prefixed with by setting the <tt>target_bucket</tt> and <tt>target_prefix</tt> option:
13
+ #
14
+ # Bucket.enable_logging_for(
15
+ # 'jukebox', 'target_bucket' => 'jukebox-logs'
16
+ # )
17
+ #
18
+ # Now instead of logging right into the jukebox bucket, the logs will go into the bucket called jukebox-logs.
19
+ #
20
+ # Once logs have accumulated, you can access them using the <tt>logs</tt> method:
21
+ #
22
+ # pp Bucket.logs('jukebox')
23
+ # [#<AWS::S3::Logging::Log '/jukebox-logs/log-2006-11-14-07-15-24-2061C35880A310A1'>,
24
+ # #<AWS::S3::Logging::Log '/jukebox-logs/log-2006-11-14-08-15-27-D8EEF536EC09E6B3'>,
25
+ # #<AWS::S3::Logging::Log '/jukebox-logs/log-2006-11-14-08-15-29-355812B2B15BD789'>]
26
+ #
27
+ # Each log has a <tt>lines</tt> method that gives you information about each request in that log. All the fields are available
28
+ # as named methods. More information is available in Logging::Log::Line.
29
+ #
30
+ # logs = Bucket.logs('jukebox')
31
+ # log = logs.first
32
+ # line = log.lines.first
33
+ # line.operation
34
+ # # => 'REST.GET.LOGGING_STATUS'
35
+ # line.request_uri
36
+ # # => 'GET /jukebox?logging HTTP/1.1'
37
+ # line.remote_ip
38
+ # # => "67.165.183.125"
39
+ #
40
+ # Disabling logging is just as simple as enabling it:
41
+ #
42
+ # Bucket.disable_logging_for('jukebox')
43
+ module Logging
44
+ # Logging status captures information about the calling bucket's logging settings. If logging is enabled for the bucket
45
+ # the status object will indicate what bucket the logs are written to via the <tt>target_bucket</tt> method as well as
46
+ # the logging key prefix with via <tt>target_prefix</tt>.
47
+ #
48
+ # See the documentation for Logging::Management::ClassMethods for more information on how to get the logging status of a bucket.
49
+ class Status
50
+ include SelectiveAttributeProxy
51
+ attr_reader :enabled
52
+ alias_method :logging_enabled?, :enabled
53
+
54
+ def initialize(attributes = {}) #:nodoc:
55
+ attributes = {'target_bucket' => nil, 'target_prefix' => nil}.merge(attributes)
56
+ @enabled = attributes.has_key?('logging_enabled')
57
+ @attributes = attributes.delete('logging_enabled') || attributes
58
+ end
59
+
60
+ def to_xml #:nodoc:
61
+ Builder.new(self).to_s
62
+ end
63
+
64
+ private
65
+ attr_reader :attributes
66
+
67
+ class Builder < XmlGenerator #:nodoc:
68
+ attr_reader :logging_status
69
+ def initialize(logging_status)
70
+ @logging_status = logging_status
71
+ super()
72
+ end
73
+
74
+ def build
75
+ xml.tag!('BucketLoggingStatus', 'xmlns' => 'http://s3.amazonaws.com/doc/2006-03-01/') do
76
+ if logging_status.target_bucket && logging_status.target_prefix
77
+ xml.LoggingEnabled do
78
+ xml.TargetBucket logging_status.target_bucket
79
+ xml.TargetPrefix logging_status.target_prefix
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ # A bucket log exposes requests made on the given bucket. Lines of the log represent a single request. The lines of a log
88
+ # can be accessed with the lines method.
89
+ #
90
+ # log = Bucket.logs_for('marcel').first
91
+ # log.lines
92
+ #
93
+ # More information about the logged requests can be found in the documentation for Log::Line.
94
+ class Log
95
+ def initialize(log_object) #:nodoc:
96
+ @log = log_object
97
+ end
98
+
99
+ # Returns the lines for the log. Each line is wrapped in a Log::Line.
100
+ def lines
101
+ log.value.map {|line| Line.new(line)}
102
+ end
103
+ memoized :lines
104
+
105
+ def path
106
+ log.path
107
+ end
108
+
109
+ def inspect #:nodoc:
110
+ "#<%s:0x%s '%s'>" % [self.class.name, object_id, path]
111
+ end
112
+
113
+ private
114
+ attr_reader :log
115
+
116
+ # Each line of a log exposes the raw line, but it also has method accessors for all the fields of the logged request.
117
+ #
118
+ # The list of supported log line fields are listed in the S3 documentation: http://docs.amazonwebservices.com/AmazonS3/2006-03-01/LogFormat.html
119
+ #
120
+ # line = log.lines.first
121
+ # line.remote_ip
122
+ # # => '72.21.206.5'
123
+ #
124
+ # If a certain field does not apply to a given request (for example, the <tt>key</tt> field does not apply to a bucket request),
125
+ # or if it was unknown or unavailable, it will return <tt>nil</tt>.
126
+ #
127
+ # line.operation
128
+ # # => 'REST.GET.BUCKET'
129
+ # line.key
130
+ # # => nil
131
+ class Line < String
132
+ DATE = /\[([^\]]+)\]/
133
+ QUOTED_STRING = /"([^"]+)"/
134
+ REST = /(\S+)/
135
+ LINE_SCANNER = /#{DATE}|#{QUOTED_STRING}|#{REST}/
136
+
137
+ cattr_accessor :decorators
138
+ @@decorators = Hash.new {|hash, key| hash[key] = lambda {|entry| CoercibleString.coerce(entry)}}
139
+ cattr_reader :fields
140
+ @@fields = []
141
+
142
+ class << self
143
+ def field(name, offset, type = nil, &block) #:nodoc:
144
+ decorators[name] = block if block_given?
145
+ fields << name
146
+ class_eval(<<-EVAL, __FILE__, __LINE__)
147
+ def #{name}
148
+ value = parts[#{offset} - 1]
149
+ if value == '-'
150
+ nil
151
+ else
152
+ self.class.decorators[:#{name}].call(value)
153
+ end
154
+ end
155
+ memoized :#{name}
156
+ EVAL
157
+ end
158
+
159
+ # Time.parse doesn't like %d/%B/%Y:%H:%M:%S %z so we have to transform it unfortunately
160
+ def typecast_time(datetime) #:nodoc:
161
+ month = datetime[/[a-z]+/i]
162
+ month_names = [nil, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
163
+ datetime.sub!(%r|^(\w{2})/(\w{3})|, '\2/\1')
164
+ datetime.sub!(month, month_names.index(month).to_s)
165
+ datetime.sub!(':', ' ')
166
+ Time.parse(datetime)
167
+ end
168
+ end
169
+
170
+ def initialize(line) #:nodoc:
171
+ super(line)
172
+ @parts = parse
173
+ end
174
+
175
+ field(:owner, 1) {|entry| Owner.new('id' => entry) }
176
+ field :bucket, 2
177
+ field(:time, 3) {|entry| typecast_time(entry)}
178
+ field :remote_ip, 4
179
+ field(:requestor, 5) {|entry| Owner.new('id' => entry) }
180
+ field :request_id, 6
181
+ field :operation, 7
182
+ field :key, 8
183
+ field :request_uri, 9
184
+ field :http_status, 10
185
+ field :error_code, 11
186
+ field :bytes_sent, 12
187
+ field :object_size, 13
188
+ field :total_time, 14
189
+ field :turn_around_time, 15
190
+ field :referrer, 16
191
+ field :user_agent, 17
192
+
193
+ # Returns all fields of the line in a hash of the form <tt>:field_name => :field_value</tt>.
194
+ #
195
+ # line.attributes.values_at(:bucket, :key)
196
+ # # => ['marcel', 'kiss.jpg']
197
+ def attributes
198
+ self.class.fields.inject({}) do |attribute_hash, field|
199
+ attribute_hash[field] = send(field)
200
+ attribute_hash
201
+ end
202
+ end
203
+
204
+ private
205
+ attr_reader :parts
206
+
207
+ def parse
208
+ scan(LINE_SCANNER).flatten.compact
209
+ end
210
+ end
211
+ end
212
+
213
+ module Management #:nodoc:
214
+ def self.included(klass) #:nodoc:
215
+ klass.extend(ClassMethods)
216
+ klass.extend(LoggingGrants)
217
+ end
218
+
219
+ module ClassMethods
220
+ # Returns the logging status for the bucket named <tt>name</tt>. From the logging status you can determine the bucket logs are delivered to
221
+ # and what the bucket object's keys are prefixed with. For more information see the Logging::Status class.
222
+ #
223
+ # Bucket.logging_status_for 'marcel'
224
+ def logging_status_for(name = nil, status = nil)
225
+ if name.is_a?(Status)
226
+ status = name
227
+ name = nil
228
+ end
229
+
230
+ path = path(name) << '?logging'
231
+ status ? put(path, {}, status.to_xml) : Status.new(get(path).parsed)
232
+ end
233
+ alias_method :logging_status, :logging_status_for
234
+
235
+ # Enables logging for the bucket named <tt>name</tt>. You can specify what bucket to log to with the <tt>'target_bucket'</tt> option as well
236
+ # as what prefix to add to the log files with the <tt>'target_prefix'</tt> option. Unless you specify otherwise, logs will be delivered to
237
+ # the same bucket that is being logged and will be prefixed with <tt>log-</tt>.
238
+ def enable_logging_for(name = nil, options = {})
239
+ name = bucket_name(name)
240
+ default_options = {'target_bucket' => name, 'target_prefix' => 'log-'}
241
+ options = default_options.merge(options)
242
+ grant_logging_access_to_target_bucket(options['target_bucket'])
243
+ logging_status(name, Status.new(options))
244
+ end
245
+ alias_method :enable_logging, :enable_logging_for
246
+
247
+ # Disables logging for the bucket named <tt>name</tt>.
248
+ def disable_logging_for(name = nil)
249
+ logging_status(bucket_name(name), Status.new)
250
+ end
251
+ alias_method :disable_logging, :disable_logging_for
252
+
253
+ # Returns true if logging has been enabled for the bucket named <tt>name</tt>.
254
+ def logging_enabled_for?(name = nil)
255
+ logging_status(bucket_name(name)).logging_enabled?
256
+ end
257
+ alias_method :logging_enabled?, :logging_enabled_for?
258
+
259
+ # Returns the collection of logs for the bucket named <tt>name</tt>.
260
+ #
261
+ # Bucket.logs_for 'marcel'
262
+ #
263
+ # Accepts the same options as Bucket.find, such as <tt>:max_keys</tt> and <tt>:marker</tt>.
264
+ def logs_for(name = nil, options = {})
265
+ if name.is_a?(Hash)
266
+ options = name
267
+ name = nil
268
+ end
269
+
270
+ name = bucket_name(name)
271
+ logging_status = logging_status_for(name)
272
+ return [] unless logging_status.logging_enabled?
273
+ objects(logging_status.target_bucket, options.merge(:prefix => logging_status.target_prefix)).map do |log_object|
274
+ Log.new(log_object)
275
+ end
276
+ end
277
+ alias_method :logs, :logs_for
278
+ end
279
+
280
+ module LoggingGrants #:nodoc:
281
+ def grant_logging_access_to_target_bucket(target_bucket)
282
+ acl = acl(target_bucket)
283
+ acl.grants << ACL::Grant.grant(:logging_write)
284
+ acl.grants << ACL::Grant.grant(:logging_read_acp)
285
+ acl(target_bucket, acl)
286
+ end
287
+ end
288
+
289
+ def logging_status
290
+ self.class.logging_status_for(name)
291
+ end
292
+
293
+ def enable_logging(*args)
294
+ self.class.enable_logging_for(name, *args)
295
+ end
296
+
297
+ def disable_logging(*args)
298
+ self.class.disable_logging_for(name, *args)
299
+ end
300
+
301
+ def logging_enabled?
302
+ self.class.logging_enabled_for?(name)
303
+ end
304
+
305
+ def logs(options = {})
306
+ self.class.logs_for(name, options)
307
+ end
308
+ end
309
+ end
310
+ end
311
+ end