|
| 1 | +# Bitwarden Secrets Manager SDK |
| 2 | + |
| 3 | +## Crates |
| 4 | + |
| 5 | +The project is structured as a monorepo using cargo workspaces. Some of the more noteworthy crates |
| 6 | +are: |
| 7 | + |
| 8 | +- [`bitwarden`](./crates/bitwarden/): Rust-friendly API for interacting with the secrets manager. |
| 9 | +- [`bitwarden-c`](./crates/bitwarden-c/): C bindings for FFI interop. |
| 10 | +- [`bitwarden-json`](./crates/bitwarden-json/): JSON wrapper around the `bitwarden` crate. Powers |
| 11 | + the other language bindings. |
| 12 | +- [`bitwarden-napi`](./crates/bitwarden-napi/): Node-API bindings for Node.js/TypeScript. |
| 13 | +- [`bitwarden-wasm`](./crates/bitwarden-wasm/): WebAssembly bindings. |
| 14 | +- [`bws`](./crates/bws/): CLI for interacting with the [Bitwarden Secrets Manager][secrets-manager]. |
| 15 | + Review the [CLI documentation][bws-help]. |
| 16 | +- [`sdk-schemas`](./crates/sdk-schemas/): Generator for the _json schemas_. |
| 17 | +- [`fake-server`](./crates/fake-server/): Development/testing server that emulates Bitwarden API |
| 18 | + endpoints for local testing without a real server instance. |
| 19 | + |
| 20 | +### Language Bindings |
| 21 | + |
| 22 | +The SDK provides bindings in the `/languages/` directory: C++, C#, Go, Java, JavaScript/TypeScript, |
| 23 | +PHP, Python, and Ruby. All bindings share a consistent API for projects and secrets management. |
| 24 | + |
| 25 | +## Schemas |
| 26 | + |
| 27 | +To minimize the amount of work required to support additional bindings the project is structured |
| 28 | +around a `json` based API, beginning with every binding only needing to implement one method, namely |
| 29 | +a `run_command`. Additional work may be required to implement other functions, like a `free` command |
| 30 | +for languages that require manual memory management. Additional language-specific implementation |
| 31 | +details will apply based on the language. |
| 32 | + |
| 33 | +To ensure type safety in the API, _json schemas_ are generated from the rust structs in `bitwarden` |
| 34 | +using [schemars](https://crates.io/crates/schemars). The _json schemas_ are later used to generate |
| 35 | +the API bindings for each language using [QuickType](https://github.com/quicktype/quicktype). |
| 36 | + |
| 37 | +```bash |
| 38 | +npm run schemas |
| 39 | +``` |
| 40 | + |
| 41 | +## Key Concepts |
| 42 | + |
| 43 | +### Architecture |
| 44 | + |
| 45 | +The SDK uses a **layered architecture** to support multiple languages efficiently: |
| 46 | + |
| 47 | +1. **Core Layer**: External Rust dependencies from |
| 48 | + [sdk-internal](https://github.com/bitwarden/sdk-internal) repository contain the core business |
| 49 | + logic and cryptography. The `Cargo.toml` file **must** be inspected for these dependencies. |
| 50 | +2. **Public API Layer**: The public `bitwarden` crate provides the Rust API. |
| 51 | +3. **JSON API Layer**: `bitwarden-json` wraps the Rust API with a single `run_command` method. |
| 52 | +4. **Language Bindings**: All language bindings call `run_command` with JSON requests/responses. |
| 53 | + |
| 54 | +This architecture means adding a new language or updating the API requires minimal per-language |
| 55 | +work, as the heavy lifting is done in Rust and exposed via JSON schemas. |
| 56 | + |
| 57 | +### Authentication |
| 58 | + |
| 59 | +The SDK uses **access tokens** for authentication. All language bindings provide an authentication |
| 60 | +method (named according to each language's conventions, such as `login_access_token`, |
| 61 | +`accessTokenLogin`, or `LoginAccessToken`) to authenticate with the Secrets Manager API. |
| 62 | + |
| 63 | +### State Files |
| 64 | + |
| 65 | +The SDK supports optional **state files** for caching, improving performance by avoiding |
| 66 | +re-authentication. State files can be specified when calling the authentication method or omitted by |
| 67 | +passing `None`/`null`. |
| 68 | + |
| 69 | +### Feature Flags |
| 70 | + |
| 71 | +Key feature flags include: |
| 72 | + |
| 73 | +- `secrets` (default): Enables Secrets Manager API |
| 74 | +- `no-memory-hardening`: Disables memory security hardening features |
| 75 | +- `wasm`: Enables WebAssembly support |
| 76 | + |
| 77 | +## Development Workflows |
| 78 | + |
| 79 | +### Generating Schemas |
| 80 | + |
| 81 | +When you modify the Rust API structs, regenerate the JSON schemas and language bindings: |
| 82 | + |
| 83 | +```bash |
| 84 | +npm run schemas |
| 85 | +``` |
| 86 | + |
| 87 | +This generates JSON schemas from Rust structs and updates all language binding code using QuickType. |
| 88 | + |
| 89 | +### Local Testing with Fake Server |
| 90 | + |
| 91 | +Use the `fake-server` crate to test locally without a real Bitwarden instance: |
| 92 | + |
| 93 | +```bash |
| 94 | +cargo run -p fake-server |
| 95 | +# Then use bws or other clients against http://localhost:3000 (default port) |
| 96 | +``` |
| 97 | + |
| 98 | +The fake server provides minimal CRUD operations for secrets and projects. |
| 99 | + |
| 100 | +### Building Language Bindings |
| 101 | + |
| 102 | +- **Python**: Requires Python 3 and `maturin` (`pip install maturin`) |
| 103 | +- **Node.js**: Uses `napi-rs` for native addons |
| 104 | +- **WebAssembly**: Requires `wasm32-unknown-unknown` target and `wasm-bindgen-cli` |
| 105 | + |
| 106 | +### Code Quality |
| 107 | + |
| 108 | +- **Formatting**: Requires nightly toolchain: `cargo +nightly fmt` |
| 109 | +- **Linting**: `cargo clippy --all-features --tests` (workspace lints deny unwrap_used and |
| 110 | + unused_async) |
| 111 | +- **MSRV**: Minimum Supported Rust Version is 1.85 |
| 112 | + |
| 113 | +## References |
| 114 | + |
| 115 | +- [SDK Architecture](https://contributing.bitwarden.com/architecture/sdk/secrets-manager/) |
| 116 | +- [Secrets Manager SDK Documentation](https://bitwarden.com/help/secrets-manager-sdk/) |
| 117 | +- [Secrets Manager CLI Documentation](https://bitwarden.com/help/secrets-manager-cli/) |
| 118 | +- [Secrets Manager Overview](https://bitwarden.com/help/secrets-manager-overview/) |
| 119 | +- [Architectural Decision Records (ADRs)](https://contributing.bitwarden.com/architecture/adr/) |
| 120 | +- [Contributing Guidelines](https://contributing.bitwarden.com/contributing/) |
| 121 | +- [Setup Guide](https://contributing.bitwarden.com/getting-started/sdk/secrets-manager/) |
| 122 | +- [Code Style](https://contributing.bitwarden.com/contributing/code-style/) |
| 123 | +- [Security Whitepaper](https://bitwarden.com/help/bitwarden-security-white-paper/) |
| 124 | +- [Security Definitions](https://contributing.bitwarden.com/architecture/security/definitions) |
0 commit comments