Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby SDK #245

Merged
merged 80 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 73 commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
eaaaf13
first version
milost77 Sep 21, 2023
0d4b945
Merge branch 'master' into first_version
milost77 Sep 21, 2023
4cd04ad
Merge pull request #1 from MaliRobot/first_version
MaliRobot Sep 21, 2023
bef5b6d
add generated ruby schemas to workflow
milost77 Sep 22, 2023
6e7fcaf
cleanup
milost77 Sep 22, 2023
de1943a
cleanup
milost77 Sep 22, 2023
592f519
Merge pull request #2 from MaliRobot/add_schemas_to_workflow
MaliRobot Sep 22, 2023
5d3a860
Merge branch 'bitwarden:master' into master
MaliRobot Sep 22, 2023
ff3c1c7
Rename lib (#4)
MaliRobot Sep 29, 2023
1e459e2
print out dirs (#5)
milost77 Sep 29, 2023
a2dc534
lib files to folders
milost77 Oct 2, 2023
2b65aac
make dirs before copy
milost77 Oct 2, 2023
a3ebb98
include libs in gem
milost77 Oct 2, 2023
7bd049c
check macos version when loading lib file
milost77 Oct 2, 2023
6116620
rename main file
milost77 Oct 4, 2023
54e6d31
dl artifact fix
milost77 Oct 4, 2023
6018d84
schemas artifact hash
milost77 Oct 4, 2023
0b42d7a
schemas artifact hash
milost77 Oct 4, 2023
103781f
schemas artifact hash
milost77 Oct 4, 2023
24c020c
chck schemas artifact
milost77 Oct 4, 2023
5b4e132
new release
milost77 Oct 4, 2023
b9ffe73
fix paths in gh wf
milost77 Oct 4, 2023
e4cae90
fix paths in gh wf
milost77 Oct 4, 2023
72e1b3e
new version
milost77 Oct 4, 2023
c6444b2
include schemas
milost77 Oct 4, 2023
28868f0
include schemas
milost77 Oct 4, 2023
6db1118
fix path issues
milost77 Oct 4, 2023
2e706a5
Merge pull request #6 from MaliRobot/lib-folders
milost77 Oct 4, 2023
b30b82b
Merge branch 'bitwarden:master' into master
milost77 Oct 4, 2023
7a5b6c0
cleanup
milost77 Oct 4, 2023
355397e
Fixes tweaks (#7)
milost77 Oct 10, 2023
2e41c6a
Merge branch 'bitwarden:master' into master
MaliRobot Oct 12, 2023
78be863
Update languages/ruby/README.md
MaliRobot Oct 12, 2023
87531f7
Update languages/ruby/README.md
MaliRobot Oct 12, 2023
4c44b3b
Update languages/ruby/README.md
MaliRobot Oct 12, 2023
e239dc8
Apply suggestions from code review
MaliRobot Oct 12, 2023
30c43b3
refactoring upon pull request
milost77 Oct 13, 2023
f599d49
changes after pull request
milost77 Oct 13, 2023
20df9a6
optimize wf
milost77 Oct 13, 2023
bb5ce21
cleanup (#8)
MaliRobot Oct 13, 2023
1bbf6d0
Merge branch 'bitwarden:master' into master
MaliRobot Oct 15, 2023
fde1a44
Cleanup (#9)
MaliRobot Oct 15, 2023
181765a
Debug key (#10)
MaliRobot Oct 15, 2023
0936d4d
final version of git action (#11)
MaliRobot Oct 16, 2023
515d86d
Final version gf (#12)
MaliRobot Oct 16, 2023
033a7f7
fixing project and secret methods
milost77 Oct 25, 2023
39c4f1a
Merge remote-tracking branch 'origin/master'
milost77 Oct 25, 2023
cea69d9
Merge branch 'bitwarden:master' into master
MaliRobot Oct 25, 2023
cbaa09e
secets client refactor
milost77 Oct 25, 2023
c090664
projects client refactor
milost77 Oct 25, 2023
1de9548
New (#14)
MaliRobot Oct 28, 2023
3e4321d
Update README.md
MaliRobot Oct 29, 2023
d3314b0
Update example.rb
MaliRobot Oct 29, 2023
809efbf
refactor git flow, handle non defined env vals in client settings (#16)
MaliRobot Nov 1, 2023
7ddf1f5
renam authorize param
milost77 Nov 2, 2023
fa9974d
Update .github/workflows/publish-ruby.yml
MaliRobot Nov 2, 2023
b625a97
Update languages/ruby/README.md
MaliRobot Nov 2, 2023
3d3bfda
Update languages/ruby/bitwarden_sdk/bitwarden-sdk-ruby.gemspec
MaliRobot Nov 2, 2023
e394c07
Update .github/workflows/publish-ruby.yml
MaliRobot Nov 2, 2023
ad650fc
Update .github/workflows/publish-ruby.yml
MaliRobot Nov 2, 2023
72dcf11
removing blank lines
milost77 Nov 2, 2023
fb9fb86
Update languages/ruby/bitwarden_sdk/bitwarden-sdk-ruby.gemspec
MaliRobot Nov 3, 2023
563d971
client settings param
milost77 Nov 3, 2023
f5322a3
bitwarden settings
milost77 Nov 3, 2023
2e347c8
Merge remote-tracking branch 'origin/master'
milost77 Nov 3, 2023
32b4b74
Update languages/ruby/bitwarden_sdk/sig/command_runner.rbs
MaliRobot Nov 3, 2023
5e2f728
renaming
milost77 Nov 3, 2023
fb5ab85
version increase
milost77 Nov 3, 2023
d2fb77f
Merge remote-tracking branch 'origin/master'
milost77 Nov 3, 2023
1733fb2
Update languages/ruby/README.md
MaliRobot Nov 3, 2023
a3ec738
refator error handling
milost77 Nov 3, 2023
2064195
handle bitwarden settings missing params
milost77 Nov 7, 2023
95bb765
key checking in clients
milost77 Nov 7, 2023
54728f4
rename authorize method to access_token_login
milost77 Nov 26, 2023
ed23e3e
rename authorize method to access_token_login
milost77 Nov 26, 2023
6363509
updated code to use client settings, updated readme and example file
milost77 Nov 26, 2023
fa3fd4f
reintroduced bitwarden settings
milost77 Nov 28, 2023
7a0b6fd
authorize returns nil
milost77 Nov 28, 2023
4221ed3
Update languages/ruby/README.md
milost77 Nov 30, 2023
17d0ee2
Merge branch 'master' into master
dani-garcia Nov 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/generate_schemas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ jobs:
path: ${{ github.workspace }}/languages/python/BitwardenClient/schemas.py
if-no-files-found: error

- name: Upload ruby schemas artifact
uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
with:
name: schemas.rb
path: ${{ github.workspace }}/languages/ruby/bitwarden_sdk/lib/schemas.rb
if-no-files-found: error

- name: Upload json schemas artifact
uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
with:
Expand Down
88 changes: 88 additions & 0 deletions .github/workflows/publish-ruby.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: Publish Ruby SDK

on:
pull_request:
branches:
- master

jobs:
generate_schemas:
uses: ./.github/workflows/generate_schemas.yml

build_rust:
uses: ./.github/workflows/build-rust-cross-platform.yml

build_ruby:
name: Build Ruby
runs-on: ubuntu-22.04
needs:
- generate_schemas
- build_rust
steps:
- name: Checkout Repository
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

- name: Set up Ruby
uses: ruby/setup-ruby@54a18e26dbbb1eabc604f317ade9a5788dddef81 # v1.159.0
with:
ruby-version: 3.2

- name: Download Ruby schemas artifact
uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
with:
name: schemas.rb
path: languages/ruby/bitwarden_sdk/lib

- name: Download x86_64-apple-darwin files
uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
with:
name: libbitwarden_c_files-x86_64-apple-darwin
path: temp/macos-x64

- name: Download aarch64-apple-darwin files
uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
with:
name: libbitwarden_c_files-aarch64-apple-darwin
path: temp/macos-arm64

- name: Download x86_64-unknown-linux-gnu files
uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
with:
name: libbitwarden_c_files-x86_64-unknown-linux-gnu
path: temp/ubuntu-x64

- name: Download x86_64-pc-windows-msvc files
uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
with:
name: libbitwarden_c_files-x86_64-pc-windows-msvc
path: temp/windows-x64

- name: Copy lib files
run: |
mkdir -p languages/ruby/bitwarden_sdk/lib/macos-arm64
mkdir -p languages/ruby/bitwarden_sdk/lib/ubuntu-x64
mkdir -p languages/ruby/bitwarden_sdk/lib/macos-x64
mkdir -p languages/ruby/bitwarden_sdk/lib/windows-x64

platforms=("macos-arm64" "ubuntu-x64" "macos-x64" "windows-x64")
files=("libbitwarden_c.dylib" "libbitwarden_c.so" "libbitwarden_c.dylib" "bitwarden_c.dll")

for ((i=0; i<${#platforms[@]}; i++)); do
cp "temp/${platforms[$i]}/${files[$i]}" "languages/ruby/bitwarden_sdk/lib/${platforms[$i]}/${files[$i]}"
done
shell: bash

- name: Build gem
run: gem build bitwarden-sdk.gemspec
working-directory: languages/ruby/bitwarden_sdk

- name: Push gem to Rubygems
run: |
mkdir -p $HOME/.gem
touch $HOME/.gem/credentials
chmod 0600 $HOME/.gem/credentials
printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
gem push *.gem
env:
GEM_HOST_API_KEY: ${{ secrets.GEM_HOST_API_KEY }}
working-directory: languages/ruby/bitwarden_sdk
3 changes: 3 additions & 0 deletions languages/ruby/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.lock
*.gem
bitwarden_sdk/lib/schemas.rb
5 changes: 5 additions & 0 deletions languages/ruby/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## [Unreleased]

## [0.1.0] - 2023-09-19

- Initial release
79 changes: 79 additions & 0 deletions languages/ruby/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Bitwarden Secrets Manager SDK

Ruby bindings for interacting with the [Bitwarden Secrets Manager]. This is a beta release and might be missing some functionality.

## Installation

Requirements: Ruby >= 3.0

Install gem: `gem install bitwarden-sdk`

Import it: require 'bitwarden-sdk'


## Usage

To interact with client first you need to obtain access token from Bitwarden.
Client will be initialized with default client settings if they are not provided
via env variables.

```ruby
api_url = ENV['BITWARDEN_API_URL'] || 'https://api.bitwarden.com'
identity_url = ENV['BITWARDEN_IDENTITY_URL'] || 'https://identity.bitwarden.com'
user_agent = ENV['BITWARDEN_USER_AGENT'] || 'SDK'

# then tou can initialize BitwardenSettings:
bitwarden_settings = BitwardenSDK::BitwardenSettings.new(
'https://api.bitwarden.com',
'https://identity.bitwarden.com/connect/token'
milost77 marked this conversation as resolved.
Show resolved Hide resolved
)

By passing these setting you can initialize bitwarden client

```

Authorization can be performed using access token like so:
```ruby
client = BitwardenSDK::BitwardenClient.new(bitwarden_settings)
client.authorize("<<YOUR ACCESS TOKEN HERE>>")
```

After successful authorization you can interact with client to manage your projects and secrets.
```ruby
# get project
project = client.project_client.get("b23818dd-827b-4a22-b97a-b07e010ae9d4")

# Create project
project = client.project_client.create_project("new_project", "5688da1f-cc25-41d7-bb9f-b0740144ef1d")

# list projects
project = client.project_client.list_projects("5688da1f-cc25-41d7-bb9f-b0740144ef1d")

# update project
project = client.project_client.update_project("ef9d3d37-f0dc-4b21-a842-b0810129bf02", "test_project_x", "5688da1f-cc25-41d7-bb9f-b0740144ef1d")

# delete projects
response = client.project_client.delete_projects(["13a015aa-e3dc-4854-875a-b08101512d2f"])
```

Similarly, you interact with secrets:
```ruby
# get secret
secret = client.secrets_client.get("fa175a5b-da76-48c3-b44b-b0810151638c")

# get by ids
secrets = client.secrets_client.get_by_ids(["e8561721-0455-438c-bbbe-b0810152f534"])

# list
secrets = client.secrets_client.list("5688da1f-cc25-41d7-bb9f-b0740144ef1d")

# delete
result = client.secrets_client.delete_secret(["b03cf64b-e894-4675-9f59-b0810152abe6", "e8561721-0455-438c-bbbe-b0810152f534"])

# create
secret = client.secrets_client.create("this", "hola!", "5688da1f-cc25-41d7-bb9f-b0740144ef1d", ["ef9d3d37-f0dc-4b21-a842-b0810129bf02"], "blah")

# update
secret = client.secrets_client.update("683c25f3-a463-49ba-bed4-b0810134a7b1", "Title", "my pass", "5688da1f-cc25-41d7-bb9f-b0740144ef1d", ["4647aede-33f1-4ad1-a258-b07a014a48a7"], "supersecret77")
```
[Bitwarden Secrets Manager]: https://bitwarden.com/products/secrets-manager/
8 changes: 8 additions & 0 deletions languages/ruby/bitwarden_sdk/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rubocop/rake_task"

RuboCop::RakeTask.new

task default: :rubocop
47 changes: 47 additions & 0 deletions languages/ruby/bitwarden_sdk/bitwarden-sdk.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require_relative 'lib/version'

Gem::Specification.new do |spec|
spec.name = 'bitwarden-sdk'
spec.version = BitwardenSDK::VERSION
spec.authors = ['Bitwarden Inc.']
spec.email = ['hello@bitwarden_sdk.com']

spec.summary = 'Bitwarden Secrets Manager SDK.'
spec.description = 'Ruby wrapper for Bitwarden secrets manager SDK.'
spec.homepage = 'https://bitwarden.com/products/secrets-manager/'
spec.required_ruby_version = '>= 3.0.0'

spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = 'https://github.com/bitwarden/sdk'
spec.metadata['changelog_uri'] = 'https://github.com/bitwarden/sdk/blob/master/languages/ruby/CHANGELOG.md'

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(__dir__) do
`git ls-files -z`.split("\x0").reject do |f|
(File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
end
end

spec.files += Dir.glob('lib/ubuntu-x64/**/*')
spec.files += Dir.glob('lib/macos-x64/**/*')
spec.files += Dir.glob('lib/windows-x64/**/*')
spec.files += Dir.glob('lib/macos-arm64/**/*')
spec.files += Dir.glob('lib/schemas.rb')

spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

# Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0"
spec.add_dependency 'dry-struct', '~> 1.6'
spec.add_dependency 'dry-types', '~> 1.7'
spec.add_dependency 'ffi', '~> 1.15'
spec.add_dependency 'json', '~> 2.6'
spec.add_dependency 'rake', '~> 13.0'
spec.add_dependency 'rubocop', '~> 1.21'

end
55 changes: 55 additions & 0 deletions languages/ruby/bitwarden_sdk/lib/bitwarden-sdk.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require 'json'
require 'dry-types'

require_relative 'schemas'
require_relative 'extended_schemas/schemas'
require_relative 'command_runner'
require_relative 'bitwarden_lib'
require_relative 'bitwarden_error'
require_relative 'projects'
require_relative 'secrets'

module BitwardenSDK
class BitwardenSettings
attr_accessor :api_url, :identity_url

def initialize(api_url, identity_url)
if api_url.nil? || identity_url.nil?
raise ArgumentError, "api_url and identity_url cannot be nil"
end

@api_url = api_url
@identity_url = identity_url
end
end

class BitwardenClient
attr_reader :bitwarden, :project_client, :secrets_client

def initialize(bitwarden_settings)
client_settings = ClientSettings.new(
api_url: bitwarden_settings.api_url,
identity_url: bitwarden_settings.identity_url,
user_agent: 'Bitwarden RUBY-SDK',
device_type: nil
dani-garcia marked this conversation as resolved.
Show resolved Hide resolved
)

@bitwarden = BitwardenLib
@handle = @bitwarden.init(client_settings.to_json)
@command_runner = CommandRunner.new(@bitwarden, @handle)
@project_client = ProjectsClient.new(@command_runner)
@secrets_client = SecretsClient.new(@command_runner)
end

def authorize(access_token_login)
dani-garcia marked this conversation as resolved.
Show resolved Hide resolved
access_token_request = AccessTokenLoginRequest.new(access_token: access_token_login)
@command_runner.run(SelectiveCommand.new(access_token_login: access_token_request))
end

def free_mem
@bitwarden.free_mem(@handle)
end
end
end
9 changes: 9 additions & 0 deletions languages/ruby/bitwarden_sdk/lib/bitwarden_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module BitwardenSDK
class BitwardenError < StandardError
def initialize(message = 'Error getting response')
super(message)
end
end
end
35 changes: 35 additions & 0 deletions languages/ruby/bitwarden_sdk/lib/bitwarden_lib.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require 'ffi'

module BitwardenSDK
module BitwardenLib
extend FFI::Library

def self.mac_with_intel?
`uname -m`.strip == 'x86_64'
end

ffi_lib case RUBY_PLATFORM
when /darwin/
local_file = if mac_with_intel?
File.expand_path('macos-x64/libbitwarden_c.dylib', __dir__)
else
File.expand_path('macos-arm64/libbitwarden_c.dylib', __dir__)
end
File.exist?(local_file) ? local_file : File.expand_path('../../../../target/debug/libbitwarden_c.dylib', __dir__)
when /linux/
local_file = File.expand_path('ubuntu-x64/libbitwarden_c.so', __dir__)
File.exist?(local_file) ? local_file : File.expand_path('../../../../target/debug/libbitwarden_c.so', __dir__)
when /mswin|mingw/
local_file = File.expand_path('windows-x64/bitwarden_c.dll', __dir__)
File.exist?(local_file) ? local_file : File.expand_path('../../../../target/debug/bitwarden_c.dll', __dir__)
else
raise "Unsupported platform: #{RUBY_PLATFORM}"
end

attach_function :init, [:string], :pointer
attach_function :run_command, %i[string pointer], :string
attach_function :free_mem, [:pointer], :void
end
end
15 changes: 15 additions & 0 deletions languages/ruby/bitwarden_sdk/lib/command_runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module BitwardenSDK
class CommandRunner
def initialize(bitwarden_sdk, handle)
@bitwarden_sdk = bitwarden_sdk
@handle = handle
end

# @param [Dry-Struct] cmd
def run(cmd)
@bitwarden_sdk.run_command(cmd.to_json, @handle)
end
end
end
Loading