Script to update firewall rules in aviatrix - python

Hi I'm trying to write a script that will automate updating firewall rules in aviatrix via apicall. currently I run curl from my admin server to achieve the goal
curl --location --request POST 'https://{{controller_hostname}}/v1/api' \
--form 'action=update_stateful_firewall_rules' \
--form 'CID={{CID}}' \
--form 'gateway_name=my-gw' \
--form 'rules=[
{
"s_ip": "1.1.1.1/32",
"d_ip": "2.2.2.2/32",
"protocol": "icmp",
"port": "",
"deny_allow": "deny",
"log_enable": "off"
},
]
'
So far i got this
import requests as req
URL = 'https://23.20.97.236/v1/api'
payload = {
'action':'list_gateway_security_policy_rules',
'CID':'',
'gateway_name':'egress-citadelvpc-prod',
}
response = req.get(url=URL, data=payload, verify=false)
print (response.json())
The catch is the source remains the same but the destination changes so it needs to be updated. The output should be please enter d_ip: when i run the script and after entering the destination it will update it for me stating The rule has been added
Please help me produce this script!!

Related

prgrammatically create CouchDB cluster fails

I am trying to programmatically set up a cluster. For that I have followed the documentation (here https://docs.couchdb.org/en/stable/setup/cluster.html#the-cluster-setup-api), and created a quick python script that does it for me.
I then get the following output: {'error': 'setup_error', 'reason': 'Cluster setup unable to sync admin passwords'}. So I decided to try the Fauxton interface (which works) and see what goes over the line.
Made some changes to the python script to match exactly what Fauxton sends to the cluster... But were the manual interface (through Fauxton) works, I still get this "unable to sync" error when replicating what happens under the hood... This is driving me crazy now...
Using the following start script:
for i in {1..3}; \
do echo "$i"5984:5984; \
docker run -d --rm -p "$i"5984:5984 \
-e NODENAME=box0$i.couch -e ERL_FLAGS='-setcookie "brumbrum"' \
-e COUCHDB_USER=admin -e COUCHDB_PASSWORD=admin \
--name box0$i --network couch \
couchdb:3.1.1; \
done
which spins up three fresh docker containers using the 3.1.1 version of couchdb.
The script I use to clusterize you can find here: https://github.com/wasperen/couchdb_clusterify/blob/main/clusterize.py
Thanks for any help. But anyway: happy new year!
Just followed the documentation step by step, with the same result:
root#75c127f35ad6:~# curl -X POST -H "Content-Type: application/json" http://admin:admin#box01.couch:5984/_cluster_setup -d '{"action": "enable_cluster", "bind_address":"0.0.0.0", "username": "admin", "password":"admin", "node_count":"3"}'
{"error":"bad_request","reason":"Cluster is already enabled"}
root#75c127f35ad6:~# curl -X POST -H "Content-Type: application/json" http://admin:admin#box01.couch:5984/_cluster_setup -d '{"action": "enable_cluster", "bind_address":"0.0.0.0", "username": "admin", "password":"admin", "port": 5984, "node_count": "3", "remote_node": "box02.couch", "remote_current_user": "admin", "remote_current_password": "admin" }'
{"ok":true}
root#75c127f35ad6:~# curl -X POST -H "Content-Type: application/json" http://admin:admin#box01.couch:5984/_cluster_setup -d '{"action": "add_node", "host":"box02.couch", "port": 5984, "username": "admin", "password":"admin"}'
{"ok":true}
root#75c127f35ad6:~# curl -X POST -H "Content-Type: application/json" http://admin:admin#box01.couch:5984/_cluster_setup -d '{"action": "enable_cluster", "bind_address":"0.0.0.0", "username": "admin", "password":"admin", "port": 5984, "node_count": "3", "remote_node": "box03.couch", "remote_current_user": "admin", "remote_current_password": "admin" }'
{"ok":true}
root#75c127f35ad6:~# curl -X POST -H "Content-Type: application/json" http://admin:admin#box01.couch:5984/_cluster_setup -d '{"action": "add_node", "host":"box03.couch", "port": 5984, "username": "admin", "password":"admin"}'
{"ok":true}
root#75c127f35ad6:~# curl -X POST -H "Content-Type: application/json" http://admin:admin#box01.couch:5984/_cluster_setup -d '{"action": "finish_cluster"}'
{"error":"setup_error","reason":"Cluster setup unable to sync admin passwords"}
And this from the browser logs, using the Fauxton user interface (on box01):
POST http://localhost:15984//_cluster_setup
DATA {"action":"enable_cluster","username":"admin","password":"admin","bind_address":"0.0.0.0","port":5984,"node_count":3,"singlenode":false}
RESPONSE 400 {"error":"bad_request","reason":"Cluster is already enabled"}
POST http://localhost:15984//_cluster_setup
DATA {"action":"enable_cluster","username":"admin","password":"admin","bind_address":"0.0.0.0","port":5984,"node_count":3,"remote_node":"box02.couch","remote_current_user":"admin","remote_current_password":"admin"}
RESPONSE 201 {"ok":true}
POST http://localhost:15984//_cluster_setup
DATA {"action":"add_node","username":"admin","password":"admin","host":"box02.couch","port":5984,"singlenode":false}
RESPONSE 201 {"ok":true}
POST http://localhost:15984//_cluster_setup
DATA {"action":"enable_cluster","username":"admin","password":"admin","bind_address":"0.0.0.0","port":5984,"node_count":3,"remote_node":"box03.couch","remote_current_user":"admin","remote_current_password":"admin"}
RESPONSE 201 {"ok":true}
POST http://localhost:15984//_cluster_setup
DATA {"action":"add_node","username":"admin","password":"admin","host":"box03.couch","port":5984,"singlenode":false}
RESPONSE 201 {"ok":true}
POST http://localhost:15984//_cluster_setup
DATA {"action":"finish_cluster"}
RESPONSE 201 {"ok":true}
GET http://localhost:15984/_membership
RESPONSE 200 {"all_nodes":["couchdb#box01.couch","couchdb#box02.couch","couchdb#box03.couch"],"cluster_nodes":["couchdb#box01.couch","couchdb#box02.couch","couchdb#box03.couch"]}
I think there might be a bug in CouchDB 3.x where it does not accept the finish_cluster action when basic authentication is being used. Seems like it works perfectly fine if you get a session cookie first through the _session endpoint and then use that for authentication.
I checked your code out, it looks all good seems like you are just hitting this issue here: https://github.com/apache/couchdb/issues/2858. It can get worked around with a GET request to the cluster coordinator node.

Forming a request.post from a curl on python

I have a small project i need a tempmail for, so i found this site https://mail.tm/en, so i was trying to acess the post via the api and instructions given here https://api.mail.tm/.
As I am new at working with curl and so on I have a lot of problems authorizing into an account there.
First of all i got an CURL link on site like this one:
curl -X POST "https://api.mail.tm/accounts" -H "accept: application/ld+json" -H "Authorization: testing" -H "Content-Type: application/ld+json" -d "{\"address\":\"test\",\"password\":\"stackoverflow\"}"
I tried to form it all in a post request, so i did this
headers = {"accept": "application/ld+json", "Authorization": "header", "Content-Type": "application/ld+json"}
data = "{\"address\":\"zashyganii\",\"password\":\"chertila\"}"
mail = requests.post("https://api.mail.tm/accounts", data = data, headers = headers)
print(mail.status_code)
the error code is 400, noted on the api site like this.
400 Error:
Response body
Download
{
"#context": "/contexts/ConstraintViolationList",
"#type": "ConstraintViolationList",
"hydra:title": "An error occurred",
"hydra:description": "address: This value is not a valid email address.\naddress: The domain \"\" is not valid.",
"violations": [
{
"propertyPath": "address",
"message": "This value is not a valid email address."
},
{
"propertyPath": "address",
"message": "The domain \"\" is not valid."
}
]
}
Could you please turn this curl in to a request.post on python so it will work for this api
Seems like there are not any problem with the request. It's a validation fail.

How to execute the following command using Python?

It would be a great help if somebody helps me to understand how the following command can be executed using python:
curl -X POST https://insights-collector.newrelic.com/v1/accounts/YOUR_ACCOUNT_ID/events -H "Content-Type: application/json" -H "X-Insert-Key: YOUR_KEY_HERE" -d '{"eventType":"Custom Event Name", "attribute1": "value"}'
SQL query results need to be converted to JSON format and need to be pushed to new relic using the above command.
try doing this
import requests
headers = {
'Content-Type': 'application/json',
'X-Insert-Key': 'YOUR_KEY_HERE',
}
data = '{"eventType":"Custom Event Name", "attribute1": "value"}'
response = requests.post('https://insights-collector.newrelic.com/v1/accounts/YOUR_ACCOUNT_ID/events', headers=headers, data=data)

How to Translate CURL to Python Requests

I am currently trying to integrate Stripe Connect and have come across the flowing CURl POST request:
curl https://connect.stripe.com/oauth/token \
-d client_secret=SECRET_CODE \
-d code="{AUTHORIZATION_CODE}" \
-d grant_type=authorization_code
However I am very new to CURL and have been doing some research and trying to use the requests package to do it. This is what my current code looks like:
data = '{"client_secret": "%s", "code": "%s", "grant_type": "authorization_code"}' % (SECRET_KEY, AUTHORIZATION_CODE)
response = requests.post('https://connect.stripe.com/oauth/token', json=data)
However this always returns a response code 400. I have no idea where I am going wrong and any guidance would be thoroughly appreciated.
The error is because you are passing your data as string, instead json param of requests.post call expects it to be dict. Your code should be:
import requests
data = {
"client_secret": SECRET_KEY,
"code": AUTHORIZATION_CODE,
"grant_type": "authorization_code"
}
response = requests.post('https://connect.stripe.com/oauth/token', json=data)
Take a look at request library's More complicated POST requests document.

What is the python requests equivalent of the following curl command

On ably.io they have an example where one can use the following curl request to publish a message to a channel:
curl -X POST https://rest.ably.io/channels/channelname/messages \
-u "some_AP.aYYMcQ:VmGHauKOqo-35Zxo" \
-H "Content-Type: application/json" \
--data '{ "name": "greeting", "data": "example" }'
The value passed to -u is an API key that has publish privileges. How does one make the same post request using Python requests library? I searched the documentation but could not find it. Note there is no password here, only the api key.
Thanks in advance.
You could use this:
requests.post("https://rest.ably.io/channels/channelname/messages",
auth=('some_AP.aYYMcQ', 'VmGHauKOqo-35Zxo'), # Equivalent of -u
json={ "name": "greeting", "data": "example" }) # Equivalent of --data
When you use the json option, -H is automatically set to Content-Type: application/json.

Categories