Skip to content

Commit cdb9638

Browse files
Implemented functionality for long-lived credentials (#5)
* Implemented functionality for long-lived credentials It will now try to get a federation token if a session token is not present in the current credentials.
1 parent 8bee546 commit cdb9638

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

aws_browser/aws.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import dataclasses
22
import json
3+
import uuid
34
from typing import Optional
45

56
import boto3
7+
import botocore
68
import requests
79

810
from .constants import ISSUER
@@ -16,8 +18,7 @@ def get_console_url(session: boto3.Session) -> str:
1618
def get_signin_token(session: boto3.Session) -> str:
1719
credentials = session.get_credentials().get_frozen_credentials()
1820
if not credentials.token:
19-
# We assume we're using SSO or AssumeRole, these work out of the box
20-
raise NotImplementedError("We only support credentials from SSO or STS")
21+
credentials = _get_federation_token(session)
2122

2223
parameters = {
2324
"Action": "getSigninToken",
@@ -61,6 +62,21 @@ def _get_session() -> boto3.Session:
6162
return boto3.Session()
6263

6364

65+
def _get_federation_token(session: boto3.Session) -> botocore.credentials.ReadOnlyCredentials:
66+
# If we are starting form a User, we need to call GetFederationToken (GetSessionToken does not work here)
67+
# in the managed policies only the PowerUserAccess and AdministratorAccess allow this
68+
sts = session.client("sts")
69+
resp = sts.get_federation_token(
70+
Name=uuid.uuid4().hex,
71+
PolicyArns=[{"arn": "arn:aws:iam::aws:policy/AdministratorAccess"}],
72+
)["Credentials"]
73+
return botocore.credentials.ReadOnlyCredentials(
74+
resp["AccessKeyId"],
75+
resp["SecretAccessKey"],
76+
resp["SessionToken"],
77+
)
78+
79+
6480
def _signin_endpoint(session: boto3.Session) -> str:
6581
region = session.region_name
6682
if region and region.startswith("us-gov"):
@@ -82,12 +98,15 @@ def _console_endpoint(session: boto3.Session) -> str:
8298
@dataclasses.dataclass
8399
class Arn:
84100
"arn:aws:sts::123456789012:assumed-role/my-role-name/my-role-session-name"
101+
"arn:aws:iam::123456789012:user/user-name-with-path"
102+
"arn:aws:sts::123456789012:federated-user/user-name"
103+
"arn:aws:iam::123456789012:root"
85104
partition: str
86105
service: str
87106
region: Optional[str]
88107
account_id: Optional[str]
89108
resource_type: str
90-
resource_id: str
109+
resource_id: Optional[str]
91110

92111
def __init__(self, arn: str):
93112
parts = arn.split(":")
@@ -104,4 +123,7 @@ def __init__(self, arn: str):
104123
# arn:partition:service:region:account-id:resource-type/resource-id
105124
resource_parts = parts[5].split("/", 1)
106125
self.resource_type = resource_parts[0]
107-
self.resource_id = resource_parts[1]
126+
if self.resource_type == "root":
127+
self.resource_id = None
128+
else:
129+
self.resource_id = resource_parts[1]

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "aws-browser"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
description = ""
55
authors = ["Ben Bridts", "Cloudar"]
66

0 commit comments

Comments
 (0)