Update check_gitea.py
This commit is contained in:
@ -1,8 +1,20 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Gitea Repository Check Script
|
||||||
|
|
||||||
|
This script verifies access to a Gitea repository by:
|
||||||
|
1. Authenticating with provided credentials
|
||||||
|
2. Checking repository availability
|
||||||
|
3. Verifying file access
|
||||||
|
|
||||||
|
Example usage:
|
||||||
|
./check_gitea.py --server gitea.example.com --repo-owner owner --repo-name repo --username user --password pass --file path/to/file.txt
|
||||||
|
"""
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
|
from urllib.parse import quote
|
||||||
|
|
||||||
def debug_print(debug, message):
|
def debug_print(debug, message):
|
||||||
if debug:
|
if debug:
|
||||||
@ -10,28 +22,27 @@ def debug_print(debug, message):
|
|||||||
|
|
||||||
def check_gitea(server_address, repo_owner, repo_name, username, password, file_path, debug=False):
|
def check_gitea(server_address, repo_owner, repo_name, username, password, file_path, debug=False):
|
||||||
try:
|
try:
|
||||||
base_url = f"https://{server_address}"
|
file_path = file_path.lstrip('/')
|
||||||
|
base_url = f"https://{server_address}".rstrip('/')
|
||||||
session = requests.Session()
|
session = requests.Session()
|
||||||
|
|
||||||
# 1. Sprawdzenie dostępności serwera Gitea
|
debug_print(debug, "Connecting to Gitea server...")
|
||||||
debug_print(debug, "Próba połączenia z serwerem Gitea...")
|
|
||||||
try:
|
try:
|
||||||
response = session.get(base_url, timeout=10)
|
response = session.get(base_url, timeout=10)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
debug_print(debug, f"Serwer Gitea dostępny (HTTP {response.status_code})")
|
debug_print(debug, f"Gitea server available (HTTP {response.status_code})")
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
print(f"CRITICAL - Nie można połączyć się z serwerem Gitea: {str(e)}")
|
print(f"CRITICAL - Could not connect to Gitea server: {str(e)}")
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
# 2. Logowanie
|
|
||||||
login_url = f"{base_url}/user/login"
|
login_url = f"{base_url}/user/login"
|
||||||
debug_print(debug, f"Próba logowania na konto {username}...")
|
debug_print(debug, f"Attempting login for user {username}...")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = session.get(login_url)
|
response = session.get(login_url)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
print(f"CRITICAL - Nie można pobrać strony logowania: {str(e)}")
|
print(f"CRITICAL - Could not load login page: {str(e)}")
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
@ -39,11 +50,11 @@ def check_gitea(server_address, repo_owner, repo_name, username, password, file_
|
|||||||
csrf_token = soup.find('input', {'name': '_csrf'})
|
csrf_token = soup.find('input', {'name': '_csrf'})
|
||||||
|
|
||||||
if not csrf_token:
|
if not csrf_token:
|
||||||
print("CRITICAL - Nie można znaleźć tokena CSRF na stronie logowania")
|
print("CRITICAL - Could not find CSRF token on login page")
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
csrf_token = csrf_token['value']
|
csrf_token = csrf_token['value']
|
||||||
debug_print(debug, f"Znaleziono token CSRF: {csrf_token}")
|
debug_print(debug, f"Found CSRF token: {csrf_token}")
|
||||||
|
|
||||||
login_data = {
|
login_data = {
|
||||||
"user_name": username,
|
"user_name": username,
|
||||||
@ -55,70 +66,73 @@ def check_gitea(server_address, repo_owner, repo_name, username, password, file_
|
|||||||
response = session.post(login_url, data=login_data)
|
response = session.post(login_url, data=login_data)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
if "invalid username or password" in response.text.lower() or "nieprawidłowa nazwa użytkownika lub hasło" in response.text.lower():
|
if "invalid username or password" in response.text.lower():
|
||||||
print("CRITICAL - Błąd logowania do Gitea: nieprawidłowa nazwa użytkownika lub hasło")
|
print("CRITICAL - Login failed: invalid username or password")
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
debug_print(debug, "Logowanie zakończone sukcesem")
|
debug_print(debug, "Login successful")
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
print(f"CRITICAL - Błąd podczas logowania: {str(e)}")
|
print(f"CRITICAL - Login error: {str(e)}")
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
# 3. Sprawdzenie repozytorium
|
|
||||||
repo_url = f"{base_url}/{repo_owner}/{repo_name}"
|
repo_url = f"{base_url}/{repo_owner}/{repo_name}"
|
||||||
debug_print(debug, f"Sprawdzanie dostępności repozytorium {repo_owner}/{repo_name}...")
|
debug_print(debug, f"Checking repository {repo_owner}/{repo_name}...")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = session.get(repo_url)
|
response = session.get(repo_url)
|
||||||
if response.status_code == 404:
|
if response.status_code == 404:
|
||||||
print(f"CRITICAL - Repozytorium {repo_owner}/{repo_name} nie istnieje")
|
print(f"CRITICAL - Repository {repo_owner}/{repo_name} not found")
|
||||||
return 2
|
return 2
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
debug_print(debug, f"Repozytorium {repo_owner}/{repo_name} dostępne")
|
debug_print(debug, f"Repository {repo_owner}/{repo_name} is accessible")
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
print(f"CRITICAL - Problem z dostępem do repozytorium: {str(e)}")
|
print(f"CRITICAL - Repository access error: {str(e)}")
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
# 4. Sprawdzenie pliku w repozytorium - ZMIENIONA METODA DOSTĘPU
|
file_url = f"{base_url}/{repo_owner}/{repo_name}/raw/branch/master/{quote(file_path)}"
|
||||||
file_url = f"{base_url}/{repo_owner}/{repo_name}/raw/branch/master/{file_path}"
|
debug_print(debug, f"Attempting to read file: {file_path}")
|
||||||
debug_print(debug, f"Próba odczytu pliku {file_path} z URL: {file_url}...")
|
debug_print(debug, f"Full file URL: {file_url}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = session.get(file_url)
|
response = session.get(file_url)
|
||||||
debug_print(debug, f"Odpowiedź HTTP: {response.status_code}")
|
debug_print(debug, f"HTTP response: {response.status_code}")
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
# Dodatkowe sprawdzenie czy to na pewno plik
|
if response.text:
|
||||||
if response.text: # Jeśli odpowiedź zawiera treść
|
print(f"OK - Successfully read file {file_path} from {repo_owner}/{repo_name}")
|
||||||
print(f"OK - Pomyślnie odczytano plik {file_path} z repozytorium {repo_owner}/{repo_name}")
|
|
||||||
return 0
|
return 0
|
||||||
else:
|
else:
|
||||||
print(f"WARNING - Plik {file_path} istnieje ale jest pusty")
|
print(f"WARNING - File {file_path} exists but is empty")
|
||||||
return 1
|
return 1
|
||||||
elif response.status_code == 404:
|
elif response.status_code == 404:
|
||||||
print(f"WARNING - Plik {file_path} nie istnieje w repozytorium {repo_owner}/{repo_name}")
|
print(f"WARNING - File {file_path} not found in repository")
|
||||||
debug_print(debug, f"Pełna ścieżka: {file_url}")
|
|
||||||
return 1
|
return 1
|
||||||
else:
|
else:
|
||||||
print(f"CRITICAL - Nie można odczytać pliku {file_path} (HTTP {response.status_code})")
|
print(f"CRITICAL - File read error (HTTP {response.status_code})")
|
||||||
return 2
|
return 2
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
print(f"CRITICAL - Błąd podczas odczytu pliku: {str(e)}")
|
print(f"CRITICAL - File access error: {str(e)}")
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"CRITICAL - Wystąpił nieoczekiwany błąd: {str(e)}")
|
print(f"CRITICAL - Unexpected error: {str(e)}")
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(description='Skrypt do weryfikacji działania Gitea')
|
parser = argparse.ArgumentParser(
|
||||||
parser.add_argument('--server', required=True, help='Adres serwera Gitea (np. gitea.linuxiarz.pl)')
|
description='Verify Gitea repository and file access',
|
||||||
parser.add_argument('--repo-owner', required=True, help='Właściciel repozytorium')
|
epilog='Example:\n'
|
||||||
parser.add_argument('--repo-name', required=True, help='Nazwa repozytorium')
|
'./check_gitea.py --server gitea.example.com --repo-owner owner \\\n'
|
||||||
parser.add_argument('--username', required=True, help='Nazwa użytkownika')
|
'--repo-name repo --username user --password pass --file path/to/file.txt',
|
||||||
parser.add_argument('--password', required=True, help='Hasło użytkownika')
|
formatter_class=argparse.RawTextHelpFormatter
|
||||||
parser.add_argument('--file', required=True, help='Ścieżka do pliku do odczytu w repozytorium')
|
)
|
||||||
parser.add_argument('--debug', action='store_true', help='Wyświetl szczegółowe informacje debugowe')
|
parser.add_argument('--server', required=True, help='Gitea server address (e.g., gitea.example.com)')
|
||||||
|
parser.add_argument('--repo-owner', required=True, help='Repository owner username')
|
||||||
|
parser.add_argument('--repo-name', required=True, help='Repository name')
|
||||||
|
parser.add_argument('--username', required=True, help='Login username')
|
||||||
|
parser.add_argument('--password', required=True, help='Login password')
|
||||||
|
parser.add_argument('--file', required=True, help='File path in repository')
|
||||||
|
parser.add_argument('--debug', action='store_true', help='Enable debug output')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
@ -131,6 +145,3 @@ if __name__ == "__main__":
|
|||||||
args.file,
|
args.file,
|
||||||
args.debug
|
args.debug
|
||||||
))
|
))
|
||||||
|
|
||||||
# Przykładowa komenda:
|
|
||||||
# ./check_gitea.py --server gitea.domena.pl --repo-owner testuser --repo-name testrepo --username testuser --password testpass --file README.md --debug
|
|
Reference in New Issue
Block a user