Skip to content

Commit 4a6e01c

Browse files
committed
code cleanup and reorg
1 parent c5ac1a5 commit 4a6e01c

File tree

7 files changed

+103
-40
lines changed

7 files changed

+103
-40
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,5 @@ logs/
187187
# Agent testing
188188
agents/test_results/
189189
agents/.env.user
190+
ssl_data/
191+
agents/.env.agent

agents/agent_w_auth.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,25 @@ def load_env_config(use_session_cookie: bool) -> Dict[str, Optional[str]]:
143143

144144
# Choose .env file based on authentication mode
145145
env_file_name = '.env.user' if use_session_cookie else '.env.agent'
146-
146+
logger.info(f"Using .env file: {env_file_name}")
147147
if DOTENV_AVAILABLE:
148148
file_found = False
149149
file_path = None
150150

151151
# Try to load from .env file in the current directory
152152
env_file = os.path.join(os.path.dirname(__file__), env_file_name)
153153
if os.path.exists(env_file):
154+
logger.info(f"Found .env file: {env_file}")
154155
load_dotenv(env_file)
155156
file_found = True
156157
file_path = env_file
157158
logger.info(f"Loading environment variables from {env_file}")
159+
logger.info(f"user pool id {os.environ.get('COGNITO_USER_POOL_ID')}")
158160
else:
159161
# Try to load from .env file in the parent directory
160162
env_file = os.path.join(os.path.dirname(__file__), '..', env_file_name)
161163
if os.path.exists(env_file):
164+
logger.info(f"Found .env file in parent directory: {env_file}")
162165
load_dotenv(env_file)
163166
file_found = True
164167
file_path = env_file
@@ -167,6 +170,7 @@ def load_env_config(use_session_cookie: bool) -> Dict[str, Optional[str]]:
167170
# Try to load from current working directory
168171
env_file = os.path.join(os.getcwd(), env_file_name)
169172
if os.path.exists(env_file):
173+
logger.info(f"Found .env file in current working directory: {env_file}")
170174
load_dotenv(env_file)
171175
file_found = True
172176
file_path = env_file
@@ -201,14 +205,15 @@ def parse_arguments() -> argparse.Namespace:
201205
"""
202206
# First, determine authentication mode to choose correct .env file
203207
use_session_cookie = get_auth_mode_from_args()
208+
logger.info(f"Using session cookie authentication: {use_session_cookie}")
204209

205210
# Load environment configuration using the appropriate .env file
206211
env_config = load_env_config(use_session_cookie)
207212

208213
parser = argparse.ArgumentParser(description='LangGraph MCP Client with Cognito Authentication')
209214

210215
# Server connection arguments
211-
parser.add_argument('--mcp-registry-url', type=str, default='http://ec2-54-146-182-47.compute-1.amazonaws.com/mcpgw/sse',
216+
parser.add_argument('--mcp-registry-url', type=str, default='https://mcpgateway.ddns.net/mcpgw/sse',
212217
help='Hostname of the MCP Registry')
213218

214219
# Model arguments
@@ -514,6 +519,7 @@ async def main():
514519
"""
515520
# Parse command line arguments
516521
args = parse_arguments()
522+
logger.info(f"Parsed command line arguments successfully, args={args}")
517523

518524
# Display configuration
519525
server_url = args.mcp_registry_url
@@ -540,6 +546,7 @@ async def main():
540546
else:
541547
# Generate Cognito M2M authentication token
542548
logger.info(f"Cognito User Pool ID: {redact_sensitive_value(args.user_pool_id)}")
549+
logger.info(f"Cognito User Pool ID: {args.user_pool_id}")
543550
logger.info(f"Cognito Client ID: {redact_sensitive_value(args.client_id)}")
544551
logger.info(f"AWS Region: {args.region}")
545552

auth_server/server.py

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -986,22 +986,34 @@ async def oauth2_login(provider: str, request: Request, redirect_uri: str = None
986986
# Create temporary session for OAuth2 flow
987987
temp_session = signer.dumps(session_data)
988988

989-
# Build authorization URL with dynamic auth server URL
990-
host = request.headers.get("host", "localhost:8888")
991-
scheme = "https" if request.headers.get("x-forwarded-proto") == "https" or request.url.scheme == "https" else "http"
992-
993-
# Special case for localhost to include port
994-
if "localhost" in host and ":" not in host:
995-
auth_server_url = f"{scheme}://localhost:8888"
989+
# Use configured external URL or build dynamically
990+
auth_server_external_url = os.environ.get('AUTH_SERVER_EXTERNAL_URL')
991+
if auth_server_external_url:
992+
# Use configured external URL (recommended for production)
993+
auth_server_url = auth_server_external_url.rstrip('/')
994+
logger.info(f"Using configured AUTH_SERVER_EXTERNAL_URL: {auth_server_url}")
996995
else:
997-
auth_server_url = f"{scheme}://{host}"
996+
# Fall back to dynamic construction (for development)
997+
host = request.headers.get("host", "localhost:8888")
998+
scheme = "https" if request.headers.get("x-forwarded-proto") == "https" or request.url.scheme == "https" else "http"
999+
1000+
# Special case for localhost to include port
1001+
if "localhost" in host and ":" not in host:
1002+
auth_server_url = f"{scheme}://localhost:8888"
1003+
else:
1004+
auth_server_url = f"{scheme}://{host}"
1005+
1006+
logger.warning(f"AUTH_SERVER_EXTERNAL_URL not set, using dynamic URL: {auth_server_url}")
1007+
1008+
callback_uri = f"{auth_server_url}/oauth2/callback/{provider}"
1009+
logger.info(f"OAuth2 callback URI: {callback_uri}")
9981010

9991011
auth_params = {
10001012
"client_id": provider_config["client_id"],
10011013
"response_type": provider_config["response_type"],
10021014
"scope": " ".join(provider_config["scopes"]),
10031015
"state": state,
1004-
"redirect_uri": f"{auth_server_url}/oauth2/callback/{provider}"
1016+
"redirect_uri": callback_uri
10051017
}
10061018

10071019
auth_url = f"{provider_config['auth_url']}?{urllib.parse.urlencode(auth_params)}"
@@ -1062,15 +1074,24 @@ async def oauth2_callback(
10621074
provider_config = OAUTH2_CONFIG["providers"][provider]
10631075

10641076
# Exchange authorization code for access token
1065-
# Build dynamic auth server URL for token exchange
1066-
host = request.headers.get("host", "localhost:8888")
1067-
scheme = "https" if request.headers.get("x-forwarded-proto") == "https" or request.url.scheme == "https" else "http"
1068-
1069-
# Special case for localhost to include port
1070-
if "localhost" in host and ":" not in host:
1071-
auth_server_url = f"{scheme}://localhost:8888"
1077+
# Use configured external URL or build dynamically
1078+
auth_server_external_url = os.environ.get('AUTH_SERVER_EXTERNAL_URL')
1079+
if auth_server_external_url:
1080+
# Use configured external URL (recommended for production)
1081+
auth_server_url = auth_server_external_url.rstrip('/')
1082+
logger.info(f"Using configured AUTH_SERVER_EXTERNAL_URL for token exchange: {auth_server_url}")
10721083
else:
1073-
auth_server_url = f"{scheme}://{host}"
1084+
# Fall back to dynamic construction (for development)
1085+
host = request.headers.get("host", "localhost:8888")
1086+
scheme = "https" if request.headers.get("x-forwarded-proto") == "https" or request.url.scheme == "https" else "http"
1087+
1088+
# Special case for localhost to include port
1089+
if "localhost" in host and ":" not in host:
1090+
auth_server_url = f"{scheme}://localhost:8888"
1091+
else:
1092+
auth_server_url = f"{scheme}://{host}"
1093+
1094+
logger.warning(f"AUTH_SERVER_EXTERNAL_URL not set, using dynamic URL for token exchange: {auth_server_url}")
10741095

10751096
token_data = await exchange_code_for_token(provider, code, provider_config, auth_server_url)
10761097
logger.info(f"Token data keys: {list(token_data.keys())}")
@@ -1159,7 +1180,7 @@ async def oauth2_callback(
11591180
logger.error(f"Error in OAuth2 callback for {provider}: {e}")
11601181
error_url = OAUTH2_CONFIG.get("registry", {}).get("error_redirect", "/login")
11611182
return RedirectResponse(url=f"{error_url}?error=oauth2_callback_failed", status_code=302)
1162-
1183+
11631184
async def exchange_code_for_token(provider: str, code: str, provider_config: dict, auth_server_url: str = None) -> dict:
11641185
"""Exchange authorization code for access token"""
11651186
if auth_server_url is None:

docker-compose.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ services:
1010
- SECRET_KEY=${SECRET_KEY}
1111
- ADMIN_USER=${ADMIN_USER:-admin}
1212
- ADMIN_PASSWORD=${ADMIN_PASSWORD}
13-
- AUTH_SERVER_URL=http://auth-server:8888
14-
- AUTH_SERVER_EXTERNAL_URL=http://localhost:8888
13+
- AUTH_SERVER_URL=${AUTH_SERVER_URL}
14+
- AUTH_SERVER_EXTERNAL_URL=${AUTH_SERVER_EXTERNAL_URL}
1515
- GITHUB_CLIENT_ID=${GITHUB_CLIENT_ID}
1616
- GITHUB_CLIENT_SECRET=${GITHUB_CLIENT_SECRET}
1717
- COGNITO_CLIENT_ID=${COGNITO_CLIENT_ID}
@@ -25,7 +25,7 @@ services:
2525
volumes:
2626
- /opt/mcp-gateway/servers:/app/registry/servers
2727
- /opt/mcp-gateway/models:/app/registry/models
28-
- ssl_data:/etc/ssl
28+
- /home/ubuntu/ssl_data:/etc/ssl
2929
- /var/log/mcp-gateway:/app/logs
3030
depends_on:
3131
- auth-server
@@ -37,6 +37,7 @@ services:
3737
context: .
3838
dockerfile: docker/Dockerfile.auth
3939
environment:
40+
- REGISTRY_URL=${REGISTRY_URL}
4041
- SECRET_KEY=${SECRET_KEY}
4142
- GITHUB_CLIENT_ID=${GITHUB_CLIENT_ID}
4243
- GITHUB_CLIENT_SECRET=${GITHUB_CLIENT_SECRET}

docker/nginx_rev_proxy.conf

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,30 @@ server {
5555
proxy_pass_request_body on;
5656
}
5757

58+
# OAuth2 Cognito login endpoint
59+
location /oauth2/login/cognito {
60+
proxy_pass http://auth-server:8888/oauth2/login/cognito;
61+
proxy_http_version 1.1;
62+
proxy_set_header Host $host;
63+
proxy_set_header X-Real-IP $remote_addr;
64+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
65+
proxy_set_header X-Forwarded-Proto $scheme;
66+
67+
# Pass through query parameters and headers
68+
proxy_pass_request_headers on;
69+
}
70+
71+
# OAuth2 Cognito logout endpoint
72+
location /oauth2/logout/ {
73+
proxy_pass http://auth-server:8888/oauth2/logout/;
74+
proxy_http_version 1.1;
75+
proxy_set_header Host $host;
76+
proxy_set_header X-Real-IP $remote_addr;
77+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
78+
proxy_set_header X-Forwarded-Proto $scheme;
79+
proxy_pass_request_headers on;
80+
}
81+
5882
# REMOVE HARDCODED /mcpgw
5983
# location /mcpgw/ {
6084
# proxy_pass http://127.0.0.1:8003/;
@@ -184,6 +208,30 @@ server {
184208
proxy_pass_request_body on;
185209
}
186210

211+
# OAuth2 Cognito login endpoint
212+
location /oauth2/login/cognito {
213+
proxy_pass http://auth-server:8888/oauth2/login/cognito;
214+
proxy_http_version 1.1;
215+
proxy_set_header Host $host;
216+
proxy_set_header X-Real-IP $remote_addr;
217+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
218+
proxy_set_header X-Forwarded-Proto $scheme;
219+
220+
# Pass through query parameters and headers
221+
proxy_pass_request_headers on;
222+
}
223+
224+
# OAuth2 Cognito logout endpoint
225+
location /oauth2/logout/ {
226+
proxy_pass http://auth-server:8888/oauth2/logout/;
227+
proxy_http_version 1.1;
228+
proxy_set_header Host $host;
229+
proxy_set_header X-Real-IP $remote_addr;
230+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
231+
proxy_set_header X-Forwarded-Proto $scheme;
232+
proxy_pass_request_headers on;
233+
}
234+
187235
# Duplicate the same location blocks for HTTPS access
188236
location / {
189237
proxy_pass http://127.0.0.1:7860/;

registry/auth/routes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ async def oauth2_login_redirect(provider: str, request: Request):
5353
registry_url = str(request.base_url).rstrip('/')
5454
auth_external_url = settings.auth_server_external_url
5555
auth_url = f"{auth_external_url}/oauth2/login/{provider}?redirect_uri={registry_url}/"
56-
56+
logger.info(f"request.base_url: {request.base_url}, registry_url: {registry_url}, auth_external_url: {auth_external_url}, auth_url: {auth_url}")
5757
logger.info(f"Redirecting to OAuth2 login for provider {provider}: {auth_url}")
5858
return RedirectResponse(url=auth_url, status_code=302)
5959

server_api_restrictions.csv

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)