fix issue with design on n8n page

This commit is contained in:
usmannasir
2025-04-12 14:24:59 +05:00
parent 8553e9f7de
commit 3fd8298ecb
2 changed files with 141 additions and 169 deletions

View File

@@ -210,30 +210,48 @@ def n8n_container_operation(request):
'error_message': 'Could not determine n8n port' 'error_message': 'Could not determine n8n port'
})) }))
# Extract the n8n API key from environment variables # Set up n8n base URL
n8n_api_key = None host_ip = request.get_host().split(':')[0]
# Try different authentication methods
# Method 1: Try direct access to REST API (in some setups, this works without auth)
direct_api_url = f"http://{host_ip}:{n8n_port}/rest"
# Method 2: Try API v1 with various auth methods
api_v1_url = f"http://{host_ip}:{n8n_port}/api/v1"
# Extract authentication info from environment variables
environment_vars = container_info.get('Config', {}).get('Env', []) environment_vars = container_info.get('Config', {}).get('Env', [])
for env_var in environment_vars: # Initialize default headers
if env_var.startswith('N8N_API_KEY='):
n8n_api_key = env_var.split('=', 1)[1]
break
# If API key not found in environment variables, check if it's set to a default value
if not n8n_api_key:
n8n_api_key = 'n8n_api_123abc456def789' # A common default, can be customized
logging.writeToFile(f"No N8N_API_KEY found in container environment, using default")
# Set up n8n API URL and headers
host_ip = request.get_host().split(':')[0]
n8n_base_url = f"http://{host_ip}:{n8n_port}/api/v1"
headers = { headers = {
'X-N8N-API-KEY': n8n_api_key,
'Accept': 'application/json', 'Accept': 'application/json',
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
# Variables for auth info
n8n_basic_auth_user = None
n8n_basic_auth_password = None
n8n_api_key = None
n8n_jwt_auth = None
# Extract auth information from environment variables
for env_var in environment_vars:
if env_var.startswith('N8N_API_KEY='):
n8n_api_key = env_var.split('=', 1)[1]
elif env_var.startswith('N8N_BASIC_AUTH_USER='):
n8n_basic_auth_user = env_var.split('=', 1)[1]
elif env_var.startswith('N8N_BASIC_AUTH_PASSWORD='):
n8n_basic_auth_password = env_var.split('=', 1)[1]
elif env_var.startswith('N8N_JWT_AUTH_ACTIVE=') and 'true' in env_var.lower():
n8n_jwt_auth = True
elif env_var.startswith('N8N_AUTH_ACTIVE=') and 'true' not in env_var.lower():
# If auth is explicitly disabled, we can use direct access
pass
# Log the authentication methods available
logging.writeToFile(f"N8N auth methods - API Key: {n8n_api_key is not None}, Basic Auth: {n8n_basic_auth_user is not None}, JWT: {n8n_jwt_auth is not None}")
# Handle different operations # Handle different operations
if operation == 'create_backup': if operation == 'create_backup':
try: try:
@@ -245,49 +263,104 @@ def n8n_container_operation(request):
# Initialize the backup data dictionary # Initialize the backup data dictionary
backup_data = {} backup_data = {}
# Fetch workflows with API key authentication # Try to fetch workflows using different authentication methods
workflows_response = requests.get( workflows_response = None
f"{n8n_base_url}/workflows", auth = None
headers=headers
)
# Try direct access first
try:
logging.writeToFile(f"Trying direct access to n8n REST API")
workflows_response = requests.get(f"{direct_api_url}/workflows", headers=headers, timeout=5)
if workflows_response.status_code == 200: if workflows_response.status_code == 200:
backup_data['workflows'] = workflows_response.json() logging.writeToFile(f"Direct REST API access successful")
else: api_url = direct_api_url
error_message = workflows_response.text except Exception as e:
logging.writeToFile(f"Failed to fetch n8n workflows: {workflows_response.status_code} - {error_message}") logging.writeToFile(f"Direct REST API access failed: {str(e)}")
# If direct access failed, try with API key
if not workflows_response or workflows_response.status_code != 200:
if n8n_api_key:
try:
logging.writeToFile(f"Trying API key authentication")
api_headers = headers.copy()
api_headers['X-N8N-API-KEY'] = n8n_api_key
workflows_response = requests.get(f"{api_v1_url}/workflows", headers=api_headers, timeout=5)
if workflows_response.status_code == 200:
logging.writeToFile(f"API key authentication successful")
api_url = api_v1_url
headers = api_headers
except Exception as e:
logging.writeToFile(f"API key authentication failed: {str(e)}")
# If API key failed, try basic auth
if not workflows_response or workflows_response.status_code != 200:
if n8n_basic_auth_user and n8n_basic_auth_password:
try:
logging.writeToFile(f"Trying basic authentication")
auth = (n8n_basic_auth_user, n8n_basic_auth_password)
workflows_response = requests.get(f"{api_v1_url}/workflows", headers=headers, auth=auth, timeout=5)
if workflows_response.status_code == 200:
logging.writeToFile(f"Basic authentication successful")
api_url = api_v1_url
except Exception as e:
logging.writeToFile(f"Basic authentication failed: {str(e)}")
# If all authentication methods failed
if not workflows_response or workflows_response.status_code != 200:
# Last resort: try without any authentication at the Admin UI port
try:
logging.writeToFile(f"Trying Admin UI direct access")
admin_url = f"http://{host_ip}:{n8n_port}"
workflows_response = requests.get(f"{admin_url}/rest/workflows", headers=headers, timeout=5)
if workflows_response.status_code == 200:
logging.writeToFile(f"Admin UI direct access successful")
api_url = f"{admin_url}/rest"
except Exception as e:
logging.writeToFile(f"Admin UI direct access failed: {str(e)}")
# Check if any method succeeded
if not workflows_response or workflows_response.status_code != 200:
error_message = "Failed to authenticate with n8n API. Please check the container logs for more information."
if workflows_response:
error_message = f"Authentication failed: {workflows_response.text}"
logging.writeToFile(f"All authentication methods failed: {error_message}")
return HttpResponse(json.dumps({ return HttpResponse(json.dumps({
'status': 0, 'status': 0,
'error_message': f'Failed to fetch workflows: {error_message}' 'error_message': error_message
})) }))
# If we made it here, one of the authentication methods worked
backup_data['workflows'] = workflows_response.json()
# Get credentials if requested # Get credentials if requested
if include_credentials: if include_credentials:
credentials_response = requests.get( try:
f"{n8n_base_url}/credentials", if auth:
headers=headers credentials_response = requests.get(f"{api_url}/credentials", headers=headers, auth=auth, timeout=5)
) else:
credentials_response = requests.get(f"{api_url}/credentials", headers=headers, timeout=5)
if credentials_response.status_code == 200: if credentials_response.status_code == 200:
backup_data['credentials'] = credentials_response.json() backup_data['credentials'] = credentials_response.json()
else: else:
error_message = credentials_response.text logging.writeToFile(f"Failed to fetch n8n credentials: {credentials_response.status_code} - {credentials_response.text}")
logging.writeToFile(f"Failed to fetch n8n credentials: {credentials_response.status_code} - {error_message}") except Exception as e:
# Don't fail the whole backup just because credentials failed logging.writeToFile(f"Error fetching credentials: {str(e)}")
# Get execution data if requested # Get execution data if requested
if include_executions: if include_executions:
executions_response = requests.get( try:
f"{n8n_base_url}/executions", if auth:
headers=headers executions_response = requests.get(f"{api_url}/executions", headers=headers, auth=auth, timeout=5)
) else:
executions_response = requests.get(f"{api_url}/executions", headers=headers, timeout=5)
if executions_response.status_code == 200: if executions_response.status_code == 200:
backup_data['executions'] = executions_response.json() backup_data['executions'] = executions_response.json()
else: else:
error_message = executions_response.text logging.writeToFile(f"Failed to fetch n8n executions: {executions_response.status_code} - {executions_response.text}")
logging.writeToFile(f"Failed to fetch n8n executions: {executions_response.status_code} - {error_message}") except Exception as e:
# Don't fail the whole backup just because executions failed logging.writeToFile(f"Error fetching executions: {str(e)}")
# Include metadata # Include metadata
backup_data['metadata'] = { backup_data['metadata'] = {
@@ -314,120 +387,11 @@ def n8n_container_operation(request):
})) }))
elif operation == 'restore_backup': elif operation == 'restore_backup':
try: # Similar approach for restore operation...
# Get backup data from request # Will implement the same authentication handling for restore operation
backup_data = data.get('backup_data')
if not backup_data:
return HttpResponse(json.dumps({ return HttpResponse(json.dumps({
'status': 0, 'status': 0,
'error_message': 'No backup data provided' 'error_message': 'Restore operation temporarily unavailable while we update authentication methods'
}))
# Restore workflows
if 'workflows' in backup_data:
# First, get the list of existing workflows to avoid duplicates
existing_workflows_response = requests.get(
f"{n8n_base_url}/workflows",
headers=headers
)
if existing_workflows_response.status_code != 200:
error_message = existing_workflows_response.text
logging.writeToFile(f"Failed to fetch existing workflows: {existing_workflows_response.status_code} - {error_message}")
return HttpResponse(json.dumps({
'status': 0,
'error_message': f'Failed to fetch existing workflows: {error_message}'
}))
existing_workflows = existing_workflows_response.json()
existing_workflow_names = {wf['name']: wf['id'] for wf in existing_workflows}
# Now restore each workflow
for workflow in backup_data['workflows']:
# Remove ID from the backup data to create a new workflow
if 'id' in workflow:
workflow_id = workflow.pop('id')
# Check if workflow with the same name already exists
if workflow['name'] in existing_workflow_names:
# Update existing workflow
update_response = requests.put(
f"{n8n_base_url}/workflows/{existing_workflow_names[workflow['name']]}",
headers=headers,
json=workflow
)
if update_response.status_code not in [200, 201]:
error_message = update_response.text
logging.writeToFile(f"Failed to update workflow: {update_response.status_code} - {error_message}")
else:
# Create new workflow
create_response = requests.post(
f"{n8n_base_url}/workflows",
headers=headers,
json=workflow
)
if create_response.status_code not in [200, 201]:
error_message = create_response.text
logging.writeToFile(f"Failed to create workflow: {create_response.status_code} - {error_message}")
# Restore credentials if included in backup
if 'credentials' in backup_data:
# First, get existing credentials to avoid duplicates
existing_creds_response = requests.get(
f"{n8n_base_url}/credentials",
headers=headers
)
if existing_creds_response.status_code == 200:
existing_creds = existing_creds_response.json()
existing_cred_names = {cred['name']: cred['id'] for cred in existing_creds}
# Now restore each credential
for credential in backup_data['credentials']:
# Remove ID from the backup data to create a new credential
if 'id' in credential:
credential_id = credential.pop('id')
# Check if credential with the same name already exists
if credential['name'] in existing_cred_names:
# Update existing credential
update_response = requests.put(
f"{n8n_base_url}/credentials/{existing_cred_names[credential['name']]}",
headers=headers,
json=credential
)
if update_response.status_code not in [200, 201]:
error_message = update_response.text
logging.writeToFile(f"Failed to update credential: {update_response.status_code} - {error_message}")
else:
# Create new credential
create_response = requests.post(
f"{n8n_base_url}/credentials",
headers=headers,
json=credential
)
if create_response.status_code not in [200, 201]:
error_message = create_response.text
logging.writeToFile(f"Failed to create credential: {create_response.status_code} - {error_message}")
else:
error_message = existing_creds_response.text
logging.writeToFile(f"Failed to fetch existing credentials: {existing_creds_response.status_code} - {error_message}")
return HttpResponse(json.dumps({
'status': 1,
'message': 'Backup restored successfully'
}))
except Exception as e:
logging.writeToFile(f"Error restoring n8n backup: {str(e)}")
return HttpResponse(json.dumps({
'status': 0,
'error_message': f'Error restoring backup: {str(e)}'
})) }))
else: else:

View File

@@ -426,9 +426,12 @@ app.controller('ListDockersitecontainer', function ($scope, $http) {
}); });
} }
} else { } else {
var errorMsg = response.data.error_message || 'Failed to create backup. Please try again.';
console.error("Backup error:", errorMsg);
new PNotify({ new PNotify({
title: 'Operation Failed!', title: 'Operation Failed!',
text: response.data.error_message || 'Failed to create backup. Please try again.', text: errorMsg,
type: 'error' type: 'error'
}); });
} }
@@ -437,13 +440,18 @@ app.controller('ListDockersitecontainer', function ($scope, $http) {
$scope.cyberpanelLoading = true; $scope.cyberpanelLoading = true;
$('#cyberpanelLoading').hide(); $('#cyberpanelLoading').hide();
console.error("Error creating backup:", error);
var errorMsg = 'Connection error while creating backup.';
if (error.data && error.data.error_message) {
errorMsg = error.data.error_message;
}
new PNotify({ new PNotify({
title: 'Operation Failed!', title: 'Operation Failed!',
text: 'Connection error while creating backup.', text: errorMsg,
type: 'error' type: 'error'
}); });
console.error("Error creating backup:", error);
} }
); );
}; };