Run black on moto & test directories.
This commit is contained in:
parent
c820395dbf
commit
96e5b1993d
507 changed files with 52541 additions and 47814 deletions
|
|
@ -7,9 +7,5 @@ class STSClientError(RESTError):
|
|||
|
||||
|
||||
class STSValidationError(STSClientError):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(STSValidationError, self).__init__(
|
||||
"ValidationError",
|
||||
*args, **kwargs
|
||||
)
|
||||
super(STSValidationError, self).__init__("ValidationError", *args, **kwargs)
|
||||
|
|
|
|||
|
|
@ -3,11 +3,15 @@ import datetime
|
|||
from moto.core import BaseBackend, BaseModel
|
||||
from moto.core.utils import iso_8601_datetime_with_milliseconds
|
||||
from moto.iam.models import ACCOUNT_ID
|
||||
from moto.sts.utils import random_access_key_id, random_secret_access_key, random_session_token, random_assumed_role_id
|
||||
from moto.sts.utils import (
|
||||
random_access_key_id,
|
||||
random_secret_access_key,
|
||||
random_session_token,
|
||||
random_assumed_role_id,
|
||||
)
|
||||
|
||||
|
||||
class Token(BaseModel):
|
||||
|
||||
def __init__(self, duration, name=None, policy=None):
|
||||
now = datetime.datetime.utcnow()
|
||||
self.expiration = now + datetime.timedelta(seconds=duration)
|
||||
|
|
@ -20,7 +24,6 @@ class Token(BaseModel):
|
|||
|
||||
|
||||
class AssumedRole(BaseModel):
|
||||
|
||||
def __init__(self, role_session_name, role_arn, policy, duration, external_id):
|
||||
self.session_name = role_session_name
|
||||
self.role_arn = role_arn
|
||||
|
|
@ -46,12 +49,11 @@ class AssumedRole(BaseModel):
|
|||
return "arn:aws:sts::{account_id}:assumed-role/{role_name}/{session_name}".format(
|
||||
account_id=ACCOUNT_ID,
|
||||
role_name=self.role_arn.split("/")[-1],
|
||||
session_name=self.session_name
|
||||
session_name=self.session_name,
|
||||
)
|
||||
|
||||
|
||||
class STSBackend(BaseBackend):
|
||||
|
||||
def __init__(self):
|
||||
self.assumed_roles = []
|
||||
|
||||
|
|
|
|||
|
|
@ -10,38 +10,38 @@ MAX_FEDERATION_TOKEN_POLICY_LENGTH = 2048
|
|||
|
||||
|
||||
class TokenResponse(BaseResponse):
|
||||
|
||||
def get_session_token(self):
|
||||
duration = int(self.querystring.get('DurationSeconds', [43200])[0])
|
||||
duration = int(self.querystring.get("DurationSeconds", [43200])[0])
|
||||
token = sts_backend.get_session_token(duration=duration)
|
||||
template = self.response_template(GET_SESSION_TOKEN_RESPONSE)
|
||||
return template.render(token=token)
|
||||
|
||||
def get_federation_token(self):
|
||||
duration = int(self.querystring.get('DurationSeconds', [43200])[0])
|
||||
policy = self.querystring.get('Policy', [None])[0]
|
||||
duration = int(self.querystring.get("DurationSeconds", [43200])[0])
|
||||
policy = self.querystring.get("Policy", [None])[0]
|
||||
|
||||
if policy is not None and len(policy) > MAX_FEDERATION_TOKEN_POLICY_LENGTH:
|
||||
raise STSValidationError(
|
||||
"1 validation error detected: Value "
|
||||
"'{\"Version\": \"2012-10-17\", \"Statement\": [...]}' "
|
||||
'\'{"Version": "2012-10-17", "Statement": [...]}\' '
|
||||
"at 'policy' failed to satisfy constraint: Member must have length less than or "
|
||||
" equal to %s" % MAX_FEDERATION_TOKEN_POLICY_LENGTH
|
||||
)
|
||||
|
||||
name = self.querystring.get('Name')[0]
|
||||
name = self.querystring.get("Name")[0]
|
||||
token = sts_backend.get_federation_token(
|
||||
duration=duration, name=name, policy=policy)
|
||||
duration=duration, name=name, policy=policy
|
||||
)
|
||||
template = self.response_template(GET_FEDERATION_TOKEN_RESPONSE)
|
||||
return template.render(token=token, account_id=ACCOUNT_ID)
|
||||
|
||||
def assume_role(self):
|
||||
role_session_name = self.querystring.get('RoleSessionName')[0]
|
||||
role_arn = self.querystring.get('RoleArn')[0]
|
||||
role_session_name = self.querystring.get("RoleSessionName")[0]
|
||||
role_arn = self.querystring.get("RoleArn")[0]
|
||||
|
||||
policy = self.querystring.get('Policy', [None])[0]
|
||||
duration = int(self.querystring.get('DurationSeconds', [3600])[0])
|
||||
external_id = self.querystring.get('ExternalId', [None])[0]
|
||||
policy = self.querystring.get("Policy", [None])[0]
|
||||
duration = int(self.querystring.get("DurationSeconds", [3600])[0])
|
||||
external_id = self.querystring.get("ExternalId", [None])[0]
|
||||
|
||||
role = sts_backend.assume_role(
|
||||
role_session_name=role_session_name,
|
||||
|
|
@ -54,12 +54,12 @@ class TokenResponse(BaseResponse):
|
|||
return template.render(role=role)
|
||||
|
||||
def assume_role_with_web_identity(self):
|
||||
role_session_name = self.querystring.get('RoleSessionName')[0]
|
||||
role_arn = self.querystring.get('RoleArn')[0]
|
||||
role_session_name = self.querystring.get("RoleSessionName")[0]
|
||||
role_arn = self.querystring.get("RoleArn")[0]
|
||||
|
||||
policy = self.querystring.get('Policy', [None])[0]
|
||||
duration = int(self.querystring.get('DurationSeconds', [3600])[0])
|
||||
external_id = self.querystring.get('ExternalId', [None])[0]
|
||||
policy = self.querystring.get("Policy", [None])[0]
|
||||
duration = int(self.querystring.get("DurationSeconds", [3600])[0])
|
||||
external_id = self.querystring.get("ExternalId", [None])[0]
|
||||
|
||||
role = sts_backend.assume_role_with_web_identity(
|
||||
role_session_name=role_session_name,
|
||||
|
|
|
|||
|
|
@ -1,10 +1,6 @@
|
|||
from __future__ import unicode_literals
|
||||
from .responses import TokenResponse
|
||||
|
||||
url_bases = [
|
||||
"https?://sts(.*).amazonaws.com"
|
||||
]
|
||||
url_bases = ["https?://sts(.*).amazonaws.com"]
|
||||
|
||||
url_paths = {
|
||||
'{0}/$': TokenResponse.dispatch,
|
||||
}
|
||||
url_paths = {"{0}/$": TokenResponse.dispatch}
|
||||
|
|
|
|||
|
|
@ -19,17 +19,20 @@ def random_secret_access_key():
|
|||
|
||||
|
||||
def random_session_token():
|
||||
return SESSION_TOKEN_PREFIX + base64.b64encode(os.urandom(266))[len(SESSION_TOKEN_PREFIX):].decode()
|
||||
return (
|
||||
SESSION_TOKEN_PREFIX
|
||||
+ base64.b64encode(os.urandom(266))[len(SESSION_TOKEN_PREFIX) :].decode()
|
||||
)
|
||||
|
||||
|
||||
def random_assumed_role_id():
|
||||
return ACCOUNT_SPECIFIC_ASSUMED_ROLE_ID_PREFIX + _random_uppercase_or_digit_sequence(9)
|
||||
return (
|
||||
ACCOUNT_SPECIFIC_ASSUMED_ROLE_ID_PREFIX + _random_uppercase_or_digit_sequence(9)
|
||||
)
|
||||
|
||||
|
||||
def _random_uppercase_or_digit_sequence(length):
|
||||
return ''.join(
|
||||
six.text_type(
|
||||
random.choice(
|
||||
string.ascii_uppercase + string.digits
|
||||
)) for _ in range(length)
|
||||
return "".join(
|
||||
six.text_type(random.choice(string.ascii_uppercase + string.digits))
|
||||
for _ in range(length)
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue