Security: 移除硬編碼的資料庫帳密
- database.py: 改從環境變數讀取 DB 設定,新增必要變數檢查 - settings.py: 改從環境變數讀取 DB 設定 - test_db_connection.py: 改從環境變數讀取 DB 設定 所有機敏資料現在必須透過 .env 檔案設定, 參考 .env.example 取得設定範本。 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -20,24 +20,35 @@ logger = logging.getLogger(__name__)
|
||||
project_root = Path(__file__).parent
|
||||
sys.path.insert(0, str(project_root))
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from hbr_crawler.hbr_crawler.database import DatabaseManager, get_database_manager
|
||||
|
||||
# 資料庫連線資訊
|
||||
# 載入 .env 檔案
|
||||
load_dotenv()
|
||||
|
||||
# 資料庫連線資訊(從環境變數讀取)
|
||||
DB_CONFIG = {
|
||||
'host': 'mysql.theaken.com',
|
||||
'port': 33306,
|
||||
'user': 'A101',
|
||||
'password': 'Aa123456',
|
||||
'database': 'db_A101'
|
||||
'host': os.environ.get('DB_HOST'),
|
||||
'port': int(os.environ.get('DB_PORT', 3306)),
|
||||
'user': os.environ.get('DB_USER'),
|
||||
'password': os.environ.get('DB_PASSWORD'),
|
||||
'database': os.environ.get('DB_NAME')
|
||||
}
|
||||
|
||||
# 檢查必要的環境變數
|
||||
missing_vars = [k for k, v in DB_CONFIG.items() if v is None and k != 'port']
|
||||
if missing_vars:
|
||||
print(f"錯誤: 缺少必要的環境變數: {', '.join(['DB_' + k.upper() for k in missing_vars])}")
|
||||
print("請在 .env 檔案中設定這些值,參考 .env.example")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def test_basic_connection():
|
||||
"""測試基本連線(不指定資料庫)"""
|
||||
print("\n" + "="*50)
|
||||
print("測試 1: 基本資料庫連線(不指定資料庫)")
|
||||
print("="*50)
|
||||
|
||||
|
||||
db_manager = DatabaseManager(
|
||||
host=DB_CONFIG['host'],
|
||||
port=DB_CONFIG['port'],
|
||||
@@ -45,7 +56,7 @@ def test_basic_connection():
|
||||
password=DB_CONFIG['password'],
|
||||
database=None
|
||||
)
|
||||
|
||||
|
||||
if db_manager.test_connection():
|
||||
print("✓ 基本連線測試成功")
|
||||
return True
|
||||
@@ -57,9 +68,9 @@ def test_basic_connection():
|
||||
def create_database():
|
||||
"""建立 HBR_scraper 資料庫(如果需要)"""
|
||||
print("\n" + "="*50)
|
||||
print("測試 2: 檢查資料庫連線(使用現有資料庫 db_A101)")
|
||||
print("測試 2: 檢查資料庫連線(使用現有資料庫)")
|
||||
print("="*50)
|
||||
|
||||
|
||||
db_manager = DatabaseManager(
|
||||
host=DB_CONFIG['host'],
|
||||
port=DB_CONFIG['port'],
|
||||
@@ -67,7 +78,7 @@ def create_database():
|
||||
password=DB_CONFIG['password'],
|
||||
database=None
|
||||
)
|
||||
|
||||
|
||||
# 嘗試建立資料庫(可能需要管理員權限)
|
||||
try:
|
||||
if db_manager.create_database('HBR_scraper'):
|
||||
@@ -85,11 +96,11 @@ def create_database():
|
||||
|
||||
|
||||
def test_database_connection():
|
||||
"""測試連接到 db_A101 資料庫"""
|
||||
"""測試連接到指定資料庫"""
|
||||
print("\n" + "="*50)
|
||||
print("測試 3: 連接到 db_A101 資料庫")
|
||||
print(f"測試 3: 連接到 {DB_CONFIG['database']} 資料庫")
|
||||
print("="*50)
|
||||
|
||||
|
||||
db_manager = DatabaseManager(
|
||||
host=DB_CONFIG['host'],
|
||||
port=DB_CONFIG['port'],
|
||||
@@ -97,7 +108,7 @@ def test_database_connection():
|
||||
password=DB_CONFIG['password'],
|
||||
database=DB_CONFIG['database']
|
||||
)
|
||||
|
||||
|
||||
if db_manager.test_connection(DB_CONFIG['database']):
|
||||
print("✓ 資料庫連線測試成功")
|
||||
return True
|
||||
@@ -111,7 +122,7 @@ def create_tables():
|
||||
print("\n" + "="*50)
|
||||
print("測試 4: 建立資料表")
|
||||
print("="*50)
|
||||
|
||||
|
||||
db_manager = DatabaseManager(
|
||||
host=DB_CONFIG['host'],
|
||||
port=DB_CONFIG['port'],
|
||||
@@ -119,13 +130,13 @@ def create_tables():
|
||||
password=DB_CONFIG['password'],
|
||||
database=DB_CONFIG['database']
|
||||
)
|
||||
|
||||
|
||||
sql_file = project_root / 'create_tables.sql'
|
||||
|
||||
|
||||
if not sql_file.exists():
|
||||
print(f"✗ SQL 檔案不存在: {sql_file}")
|
||||
return False
|
||||
|
||||
|
||||
if db_manager.execute_sql_file(str(sql_file), DB_CONFIG['database']):
|
||||
print("✓ 資料表建立成功")
|
||||
return True
|
||||
@@ -139,7 +150,7 @@ def verify_tables():
|
||||
print("\n" + "="*50)
|
||||
print("測試 5: 驗證資料表")
|
||||
print("="*50)
|
||||
|
||||
|
||||
db_manager = DatabaseManager(
|
||||
host=DB_CONFIG['host'],
|
||||
port=DB_CONFIG['port'],
|
||||
@@ -147,26 +158,26 @@ def verify_tables():
|
||||
password=DB_CONFIG['password'],
|
||||
database=DB_CONFIG['database']
|
||||
)
|
||||
|
||||
|
||||
expected_tables = ['articles', 'tags', 'article_tags']
|
||||
|
||||
|
||||
try:
|
||||
tables = db_manager.execute_query(
|
||||
"SHOW TABLES",
|
||||
database=DB_CONFIG['database']
|
||||
)
|
||||
|
||||
|
||||
# 取得資料表名稱列表
|
||||
table_names = [list(table.values())[0] for table in tables]
|
||||
|
||||
|
||||
print(f"找到 {len(table_names)} 個資料表: {', '.join(table_names)}")
|
||||
|
||||
|
||||
for table in expected_tables:
|
||||
if table in table_names:
|
||||
print(f"✓ 資料表 {table} 存在")
|
||||
else:
|
||||
print(f"✗ 資料表 {table} 不存在")
|
||||
|
||||
|
||||
return all(table in table_names for table in expected_tables)
|
||||
except Exception as e:
|
||||
print(f"✗ 驗證資料表失敗: {e}")
|
||||
@@ -178,27 +189,27 @@ def main():
|
||||
print("\n" + "="*60)
|
||||
print("HBR 爬蟲系統 - 資料庫連線測試")
|
||||
print("="*60)
|
||||
|
||||
|
||||
results = []
|
||||
|
||||
|
||||
# 執行測試
|
||||
results.append(("基本連線", test_basic_connection()))
|
||||
results.append(("建立資料庫", create_database()))
|
||||
results.append(("資料庫連線", test_database_connection()))
|
||||
results.append(("建立資料表", create_tables()))
|
||||
results.append(("驗證資料表", verify_tables()))
|
||||
|
||||
|
||||
# 顯示測試結果摘要
|
||||
print("\n" + "="*60)
|
||||
print("測試結果摘要")
|
||||
print("="*60)
|
||||
|
||||
|
||||
for test_name, result in results:
|
||||
status = "✓ 通過" if result else "✗ 失敗"
|
||||
print(f"{test_name}: {status}")
|
||||
|
||||
|
||||
all_passed = all(result for _, result in results)
|
||||
|
||||
|
||||
if all_passed:
|
||||
print("\n✓ 所有測試通過!資料庫設定完成。")
|
||||
return 0
|
||||
@@ -209,4 +220,3 @@ def main():
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user