Skip to content

Commit 7d19f58

Browse files
committed
fix(identity): add missing utils.py and sys.path setup
Notebooks import setup_cognito_user_pool and reauthenticate_user from utils but no utils.py existed, causing ModuleNotFoundError. - Add shared utils.py to 03-AgentCore-identity/ with both functions - Add sys.path cell to the two 3LO notebooks that were missing it (inbound auth notebook already had the sys.path setup) Fixes #969
1 parent 078fdbf commit 7d19f58

3 files changed

Lines changed: 106 additions & 2 deletions

File tree

01-tutorials/03-AgentCore-identity/05-Outbound_Auth_3lo/runtime_with_strands_and_egress_3lo.ipynb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,19 @@
139139
"print(\"sys.path[0]:\", sys.path[0])"
140140
]
141141
},
142+
{
143+
"cell_type": "code",
144+
"execution_count": null,
145+
"metadata": {},
146+
"outputs": [],
147+
"source": [
148+
"import sys\n",
149+
"import os\n",
150+
"\n",
151+
"# Add parent directory to path to find shared utils.py\n",
152+
"sys.path.insert(0, os.path.abspath(os.path.join(os.getcwd(), '..')))\n"
153+
]
154+
},
142155
{
143156
"cell_type": "code",
144157
"execution_count": null,
@@ -950,4 +963,4 @@
950963
},
951964
"nbformat": 4,
952965
"nbformat_minor": 5
953-
}
966+
}

01-tutorials/03-AgentCore-identity/06-Outbound_Auth_Github/runtime_with_strands_and_egress_github_3lo.ipynb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,19 @@
139139
"print(\"sys.path[0]:\", sys.path[0])"
140140
]
141141
},
142+
{
143+
"cell_type": "code",
144+
"execution_count": null,
145+
"metadata": {},
146+
"outputs": [],
147+
"source": [
148+
"import sys\n",
149+
"import os\n",
150+
"\n",
151+
"# Add parent directory to path to find shared utils.py\n",
152+
"sys.path.insert(0, os.path.abspath(os.path.join(os.getcwd(), '..')))\n"
153+
]
154+
},
142155
{
143156
"cell_type": "code",
144157
"execution_count": null,
@@ -904,4 +917,4 @@
904917
},
905918
"nbformat": 4,
906919
"nbformat_minor": 4
907-
}
920+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import boto3
2+
3+
4+
def setup_cognito_user_pool(region):
5+
"""Create a Cognito user pool with a test user and return config."""
6+
cognito_client = boto3.client("cognito-idp", region_name=region)
7+
try:
8+
pool = cognito_client.create_user_pool(
9+
PoolName="AgentCorePool",
10+
Policies={"PasswordPolicy": {"MinimumLength": 8}},
11+
)
12+
pool_id = pool["UserPool"]["Id"]
13+
14+
client = cognito_client.create_user_pool_client(
15+
UserPoolId=pool_id,
16+
ClientName="AgentCorePoolClient",
17+
GenerateSecret=False,
18+
ExplicitAuthFlows=["ALLOW_USER_PASSWORD_AUTH", "ALLOW_REFRESH_TOKEN_AUTH"],
19+
)
20+
client_id = client["UserPoolClient"]["ClientId"]
21+
22+
cognito_client.admin_create_user(
23+
UserPoolId=pool_id,
24+
Username="testuser1",
25+
TemporaryPassword="Temp123!",
26+
MessageAction="SUPPRESS",
27+
)
28+
cognito_client.admin_set_user_password(
29+
UserPoolId=pool_id,
30+
Username="testuser1",
31+
Password="MyPassword123!",
32+
Permanent=True,
33+
)
34+
35+
auth = cognito_client.initiate_auth(
36+
ClientId=client_id,
37+
AuthFlow="USER_PASSWORD_AUTH",
38+
AuthParameters={"USERNAME": "testuser1", "PASSWORD": "MyPassword123!"},
39+
)
40+
bearer_token = auth["AuthenticationResult"]["AccessToken"]
41+
42+
discovery_url = (
43+
f"https://cognito-idp.{region}.amazonaws.com/{pool_id}"
44+
"/.well-known/openid-configuration"
45+
)
46+
print(f"Pool ID: {pool_id}")
47+
print(f"Client ID: {client_id}")
48+
print(f"Discovery URL: {discovery_url}")
49+
print(f"Bearer Token: {bearer_token}")
50+
51+
return {
52+
"pool_id": pool_id,
53+
"client_id": client_id,
54+
"bearer_token": bearer_token,
55+
"discovery_url": discovery_url,
56+
}
57+
except Exception as e:
58+
print(f"Error setting up Cognito user pool: {e}")
59+
return None
60+
61+
62+
def reauthenticate_user(client_id, region=None, username="testuser1", password="MyPassword123!"):
63+
"""Reauthenticate a single Cognito user and return their access token."""
64+
if region is None:
65+
region = boto3.session.Session().region_name
66+
cognito_client = boto3.client("cognito-idp", region_name=region)
67+
try:
68+
auth = cognito_client.initiate_auth(
69+
ClientId=client_id,
70+
AuthFlow="USER_PASSWORD_AUTH",
71+
AuthParameters={"USERNAME": username, "PASSWORD": password},
72+
)
73+
token = auth["AuthenticationResult"]["AccessToken"]
74+
print(f"Successfully reauthenticated {username}")
75+
return token
76+
except Exception as e:
77+
print(f"Error reauthenticating {username}: {e}")
78+
return None

0 commit comments

Comments
 (0)