Skip to content

Instantly share code, notes, and snippets.

@ranfysvalle02
Created January 16, 2024 03:17
Show Gist options
  • Save ranfysvalle02/ee76acdd7b17cfca13a6f9d3082c40a6 to your computer and use it in GitHub Desktop.
Save ranfysvalle02/ee76acdd7b17cfca13a6f9d3082c40a6 to your computer and use it in GitHub Desktop.
MongoDB + OSO Cloud
from oso_cloud import Oso
import pymongo
api_key = "<api-key-goes-here>"
oso = Oso(url="https://cloud.osohq.com", api_key=api_key)
def test_oso():
actor = {"type": "User", "id": "1233"}
resource = {"type": "Repository", "id": "456"}
if not oso.authorize(actor, "view", resource):
# Handle authorization failure
return "unauthorized by oso"
return "authorized by oso"
print(
test_oso()
)
def query_collection(coll,query,projection):
# Execute the query
results = coll.find(query,projection)
results_array = list(results)
print(results_array)
if test_oso() == "authorized by oso":
client = pymongo.MongoClient("mongodb+srv://<user>:<pwd>@shared.x.mongodb.net/test")
db = client["database"]
collection = db["collection"]
# Define your query conditions using $match
query = {
#"$match": {
#"field_name": "value_to_match", # Replace with your actual field and value
# Add more conditions as needed
#}
}
query_collection(collection,query,{"_id":0,"text":1})
else:
# Handle authorization failure - connect to public repository
client = pymongo.MongoClient("mongodb+srv://<user>:<pwd>@shared.x.mongodb.net/test")
db = client["database"]
collection = db["collection"]
# Define your query conditions using $match
query = {
#"$match": {
#"field_name": "value_to_match", # Replace with your actual field and value
# Add more conditions as needed
#}
}
query_collection(collection,query,{"_id":0})
@ranfysvalle02
Copy link
Author

Screenshot 2024-01-15 at 10 23 23 PM

@ranfysvalle02
Copy link
Author

Screenshot 2024-01-15 at 10 23 10 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment