aws-sdk-cloudfront 1.0.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,169 @@
1
+ require 'base64'
2
+ require 'uri'
3
+ require 'time'
4
+ require 'json'
5
+ require 'openssl'
6
+
7
+ module Aws
8
+ module CloudFront
9
+
10
+ # Allows you to create signed URLs for Amazon CloudFront resources
11
+ #
12
+ # signer = Aws::CloudFront::UrlSigner.new(
13
+ # key_pair_id: "cf-keypair-id",
14
+ # private_key_path: "./cf_private_key.pem"
15
+ # )
16
+ # url = signer.signed_url(url,
17
+ # policy: policy.to_json
18
+ # )
19
+ #
20
+ class UrlSigner
21
+
22
+ # @option options [String] :key_pair_id
23
+ # @option options [String] :private_key
24
+ # @option options [String] :private_key_path
25
+ def initialize(options = {})
26
+ @key_pair_id = key_pair_id(options)
27
+ @private_key = private_key(options)
28
+ end
29
+
30
+ # create a signed Amazon CloudFront URL
31
+ # @param [String] url
32
+ # @option params [Time, DateTime, Date, String, Integer<timestamp>] :expires
33
+ # @option params [String<JSON>] :policy
34
+ def signed_url(url, params = {})
35
+ url_sections = url.split('://')
36
+ if url_sections.length < 2
37
+ raise ArgumentError, "Invaild URL:#{url}"
38
+ end
39
+ # removing wildcard character to get real scheme
40
+ scheme = url_sections[0].gsub('*', '')
41
+ uri = "#{scheme}://#{url_sections[1]}"
42
+ signed_content = signature(
43
+ :resource => resource(scheme, uri),
44
+ :expires => time(params[:expires]),
45
+ :policy => params[:policy]
46
+ )
47
+
48
+ start_flag = URI.parse(uri).query ? '&' : '?'
49
+ uri = "#{uri}#{start_flag}#{signed_content}"
50
+
51
+ if scheme == 'rtmp'
52
+ rtmp_url(URI(uri))
53
+ else
54
+ uri
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def time(expires)
61
+ case expires
62
+ when Time then expires.to_i
63
+ when DateTime, Date then expires.to_time.to_i
64
+ when String then Time.parse(expires).to_i
65
+ when Integer, NIL then expires
66
+ else
67
+ msg = "expected a time value for :expires, got `#{expires.class}'"
68
+ raise ArgumentError, msg
69
+ end
70
+ end
71
+
72
+ # create a relative signed URL for RTMP distribution
73
+ def rtmp_url(uri)
74
+ result = uri.path.gsub(' ', '/')
75
+ result[0] = ''
76
+ if uri.query
77
+ "#{result}?#{uri.query}"
78
+ else
79
+ result
80
+ end
81
+ end
82
+
83
+ # prepare resource for signing
84
+ def resource(scheme, url)
85
+ case scheme
86
+ when 'http', 'http*', 'https' then url
87
+ when 'rtmp'
88
+ url_info = URI.parse(url)
89
+ path = url_info.path
90
+ path[0] = ''
91
+ resource_content = "#{File.dirname(path)}/#{File.basename(path)}".gsub(' ', '/')
92
+ if url_info.query
93
+ "#{resource_content}?#{uri.query}"
94
+ else
95
+ resource_content
96
+ end
97
+ else
98
+ msg = "Invaild URI scheme:#{scheme}.Scheme must be one of: http, https or rtmp."
99
+ raise ArgumentError, msg
100
+ end
101
+ end
102
+
103
+ # create signed values that used to construct signed URLs
104
+ # @option param [String] :resource
105
+ # @option param [Integer<timestamp>] :expires
106
+ # @option param [String<JSON>] :policy
107
+ def signature(params = {})
108
+ signature_content = []
109
+ if params[:policy]
110
+ policy = params[:policy].gsub('/\s/s', '')
111
+ signature_content << "Policy=#{encode(policy)}"
112
+ elsif params[:resource] && params[:expires]
113
+ policy = canned_policy(params[:resource], params[:expires])
114
+ signature_content << "Expires=#{params[:expires]}"
115
+ else
116
+ msg = "Either a policy or a resource with an expiration time must be provided."
117
+ raise ArgumentError, msg
118
+ end
119
+
120
+ signature_content << "Signature=#{encode(sign_policy(policy))}"
121
+ signature_content << "Key-Pair-Id=#{@key_pair_id}"
122
+ signature_content.join('&').gsub("\n", '')
123
+ end
124
+
125
+ # create the signature string with policy signed
126
+ def sign_policy(policy)
127
+ key = OpenSSL::PKey::RSA.new(@private_key)
128
+ key.sign(OpenSSL::Digest::SHA1.new, policy)
129
+ end
130
+
131
+ # create canned policy that used for signing
132
+ def canned_policy(resource, expires)
133
+ json_hash = {
134
+ 'Statement' => [
135
+ 'Resource' => resource,
136
+ 'Condition' => {
137
+ 'DateLessThan' => {'AWS:EpochTime' => expires}
138
+ }
139
+ ]
140
+ }
141
+ json_hash.to_json
142
+ end
143
+
144
+ def encode(policy)
145
+ Base64.encode64(policy).gsub(/[+=\/]/, '+' => '-', '=' => '_', '/' => '~')
146
+ end
147
+
148
+ def key_pair_id(options)
149
+ if options[:key_pair_id].nil? or options[:key_pair_id] == ''
150
+ raise ArgumentError, ":key_pair_id must not be blank"
151
+ else
152
+ options[:key_pair_id]
153
+ end
154
+ end
155
+
156
+ def private_key(options)
157
+ if options[:private_key]
158
+ options[:private_key]
159
+ elsif options[:private_key_path]
160
+ File.open(options[:private_key_path], 'rb') { |f| f.read }
161
+ else
162
+ msg = ":private_key or :private_key_path should be provided"
163
+ raise ArgumentError, msg
164
+ end
165
+ end
166
+
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,125 @@
1
+ # WARNING ABOUT GENERATED CODE
2
+ #
3
+ # This file is generated. See the contributing for info on making contributions:
4
+ # https://github.com/aws/aws-sdk-ruby/blob/master/CONTRIBUTING.md
5
+ #
6
+ # WARNING ABOUT GENERATED CODE
7
+
8
+ require 'aws-sdk-core/waiters'
9
+
10
+ module Aws
11
+ module CloudFront
12
+ module Waiters
13
+ class DistributionDeployed
14
+
15
+ # @param [Hash] options
16
+ # @option options [required, Client] :client
17
+ # @option options [Integer] :max_attempts (25)
18
+ # @option options [Integer] :delay (60)
19
+ # @option options [Proc] :before_attempt
20
+ # @option options [Proc] :before_wait
21
+ def initialize(options)
22
+ @client = options.fetch(:client)
23
+ @waiter = Aws::Waiters::Waiter.new({
24
+ max_attempts: 25,
25
+ delay: 60,
26
+ poller: Aws::Waiters::Poller.new(
27
+ "description" => "Wait until a distribution is deployed.",
28
+ operation_name: :get_distribution,
29
+ acceptors: [{
30
+ "expected" => "Deployed",
31
+ "matcher" => "path",
32
+ "state" => "success",
33
+ "argument" => "distribution.status"
34
+ }]
35
+ )
36
+ }.merge(options))
37
+ end
38
+
39
+ # @option (see Client#get_distribution)
40
+ # @return (see Client#get_distribution)
41
+ def wait(params = {})
42
+ @waiter.wait(client: @client, params: params)
43
+ end
44
+
45
+ # @api private
46
+ attr_reader :waiter
47
+
48
+ end
49
+
50
+ class InvalidationCompleted
51
+
52
+ # @param [Hash] options
53
+ # @option options [required, Client] :client
54
+ # @option options [Integer] :max_attempts (30)
55
+ # @option options [Integer] :delay (20)
56
+ # @option options [Proc] :before_attempt
57
+ # @option options [Proc] :before_wait
58
+ def initialize(options)
59
+ @client = options.fetch(:client)
60
+ @waiter = Aws::Waiters::Waiter.new({
61
+ max_attempts: 30,
62
+ delay: 20,
63
+ poller: Aws::Waiters::Poller.new(
64
+ "description" => "Wait until an invalidation has completed.",
65
+ operation_name: :get_invalidation,
66
+ acceptors: [{
67
+ "expected" => "Completed",
68
+ "matcher" => "path",
69
+ "state" => "success",
70
+ "argument" => "invalidation.status"
71
+ }]
72
+ )
73
+ }.merge(options))
74
+ end
75
+
76
+ # @option (see Client#get_invalidation)
77
+ # @return (see Client#get_invalidation)
78
+ def wait(params = {})
79
+ @waiter.wait(client: @client, params: params)
80
+ end
81
+
82
+ # @api private
83
+ attr_reader :waiter
84
+
85
+ end
86
+
87
+ class StreamingDistributionDeployed
88
+
89
+ # @param [Hash] options
90
+ # @option options [required, Client] :client
91
+ # @option options [Integer] :max_attempts (25)
92
+ # @option options [Integer] :delay (60)
93
+ # @option options [Proc] :before_attempt
94
+ # @option options [Proc] :before_wait
95
+ def initialize(options)
96
+ @client = options.fetch(:client)
97
+ @waiter = Aws::Waiters::Waiter.new({
98
+ max_attempts: 25,
99
+ delay: 60,
100
+ poller: Aws::Waiters::Poller.new(
101
+ "description" => "Wait until a streaming distribution is deployed.",
102
+ operation_name: :get_streaming_distribution,
103
+ acceptors: [{
104
+ "expected" => "Deployed",
105
+ "matcher" => "path",
106
+ "state" => "success",
107
+ "argument" => "streaming_distribution.status"
108
+ }]
109
+ )
110
+ }.merge(options))
111
+ end
112
+
113
+ # @option (see Client#get_streaming_distribution)
114
+ # @return (see Client#get_streaming_distribution)
115
+ def wait(params = {})
116
+ @waiter.wait(client: @client, params: params)
117
+ end
118
+
119
+ # @api private
120
+ attr_reader :waiter
121
+
122
+ end
123
+ end
124
+ end
125
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws-sdk-cloudfront
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.rc1
5
+ platform: ruby
6
+ authors:
7
+ - Amazon Web Services
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0.rc1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0.rc1
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sigv4
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ description: Official AWS Ruby gem for Amazon CloudFront (CloudFront). This gem is
42
+ part of the AWS SDK for Ruby.
43
+ email:
44
+ - trevrowe@amazon.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - lib/aws-sdk-cloudfront.rb
50
+ - lib/aws-sdk-cloudfront/client.rb
51
+ - lib/aws-sdk-cloudfront/client_api.rb
52
+ - lib/aws-sdk-cloudfront/customizations.rb
53
+ - lib/aws-sdk-cloudfront/errors.rb
54
+ - lib/aws-sdk-cloudfront/resource.rb
55
+ - lib/aws-sdk-cloudfront/types.rb
56
+ - lib/aws-sdk-cloudfront/url_signer.rb
57
+ - lib/aws-sdk-cloudfront/waiters.rb
58
+ homepage: http://github.com/aws/aws-sdk-ruby
59
+ licenses:
60
+ - Apache-2.0
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.3.1
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.5.1
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: AWS SDK for Ruby - CloudFront
82
+ test_files: []