moto/tests/test_sts/test_sts.py

85 lines
3.2 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
2013-05-24 21:22:34 +00:00
import json
import boto
import boto3
2013-05-24 21:22:34 +00:00
from freezegun import freeze_time
2013-08-03 21:21:25 +00:00
import sure # noqa
2013-05-24 21:22:34 +00:00
2017-02-16 03:35:45 +00:00
from moto import mock_sts, mock_sts_deprecated
2013-05-24 21:22:34 +00:00
@freeze_time("2012-01-01 12:00:00")
2017-02-16 03:35:45 +00:00
@mock_sts_deprecated
2013-05-24 21:22:34 +00:00
def test_get_session_token():
conn = boto.connect_sts()
token = conn.get_session_token(duration=123)
2014-11-30 04:34:40 +00:00
token.expiration.should.equal('2012-01-01T12:02:03.000Z')
2017-02-24 02:37:43 +00:00
token.session_token.should.equal(
"AQoEXAMPLEH4aoAH0gNCAPyJxz4BlCFFxWNE1OPTgk5TthT+FvwqnKwRcOIfrRh3c/LTo6UDdyJwOOvEVPvLXCrrrUtdnniCEXAMPLE/IvU1dYUg2RVAJBanLiHb4IgRmpRV3zrkuWJOgQs8IZZaIv2BXIa2R4OlgkBN9bkUDNCJiBeb/AXlzBBko7b15fjrBs2+cTQtpZ3CYWFXG8C5zqx37wnOE49mRl/+OtkIKGO7fAE")
2013-05-24 21:22:34 +00:00
token.access_key.should.equal("AKIAIOSFODNN7EXAMPLE")
token.secret_key.should.equal("wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY")
@freeze_time("2012-01-01 12:00:00")
2017-02-16 03:35:45 +00:00
@mock_sts_deprecated
def test_get_federation_token():
conn = boto.connect_sts()
token = conn.get_federation_token(duration=123, name="Bob")
2014-11-30 04:34:40 +00:00
token.credentials.expiration.should.equal('2012-01-01T12:02:03.000Z')
2017-02-24 02:37:43 +00:00
token.credentials.session_token.should.equal(
"AQoDYXdzEPT//////////wEXAMPLEtc764bNrC9SAPBSM22wDOk4x4HIZ8j4FZTwdQWLWsKWHGBuFqwAeMicRXmxfpSPfIeoIYRqTflfKD8YUuwthAx7mSEI/qkPpKPi/kMcGdQrmGdeehM4IC1NtBmUpp2wUE8phUZampKsburEDy0KPkyQDYwT7WZ0wq5VSXDvp75YU9HFvlRd8Tx6q6fE8YQcHNVXAkiY9q6d+xo0rKwT38xVqr7ZD0u0iPPkUL64lIZbqBAz+scqKmlzm8FDrypNC9Yjc8fPOLn9FX9KSYvKTr4rvx3iSIlTJabIQwj2ICCR/oLxBA==")
token.credentials.access_key.should.equal("AKIAIOSFODNN7EXAMPLE")
2017-02-24 02:37:43 +00:00
token.credentials.secret_key.should.equal(
"wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY")
token.federated_user_arn.should.equal(
"arn:aws:sts::123456789012:federated-user/Bob")
token.federated_user_id.should.equal("123456789012:Bob")
2013-05-24 21:22:34 +00:00
@freeze_time("2012-01-01 12:00:00")
2017-02-16 03:35:45 +00:00
@mock_sts_deprecated
2013-05-24 21:22:34 +00:00
def test_assume_role():
conn = boto.connect_sts()
policy = json.dumps({
"Statement": [
{
"Sid": "Stmt13690092345534",
"Action": [
"S3:ListBucket"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::foobar-tester"
]
},
]
})
s3_role = "arn:aws:iam::123456789012:role/test-role"
2017-02-24 02:37:43 +00:00
role = conn.assume_role(s3_role, "session-name",
policy, duration_seconds=123)
2013-05-24 21:22:34 +00:00
credentials = role.credentials
2014-11-30 04:34:40 +00:00
credentials.expiration.should.equal('2012-01-01T12:02:03.000Z')
credentials.session_token.should.have.length_of(356)
assert credentials.session_token.startswith("FQoGZXIvYXdzE")
credentials.access_key.should.have.length_of(20)
assert credentials.access_key.startswith("ASIA")
credentials.secret_key.should.have.length_of(40)
2013-05-24 21:22:34 +00:00
role.user.arn.should.equal("arn:aws:iam::123456789012:role/test-role")
role.user.assume_role_id.should.contain("session-name")
2017-02-24 02:37:43 +00:00
@mock_sts
def test_get_caller_identity():
2017-02-24 02:37:43 +00:00
identity = boto3.client(
"sts", region_name='us-east-1').get_caller_identity()
identity['Arn'].should.equal('arn:aws:sts::123456789012:user/moto')
identity['UserId'].should.equal('AKIAIOSFODNN7EXAMPLE')
identity['Account'].should.equal('123456789012')