Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/aleph_client/commands/instance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
fetch_crn_info,
fetch_vm_info,
find_crn_of_vm,
sanitize_url,
)
from aleph_client.commands.instance.superfluid import FlowUpdate, update_flow
from aleph_client.commands.node import NodeInfo, _fetch_nodes
Expand All @@ -67,7 +66,7 @@
wait_for_processed_instance,
)
from aleph_client.models import CRNInfo
from aleph_client.utils import AsyncTyper
from aleph_client.utils import AsyncTyper, sanitize_url

logger = logging.getLogger(__name__)
app = AsyncTyper(no_args_is_help=True)
Expand Down
40 changes: 1 addition & 39 deletions src/aleph_client/commands/instance/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from ipaddress import IPv6Interface
from json import JSONDecodeError
from typing import Optional
from urllib.parse import ParseResult, urlparse

import aiohttp
from aleph.sdk import AlephHttpClient
Expand All @@ -18,52 +17,15 @@
from aleph_client.commands.node import NodeInfo, _fetch_nodes
from aleph_client.commands.utils import safe_getattr
from aleph_client.models import MachineUsage
from aleph_client.utils import fetch_json
from aleph_client.utils import AsyncTyper, fetch_json, sanitize_url

logger = logging.getLogger(__name__)

# Some users had fun adding URLs that are obviously not CRNs.
# If you work for one of these companies, please send a large check to the Aleph team,
# and we may consider removing your domain from the blacklist. Or just use a subdomain.
FORBIDDEN_HOSTS = [
"amazon.com",
"apple.com",
"facebook.com",
"google.com",
"google.es",
"microsoft.com",
"openai.com",
"twitter.com",
"x.com",
"youtube.com",
]

PATH_STATUS_CONFIG = "/status/config"
PATH_ABOUT_USAGE_SYSTEM = "/about/usage/system"


def sanitize_url(url: str) -> str:
"""Ensure that the URL is valid and not obviously irrelevant.

Args:
url: URL to sanitize.
Returns:
Sanitized URL.
"""
if not url:
raise aiohttp.InvalidURL("Empty URL")
parsed_url: ParseResult = urlparse(url)
if parsed_url.scheme not in ["http", "https"]:
raise aiohttp.InvalidURL(f"Invalid URL scheme: {parsed_url.scheme}")
if parsed_url.hostname in FORBIDDEN_HOSTS:
logger.debug(
f"Invalid URL {url} hostname {parsed_url.hostname} is in the forbidden host list "
f"({', '.join(FORBIDDEN_HOSTS)})"
)
raise aiohttp.InvalidURL("Invalid URL host")
return url


async def fetch_crn_info(node_url: str) -> dict | None:
"""
Fetches compute node usage information and version.
Expand Down
64 changes: 61 additions & 3 deletions src/aleph_client/commands/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@

import json
import logging
import sys
from base64 import b16decode, b32encode
from collections.abc import Mapping
from pathlib import Path
from typing import List, Optional
from typing import Any, List, Optional, Tuple
from zipfile import BadZipFile

import typer
from aiohttp import ClientResponse
from aiohttp.client import _RequestContextManager
from aleph.sdk import AuthenticatedAlephHttpClient
from aleph.sdk.account import _load_account
from aleph.sdk.client.vm_client import VmClient
from aleph.sdk.conf import settings
from aleph.sdk.types import AccountFromPrivateKey, StorageEnum
from aleph_message.models import ProgramMessage, StoreMessage
from aleph_message.models import Chain, ProgramMessage, StoreMessage
from aleph_message.models.execution.program import ProgramContent
from aleph_message.models.item_hash import ItemHash
from aleph_message.status import MessageStatus
from click import echo

from aleph_client.commands import help_strings
from aleph_client.commands.utils import (
Expand All @@ -25,7 +30,7 @@
setup_logging,
yes_no_input,
)
from aleph_client.utils import AsyncTyper, create_archive
from aleph_client.utils import AsyncTyper, create_archive, sanitize_url

logger = logging.getLogger(__name__)
app = AsyncTyper(no_args_is_help=True)
Expand Down Expand Up @@ -236,3 +241,56 @@
channel=message.channel,
)
typer.echo(f"{message.json(indent=4)}")


@app.command()
async def logs(
item_hash: str = typer.Argument(..., help="Item hash of program"),
private_key: Optional[str] = settings.PRIVATE_KEY_STRING,
private_key_file: Optional[Path] = settings.PRIVATE_KEY_FILE,
domain: str = typer.Option(None, help="CRN domain on which the VM is stored or running"),
chain: Chain = typer.Option(None, help=help_strings.ADDRESS_CHAIN),
debug: bool = False,
):
"""Display logs for the program.
Will only show logs frp, one select CRN"""

setup_logging(debug)

Check warning on line 259 in src/aleph_client/commands/program.py

View check run for this annotation

Codecov / codecov/patch

src/aleph_client/commands/program.py#L259

Added line #L259 was not covered by tests

account = _load_account(private_key, private_key_file, chain=chain)
domain = sanitize_url(domain)

Check warning on line 262 in src/aleph_client/commands/program.py

View check run for this annotation

Codecov / codecov/patch

src/aleph_client/commands/program.py#L261-L262

Added lines #L261 - L262 were not covered by tests

async with VmClient2(account, domain) as client:
async with await client.operate(vm_id=item_hash, operation="logs.json", method="GET") as response:

Check warning on line 265 in src/aleph_client/commands/program.py

View check run for this annotation

Codecov / codecov/patch

src/aleph_client/commands/program.py#L264-L265

Added lines #L264 - L265 were not covered by tests

logger.debug("Request %s %s", response.url, response.status)

Check warning on line 267 in src/aleph_client/commands/program.py

View check run for this annotation

Codecov / codecov/patch

src/aleph_client/commands/program.py#L267

Added line #L267 was not covered by tests
if response.status != 200:
logger.debug(response)
logger.debug(await response.text())

Check warning on line 270 in src/aleph_client/commands/program.py

View check run for this annotation

Codecov / codecov/patch

src/aleph_client/commands/program.py#L269-L270

Added lines #L269 - L270 were not covered by tests

if response.status == 404:
echo(f"Execution not found on this server")
return 1

Check warning on line 274 in src/aleph_client/commands/program.py

View check run for this annotation

Codecov / codecov/patch

src/aleph_client/commands/program.py#L273-L274

Added lines #L273 - L274 were not covered by tests
elif response.status == 403:

echo(f"You are not the owner of this VM. Maybe try with another wallet?")

Check warning on line 277 in src/aleph_client/commands/program.py

View check run for this annotation

Codecov / codecov/patch

src/aleph_client/commands/program.py#L277

Added line #L277 was not covered by tests

return 1

Check warning on line 279 in src/aleph_client/commands/program.py

View check run for this annotation

Codecov / codecov/patch

src/aleph_client/commands/program.py#L279

Added line #L279 was not covered by tests
elif response.status != 200:
echo(f"Server error: {response.status}. Please try again latter")
return 1
echo("Received logs")
log_entries = await response.json()

Check warning on line 284 in src/aleph_client/commands/program.py

View check run for this annotation

Codecov / codecov/patch

src/aleph_client/commands/program.py#L281-L284

Added lines #L281 - L284 were not covered by tests
for log in log_entries:
echo(f'{log["__REALTIME_TIMESTAMP"]}> {log["MESSAGE"]}')

Check warning on line 286 in src/aleph_client/commands/program.py

View check run for this annotation

Codecov / codecov/patch

src/aleph_client/commands/program.py#L286

Added line #L286 was not covered by tests


class VmClient2(VmClient):
async def operate(self, vm_id: ItemHash, operation: str, method: str = "POST") -> _RequestContextManager:
if not self.pubkey_signature_header:
self.pubkey_signature_header = await self._generate_pubkey_signature_header()

Check warning on line 292 in src/aleph_client/commands/program.py

View check run for this annotation

Codecov / codecov/patch

src/aleph_client/commands/program.py#L292

Added line #L292 was not covered by tests

url, header = await self._generate_header(vm_id=vm_id, operation=operation, method=method)

Check warning on line 294 in src/aleph_client/commands/program.py

View check run for this annotation

Codecov / codecov/patch

src/aleph_client/commands/program.py#L294

Added line #L294 was not covered by tests

return self.session.request(method=method, url=url, headers=header)

Check warning on line 296 in src/aleph_client/commands/program.py

View check run for this annotation

Codecov / codecov/patch

src/aleph_client/commands/program.py#L296

Added line #L296 was not covered by tests
43 changes: 43 additions & 0 deletions src/aleph_client/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import asyncio
import inspect
import logging
Expand All @@ -7,8 +9,10 @@
from pathlib import Path
from shutil import make_archive
from typing import List, Optional, Tuple, Type, Union
from urllib.parse import ParseResult, urlparse
from zipfile import BadZipFile, ZipFile

import aiohttp
import typer
from aiohttp import ClientSession
from aleph.sdk.conf import MainConfiguration, load_main_configuration, settings
Expand Down Expand Up @@ -130,3 +134,42 @@ async def list_unlinked_keys() -> Tuple[List[Path], Optional[MainConfiguration]]
unlinked_keys: List[Path] = [key_file for key_file in all_private_key_files if key_file != active_key_path]

return unlinked_keys, config


# Some users had fun adding URLs that are obviously not CRNs.
# If you work for one of these companies, please send a large check to the Aleph team,
# and we may consider removing your domain from the blacklist. Or just use a subdomain.
FORBIDDEN_HOSTS = [
"amazon.com",
"apple.com",
"facebook.com",
"google.com",
"google.es",
"microsoft.com",
"openai.com",
"twitter.com",
"x.com",
"youtube.com",
]


def sanitize_url(url: str) -> str:
"""Ensure that the URL is valid and not obviously irrelevant.

Args:
url: URL to sanitize.
Returns:
Sanitized URL.
"""
if not url:
raise aiohttp.InvalidURL("Empty URL")
parsed_url: ParseResult = urlparse(url)
if parsed_url.scheme not in ["http", "https"]:
raise aiohttp.InvalidURL(f"Invalid URL scheme: {parsed_url.scheme}")
if parsed_url.hostname in FORBIDDEN_HOSTS:
logger.debug(
f"Invalid URL {url} hostname {parsed_url.hostname} is in the forbidden host list "
f"({', '.join(FORBIDDEN_HOSTS)})"
)
raise aiohttp.InvalidURL("Invalid URL host")
return url
9 changes: 2 additions & 7 deletions tests/unit/test_instance.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from __future__ import annotations

from datetime import datetime, timezone
from decimal import Decimal
from typing import cast
from unittest.mock import AsyncMock, MagicMock, patch

import pytest
Expand All @@ -15,11 +13,7 @@
from multidict import CIMultiDict, CIMultiDictProxy

from aleph_client.commands.instance import delete
from aleph_client.commands.instance.network import (
FORBIDDEN_HOSTS,
fetch_crn_info,
sanitize_url,
)
from aleph_client.commands.instance.network import fetch_crn_info
from aleph_client.models import (
CoreFrequencies,
CpuUsage,
Expand All @@ -31,6 +25,7 @@
MemoryUsage,
UsagePeriod,
)
from aleph_client.utils import FORBIDDEN_HOSTS, sanitize_url


def dummy_machine_info() -> MachineInfo:
Expand Down
Loading