Skip to main content

An OPAL fetch provider to bring authorization state from MongoDB.

Project description

opal

OPAL Fetcher for MongoDB

Made with ❤️ at treedom, join us in making a difference!

plant-a-tree

Check out OPAL main repo here.

What's in this repo?

An OPAL custom fetch provider to bring authorization state from MongoDB.

How to try this custom fetcher in one command? (Example docker-compose configuration)

You can test this fetcher with the example docker compose file in this repository root. Clone this repo, cd into the cloned repo, and then run:

docker compose up

this docker compose configuration already correctly configures OPAL to load the MongoDB Fetch Provider, and correctly configures OPAL_DATA_CONFIG_SOURCES to include an entry that uses this fetcher.

✏️ How to use this fetcher in your OPAL Setup

1) Build a custom opal-client Dockerfile

The official docker image only contains the built-in fetch providers. You need to create your own Dockerfile (that is based on the official docker image), that includes this fetcher's pip package.

Your Dockerfile should look like this:

FROM permitio/opal-client:latest
RUN pip install --no-cache-dir --user opal-fetcher-mongodb

2) Build your custom opal-client container

Say your special Dockerfile from step one is called custom_client.Dockerfile.

You must build a customized OPAL container from this Dockerfile, like so:

docker build -t yourcompany/opal-client -f custom_client.Dockerfile .

3) When running OPAL, set OPAL_FETCH_PROVIDER_MODULES

Pass the following environment variable to the OPAL client docker container (comma-separated provider modules):

OPAL_FETCH_PROVIDER_MODULES=opal_common.fetcher.providers,opal_fetcher_mongodb.provider

Notice that OPAL receives a list from where to search for fetch providers. The list in our case includes the built-in providers (opal_common.fetcher.providers) and our custom MongoDB provider.

4) Using the custom provider in your DataSourceEntry objects

Your DataSourceEntry objects (either in OPAL_DATA_CONFIG_SOURCES or in dynamic updates sent via the OPAL publish API) can now include this fetcher's config.

Example value of OPAL_DATA_CONFIG_SOURCES (formatted nicely, but in env var you should pack this to one-line and no-spaces):

{
  "config": {
    "entries": [
      {
        "url": "mongodb://user:password@mongodb/test_database?authSource=admin",
        "config": {
          "fetcher": "MongoDBFetchProvider",
          "database": "opal_fetcher_mongodb",
          "collection": "cities_collection",
          "find": { "query": {} }
        },
        "topics": ["policy_data"],
        "dst_path": "cities"
      }
    ]
  }
}

Notice how config is an instance of MongoDBFetchProvider (code is in opal_fetcher_mongodb/provider.py).

Values for this fetcher config:

  • The url is actually a MongoDB uri.
  • Your config must include the fetcher key to indicate to OPAL that you use a custom fetcher.
  • Your config must include the collection key to indicate what collection to query in MongoDB.
  • Your config may include the database key to indicate what database to query in MongoDB. If not specified, the default database will be used.
  • Your config must include one of findOne, find or aggregate keys to indicate what query to run against MongoDB.
  • Your config may include the transform key to transform the results from the find or aggregate queries.

Query methods

All the three available query methods accept the same input parameters as defined in the MongoDB documentation.

findOne
Example configuration
{
  "config": {
    "entries": [
      {
        ...
        "config": {
          ...
          "findOne": {
            "query": {
              ...
            },
            "projection": {
              ...
            },
            "options": {
              ...
            }
          }
        }
      }
    ]
  }
}
find
Example configuration
{
  "config": {
    "entries": [
      {
        ...
        "config": {
          ...
          "find": {
            "query": {
              ...
            },
            "projection": {
              ...
            },
            "options": {
              ...
            }
          },
          "transform": {
            "first": false,
            "mapKey": "",
            "merge": true
          }
        }
      }
    ]
  }
}
aggregate
Example configuration
{
  "config": {
    "entries": [
      {
        ...
        "config": {
          ...
          "aggregate": {
            "pipeline": [
              ...
            ],
            "options": {
              ...
            }
          },
          "transform": {
            "first": false,
            "mapKey": ""
          }
        }
      }
    ]
  }
}

Query transform

transform allows you to transform the results from the find or aggregate queries.

first

transform.first allows you to return only the first result from the query.

Equivalent to the following Python code:

result = query_result[0]
mapKey

transform.mapKey allows you to map the original list-like result to a dictionary-like result using the property specified in the mapKey as the key for the dictionary.

Equivalent to the following Python code:

result = {}
for item in query_result:
    result[item['key']] = item

Only properties in the root of the document can be used as the key for the dictionary.

merge

transform.merge allows you to merge the results from the query into a single document. Duplicate keys will be overwritten by the last document in the list.

Equivalent to the following Python code:

result = {}
for item in query_result:
    for key, value in item.items():
        result[key] = value

🌳 Join Us in Making a Difference! 🌳

We invite all developers who use Treedom's open-source code to support our mission of sustainability by planting a tree with us. By contributing to reforestation efforts, you help create a healthier planet and give back to the environment. Visit our Treedom Open Source Forest to plant your tree today and join our community of eco-conscious developers.

Additionally, you can integrate the Treedom GitHub badge into your repository to showcase the number of trees in your Treedom forest and encourage others to plant new ones. Check out our integration guide to get started.

Together, we can make a lasting impact! 🌍💚

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

opal_fetcher_mongodb-1.0.0.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

opal_fetcher_mongodb-1.0.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file opal_fetcher_mongodb-1.0.0.tar.gz.

File metadata

  • Download URL: opal_fetcher_mongodb-1.0.0.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.0

File hashes

Hashes for opal_fetcher_mongodb-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4df33ad88e3cad1ab8d92913eeb90d8068bc9d27225c9f619f8fc2f2f7f9b988
MD5 3fe1e1068727eabdbffec382f75636ae
BLAKE2b-256 fb5aa02cb77b20486c6e2c30fddf7203328a8b7e6feec2cead45953feceff5fa

See more details on using hashes here.

File details

Details for the file opal_fetcher_mongodb-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for opal_fetcher_mongodb-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0579de600dd1ffd50c931ba38c9c5c53f777b656f5663e6424ce1de30094db6d
MD5 8ed0701b0ca7128530ae37b81c45c54e
BLAKE2b-256 1d1dea6d011c37e9766d74ef8d173bc30d04bbf437befd6e16e8d203ce3cfea6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page