fastly 2.4.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a2ede9580b2a60043debfc3a95545c6ec6d88dce770e53f626975d508922230
4
- data.tar.gz: 88471ec1a779279a3b35bbf42c2882caef0adc6fd54161eb0f0793180a1f9e0a
3
+ metadata.gz: cc8a553cdbdfbe2e476bae280ce2773a1a12d82810ca7e35d6d32242f6e89adc
4
+ data.tar.gz: 8985329230915385a17eb5db0ce577296ad1e4913cf74343c67051aba383c86f
5
5
  SHA512:
6
- metadata.gz: d66d4a875d04d5889dd0f7d094073abc575cc9ef3f8c698d03050e35a1236ce3f2d94356503d9391f494a95f7cc47ff0dc581e04a342db79e0689b8038d4ed74
7
- data.tar.gz: 257ca1ae2413695c47394fecd8b9a77e9be2d073232ea0a2ea186045c7356e96b562a1d66cafb9b7e2a71fe2760b52c5f121aacae98d99184cd768c10f4aa5b2
6
+ metadata.gz: 80f1b15c3236123511de643ac4d3c5b9a2890f76b445eefb8386ae5576d3446588383f83e21816312d4d8f8df7e0b008c563eec6dd9df4ed8d3cc4b86a181426
7
+ data.tar.gz: fc81a4d00879da356801482e75d763b88c2a584fc8c7d117f8ab108b2e7d32475817c81a6a53d3265255859e773a8eb5b0e003cd833206a9e35049f06ec92c99
@@ -1,5 +1,12 @@
1
1
  # Change Log
2
2
 
3
+ ## [v2.5.0](https://github.com/fastly/fastly-ruby/tree/v2.5.0) (2020-01-16)
4
+ [Full Changelog](https://github.com/fastly/fastly-ruby/compare/v2.4.0...v2.5.0)
5
+
6
+ **Merged pull requests:**
7
+
8
+ - Add tokens.rb [\#150](https://github.com/fastly/fastly-ruby/pull/150) ([malachy-mcconnell](https://github.com/malachy-mcconnell))
9
+
3
10
  ## [v2.4.0](https://github.com/fastly/fastly-ruby/tree/v2.4.0) (2019-07-29)
4
11
  [Full Changelog](https://github.com/fastly/fastly-ruby/compare/v2.3.1...v2.4.0)
5
12
 
@@ -471,4 +478,4 @@ Add Healthchecks and Syslog endpoint streaming
471
478
  Initial releasee
472
479
 
473
480
 
474
- \* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
481
+ \* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
@@ -34,6 +34,7 @@ require 'fastly/snippet'
34
34
  require 'fastly/dynamic_snippet'
35
35
  require 'fastly/sumologic_logging'
36
36
  require 'fastly/syslog'
37
+ require 'fastly/token'
37
38
  require 'fastly/s3_logging'
38
39
  require 'fastly/gcs_logging'
39
40
  require 'fastly/user'
@@ -151,7 +152,7 @@ class Fastly
151
152
  client.get_stats('/stats/regions')
152
153
  end
153
154
 
154
- [ACL, ACLEntry, User, Customer, Backend, CacheSetting, Condition, Dictionary, DictionaryItem, Director, Domain, Header, Healthcheck, Gzip, Match, PapertrailLogging, RequestSetting, ResponseObject, Service, Snippet, S3Logging, Syslog, VCL, Version].each do |klass|
155
+ [ACL, ACLEntry, User, Customer, Backend, CacheSetting, Condition, Dictionary, DictionaryItem, Director, Domain, Header, Healthcheck, Gzip, Match, PapertrailLogging, RequestSetting, ResponseObject, Service, Snippet, S3Logging, Syslog, Token, VCL, Version].each do |klass|
155
156
  type = Util.class_to_path(klass)
156
157
 
157
158
  if klass.respond_to?(:pluralize)
@@ -28,8 +28,11 @@ class Fastly
28
28
  return self unless fully_authed?
29
29
 
30
30
  # If full auth creds (user/pass) then log in and set a cookie
31
- resp = http.post('/login', make_params(user: user, password: password))
32
-
31
+ resp = http.post(
32
+ '/login',
33
+ make_params(user: user, password: password),
34
+ {'Content-Type' => 'application/x-www-form-urlencoded'}
35
+ )
33
36
  if resp.kind_of?(Net::HTTPSuccess)
34
37
  @cookie = resp['Set-Cookie']
35
38
  else
@@ -59,8 +62,9 @@ class Fastly
59
62
 
60
63
  def get(path, params = {})
61
64
  extras = params.delete(:headers) || {}
65
+ include_auth = params.key?(:include_auth) ? params.delete(:include_auth) : true
62
66
  path += "?#{make_params(params)}" unless params.empty?
63
- resp = http.get(path, headers(extras))
67
+ resp = http.get(path, headers(extras, include_auth))
64
68
  fail Error, resp.body unless resp.kind_of?(Net::HTTPSuccess)
65
69
  JSON.parse(resp.body)
66
70
  end
@@ -86,7 +90,8 @@ class Fastly
86
90
 
87
91
  def delete(path, params = {})
88
92
  extras = params.delete(:headers) || {}
89
- resp = http.delete(path, headers(extras))
93
+ include_auth = params.key?(:include_auth) ? params.delete(:include_auth) : true
94
+ resp = http.delete(path, headers(extras, include_auth))
90
95
  resp.kind_of?(Net::HTTPSuccess)
91
96
  end
92
97
 
@@ -132,16 +137,20 @@ class Fastly
132
137
 
133
138
  def post_and_put(method, path, params = {})
134
139
  extras = params.delete(:headers) || {}
140
+ include_auth = params.key?(:include_auth) ? params.delete(:include_auth) : true
135
141
  query = make_params(params)
136
- resp = http.send(method, path, query, headers(extras).merge('Content-Type' => 'application/x-www-form-urlencoded'))
142
+ resp = http.send(method, path, query, headers(extras, include_auth).merge('Content-Type' => 'application/x-www-form-urlencoded'))
137
143
  fail Error, resp.body unless resp.kind_of?(Net::HTTPSuccess)
138
144
  JSON.parse(resp.body)
139
145
  end
140
146
 
141
- def headers(extras={})
142
- headers = fully_authed? ? { 'Cookie' => cookie } : {}
143
- headers['Fastly-Key'] = api_key if api_key
144
-
147
+ def headers(extras={}, include_auth=true)
148
+ headers = {}
149
+ # Some endpoints (POST /tokens) break if any auth headers including cookies are sent
150
+ if include_auth
151
+ headers['Cookie'] = cookie if fully_authed?
152
+ headers['Fastly-Key'] = api_key if api_key
153
+ end
145
154
  headers.merge('Content-Accept' => 'application/json', 'User-Agent' => "fastly-ruby-v#{Fastly::VERSION}").merge(extras.keep_if {|k,v| !v.nil? })
146
155
  end
147
156
 
@@ -1,4 +1,4 @@
1
1
  # The current version of the library
2
2
  class Fastly
3
- VERSION = "2.4.0"
3
+ VERSION = "2.5.0"
4
4
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Token object
4
+ class Fastly
5
+ class Token < Base
6
+ attr_accessor :id, :access_token, :user_id, :services, :name, :scope, :created_at, :last_used_at, :expires_at, :ip, :user_agent
7
+
8
+ private
9
+
10
+ def self.get_path(*_args)
11
+ '/tokens'
12
+ end
13
+
14
+ def self.post_path(*_args)
15
+ '/tokens'
16
+ end
17
+
18
+ def self.delete_path(opts)
19
+ "/tokens/#{opts.id}"
20
+ end
21
+ end
22
+
23
+ def new_token(opts)
24
+ opts[:username] = client.user
25
+ opts[:password] = client.password
26
+ opts[:include_auth] = false
27
+
28
+ token = create(Token, opts)
29
+ token.nil? ? nil : token
30
+ end
31
+
32
+ def customer_tokens(opts)
33
+ hash = client.get("/customer/#{opts[:customer_id]}/tokens")
34
+ hash.map { |token_hash| Token.new(token_hash, Fastly::Fetcher) }
35
+ end
36
+ end
@@ -98,6 +98,25 @@ describe Fastly::Client do
98
98
  assert_equal resp.class, Hash
99
99
  assert_includes resp, "i"
100
100
  end
101
+
102
+ it 'can make a POST without auth if asked to do so' do
103
+ stub_request(:post, /api.fastly.com/).
104
+ with(headers: {
105
+ 'Accept'=>'*/*',
106
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
107
+ 'Content-Accept'=>'application/json',
108
+ 'Content-Type'=>'application/x-www-form-urlencoded',
109
+ #'Fastly-Key'=>'notasecreteither',
110
+ 'User-Agent'=>'fastly-ruby-v2.4.0'
111
+ }).
112
+ to_return(body: JSON.generate(i: "dont care"), status: 200)
113
+
114
+ resp = client.post(
115
+ '/service/blah',
116
+ {include_auth: false}
117
+ )
118
+ end
119
+
101
120
  end
102
121
 
103
122
  describe 'get_stats' do
@@ -0,0 +1,131 @@
1
+ require_relative '../test_helper'
2
+
3
+ describe Fastly::Token do
4
+
5
+ let(:elevated_fastly) { Fastly.new(user: 'test@example.com', password: 'password') }
6
+ let(:fastly) { Fastly.new(api_key:'my_api_key') }
7
+
8
+ before {
9
+ stub_request(:post, "#{Fastly::Client::DEFAULT_URL}/login").to_return(body: '{}', status: 200, headers: { 'Set-Cookie' => 'tasty!' })
10
+ }
11
+
12
+ describe '#fastly' do
13
+
14
+ it 'cannot create itself because POST /tokens must have no auth headers)' do
15
+ stub_request(:post, "https://api.fastly.com/tokens").
16
+ with(
17
+ body: {"name"=>"name_of_token", "scope"=>"token_scope such_as purge_all purge_select", "services"=>"service_id_that_token_can_access"},
18
+ headers: {
19
+ 'Accept'=>'*/*',
20
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
21
+ 'Content-Accept'=>'application/json',
22
+ 'Content-Type'=>'application/x-www-form-urlencoded',
23
+ 'Cookie'=>'tasty!',
24
+ 'User-Agent'=>'fastly-ruby-v2.4.0'
25
+ }).
26
+ to_return(status: 403, body: '{"msg":"You must POST /sudo to access this endpoint"}', headers: {})
27
+
28
+ assert_raises(Fastly::Error,'{"msg":"You must POST /sudo to access this endpoint"}') do
29
+ elevated_fastly.create_token(
30
+ name: 'name_of_token',
31
+ services: 'service_id_that_token_can_access',
32
+ scope: 'token_scope such_as purge_all purge_select'
33
+ )
34
+ end
35
+ end
36
+
37
+ it 'can create a new token only if there are no auth headers' do
38
+ response_body = %q(
39
+ {
40
+ "id": "5Yo3XXnrQpjc20u0ybrf2g",
41
+ "access_token": "YOUR_FASTLY_TOKEN",
42
+ "user_id": "4y5K5trZocEAQYkesWlk7M",
43
+ "services": ["service_id_that_token_can_access"],
44
+ "name": "name_of_token",
45
+ "scope": "optional token_scope such_as purge_all purge_select",
46
+ "created_at": "2016-06-22T03:19:48+00:00",
47
+ "last_used_at": "2016-06-22T03:19:48+00:00",
48
+ "expires_at": "2016-07-28T19:24:50+00:00",
49
+ "ip": "127.17.202.173",
50
+ "user_agent": "fastly-ruby-v2.4.0"
51
+ }
52
+ )
53
+
54
+ stub_request(:post, "https://api.fastly.com/tokens").
55
+ with(
56
+ body: {"name"=>"name_of_token", "password"=>"password", "scope"=>"optional token_scope such_as purge_all purge_select", "services"=>"service_id_that_token_can_access", "username"=>"test@example.com"},
57
+ headers: {
58
+ 'Accept'=>'*/*',
59
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
60
+ 'Content-Accept'=>'application/json',
61
+ 'Content-Type'=>'application/x-www-form-urlencoded',
62
+ 'User-Agent'=>'fastly-ruby-v2.4.0'
63
+ }).
64
+ to_return(status: 200, body: response_body, headers: {})
65
+
66
+ token = elevated_fastly.new_token(
67
+ name: 'name_of_token',
68
+ services: 'service_id_that_token_can_access',
69
+ scope: 'optional token_scope such_as purge_all purge_select'
70
+ )
71
+ assert_equal token.id, '5Yo3XXnrQpjc20u0ybrf2g'
72
+ assert_equal token.user_id, '4y5K5trZocEAQYkesWlk7M'
73
+ assert_equal token.services[0], 'service_id_that_token_can_access'
74
+ assert_equal token.name, 'name_of_token'
75
+ assert_equal token.scope, 'optional token_scope such_as purge_all purge_select'
76
+ assert_equal token.created_at, '2016-06-22T03:19:48+00:00'
77
+ assert_equal token.last_used_at, '2016-06-22T03:19:48+00:00'
78
+ assert_equal token.expires_at, '2016-07-28T19:24:50+00:00'
79
+ assert_equal token.ip, '127.17.202.173'
80
+ assert_equal token.user_agent, 'fastly-ruby-v2.4.0'
81
+ assert_equal token.access_token, 'YOUR_FASTLY_TOKEN'
82
+ end
83
+
84
+ it 'would delete a token' do
85
+ stub_request(:delete, "https://api.fastly.com/tokens/").
86
+ with(
87
+ headers: {
88
+ 'Accept'=>'*/*',
89
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
90
+ 'Content-Accept'=>'application/json',
91
+ 'Fastly-Key'=>'my_api_key',
92
+ 'User-Agent'=>'fastly-ruby-v2.4.0'
93
+ }).
94
+ to_return(status: 204, body: "", headers: {})
95
+
96
+ token = Fastly::Token.new({acess_token: 'my_api_key'}, Fastly::Fetcher)
97
+ fastly.delete_token(token)
98
+ end
99
+
100
+ it 'would list all the tokens belonging to a token' do
101
+ stub_request(:get, "https://api.fastly.com/tokens").
102
+ with(
103
+ headers: {
104
+ 'Accept'=>'*/*',
105
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
106
+ 'Content-Accept'=>'application/json',
107
+ 'Fastly-Key'=>'my_api_key',
108
+ 'User-Agent'=>'fastly-ruby-v2.4.0'
109
+ }).
110
+ to_return(status: 200, body: "[]", headers: {})
111
+
112
+ fastly.list_tokens()
113
+ end
114
+
115
+ it 'would list all the tokens belonging to a customer' do
116
+ stub_request(:get, "https://api.fastly.com/customer/customer_account_number/tokens").
117
+ with(
118
+ headers: {
119
+ 'Accept'=>'*/*',
120
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
121
+ 'Content-Accept'=>'application/json',
122
+ 'Fastly-Key'=>'my_api_key',
123
+ 'User-Agent'=>'fastly-ruby-v2.4.0'
124
+ }).
125
+ to_return(status: 200, body: "[]", headers: {})
126
+
127
+ fastly.customer_tokens({customer_id: 'customer_account_number'})
128
+ end
129
+
130
+ end
131
+ end
@@ -0,0 +1,64 @@
1
+ require 'helper'
2
+
3
+ # Testing client components related to tokens
4
+ class Fastly
5
+ describe 'TokenTest' do
6
+ let(:opts) { login_opts(:both) }
7
+ let(:fastly) { Fastly.new(opts) }
8
+
9
+ let(:service_name) { "fastly-test-service-#{random_string}" }
10
+ let(:service) { fastly.create_service(:name => service_name) }
11
+ let(:current_customer) { fastly.current_customer }
12
+
13
+ def create_a_test_token(name, service, scope)
14
+ fastly.new_token(
15
+ name: name,
16
+ services: service,
17
+ scope: scope
18
+ )
19
+ end
20
+
21
+ it 'creates a new token' do
22
+ token = create_a_test_token('name_vi',service.id,'purge_all purge_select')
23
+ assert_equal token.name, 'name_vi'
24
+ assert_equal token.services[0], service.id
25
+ assert_equal token.scope, 'purge_all purge_select'
26
+ fastly.delete_token(token)
27
+ end
28
+
29
+ it 'deletes a token by passed-in token' do
30
+ token = create_a_test_token('name_vi',service.id,'purge_all purge_select')
31
+
32
+ tokens = fastly.customer_tokens({customer_id: current_customer.id})
33
+ before_count = tokens.count
34
+
35
+ fastly.delete_token(token)
36
+
37
+ tokens = fastly.customer_tokens({customer_id: current_customer.id})
38
+ after_count = tokens.count
39
+
40
+ assert_equal 1, before_count - after_count
41
+ end
42
+
43
+ it 'returns a list of tokens belonging to the customer or to the auth token' do
44
+ @token_i = create_a_test_token('name_i',service.id,'purge_all purge_select')
45
+ @token_ii = create_a_test_token('name_ii',service.id,'purge_all purge_select')
46
+ @token_iii = create_a_test_token('name_iii',service.id,'purge_all purge_select')
47
+ @token_iv = create_a_test_token('name_iv',service.id,'purge_all purge_select')
48
+ @token_v = create_a_test_token('name_v',service.id,'purge_all purge_select')
49
+
50
+ tokens = fastly.customer_tokens({customer_id: current_customer.id})
51
+ assert tokens.count > 4
52
+
53
+ tokens = fastly.list_tokens
54
+ assert tokens.count > 4
55
+
56
+ fastly.delete_token(@token_i)
57
+ fastly.delete_token(@token_ii)
58
+ fastly.delete_token(@token_iii)
59
+ fastly.delete_token(@token_iv)
60
+ fastly.delete_token(@token_v)
61
+ fastly.delete_service(service)
62
+ end
63
+ end
64
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastly
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fastly
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-29 00:00:00.000000000 Z
11
+ date: 2020-01-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Client library for the Fastly acceleration system
14
14
  email:
@@ -68,6 +68,7 @@ files:
68
68
  - lib/fastly/snippet.rb
69
69
  - lib/fastly/sumologic_logging.rb
70
70
  - lib/fastly/syslog.rb
71
+ - lib/fastly/token.rb
71
72
  - lib/fastly/user.rb
72
73
  - lib/fastly/util.rb
73
74
  - lib/fastly/vcl.rb
@@ -81,12 +82,14 @@ files:
81
82
  - test/fastly/customer_test.rb
82
83
  - test/fastly/dictionary_test.rb
83
84
  - test/fastly/syslog_test.rb
85
+ - test/fastly/token_test.rb
84
86
  - test/fastly/util_test.rb
85
87
  - test/full_login_test.rb
86
88
  - test/helper.rb
87
89
  - test/missing_api_key_test.rb
88
90
  - test/stats_test.rb
89
91
  - test/test_helper.rb
92
+ - test/token_test.rb
90
93
  homepage: http://github.com/fastly/fastly-ruby
91
94
  licenses:
92
95
  - MIT
@@ -106,8 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
109
  - !ruby/object:Gem::Version
107
110
  version: '0'
108
111
  requirements: []
109
- rubyforge_project:
110
- rubygems_version: 2.7.6.2
112
+ rubygems_version: 3.0.3
111
113
  signing_key:
112
114
  specification_version: 4
113
115
  summary: Client library for the Fastly acceleration system
@@ -121,9 +123,11 @@ test_files:
121
123
  - test/fastly/customer_test.rb
122
124
  - test/fastly/dictionary_test.rb
123
125
  - test/fastly/syslog_test.rb
126
+ - test/fastly/token_test.rb
124
127
  - test/fastly/util_test.rb
125
128
  - test/full_login_test.rb
126
129
  - test/helper.rb
127
130
  - test/missing_api_key_test.rb
128
131
  - test/stats_test.rb
129
132
  - test/test_helper.rb
133
+ - test/token_test.rb