File: client.rb

package info (click to toggle)
ruby-aws-sdk 1.67.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,840 kB
  • sloc: ruby: 28,436; makefile: 7
file content (162 lines) | stat: -rw-r--r-- 4,856 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

module AWS
  class EC2

    # Client class for Amazon Elastic Compute Cloud (EC2).
    class Client < Core::QueryClient

      API_VERSION = '2014-10-01'

      signature_version :Version4, 'ec2'

      def retryable_error?(response)
        if response.error.is_a?(Errors::InsufficientInstanceCapacity)
          false
        else
          super
        end
      end

      # @api private
      CACHEABLE_REQUESTS = Set[
        :describe_addresses,
        :describe_availability_zones,
        :describe_bundle_tasks,
        :describe_customer_gateways,
        :describe_dhcp_options,
        :describe_image_attribute,
        :describe_images,
        :describe_instances,
        :describe_key_pairs,
        :describe_regions,
        :describe_reserved_instances,
        :describe_reserved_instances_offerings,
        :describe_security_groups,
        :describe_snapshot_attribute,
        :describe_snapshots,
        :describe_subnets,
        :describe_volume_status,
        :describe_volumes,
        :describe_vpcs,
        :describe_vpn_connections,
        :describe_vpn_gateways,
        :describe_instance_attribute,
        :describe_spot_instance_requests,
        :describe_spot_price_history,
        :describe_spot_datafeed_subscription,
        :describe_licenses,
        :describe_placement_groups,
        :describe_tags,
        :describe_internet_gateways,
        :describe_route_tables,
        :describe_network_acls,
        :describe_instance_status,
        :describe_conversion_tasks,
        :describe_network_interfaces,
        :describe_network_interface_attribute,
      ]

      protected

      # @return [Core::Signers::Version2]
      def v2_signer
        @v2_signer ||= Core::Signers::Version2.new(credential_provider)
      end

      # @return [Core::Signers::Version4]
      def v4_signer
        @v4_signer ||=
          Core::Signers::Version4.new(credential_provider, 'ec2', @region)
      end

    end

    class Client::V20130815 < Client
      define_client_methods('2013-08-15')
    end

    class Client::V20131001 < Client
      define_client_methods('2013-10-01')
    end

    class Client::V20131015 < Client
      define_client_methods('2013-10-15')
    end

    class Client::V20140201 < Client
      define_client_methods('2014-02-01')
    end

    class Client::V20140501 < Client
      define_client_methods('2014-05-01')
    end

    class Client::V20140901 < Client
      define_client_methods('2014-09-01')
    end

    class Client::V20141001 < Client

      define_client_methods('2014-10-01')

      alias basic_copy_snapshot copy_snapshot

      def copy_snapshot(params = {})
        # Adding logic to auto-compute the destination group and presigned
        # url params for the copy snapshot operation.  This is necessary
        # when calling copy snapshot on snapshots for encrypted volumes.
        # This addition should be transparent to the API user.
        params = params.dup
        params[:destination_region] = @region
        params[:presigned_url] = presigned_copy_snapshot_url(params)
        basic_copy_snapshot(params)
      end

      private

      def presigned_copy_snapshot_url(params)
        token = credential_provider.session_token

        client = self.with_options(:ec2_region => params[:source_region])

        req = client.build_request(:copy_snapshot, params)

        now = req.remove_param("Timestamp").value
        now = Time.parse(now).strftime("%Y%m%dT%H%M%SZ")

        req.add_param("X-Amz-Algorithm", "AWS4-HMAC-SHA256")
        req.add_param("X-Amz-Date", now)
        req.add_param("X-Amz-SignedHeaders", 'host')
        req.add_param("X-Amz-Expires", "3600")
        req.add_param('X-Amz-Security-Token', token) if token
        req.add_param("X-Amz-Credential", client.v4_signer.credential(now))

        req.http_method = 'GET'
        req.uri = '/?' + req.url_encoded_params
        req.body = ''
        req.headers.clear
        req.headers['host'] = client.config.ec2_endpoint

        key = client.v4_signer.derive_key(now)
        sig = client.v4_signer.signature(req, key, now, client.v4_signer.class::EMPTY_DIGEST)

        req.add_param('X-Amz-Signature', sig)

        req.endpoint + '/?' + req.url_encoded_params
      end

    end
  end
end