yandex-cloud / python-sdk

Yandex.Cloud Python SDK
MIT License
75 stars 24 forks source link

ServiceAccount example is not working #101

Closed kuzaxak closed 1 month ago

kuzaxak commented 1 month ago

When I'm using SDK instance with service account directly I've got an error like:

Failed to ingest logs:
Getting metadata from plugin failed with error: 'str' object has no attribute 'unary_unary'
UNAVAILABLE
(14, 'unavailable')
with open(sa_key_path) as infile:
    sdk = SDK(interceptor=interceptor, service_account_key=json.load(infile))

But if I exchange it to IAM via dedicated function get_auth_token everything works fine. I believe it is a bug in SDK part or SA related auth.

with open(sa_key_path) as infile:
    token = get_auth_token(service_account_key=json.load(infile))
    sdk = SDK(interceptor=interceptor, iam_token=token)

Full code:

import json
import grpc
from yandexcloud import SDK, RetryInterceptor

from yandex.cloud.logging.v1.log_ingestion_service_pb2 import WriteRequest
from yandex.cloud.logging.v1.log_entry_pb2 import IncomingLogEntry, Destination
from yandex.cloud.logging.v1.log_ingestion_service_pb2_grpc import LogIngestionServiceStub
from google.protobuf.timestamp_pb2 import Timestamp
from google.protobuf.struct_pb2 import Struct

from yandexcloud.auth import get_auth_token

import logging
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)

# you can store and read it from JSON file
# Load service account key
sa_key_path = 'authorized_key.json'  # replace with your SA JSON key file path

interceptor = RetryInterceptor(max_retry_count=5, retriable_codes=[grpc.StatusCode.UNAVAILABLE])
with open(sa_key_path) as infile:
    token = get_auth_token(service_account_key=json.load(infile))
    sdk = SDK(interceptor=interceptor, iam_token=token)

logging_service = sdk.client(LogIngestionServiceStub)

s = Struct()
s.update({"key": "value"})

# Create log entry
timestamp = Timestamp()
timestamp.GetCurrentTime()
log_entry = IncomingLogEntry(
    timestamp=timestamp,
    message='Log error 2',
    json_payload=s
)

# Call Write method
try:
    response = logging_service.Write(
        WriteRequest(
            destination=Destination(log_group_id='e23....'),
            entries=[log_entry]
        )
    )
    print('Logs ingested successfully:', response)
except grpc.RpcError as e:
    print('Failed to ingest logs:')
    print(e.details())
    status_code = e.code()
    print(status_code.name)
    print(status_code.value)
saiks24 commented 1 month ago

Hi, kuzaxak! Thank you for the bug report. I've sent this information to the developers.

PeppaTheC commented 1 month ago

Hi, issue was resolved in v0.289.0 version. Please upgrade python SDK.