How to get python code to run as a lambda function - python

I'm trying to use this code as a lambda function so it will print the text as the output but I can't seem to figure it out. The lambda should be invoked in the event any of the values stored in critical_count, medium_count, or high_count exceed the values stored in CRITICAL, MEDIUM OR HIGH.
I have tried printing them directly but that doesn't work. I trying making them into variables but that didn't work either. Any help would be greatly appreciated
import json
def lambda_handler(event, context):
CRITICAL = 0
MEDIUM = 1
HIGH = 0
f = open('ECR_scan.json', )
test = json.loads(f)
resource = test["resources"][0]
finding_severity_counts = test["detail"]["finding-severity-counts"]
if "CRITICAL" in finding_severity_counts:
critical_count = finding_severity_counts["CRITICAL"]
if critical_count > CRITICAL:
print("Resource {} has {} critical findings".format(resource, critical_count))
if "MEDIUM" in finding_severity_counts:
medium_count = finding_severity_counts["MEDIUM"]
f medium_count > MEDIUM:
print("Resource {} has {} medium findings".format(resource, medium_count))
if "HIGH" in finding_severity_counts:
high_count = finding_severity_counts["HIGH"]
if high_count > HIGH:
print("Resource {} has {} high findings".format(resource, high_count))
return {
'statusCode': 200,
'body':
Here is the json file I am envoking as well
"version": "0",
"id": "85fc3613-e913-7fc4-a80c-a3753e4aa9ae",
"detail-type": "ECR Image Scan",
"source": "aws.ecr",
"account":
"123456789012",
"time": "2019-10-29T02:36:48Z",
"region": "us-east-1",
"resources": [
"arn:aws:ecr:us-east-1:123456789012:repository/my-repo"
],
"detail": {
"scan-status": "COMPLETE",
"repository-name": "my-repo",
"finding-severity-counts": {
"CRITICAL": 10,
"MEDIUM": 9
}
}
}

Related

POST API Request - Only Print if JSON Data in Response Has Changed - Python

I am trying to create an way to actively monitor sales/listings. I have a created POST api that will call the website and it currently is successfully pulling down the JSON data that has the item that sold, the price, the time, seller, and buyer.
Here's an example of the data returned in the JSON.
Item: 1
Price: 50$
Sold Date: 10/28/2021 10:00AM
Seller: John
Buyer: Frank
Say this script runs every 5 minutes and prints out the last sale. At 10:05, it sees Item 1 sold so it prints out the data. At 10:10, no new items sold, so it prints out Item 1 again. I am looking for a way to only print out if Item 2 sells(aka updated JSON data), but I am having trouble figuring out the best way to handle this logic in python.
Would you just use date/time minus the last 5mins? Or is there a better way?
Code is simple:
asset_url = 'www.sample.com/api/'
seller_payload = json.dumps({
"name": "find",
"arguments": [
{
"database": "prod",
"data": "SALES",
"query": {
"seller": {
"$in": [
"sellerid"
]
},
},
"sort": {
"epoch": {
"$numberInt": "-1"
}
},
"limit": {
"$numberInt": "1"
}
}
],
"service": "db"
})
seller_response = requests.request("POST", asset_url, headers=profile_headers, data=seller_payload)
asset_id = seller_response[0]['asset']
seller = seller_response[0]['seller']
price = seller_response[0]['price']
print(asset_id)
print(seller)
print(price)
If the script runs every 5 minutes, store each transaction in a file. Something like this:
from pickle import dump, loads
transactions = []
try:
transactions = loads("transactions.txt") # try to read transactions from file.
except:
print("An exception occurred.")
# Check if the last transaction is the same as it was five minutes ago.
if (not (transactions[-1]["asset"] == seller_response[0]["asset"] && transactions[-1]["seller"] == seller_response[0]["seller"] && transactions[-1]["soldDate"] == seller_response[0]["soldDate"] && transactions[-1]["buyer"] == seller_response[0]["buyer"] && transactions[-1]["item"] == seller_response[0]["item"])):
print(seller_response[0])
transactions.push(seller_response[0])
dump(transactions,"transactions.txt")

coinbase api client.get_accounts() not returning all wallets

Currently the coinbase api will not get my atom wallet. with other testing code i was able to see that it was return 94 wallets however none them are my atom wallet and the accounts data doesn't even seem like it has 94 elements.
Any ideas on how to get my atom wallet to show? i tried the client.get_accounts(limit=200) trick with no success.
#import coinbase api
from coinbase.wallet.client import Client
# Coinbase Credentials
api_key = 'xxx'
api_secret = 'xxx'
# create a coinbase client
cb_client = Client(api_key, api_secret)
cb_accounts = cb_client.get_accounts()
# coin setting
coin = 'ATOM'
#print accounts
print(cb_accounts)
#get coin balance
for i in cb_accounts['data']:
if i['currency'] == coin:
coin_balance = float(i['native_balance']['amount'])
#print coin balance
print(coin_balance)
Sample Data returned:
{
"data": [
{
"allow_deposits": true,
"allow_withdrawals": true,
"balance": {
"amount": "0.00000000",
"currency": "FET"
},
"created_at": "xxx",
"currency": "FET",
"id": "xxx",
"name": "FET Wallet",
"native_balance": {
"amount": "0.00",
"currency": "USD"
},
"primary": false,
"resource": "account",
"resource_path": "xxx",
"type": "wallet",
"updated_at": "xxx"
}
]
}
the better way is to use coinbase api pagination parameteres:
#iniliase next wallet id
next = None
# this loop will run until the next_uri parameter is none ( it means that there is no other page to show)
while True:
accounts = client.get_accounts(starting_after=next)
next = accounts.pagination.next_starting_after
print(accounts.data)
if accounts.pagination.next_uri == None :
print("end")
break
IF you like your script print your wallets with none zero balance, try this:
#iniliase next wallet id
next=None
# this loop wil run until next_uri parameter is none ( it means that there is no other page to show)
while True:
accounts=client.get_accounts(starting_after=next)
next=accounts.pagination.next_starting_after
for wallet in accounts.data:
if wallet['native_balance']['amount'] != '0.00':
print(str(wallet['name']) + ' ' + str(wallet['native_balance']))
if accounts.pagination.next_uri==None :
print("end")
break

copying data from json response [Python]

I have a scenario where I am trying to extract data from json response which is obtained from the GET request and then rebuilding the json data by changing some values and then sending a PUT request at same time after rebuilding the json data(i.e, after changing idter value)
below is the target json response.
target_json = {
"name": "toggapp",
"ts": [
1234,
3456
],
"gs": [
{
"id": 4491,
"con": "mno"
},
{
"id": 4494,
"con": "hkl"
}
],
"idter": 500,
"datapart": false
}
from the above json I am trying to change the idter value to my custom value and rebuild it into json data again and post the new json data.
Here is what I have tried :
headers = {'Authorization': 'bearer ' + auth_token, 'Content-Type':'application/json', 'Accept':'application/json'}
tesstid =[7865, 7536, 7789]
requiredbdy = []
for key in testid:
get_metadata_targetjson= requests.get('https://myapp.com/%s' %key, headers = headers)
metadata=get_metadata_target.json()
for key1 in metadata:
requiredbdy.append(
{
"metadata" : [{
"name": key1['name'],
"ts": key1['ts'],
"gs": key1[gs],
"idter": 100, #custom value which I want to change
"datapart": false
} ]
}
)
send_metadata_newjson= requests.put('https://myapp.com/%s' %key, headers = headers data = requiredbdy)
print(send_metadata_newjson.status_code)
Is this approach fine or How do I proceed in order to achieve this scenario.
You can use the built-in json module for this like so
import json
my_json = """
{
"name": "toggapp",
"ts": [
1234,
3456
],
"gs": [
{
"id": 4491,
"con": "mno"
},
{
"id": 4494,
"con": "hkl"
}
],
"idter": 500,
"datapart": false
}
"""
json_obj = json.loads(my_json)
json_obj['idter'] = 600
print(json.dumps(json_obj))
Prints
{"name": "toggapp", "ts": [1234, 3456], "gs": [{"id": 4491, "con": "mno"}, {"id": 4494, "con": "hkl"}], "idter": 600, "datapart": false}
There's this small script used it to find entries in some very long and unnerving JSONs. not very beautifull und badly documented but maybe helps in your scenario.
from RecursiveSearch import Retriever
def alter_data(json_data, key, original, newval):
'''
Alter *all* values of said keys
'''
retr = Retriever(json_data)
for item_no, item in enumerate(retr.__track__(key)): # i.e. all 'value'
# Pick parent objects with a last element False in the __track__() result,
# indicating that `key` is either a dict key or a set element
if not item[-1]:
parent = retr.get_parent(key, item_no)
try:
if parent[key] == original:
parent[key] = newval
except TypeError:
# It's a set, this is not the key you're looking for
pass
if __name__ == '__main__':
alter_data(notification, key='value',
original = '********** THIS SHOULD BE UPDATED **********',
newval = '*UPDATED*')

Cant Access Data in List of Dictionaries

I'm trying to access some data within nested ordered dictionaries. This dictionary was created by using the XMLTODICT module. Obviously I would like to create my own dictionaries but this one is out of my control.
I've tried to access them numerous ways.
Example:
Using a for loop:
I can access the first level using v["name"] which gives me Child_Policy and Parent Policy
When I do v["class"]["name"] I would expect to get "Test1" but that's not the case.
I've also tried v[("class", )] variations as well with no luck.
Any input would be much appreciated
The data below is retrieved from a device via XML and converted to dictionary with XMLTODICT.
[
{
"#xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XE-policy",
"name": "Child_Policy",
"class": [
{
"name": "Test1",
"action-list": {
"action-type": "bandwidth",
"bandwidth": {
"percent": "30"
}
}
},
{
"name": "Test2",
"action-list": {
"action-type": "bandwidth",
"bandwidth": {
"percent": "30"
}
}
}
]
},
{
"#xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XE-policy",
"name": "Parent_Policy",
"class": {
"name": "class-default",
"action-list": [
{
"action-type": "shape",
"shape": {
"average": {
"bit-rate": "10000000"
}
}
},
{
"action-type": "service-policy",
"service-policy": "Child_Policy"
}
]
}
}
]
My expectations result is to retrieve values from the nested dictionary and produce and output similar to this:
Queue_1: Test1
Action_1: bandwidth
Allocation_1: 40
Queue_2: Test2
Action_2: bandwidth
Allocation_2: 10
I have now issue formatting the output, just getting the values is the issue.
#
I had some time tonight so I changed the code be be dynamic:
int = 0
int_2 = 0
for v in policy_dict.values():
print("\n")
print("{:15} {:<35}".format("Policy: ", v[0]["name"]))
print("_______")
for i in v:
int_2 = int_2 + 1
try:
print("\n")
print("{:15} {:<35}".format("Queue_%s: " % int_2, v[0]["class"][int]["name"]))
print("{:15} {:<35}".format("Action_%s: " % int_2, v[0]["class"][int]["action-list"]["action-type"]))
print("{:15} {:<35}".format("Allocation_%s: " % int_2, v[0]["class"][int]["action-list"]["bandwidth"]["percent"]))
int = int + 1
except KeyError:
break
pass
According to the sample you posted you can try to retrieve values like:
v[0]["class"][0]["name"]
This outputs:
Test1

Aws Lambda function returns confidence 99% to anything i upload

I am doing some thing wrong over here, while comparing two images in different S3 Bucket.
Even though, I am comparing images of male and female it would give 99% confidence
or am i missing something in the declaration yet
Maybe This line is causing a problem
key_target = "targett/" + key
Or my event code is error prone this is where i have mentioned my source bucket ,even though i have mentioned it in lambda function for testing below. What else do i need to correct so that it will return the confidence within the rang specified
from __future__ import print_function
import boto3
from decimal import Decimal
import json
import urllib
print('Loading function')
rekognition = boto3.client('rekognition')
#iot = boto3.client('iot-data')
# --------------- Helper Functions to call Rekognition APIs ------------------
def compare_faces(bucket, key, key_target, threshold=75):
response = rekognition.compare_faces(
SourceImage={
"S3Object": {
"Bucket": 'dacss',
"Name": 'obama.jpg',
}
},
TargetImage={
"S3Object": {
"Bucket": 'targett',
"Name": 'michelle.jpg',
}
},
SimilarityThreshold=threshold,
)
return response['SourceImageFace'], response['FaceMatches']
# --------------- Main handler ------------------
def lambda_handler(event, context):
print("Received event: " + json.dumps(event, indent=2))
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']
['key'].encode('utf8'))
key_target = "targett/" + key
try:
response = compare_faces(bucket, key, key_target)
print(response)
# mypayload = json.dumps(response)
# iotResponse = iot.publish(
# topic="rekognition/result",
# qos=1,
# payload=mypayload)
# print(iotResponse)
# return iotResponse
print(response)
return response
except Exception as e:
print(e)
print("Error processing object {} from bucket {}. ".format(key,
bucket)
+
"Make sure your object and bucket exist and your bucket is in
the
same region as this function.")
raise e
---------------output-----------------
Response:
[
{
"BoundingBox": {
"Width": 0.7813892960548401,
"Top": 0.15193353593349457,
"Left": 0.1047489121556282,
"Height": 0.8365015387535095
},
"Confidence": 99.99993896484375
},
[]
]
i think you are having some misunderstanding here
{
'FaceMatches': [
{
'Face': {
'BoundingBox': {
'Height': 0.33481481671333313,
'Left': 0.31888890266418457,
'Top': 0.4933333396911621,
'Width': 0.25,
},
'Confidence': 99.9991226196289,
},
'Similarity': 100,
},
],
'SourceImageFace': {
'BoundingBox': {
'Height': 0.33481481671333313,
'Left': 0.31888890266418457,
'Top': 0.4933333396911621,
'Width': 0.25,
},
'Confidence': 99.9991226196289,
},
'ResponseMetadata': {
'...': '...',
},
}
Here the confidence score does'nt show weather the face matches or not it shows that it found the face in the image. "Similarty" shows the actual match of the image.
These lines:
TargetImage={
"S3Object": {
"Bucket": targett,
"Name": obama.jpg,
}
should be:
TargetImage={
"S3Object": {
"Bucket": 'targett',
"Name": key_target,
}

Categories

Resources