Class: ObjectStorage::CDN::GoogleCDN

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/uploaders/object_storage/cdn/google_cdn.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ GoogleCDN

Returns a new instance of GoogleCDN.



11
12
13
14
15
# File 'app/uploaders/object_storage/cdn/google_cdn.rb', line 11

def initialize(options)
  @options = HashWithIndifferentAccess.new(options.to_h)

  GoogleIpCache.async_refresh unless GoogleIpCache.ready?
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'app/uploaders/object_storage/cdn/google_cdn.rb', line 9

def options
  @options
end

Instance Method Details

#signed_url(path, expiry: 10.minutes, params: {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/uploaders/object_storage/cdn/google_cdn.rb', line 27

def signed_url(path, expiry: 10.minutes, params: {})
  expiration = (Time.current + expiry).utc.to_i

  uri = Addressable::URI.parse(cdn_url)
  uri.path = Addressable::URI.encode_component(path, Addressable::URI::CharacterClasses::PATH)
  # Use an Array to preserve order: Google CDN needs to have
  # Expires, KeyName, and Signature in that order or it will return a 403 error:
  # https://cloud.google.com/cdn/docs/troubleshooting-steps#signing
  query_params = params.to_a
  query_params << ['Expires', expiration]
  query_params << ['KeyName', key_name]
  uri.query_values = query_params

  unsigned_url = uri.to_s
  signature = OpenSSL::HMAC.digest('SHA1', decoded_key, unsigned_url)
  encoded_signature = Base64.urlsafe_encode64(signature)

  "#{unsigned_url}&Signature=#{encoded_signature}"
end

#use_cdn?(request_ip) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
# File 'app/uploaders/object_storage/cdn/google_cdn.rb', line 17

def use_cdn?(request_ip)
  return false unless config_valid?

  ip = IPAddr.new(request_ip)

  return false if ip.private? || ip.link_local? || ip.loopback?

  !GoogleIpCache.google_ip?(request_ip)
end