Class: AWS::STS

Inherits:
Object
  • Object
show all
Includes:
Core::ServiceInterface
Defined in:
lib/aws/sts.rb,
lib/aws/sts/client.rb,
lib/aws/sts/errors.rb,
lib/aws/sts/policy.rb,
lib/aws/sts/request.rb,
lib/aws/sts/session.rb,
lib/aws/sts/federated_session.rb

Overview

This class is a starting point for working with the AWS Security Token Service. The AWS Security Token Service is a web service that enables you to request temporary, limited-privilege credentials for users that you authenticate (federated users), or IAM users.

Examples:

Getting temporary credentials and using them to make an EC2 request

sts = AWS::STS.new(:access_key_id => "LONG_TERM_KEY",
                   :secret_access_key => "LONG_TERM_SECRET")
session = sts.new_session(:duration => 60*60)
ec2 = AWS::EC2.new(session.credentials)
ec2.instances.to_a

Getting temporary credentials with restricted permissions

policy = AWS::STS::Policy.new
policy.allow(:actions => ["s3:*", "ec2:*"],
             :resources => :any)
session = sts.new_federated_session("TemporaryUser", :policy => policy)
ec2 = AWS::EC2.new(session.credentials)
ec2.instances.to_a

Defined Under Namespace

Modules: Errors Classes: Client, FederatedSession, Policy, Session

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Core::ServiceInterface

#initialize, #inspect

Instance Attribute Details

#clientClient (readonly)

Returns the low-level STS client object.

Returns:

  • (Client)

    the low-level STS client object



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
# File 'lib/aws/sts.rb', line 42

class STS

  AWS.register_autoloads(self) do
    autoload :Client,           'client'
    autoload :Errors,           'errors'
    autoload :FederatedSession, 'federated_session'
    autoload :Policy,           'policy'
    autoload :Request,          'request'
    autoload :Session,          'session'
  end

  include Core::ServiceInterface

  # (see Client#assume_role)
  def assume_role options = {}
    client.assume_role(options).data
  end

  # Returns a set of temporary credentials for an AWS account or IAM
  # User. The credentials consist of an Access Key ID, a Secret
  # Access Key, and a security token. These credentials are valid
  # for the specified duration only. The session duration for IAM
  # users can be between one and 36 hours, with a default of 12
  # hours. The session duration for AWS account owners is restricted
  # to one hour.
  #
  # @param [Hash] opts Options for getting temporary credentials.
  #
  # @option opts [Integer] :duration The duration, in seconds, that
  #   the session should last. Acceptable durations for IAM user
  #   sessions range from 3600s (one hour) to 129600s (36 hours),
  #   with 43200s (12 hours) as the default. Sessions for AWS
  #   account owners are restricted to a maximum of 3600s (one
  #   hour).
  #
  # @option opts [String] :serial_number The identification number of the
  #   Multi-Factor Authentication (MFA) device for the user.
  #
  # @option opts [String] :token_code The value provided by the MFA device.
  #   If the user has an access policy requiring an MFA code, provide the
  #   value here to get permission to resources as specified in the access
  #   policy.
  #
  # @return [Session]
  def new_session(opts = {})
    get_session(:get_session_token, opts) do |resp, session_opts|
      Session.new(session_opts)
    end
  end

  # Returns a set of temporary credentials for a federated user with
  # the user name and policy specified in the request. The
  # credentials consist of an Access Key ID, a Secret Access Key,
  # and a security token. The credentials are valid for the
  # specified duration, between one and 36 hours.
  #
  # The federated user who holds these credentials has only those
  # permissions allowed by intersection of the specified policy and
  # any resource or user policies that apply to the caller of the
  # GetFederationToken API. For more information about how token
  # permissions work, see
  # {http://docs.amazonwebservices.com/IAM/latest/UserGuide/TokenPermissions.html
  # Controlling Token Permissions} in Using AWS Identity and Access
  # Management.
  #
  # @param [String] name The name of the federated user associated
  #   with the session.  Must be between 2 and 32 characters in
  #   length.
  #
  # @param [Hash] opts Options for getting temporary credentials.
  #
  # @option opts [Integer] :duration The duration, in seconds, that
  #   the session should last. Acceptable durations for federation
  #   sessions range from 3600s (one hour) to 129600s (36 hours),
  #   with one hour as the default.
  #
  # @option opts [String, AWS::STS::Policy] :policy A policy
  #   specifying the permissions to associate with the session. The
  #   caller can delegate their own permissions by specifying a
  #   policy for the session, and both policies will be checked when
  #   a service call is made. In other words, permissions of the
  #   session credentials are the intersection of the policy
  #   specified in the API and the policies associated with the user
  #   who issued the session.
  #
  # @return [FederatedSession]
  #
  def new_federated_session(name, opts = {})
    opts = opts.merge(:name => name)
    case
    when opts[:policy].kind_of?(String) || !opts[:policy]
      # leave it alone
    when opts[:policy].respond_to?(:to_json)
      opts[:policy] = opts[:policy].to_json
    end
    get_session(:get_federation_token, opts) do |resp, session_opts|
      session_opts.merge!(
        :user_id => resp[:federated_user][:federated_user_id],
        :user_arn => resp[:federated_user][:arn],
        :packed_policy_size => resp[:packed_policy_size]
      )
      FederatedSession.new(session_opts)
    end
  end

  protected

  def get_session(method, opts = {})
    opts[:duration_seconds] = opts.delete(:duration) if
      opts[:duration]
    resp = client.send(method, opts)
    credentials = resp[:credentials].dup
    session_opts = {
      :credentials => credentials,
      :expires_at => credentials.delete(:expiration),
    }
    yield(resp, session_opts)
  end

end

Instance Method Details

#assume_role(options = {}) ⇒ Core::Response

Calls the AssumeRole API operation.

Parameters:

  • options (Hash) (defaults to: {})
    • :role_arn - required - (String) The Amazon Resource Name (ARN) of the role that the caller is assuming.

    • :role_session_name - required - (String) An identifier for the assumed role session. The session name is included as part of the AssumedRoleUser.

    • :policy - (String) A supplemental policy that can be associated with the temporary security credentials. The caller can restrict the permissions that are available on the role’s temporary security credentials to maintain the least amount of privileges. When a service call is made with the temporary security credentials, both the role’s permission policy and supplemental policy are checked. For more information about how permissions work in the context of temporary credentials, see Controlling Permissions in Temporary Credentials.

    • :duration_seconds - (Integer) The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) to 3600 seconds (1 hour). By default, the value is set to 3600 seconds (1 hour).

    • :external_id - (String) A unique identifier that is generated by a third party for each of their customers. For each role that the third party can assume, they should instruct their customers to create a role with the external ID that was generated by the third party. Each time the third party assumes the role, they must pass the customer’s correct external ID. The external ID is useful in order to help third parties bind a role to the customer who created it. For more information about the external ID, see About the External ID in Using Temporary Security Credentials.

Returns:

  • (Core::Response)

    The #data method of the response object returns a hash with the following structure:

    • :credentials - (Hash)

      • :access_key_id - (String)

      • :secret_access_key - (String)

      • :session_token - (String)

      • :expiration - (Time)

    • :assumed_role_user - (Hash)

      • :assumed_role_id - (String)

      • :arn - (String)

    • :packed_policy_size - (Integer)



56
57
58
# File 'lib/aws/sts.rb', line 56

def assume_role options = {}
  client.assume_role(options).data
end

#new_federated_session(name, opts = {}) ⇒ FederatedSession

Returns a set of temporary credentials for a federated user with the user name and policy specified in the request. The credentials consist of an Access Key ID, a Secret Access Key, and a security token. The credentials are valid for the specified duration, between one and 36 hours.

The federated user who holds these credentials has only those permissions allowed by intersection of the specified policy and any resource or user policies that apply to the caller of the GetFederationToken API. For more information about how token permissions work, see Controlling Token Permissions in Using AWS Identity and Access Management.

Parameters:

  • name (String)

    The name of the federated user associated with the session. Must be between 2 and 32 characters in length.

  • opts (Hash) (defaults to: {})

    Options for getting temporary credentials.

Options Hash (opts):

  • :duration (Integer)

    The duration, in seconds, that the session should last. Acceptable durations for federation sessions range from 3600s (one hour) to 129600s (36 hours), with one hour as the default.

  • :policy (String, AWS::STS::Policy)

    A policy specifying the permissions to associate with the session. The caller can delegate their own permissions by specifying a policy for the session, and both policies will be checked when a service call is made. In other words, permissions of the session credentials are the intersection of the policy specified in the API and the policies associated with the user who issued the session.

Returns:



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/aws/sts.rb', line 129

def new_federated_session(name, opts = {})
  opts = opts.merge(:name => name)
  case
  when opts[:policy].kind_of?(String) || !opts[:policy]
    # leave it alone
  when opts[:policy].respond_to?(:to_json)
    opts[:policy] = opts[:policy].to_json
  end
  get_session(:get_federation_token, opts) do |resp, session_opts|
    session_opts.merge!(
      :user_id => resp[:federated_user][:federated_user_id],
      :user_arn => resp[:federated_user][:arn],
      :packed_policy_size => resp[:packed_policy_size]
    )
    FederatedSession.new(session_opts)
  end
end

#new_session(opts = {}) ⇒ Session

Returns a set of temporary credentials for an AWS account or IAM User. The credentials consist of an Access Key ID, a Secret Access Key, and a security token. These credentials are valid for the specified duration only. The session duration for IAM users can be between one and 36 hours, with a default of 12 hours. The session duration for AWS account owners is restricted to one hour.

Parameters:

  • opts (Hash) (defaults to: {})

    Options for getting temporary credentials.

Options Hash (opts):

  • :duration (Integer)

    The duration, in seconds, that the session should last. Acceptable durations for IAM user sessions range from 3600s (one hour) to 129600s (36 hours), with 43200s (12 hours) as the default. Sessions for AWS account owners are restricted to a maximum of 3600s (one hour).

  • :serial_number (String)

    The identification number of the Multi-Factor Authentication (MFA) device for the user.

  • :token_code (String)

    The value provided by the MFA device. If the user has an access policy requiring an MFA code, provide the value here to get permission to resources as specified in the access policy.

Returns:



86
87
88
89
90
# File 'lib/aws/sts.rb', line 86

def new_session(opts = {})
  get_session(:get_session_token, opts) do |resp, session_opts|
    Session.new(session_opts)
  end
end