How to list/create a domain using designateclient? - python

I've been trying to use the designate client to create DNS entries, but even before creating them, I tried listing all the entries.
Started with listing zones first and it doesn't seem to work.
#!/usr/bin/env python3
import os
import json
from designateclient.v2 import client
from keystoneauth1.identity import generic
from keystoneauth1 import session as keystone_session
auth = generic.Password(
auth_url='url',
username='username',
password=os.environ['password'],
project_name='domain name',
project_domain_id='default',
user_domain_id='default')
session = keystone_session.Session(auth=auth)
client = client.Client(session=session)
print(client.zones.list())
Any help how I could get this data? Thank you in advance :)

So I got over the issue of authentication, by using the following code:
import os
from keystoneclient.auth.identity import v3
from keystoneclient import session
from keystoneclient.v3 import client
from designateclient.v2 import client as d_client
v3_auth = v3.Password(auth_url='auth_url',
username='username',
password=os.environ['pass'],
project_name='project_name',
project_domain_name="project_domain_name",
user_domain_name="user_domain_name")
v3_ses = session.Session(auth=v3_auth)
auth_token = v3_ses.get_token()
session = session.Session(auth=v3_auth, timeout=10)
desig_client = d_client.Client(session=session)
print(desig_client)
list_of_zones = desig_client.zones.list()
The next question is how do I create a new domain?

Related

Making asynchronous requests to a Vertex AI endpoint (Google cloud platform)

I deployed a model to the model registry on Vertex AI. I added an endpoint too, and I am able to make inferences. Below is the code that I wrote (using Python 3.9.12):
from google.cloud import aiplatform
from google.oauth2 import service_account
# settings is a Pydantic BaseSettings subclass object
credentials_json = json.loads(settings.GCP_VERTEX_SERVICE_ACC)
credentials = service_account.Credentials.from_service_account_info(
info=credentials_json
)
aiplatform.init(project=settings.GCLOUD_PROJECT_NUMBER,
location=settings.GCLOUD_LOCATION,
credentials=credentials)
endpoint = aiplatform.Endpoint(settings.GCLOUD_SBERT_ENDPOINT_ID)
...
async def do_inference(list_strs: List[str]):
result = endpoint.predict(instances=list_strs)
return result.predictions
Right now I'm not able to make asynchronous requests. Is there a way around this? For instance, would using the aiplatform_v1beta1.PredictionServiceAsyncClient library be a solution? Thanks in advance!
---- EDIT -----
Below is the piece of code that did it for me in case someone else is struggling with the same thing.
import asyncio
from google.cloud import aiplatform_v1beta1
from google.oauth2 import service_account
from google.protobuf import json_format
from google.protobuf.struct_pb2 import Value
# settings is a Pydantic BaseSettings subclass object
credentials_json = json.loads(settings.GCP_VERTEX_SERVICE_ACC)
credentials = service_account.Credentials.from_service_account_info(
info=credentials_json
)
client_options = {"api_endpoint": f"{settings.GCLOUD_LOCATION}-aiplatform.googleapis.com"}
client = aiplatform_v1beta1.PredictionServiceAsyncClient(credentials=credentials, client_options=client_options)
...
async def do_inference(list_strs: List[str]):
request = aiplatform_v1beta1.PredictRequest(endpoint=endpoint)
request.instances.extend(list_strs)
response = await client.predict(request)
predictions = response.predictions
return predictions
asyncio.get_event_loop().run_until_complete(do_inference())
This code owes a lot to #milad_raesi's answer!

Bad credentials from Astra DB connect

Currently I am learning about Astra DB from this youtube link
https://www.youtube.com/watch?v=NyDT3KkscSk&t=2439s
I manage to download the connection.zip file from Astra DB and generated admin token keys. But when I try to do connection such as:
from app.crud import create_entry
I will get this error:
raise NoHostAvailable("Unable to connect to any servers", errors)
cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'98db9cb2-907a-4f9d-a935-69b69fb2157f-asia-south1.db.astra.datastax.com:29042:611a8370-f129-4099-84e2-c3b2f426ebdc': AuthenticationFailed('Failed to authenticate to 98db9cb2-907a-4f9d-a935-69b69fb2157f-asia-south1.db.astra.datastax.com:29042:611a8370-f129-4099-84e2-c3b2f426ebdc: Error from server: code=0100 [Bad credentials] message="We recently improved your database security. To find out more and reconnect, see https://docs.datastax.com/en/astra/docs/manage-application-tokens.html"'), '98db9cb2-907a-4f9d-a935-69b69fb2157f-asia-south1.db.astra.datastax.com:29042:040ab116-8c77-4eb4-a357-c9bdcbb637d4': AuthenticationFailed('Failed to authenticate to 98db9cb2-907a-4f9d-a935-69b69fb2157f-asia-south1.db.astra.datastax.com:29042:040ab116-8c77-4eb4-a357-c9bdcbb637d4: Error from server: code=0100 [Bad credentials] message="We recently improved your database security. To find out more and reconnect, see https://docs.datastax.com/en/astra/docs/manage-application-tokens.html"'), '98db9cb2-907a-4f9d-a935-69b69fb2157f-asia-south1.db.astra.datastax.com:29042:536e6e99-ef4e-47d0-9308-b0c6cdf4aa37': AuthenticationFailed('Failed to authenticate to 98db9cb2-907a-4f9d-a935-69b69fb2157f-asia-south1.db.astra.datastax.com:29042:536e6e99-ef4e-47d0-9308-b0c6cdf4aa37: Error from server: code=0100 [Bad credentials] message="We recently improved your database security. To find out more and reconnect, see https://docs.datastax.com/en/astra/docs/manage-application-tokens.html"')})
Here is my db.py:
import os
import pathlib
from dotenv import load_dotenv
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
from cassandra.cqlengine.connection import register_connection, set_default_connection
BASE_DIR = pathlib.Path(__file__).parent
CLUSTER_BUNDLE = BASE_DIR / 'ignored'/ 'connect.zip'
load_dotenv()
astra_db_client_id = os.environ.get('ASTRA_DB_CLIENT_ID')
astra_db_client_secret = os.environ.get('ASTRA_DB_CLIENT_SECRET')
def get_cluster():
cloud_config= {
'secure_connect_bundle': CLUSTER_BUNDLE
}
auth_provider = PlainTextAuthProvider(astra_db_client_id, astra_db_client_secret)
cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider, control_connection_timeout=30,
connect_timeout=30)
return cluster
def get_session():
cluster = get_cluster()
session = cluster.connect()
register_connection(str(session), session=session)
set_default_connection(str(session))
return session
# session = get_session()
# row = session.execute("select release_version from system.local").one()
# if row:
# print(row[0])
# else:
# print("An error occurred.")
I tried to recreate the token key many times and re download the drivers as well but I still have no luck in passing the bad credential errors here:
my crud.py
from .db import get_session
from .models import Product
from cassandra.cqlengine.management import sync_table
session = get_session()
sync_table(Product)
def create_entry(data:dict):
return Product.create(**data)
models.py
from cassandra.cqlengine import columns
from cassandra.cqlengine.models import Model
class Product(Model): # -> table
__keyspace__ = "testing" #
asin = columns.Text(primary_key=True, required=True)
title = columns.Text()
You might want to take a look at this
https://docs.datastax.com/en/astra/docs/docs/using-the-datastax-python-driver-to-connect-to-your-database.html
Specifically the section here:
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
cloud_config= {
'secure_connect_bundle': '/path/to/secure-connect-database_name.zip'
}
auth_provider = PlainTextAuthProvider('username', 'password')
cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider)
session = cluster.connect()
When creating the connection you’ll want to pass the secure connect bundle zip. You’ll then provide the clientId and clientSecret as the username and password from the connections file you downloaded.

403 Forbidden - cloud_storage_bucket get_media

import json
from httplib2 import Http
from oauth2client.client import SignedJwtAssertionCredentials
from googleapiclient.discovery import build
json_file = 'my.json'
client_email = json.loads(open(json_file).read())['client_email']
private_key = json.loads(open(json_file).read())['private_key']
cloud_storage_bucket = 'my_bucket'
report_to_download = 'sales/salesreport_201907.zip'
credentials = SignedJwtAssertionCredentials(client_email, private_key,['https://www.googleapis.com/auth/devstorage.read_only','https://www.googleapis.com/auth/devstorage.read_write','https://www.googleapis.com/auth/devstorage.full_control'])
storage = build('storage', 'v1', http=credentials.authorize(Http()))
object_metadata = storage.objects().get(bucket = cloud_storage_bucket, object = report_to_download).execute()
object_content = storage.objects().get_media(bucket = cloud_storage_bucket, object = report_to_download).execute()
I can read the object_metadata, but when requesting object content I get:
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/storage/v1/b/my_bucket/o/sales%2Fsalesreport_201907.zip?alt=media returned "Forbidden">
As u can see I added the three scopes in the credentials sections just to ensure to have all the permissions. Despite that I still getting the error.
I am using Python 2.7.
Edit:
I tried to download the file using gsutil and it works. So it isn't related with permissions.
Edit 2:
Changed the code to use a non deprecated library.
from logs import logger
import google.auth
import google.auth.transport.requests as tr_requests
from google.resumable_media.requests import Download
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="my.json"
ro_scope=u'https://www.googleapis.com/auth/devstorage.full_control'
credentials,_ = google.auth.default(scopes=(ro_scope,))
transport = tr_requests.AuthorizedSession(credentials)
cloud_storage_bucket = 'pubsite_prod_rev_xxxx'
report_to_download = 'sales/salesreport_201907.zip'
#option 1
media_url='https://www.googleapis.com/download/storage/v1/b/pubsite_prod_rev_xxxx/o/sales%2Fsalesreport_201907.zip?generation=1234&alt=media'
#option 2
media_url='https://www.googleapis.com/storage/v1/b/pubsite_prod_rev_xxxx/o/sales%2Fsalesreport_201907.zip'
download = Download(media_url)
response = download.consume(transport)
print download.finished
When running the code with option 1 url the status is 200 but the response.content is just the metadata, has happened before with the get method.
When running the option 2, like get_media method, the error stills 403.
File "/anaconda2/envs/enviro27/lib/python2.7/site-packages/google/resumable_media/_helpers.py", line 93, in require_status_code
status_code, u'Expected one of', *status_codes)
google.resumable_media.common.InvalidResponse: (u'Request failed with status code', 403, u'Expected one of', 200, 206)
The doc of this code.
The solution has been to discover the true admin account.
Despite Google Play shows contact information to one email (the one that we believe to be the admin), the real admin is anothe account. After find it, we applied all the process explained here.
And with this code we are able to access the content:
from logs import logger
import google.auth
import google.auth.transport.requests as tr_requests
from google.resumable_media.requests import Download
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="my.json"
ro_scope=u'https://www.googleapis.com/auth/devstorage.full_control'
credentials,_ = google.auth.default(scopes=(ro_scope,))
transport = tr_requests.AuthorizedSession(credentials)
cloud_storage_bucket = 'pubsite_prod_rev_xxxx'
report_to_download = 'sales/salesreport_201907.zip'
#option 1
media_url='https://www.googleapis.com/download/storage/v1/b/pubsite_prod_rev_xxxx/o/sales%2Fsalesreport_201907.zip?generation=1234&alt=media'
#option 2
media_url='https://www.googleapis.com/storage/v1/b/pubsite_prod_rev_xxxx/o/sales%2Fsalesreport_201907.zip'
download = Download(media_url)
response = download.consume(transport)
print download.finished

How to get secret values from paged.SecretItemPaged type object

I would like to get the Secret value from given SecretID using Python API's.
I have folowing code. On the last line I am trying to get the SECRET_VERSION using get_secret_versions function. The function returns SecretItemPaged type of Object. Can someone please help to to retrieve secret_version from this objet.
#!/usr/bin/python
from azure.keyvault import KeyVaultClient
from azure.common.credentials import ServicePrincipalCredentials
import pprint
#from msrestazure.azure_active_directory import
ServicePrincipalCredentials
credentials = ServicePrincipalCredentials(
client_id = 'XXX',
secret = 'XXX',
tenant = 'XXX'
)
client = KeyVaultClient(credentials)
VAULT_URL='https://xxxxxxx.vault.azure.net'
SECRET_ID='https://xxxxxxxx.vault.azure.net/secrets/2345mat'
SECRET_VERSION= client.get_secret_versions(VAULT_URL , SECRET_ID)
SecretItemPaged is an iterator of objects, following the Python protocol for this:
https://docs.python.org/3/glossary.html#term-iterable
https://docs.python.org/3/glossary.html#term-iterator
That being said, you can then use a list to consume it, or a next call until you get a StopIteration object, etc.
Simplest way would be to consume it as a list:
list(client.get_secret_versions(VAULT_URL , SECRET_ID))

Python FIX API - no response at Logon

I've been trying to set up a connection to the FIX API for the GDAX crpyto exchange, but I can't seem to logon properly. I'm using the code below to generate the message:
import time
import simplefix
import socket
import base64
import hmac
import hashlib
from datetime import datetime
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 4197))
API_KEY = "KEY_GOES_HERE"
PASSPHRASE = "PASSPHRASE_GOES_HERE"
API_SECRET = "SECRET_GOES_HERE"
seq_num = "1"
sendingTime = str(datetime.utcnow()).replace("-","").replace(" ", "-")[:-3]
rawSig = "\x01".join([sendingTime, "A", seq_num, API_KEY, "Coinbase", PASSPHRASE]).encode("utf-8")
hmac_key = base64.b64decode(API_SECRET)
signature = hmac.new(hmac_key, rawSig, hashlib.sha256)
sign_b64 = base64.b64encode(signature.digest()).decode()
msg = simplefix.FixMessage()
msg.append_pair(8, "FIX.4.2")
msg.append_pair(35, "A")
msg.append_pair(49, API_KEY)
msg.append_pair(52, sendingTime)
msg.append_pair(56, "Coinbase")
msg.append_pair(98, "0")
msg.append_pair(108, "30")
msg.append_pair(554, PASSPHRASE)
msg.append_pair(96, sign_b64)
msg.append_pair(8013, "Y")
print(msg.encode())
s.sendall(msg.encode("ascii"))
print(s.recv(4096))
And I'm getting 0 bytes of response from the server. As far as I can tell, the stunnel is working properly (connects and validates certificates successfully, but disconnects after sending my logon message).
Have just tried with a newly generated API key, passphrase and secret but to no avail.
For reference, I was working from the question asked here: How to send FIX logon message with Python to GDAX but I'm not allowed to comment there.
If anyone has any ideas, would be appreciated. Below is an example of the fix message generated, passwords removed in post:
8=FIX.4.2\x019=161\x0135=A\x0149=[KEY_REMOVED]\x0152=20180113-18:24:07.889\x0156=Coinbase\x0198=0\x01108=30\x01554=[PASSPHRASE_REMOVED]\x0196=jueSJHoSNQM2BOCN3KM0mgB2/9tXpICbg4amqDKc2wY=\x018013=Y\x0110=053\x01

Categories

Resources