Ruby SDK
The Ruby language wrapper for interacting with the Bitwarden Secrets Manager. The SDK, like the Secrets Manager CLI built on-top of it, can be used to execute the following operations:
- Authenticate using an access token.
- Retrieve a single secret or all secrets in a project.
- List all secrets, secrets in a project, or projects.
Requirements
- Setting up a Secrets Manager account prior to using the Ruby SDK is recommended. This includes:
- Enabling the Secrets Manager CLI.
- Setting up machine accounts.
- Setting up access tokens.
Dependencies
-
Ruby version 3.0 or newer
-
Install
gem
gem install bitwarden-sdk-secrets- Import it: require
'bitwarden-sdk-secrets'
GitHub Repository
Locate the Ruby GitHub repository here.
Local Build
To interact with the client, you must first obtain an access token from Bitwarden. Client will be initialized with default client settings if they are not provided from env variables.
require 'bitwarden-sdk-secrets'
# then you can initialize BitwardenSettings:bitwarden_settings = BitwardenSDK::BitwardenSettings.new( 'https://api.bitwarden.com', 'https://identity.bitwarden.com')
# By passing these setting you can initialize BitwardenClient
bw_client = BitwardenSDK::BitwardenClient.new(bitwarden_settings)response = bw_client.access_token_login(token)puts responseOnce the Secrets Manager session has been authorized, you may interact with the client.
Secrets Manager operations
Once the Bitwarden client has been created and authorized, Secrets Manager CLI commands can be passed into the client.
Projects
The project command is used to access, manipulate, and create projects. The scope of access assigned to your machine account will determine what actions can be completed with the project command.
create project
project_name = 'Test project 1'response = bw_client.project_client.create_project(project_name, organization_id)puts responseproject_id = response['id']get project
response = bw_client.project_client.get(project_id)puts responselist project
response = bw_client.project_client.get(project_id)puts responselist projects
response = bw_client.project_client.list_projects(organization_id)puts responseupdate project
name = 'Updated test project 1'response = bw_client.project_client.update_project(project_id, name, organization_id)puts responsedelete project
response = bw_client.project_client.delete_projects([project_id])puts responseSecrets
The secret command is used to access, manipulate and create secrets. As with all commands, secrets and projects outside your access token’s scope of access cannot be read or written-to.
create project
key = 'AWS-SES'note = 'Private account'value = '8t27.dfj;'response = bw_client.secrets_client.create(key, note, organization_id, [project_id], value)puts responsesecret_id = response['id']get secret
response = bw_client.secrets_client.get(secret_id)puts responseget secrets by ids
response = bw_client.secrets_client.get_by_ids([secret_id])puts responselist secrets
response = bw_client.secrets_client.list(organization_id)puts responseupdate secret
note = 'updated password'value = '7I.ert10AjK'response = bw_client.secrets_client.update(secret_id, key, note,organization_id, [project_id], value)puts responsedelete secret
response = bw_client.secrets_client.delete_secret([secret_id])puts responseDevelopment
Prerequisites:
-
Ruby version 3.0 or newer installed
-
Generate schemas:
npm run schemas- Navigate to the Ruby language folder
cd languages/ruby- Create the binary folder if it does not already existing
mkdir -p ./bitwarden_sdk_secrets/lib/macos-arm64- Build and copy the
bitwarden-clibrary
cargo build --package bitwarden-ccp ../../target/debug/libbitwarden_c.dylib ./bitwarden_sdk_secrets/lib/macos-arm64/libbitwarden_c.dylib- Install Ruby dependencies:
cd ./bitwarden_sdk_secretsbundle install- Install the gem
bundle exec rake installRun example tests:
cd ..export ACCESS_TOKEN=""export ORGANIZATION_ID=""
export API_URL=http://localhost:4000export IDENTITY_URL=http://localhost:33656ruby examples/example.rb