google-cloud-env 2.0.0 → 2.0.1

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: 743c1259c72af3a3cd7872d88adfa2d4ba8d4850733dfba06403ad9014e510fb
4
- data.tar.gz: f1d426cd09a3591e4e60e95c0d47ada831ec498bdfc04ab709a90c6333cae560
3
+ metadata.gz: 5ac62dcc3131def7be6bf69813f79c3d23b6d6ce5199d0250ffad596cf3b7fcb
4
+ data.tar.gz: d9eadf5f767d8225022ed9551ea3a5a2ee37e8ad5b68da9eea7c94f416333f4f
5
5
  SHA512:
6
- metadata.gz: f851d76b537d777a94973491da78ab454dbcbe23f1809ce981d86a654a28af8e1b22c7f2595ea3d4a5c64d0648d263790a61fa36e56a63eb70f83ff9a0862922
7
- data.tar.gz: 6b2160f8ab772f8af3bdbc8ee613cbc33e92310d99ac36dbf9a8962a8c75f5db6a6a7145c1560e2aa73e51d5e07acb04d3aa4638d222d2814de5288b043686b5
6
+ metadata.gz: def8ccefb5d889744cb562a85cc78a9d3b718f80f2d48eab409c6fcdbcbe2dfe9adae14393f06aab3b6a6db83b6fc6d631902d11c1a932e2a77ee2f93a5ca82f
7
+ data.tar.gz: 5faabdeb543a4237dd19a69a9963237e4b843f93aeeaa1f002cc08bd0269ba005da6a89baee20989076330b97de9551a41a50e658febd173847166fc29013bcb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Release History
2
2
 
3
+ ### 2.0.1 (2023-12-01)
4
+
5
+ #### Bug Fixes
6
+
7
+ * Bad response status or flavor headers no longer signal positive metadata existence ([#61](https://github.com/googleapis/ruby-cloud-env/issues/61))
8
+ * Increase token expiry buffer to three and a half minutes ([#59](https://github.com/googleapis/ruby-cloud-env/issues/59))
9
+
3
10
  ### 2.0.0 (2023-11-14)
4
11
 
5
12
  This is a major overhaul of the mechanisms underlying this gem, to improve reliability and provide better mocking interfaces. Environment interrogation calls are unchanged, but the mocking override parameters from 1.x have been removed in favor of the new interfaces, hence the semver-major version bump.
@@ -124,6 +124,14 @@ module Google
124
124
  # @return [Hash{String=>String}]
125
125
  #
126
126
  attr_reader :headers
127
+
128
+ ##
129
+ # Returns true if the metadata-flavor is correct for Google Cloud
130
+ # @return [boolean]
131
+ #
132
+ def google_flavor?
133
+ headers["Metadata-Flavor"] == "Google"
134
+ end
127
135
  end
128
136
 
129
137
  ##
@@ -233,20 +241,10 @@ module Google
233
241
  compute_smbios: nil
234
242
  @variables = variables || Variables.new
235
243
  @compute_smbios = compute_smbios || ComputeSMBIOS.new
236
- self.host = nil
237
- @connection = Faraday.new url: host
238
- self.open_timeout = DEFAULT_OPEN_TIMEOUT
239
- self.request_timeout = DEFAULT_REQUEST_TIMEOUT
240
- self.retry_count = DEFAULT_RETRY_COUNT
241
- self.retry_timeout = DEFAULT_RETRY_TIMEOUT
242
- self.retry_interval = DEFAULT_RETRY_INTERVAL
243
- self.warmup_time = DEFAULT_WARMUP_TIME
244
- @cache = create_cache
245
244
  # This mutex protects the overrides and existence settings.
246
245
  # Those values won't change within a synchronize block.
247
246
  @mutex = Thread::Mutex.new
248
- reset_existence!
249
- @overrides = nil
247
+ reset!
250
248
  end
251
249
 
252
250
  ##
@@ -443,7 +441,7 @@ module Google
443
441
  request_timeout: request_timeout,
444
442
  retry_count: retry_count,
445
443
  retry_timeout: retry_timeout
446
- return nil unless response.status == 200 && response.headers["Metadata-Flavor"] == "Google"
444
+ return nil unless response.status == 200 && response.google_flavor?
447
445
  response.body
448
446
  end
449
447
 
@@ -618,6 +616,26 @@ module Google
618
616
  #
619
617
  attr_reader :compute_smbios
620
618
 
619
+ ##
620
+ # @private
621
+ # Reset the cache, overrides, and all settings to default, for testing.
622
+ #
623
+ def reset!
624
+ @mutex.synchronize do
625
+ self.host = nil
626
+ @connection = Faraday.new url: host
627
+ self.open_timeout = DEFAULT_OPEN_TIMEOUT
628
+ self.request_timeout = DEFAULT_REQUEST_TIMEOUT
629
+ self.retry_count = DEFAULT_RETRY_COUNT
630
+ self.retry_timeout = DEFAULT_RETRY_TIMEOUT
631
+ self.retry_interval = DEFAULT_RETRY_INTERVAL
632
+ self.warmup_time = DEFAULT_WARMUP_TIME
633
+ @cache = create_cache
634
+ @overrides = nil
635
+ end
636
+ reset_existence!
637
+ end
638
+
621
639
  ##
622
640
  # @private
623
641
  # Clear the existence cache, for testing.
@@ -627,6 +645,7 @@ module Google
627
645
  @existence = nil
628
646
  @startup_time = Process.clock_gettime Process::CLOCK_MONOTONIC
629
647
  end
648
+ self
630
649
  end
631
650
 
632
651
  private
@@ -645,6 +664,29 @@ module Google
645
664
  Timeout::Error
646
665
  ].freeze
647
666
 
667
+ ##
668
+ # @private
669
+ #
670
+ # A buffer in seconds for token expiry. Our cache for the token will
671
+ # expire approximately this many seconds before the declared expiry
672
+ # time of the token itself.
673
+ #
674
+ # We want this value to be positive so that we provide some buffer to
675
+ # offset any clock skew and Metadata Server latency that might affect
676
+ # our calculation of the expiry time, but more importantly so that a
677
+ # client has approximately this amount of time to use a token we give
678
+ # them before it expires.
679
+ #
680
+ # We don't want this to be much higher, however, to keep the load down
681
+ # on the Metadata Server. We've been advised by the compute/serverless
682
+ # engineering teams to set this value less than 4 minutes because the
683
+ # Metadata Server can refresh the token as late as 4 minutes before the
684
+ # actual expiry of the previous token. If our cache expires and we
685
+ # request a new token, we actually want to receive a new token rather
686
+ # than the previous old token. See internal issue b/311414224.
687
+ #
688
+ TOKEN_EXPIRY_BUFFER = 210
689
+
648
690
  ##
649
691
  # @private
650
692
  #
@@ -708,8 +750,12 @@ module Google
708
750
  req.options.timeout = request_timeout if request_timeout
709
751
  req.options.open_timeout = open_timeout if open_timeout
710
752
  end
711
- post_update_existence true
712
753
  response = Response.new http_response.status, http_response.body, http_response.headers
754
+ if path.nil?
755
+ post_update_existence(response.status == 200 && response.google_flavor?)
756
+ elsif response.google_flavor?
757
+ post_update_existence true
758
+ end
713
759
  lifetime = determine_data_lifetime path, response.body.strip
714
760
  LazyValue.expiring_value lifetime, response
715
761
  rescue *TRANSIENT_EXCEPTIONS
@@ -754,8 +800,7 @@ module Google
754
800
  def access_token_lifetime data
755
801
  json = JSON.parse data rescue nil
756
802
  return 0 unless json&.key? "expires_in"
757
- # Buffer of 10 seconds to account for MDS latency
758
- lifetime = json["expires_in"].to_i - 10
803
+ lifetime = json["expires_in"].to_i - TOKEN_EXPIRY_BUFFER
759
804
  lifetime = 0 if lifetime.negative?
760
805
  lifetime
761
806
  end
@@ -769,8 +814,7 @@ module Google
769
814
  base64 = Base64.decode64 Regexp.last_match[1]
770
815
  json = JSON.parse base64 rescue nil
771
816
  return 0 unless json&.key? "exp"
772
- # Buffer of 10 seconds in case of clock skew
773
- lifetime = json["exp"].to_i - Time.now.to_i - 10
817
+ lifetime = json["exp"].to_i - Time.now.to_i - TOKEN_EXPIRY_BUFFER
774
818
  lifetime = 0 if lifetime.negative?
775
819
  lifetime
776
820
  end
@@ -20,7 +20,7 @@ module Google
20
20
  # Library version
21
21
  # @return [String]
22
22
  #
23
- VERSION = "2.0.0".freeze
23
+ VERSION = "2.0.1".freeze
24
24
  end
25
25
  end
26
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-env
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Azuma
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-15 00:00:00.000000000 Z
11
+ date: 2023-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -59,10 +59,10 @@ homepage: https://github.com/googleapis/ruby-cloud-env
59
59
  licenses:
60
60
  - Apache-2.0
61
61
  metadata:
62
- changelog_uri: https://rubydoc.info/gems/google-cloud-env/2.0.0/CHANGELOG.md
62
+ changelog_uri: https://rubydoc.info/gems/google-cloud-env/2.0.1/CHANGELOG.md
63
63
  source_code_uri: https://github.com/googleapis/ruby-cloud-env
64
64
  bug_tracker_uri: https://github.com/googleapis/ruby-cloud-env/issues
65
- documentation_uri: https://rubydoc.info/gems/google-cloud-env/2.0.0
65
+ documentation_uri: https://rubydoc.info/gems/google-cloud-env/2.0.1
66
66
  post_install_message:
67
67
  rdoc_options: []
68
68
  require_paths: