Skip to main content

BOSA Core Library

Project description

BOSA Core

BOSA Core is a flexible plugin-based architecture that allows you to build modular and extensible applications. It provides a robust framework for managing plugins, handling dependencies, and injecting services.

Key Features

  • 🔌 Plugin-based Architecture
  • 💉 Dependency Injection
  • 🔄 Service Registry
  • 🛠️ Flexible Plugin Handlers
  • 🌐 HTTP Interface Support
  • 📊 OpenTelemetry Integration
  • 🛡️ Sentry Error Tracking
  • 📜 Logging

Installation

Prerequisites

1. Installation from Pypi

Choose one of the following methods to install the package:

Using pip

pip install bosa-core-binary

Using Poetry

poetry add bosa-core-binary

2. Development Installation (Git)

For development purposes, you can install directly from the Git repository:

poetry add "git+ssh://git@github.com/GDP-ADMIN/bosa-sdk.git#subdirectory=python/bosa-core"

Plugin Architecture Overview

Plugin Architecture

How It Works

  1. Plugin Manager: The central piece that orchestrates everything

    • Manages plugin lifecycle
    • Handles service registration
    • Initializes plugins with required dependencies
  2. Service Registry: A container for all services

    • Stores service instances
    • Handles dependency injection
    • Maps service types to their implementations
    • Note: You don't need to create your own Service Registry - it's integrated into the Manager!
  3. Plugin Handler: Interface for providing services to plugins

    • Creates service injections through create_injections
    • Initializes plugin-specific resources
    • Can be extended for different types of plugins (e.g., HTTP handlers)
  4. Plugin: Base class for all plugins

    • Has basic metadata (name, description, version)
    • Receives injected services automatically
    • Can be extended with specific functionality
  5. Open Telemetry: Initializer class to use open telemetry

    • Integrates with OpenTelemetry for tracing and metrics
    • Provides observability for plugins and services
    • Can be configured to export data to various backends (e.g., Jaeger, Prometheus)
  6. Sentry: Initializer class to use sentry

    • Integrates with Sentry for error tracking and performance monitoring
    • Captures exceptions and sends them to Sentry
    • Provides insights into application performance and errors

Authentication

BOSA Core provides a robust, multi-tenancy authentication system, allowing secure client and user management.

  1. Client Management:

    • Clients are created using a Master Key (BOSA_WHITELISTED_KEYS).
    • Each client has a unique API key used for authentication.
    • API Key Format: sk-client-<base64-client-id>.<base64-client-name>.<client-secret>.
    • Clients can manage their users and authentication settings.
  2. User Authentication:

    • Clients create users by providing an identifier.
    • Each user receives a secret, which is displayed only once.
    • Users authenticate using their identifier and secret.
    • User API Key Format: sk-user-<base64-user-id>.<base64-user-identifier>.<user-secret>.
  3. Token-Based Access:

    • Upon authentication, users receive JWT tokens for secure access.
    • Tokens are verified for subsequent requests.
    • Tokens expire after 30 days (default: 43,200 minutes).
    • Tokens can be revoked when necessary.
  4. Third-Party Integrations:

    • Users can create integrations with third-party services.
    • Authentication credentials are securely stored.
    • Each integration can be assigned scoped access.

For detailed implementation, refer to the Authentication README.

Implementation Details

The plugin system works through these key components:

1. Plugin Manager

from fastapi_interface import FastApiHttpInterface

# Create FastAPI handler
fastapi_handler = FastApiHttpInterface()

# Initialize manager with the handler
manager = PluginManager(handlers=[fastapi_handler])
manager.register_plugin(MyPlugin)
  • Initializes the plugin system
  • Manages plugin lifecycle
  • Handles service registration

2. Plugin Handler

from bosa_core.plugin import PluginHandler

class MyHandler(PluginHandler):
    @classmethod
    def create_injections(cls, instance):
        # Tell the manager what services this handler provides
        return {MyService: instance}

    @classmethod
    def initialize_plugin(cls, instance, plugin):
        # Initialize plugin resources
        pass
  • Creates service mappings via create_injections
  • Initializes plugin resources
  • Can be customized for different plugin types

3. Plugin

from bosa_core.plugin import Plugin
from fastapi_interface import FastApiService

class MyPlugin(Plugin):
    # Services will be injected automatically
    fastapi_service: FastApiService

    def __init__(self, name, description, version):
        super().__init__(name, description, version)
  • Base class for all plugins
  • Receives injected services
  • Can be extended with custom functionality

4. Open telemetry external exporter (enable fastapi & langchain instrumentation)

from bosa_core.telemetry import OpenTelemetryConfig, FastAPIConfig, init_telemetry
from fastapi import FastAPI

endpoint = "your-exporter-endpoint"
port = "your-exporter-port"
attributes = {
   "key1":"val1"
}
app = FastAPI()
use_langchain_instrumentation = True
fastapi_config = FastAPIConfig(app=app)
otel_config = OpenTelemetryConfig(endpoint=endpoint, port=port, attributes=attributes, fastapi_config=fastapi_config, use_langchain=use_langchain_instrumentation)
init_telemetry(TelemetryConfig(otel_config=otel_config))

5. Sentry

from bosa_core.telemetry import SentryConfig, init_telemetry

def traces_sampler(sampling_context: dict) -> float:
    """Determine appropriate sampling rate for Sentry transactions.
    Args:
        sampling_context: Context dictionary containing transaction information
    Returns:
        float: Sampling rate between 0 and 1
    """

    ### you can use customization (ex: for certain name sampler will be 0.1) for trace_sampler in this class
    return 1.0


dsn = "your-dsn"
environment = "development/staging/production"
release = "your release tag"
sentry_config = SentryConfig(
        dsn=dsn,
        environment=environment,
        release=release,
        traces_sampler=traces_sampler,
        send_default_pii=True,
    )
init_telemetry(TelemetryConfig(sentry_config=sentry_config))

6. Sentry with Open Telemetry (enable fastapi & langchain instrumentation)

from bosa_core.telemetry import SentryConfig, init_telemetry, FastAPIConfig, OpenTelemetryConfig
from fastapi import FastAPI


def traces_sampler(sampling_context: dict) -> float:
    """Determine appropriate sampling rate for Sentry transactions.
    Args:
        sampling_context: Context dictionary containing transaction information
    Returns:
        float: Sampling rate between 0 and 1
    """

    ### you can use customization (ex: for certain name sampler will be 0.1) for trace_sampler in this class
    return 1.0


dsn = "your-dsn"
environment = "development/staging/production"
release = "your release tag"

attributes = {
   "key1":"val1"
}
app = FastAPI()
use_langchain_instrumentation = True
fastapi_config = FastAPIConfig(app=app)
opentelemetry_init = OpenTelemetryConfig(attributes=attributes, fastapi_config=fastapi_config, use_langchain=use_langchain_instrumentation)

sentry_config = SentryConfig(
        dsn=dsn,
        environment=environment,
        release=release,
        traces_sampler=traces_sampler,
        send_default_pii=True,
        open_telemetry_initiliazer=opentelemetry_init
    )
init_telemetry(sentry_config=sentry_config)

7. Logging Ner API Handler

from bosa_core.logger import init_ner_pii_logging_handler

init_ner_pii_logging_handler(logger_name="logging-name", api_url="https://dev-api-gdplabs-ner-api.obrol.id/anonymize", api_field="text", pii_ner_process_enabled=True)


logger.info("contoh nomor ktp 3525011212941001\ncontoh email john.doe@example.com\ncontoh nomor telepon +628121729819 dan 0812898029384.\ncontoh npwp 01.123.456.7-891.234" ) # if you using gdplabs ner API output will be contoh nomor ktp <ID_KTP_1>\ncontoh email <EMAIL_ADDRESS_1>\ncontoh nomor telepon <PHONE_NUMBER_1> dan <PHONE_NUMBER_2>.\ncontoh npwp <ID_NPWP_1>

[!WARNING] If you are using Logger Ner API handler, the logging process may take some time because it needs to call an API for every log entry synchronously.

8. Logging Regex Handler

from bosa_core.logger import init_regex_pii_logging_handler

init_regex_pii_logging_handler(logger_name="logging-name", pii_regex_process_enabled=True)


logger.info("contoh nomor ktp 3525011212941001\ncontoh email john.doe@example.com\ncontoh nomor telepon +628121729819 dan 0812898029384.\ncontoh npwp 01.123.456.7-891.234" ) # if you using gdplabs ner API output will be contoh nomor ktp <ID_KTP_1>\ncontoh email <EMAIL_ADDRESS_1>\ncontoh nomor telepon <PHONE_NUMBER_1> dan <PHONE_NUMBER_2>.\ncontoh npwp <ID_NPWP_1>

Getting Started

  1. Install dependencies:

    poetry install
    
  2. Create a plugin:

    class MyPlugin(Plugin):
        name = "my-plugin"
        description = "My awesome plugin"
        version = "1.0.0"
    
  3. Register and use:

    # Using FastAPI as an example
    from fastapi import FastAPI
    from fastapi_interface import FastApiHttpInterface
    
    app = FastAPI()
    fastapi_handler = FastApiHttpInterface(app)
    manager = PluginManager(handlers=[fastapi_handler])
    manager.register_plugin(MyPlugin)
    

Development

  • Use Poetry for dependency management
  • Install pre-commit hooks poetry run pre-commit install. This will run checks before each commit.
  • Run ./build.local.sh to perform all checks:
    • Code formatting and validation (ruff)
    • Documentation style (pydocstyle)
    • Tests (pytest)
    • And more!

References

Product Requirements Documents(PRD):

  • N/A

Architecture Documents:

Design Documents:

Implementation Documents:

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

bosa_core_binary-0.9.7-cp313-cp313-win_amd64.whl (800.0 kB view details)

Uploaded CPython 3.13Windows x86-64

bosa_core_binary-0.9.7-cp313-cp313-manylinux_2_31_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.31+ x86-64

bosa_core_binary-0.9.7-cp313-cp313-macosx_13_0_arm64.whl (836.0 kB view details)

Uploaded CPython 3.13macOS 13.0+ ARM64

bosa_core_binary-0.9.7-cp312-cp312-win_amd64.whl (801.0 kB view details)

Uploaded CPython 3.12Windows x86-64

bosa_core_binary-0.9.7-cp312-cp312-manylinux_2_31_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.31+ x86-64

bosa_core_binary-0.9.7-cp312-cp312-macosx_13_0_arm64.whl (831.1 kB view details)

Uploaded CPython 3.12macOS 13.0+ ARM64

bosa_core_binary-0.9.7-cp311-cp311-win_amd64.whl (815.4 kB view details)

Uploaded CPython 3.11Windows x86-64

bosa_core_binary-0.9.7-cp311-cp311-manylinux_2_31_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.31+ x86-64

bosa_core_binary-0.9.7-cp311-cp311-macosx_13_0_arm64.whl (810.4 kB view details)

Uploaded CPython 3.11macOS 13.0+ ARM64

File details

Details for the file bosa_core_binary-0.9.7-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for bosa_core_binary-0.9.7-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 834c20184f59ccbf4f9559a3772da8eebdfa4f13554c01e40c02f537e6165a16
MD5 92b1d1b3c045a3581b17d024aa2be27f
BLAKE2b-256 f83ef1c9571ce9e076bfe90600c7845e8d2334db7014e849f7495bfe9ae61354

See more details on using hashes here.

Provenance

The following attestation bundles were made for bosa_core_binary-0.9.7-cp313-cp313-win_amd64.whl:

Publisher: build-binary.yml on GDP-ADMIN/bosa-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bosa_core_binary-0.9.7-cp313-cp313-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for bosa_core_binary-0.9.7-cp313-cp313-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 d9cbdc4b58d8f7275c19bf9a2553fab4adc2754fd82effc10c90ce8b6ccd4a87
MD5 553e4a1f62f97a159d2ca1cda1e86f06
BLAKE2b-256 e4646ea2a1e7ccbd193f32bf6eb139439925bb5dadde0d027776d0255fb4affb

See more details on using hashes here.

File details

Details for the file bosa_core_binary-0.9.7-cp313-cp313-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for bosa_core_binary-0.9.7-cp313-cp313-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 bcaa06116ddc2e89431c5483cd299f1882c4c7e7f50642d3c9be441c19de8ad7
MD5 71be6a574ea920b1713f3c1ac7b9b6d2
BLAKE2b-256 17029ad65c7efe17b5799816cf0b1e8c69facca7fe6f4e74f7a556b9981f6cbb

See more details on using hashes here.

Provenance

The following attestation bundles were made for bosa_core_binary-0.9.7-cp313-cp313-macosx_13_0_arm64.whl:

Publisher: build-binary.yml on GDP-ADMIN/bosa-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bosa_core_binary-0.9.7-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for bosa_core_binary-0.9.7-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 75a153635108da7bffdf20bb56d6b7d1f97151fb98b0dc1c67e6730c719d207c
MD5 89ba988885018639b66d8ba2f27c0083
BLAKE2b-256 657d90dc403b3dd80aa20be9880e4b29597ceaf8880c5ecf6b9f41768b6540a8

See more details on using hashes here.

Provenance

The following attestation bundles were made for bosa_core_binary-0.9.7-cp312-cp312-win_amd64.whl:

Publisher: build-binary.yml on GDP-ADMIN/bosa-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bosa_core_binary-0.9.7-cp312-cp312-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for bosa_core_binary-0.9.7-cp312-cp312-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 8a74852412323b7294c4b6ceac24f5ad3e867400c35d21c7031b318da1588b1e
MD5 826ad758fa840fe80b5983f1db068a94
BLAKE2b-256 ba5f7cf3fab374fb513ca4fabb219e7c275c4106b8e96d5d64dcdb536158f088

See more details on using hashes here.

File details

Details for the file bosa_core_binary-0.9.7-cp312-cp312-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for bosa_core_binary-0.9.7-cp312-cp312-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 c8f22e19da834002e8897522be1b51747882bf3fd92c9fa11457d39693c643c6
MD5 3b0a1139889ed3d80899b4af15fa47d0
BLAKE2b-256 14d5c9b2187d2cfdfd699199a7291c699415b3dc8c4a89e123078fd93c7f8512

See more details on using hashes here.

Provenance

The following attestation bundles were made for bosa_core_binary-0.9.7-cp312-cp312-macosx_13_0_arm64.whl:

Publisher: build-binary.yml on GDP-ADMIN/bosa-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bosa_core_binary-0.9.7-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for bosa_core_binary-0.9.7-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 480fde91063353e76d27536f0f28938fc66f0af617d5ae4a85ab4d33d52b36d4
MD5 5e0364153d0a68ee5a0123bf46a9636d
BLAKE2b-256 589f9eabcba7d29703c9afa0d14765e643ac187c91edcaec886c33adcc084a01

See more details on using hashes here.

Provenance

The following attestation bundles were made for bosa_core_binary-0.9.7-cp311-cp311-win_amd64.whl:

Publisher: build-binary.yml on GDP-ADMIN/bosa-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bosa_core_binary-0.9.7-cp311-cp311-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for bosa_core_binary-0.9.7-cp311-cp311-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 91feb615751ba27efb385b43075d6e8cbc49a7b1033125bf063ae0053b203ace
MD5 6ccad3d25c9b224a85f9f39a52bb2fcd
BLAKE2b-256 9a1af13de5290e27a42d04ee2837bcc23c44c64b6c510b04560be31126707a09

See more details on using hashes here.

File details

Details for the file bosa_core_binary-0.9.7-cp311-cp311-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for bosa_core_binary-0.9.7-cp311-cp311-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 3c447d6542520f139260e54a8a00c47082ee3ad3f7cf07cb6fb8b0e1de3fa2b5
MD5 7d09f79c85158966ef76fa6314ef4f7b
BLAKE2b-256 684c08682f5d983ad8fcaea3915258cf483e6097ff6aa5784c009ba552b508c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for bosa_core_binary-0.9.7-cp311-cp311-macosx_13_0_arm64.whl:

Publisher: build-binary.yml on GDP-ADMIN/bosa-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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