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

.NET wrapper for SDK #193

Merged
merged 114 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
114 commits
Select commit Hold shift + click to select a range
b435ac8
Implemented .NET library that wraps native C library and exposed its…
Aug 26, 2023
5fc8570
add NuGet package configuration
Aug 28, 2023
e6b9943
Merge branch 'master' into feature/csharp_-wrapper
bbujak Aug 28, 2023
9bbf5d8
buidl on PR created
Aug 28, 2023
d2c0ba0
Merge remote-tracking branch 'origin/feature/csharp_-wrapper' into fe…
Aug 28, 2023
21053bd
check if the same version exists , and if it doesn't deploy
Aug 28, 2023
748b1eb
name fix
Aug 28, 2023
796a4ef
update github actions
Aug 29, 2023
1cdc73d
build rust added in gitflow actions
Aug 29, 2023
8addb59
run prettier and re-write part for schemas
Aug 29, 2023
6337f7f
deploy to nuget fix
Aug 29, 2023
66d5273
add version
Aug 29, 2023
c0f00ab
copy lib to output when package is used
Aug 29, 2023
4979bcb
Merge branch 'master' into feature/csharp_-wrapper
bbujak Aug 30, 2023
6d94e77
- added editorconfig, code reformated
Aug 30, 2023
634cc18
Merge remote-tracking branch 'origin/feature/csharp_-wrapper' into fe…
Aug 30, 2023
14f7deb
Merge branch 'master' into feature/csharp_-wrapper
bbujak Aug 31, 2023
d214391
Merge branch 'master' into feature/csharp_-wrapper
bbujak Aug 31, 2023
f00851f
Merge branch 'master' into feature/csharp_-wrapper
bbujak Sep 2, 2023
f6f8ca2
temporary add pull_request so git action is registered
Sep 2, 2023
18f213a
fix version
Sep 2, 2023
0001770
just run on PR
Sep 2, 2023
8a220cd
we want to build the package
Sep 2, 2023
bf926bf
pack with specific version and publish the package
Sep 2, 2023
ade9277
use test name for now
Sep 2, 2023
6bfe7e0
changed api access
Sep 2, 2023
11fb8c9
copy libs after cargo build and pack them
Sep 2, 2023
28fb2a4
copy libs after cargo build and pack them
Sep 2, 2023
54c6682
find all id: libbitwarden_c files
Sep 2, 2023
62fd39d
use libbitwarden_c files
Sep 2, 2023
f31a094
use separated folder
Sep 2, 2023
b1ab18b
use separated folder
Sep 2, 2023
5ebe0a9
use separated folder
Sep 2, 2023
e2f690f
use separated folder
Sep 2, 2023
0d8c439
use separated folder
Sep 2, 2023
c92299e
use separated folder
Sep 3, 2023
07c5d4a
list files
Sep 3, 2023
c3f0a1d
separate folders
Sep 3, 2023
67400af
separate folders
Sep 3, 2023
3e238f1
fixes
Sep 3, 2023
97c856c
fixes
Sep 3, 2023
23c9bd1
fixes
Sep 3, 2023
f40340f
fixes
Sep 3, 2023
0fc144e
fixes
Sep 3, 2023
0b0be5c
fixes
Sep 4, 2023
07ef431
fixes
Sep 4, 2023
6ffeb2d
fixes
Sep 4, 2023
b25e106
fixes
Sep 4, 2023
0f3524f
fixes
Sep 4, 2023
5d899fb
fixes
Sep 4, 2023
d145d7e
fixes
Sep 4, 2023
52614a9
fixes
Sep 4, 2023
c07d133
fixes
Sep 4, 2023
06dc693
fixes
Sep 4, 2023
11b7172
fixes
Sep 4, 2023
4999d8b
one package per arch and system
Sep 4, 2023
da7eda7
one package per arch and system
Sep 4, 2023
29a39f0
one package per arch and system
Sep 4, 2023
dc677c0
one package per arch and system
Sep 4, 2023
d469e1e
one package per arch and system
Sep 4, 2023
ac15608
one package per arch and system
Sep 4, 2023
08dfc18
one package per arch and system
Sep 4, 2023
0e9140a
remove id
Sep 4, 2023
e8f50c8
remove id
Sep 4, 2023
12b5f37
api key
Sep 4, 2023
2c50b2e
api key
Sep 4, 2023
bed17fc
api key
Sep 4, 2023
23cb857
api key
Sep 5, 2023
93e54be
open folder
Sep 5, 2023
ba97623
final check
Sep 5, 2023
954762c
run just on demand
Sep 5, 2023
c459a7e
use correct name
Sep 5, 2023
a6f1901
Merge branch 'master' into feature/csharp_-wrapper
bbujak Sep 5, 2023
9f31308
pack all in one package
Sep 7, 2023
a2ad083
Merge remote-tracking branch 'origin/feature/csharp_-wrapper' into fe…
Sep 7, 2023
79bf078
Merge branch 'master' into feature/csharp_-wrapper
bbujak Sep 7, 2023
781d5fc
fix lint
Sep 7, 2023
76bb33f
fix lint
Sep 7, 2023
9e7d717
fix pk name
Sep 7, 2023
21249f3
use correct folder for arm arch
Sep 7, 2023
b3ec3ad
PR comments addressed
Sep 8, 2023
a80d802
Did refactoring to address PR comments
Sep 9, 2023
cfd9297
use SafeHandleZeroOrMinusOneIsInvalid
Sep 9, 2023
1782057
removed ids
Sep 9, 2023
40e6f1d
Merge branch 'master' into feature/csharp_-wrapper
bbujak Sep 9, 2023
bd6ac8a
merged master branch
Sep 11, 2023
fa6fe0c
address PR comments
Sep 11, 2023
b00d525
change solution structure
Sep 11, 2023
f043f86
fix path for generating schemas
Sep 11, 2023
0e9d502
pack NuGet and upload artifact
Sep 11, 2023
e618079
change the way rust is build
Sep 12, 2023
badbd79
forgot --target
Sep 12, 2023
d084c6e
remove after error building OpenSSL
Sep 12, 2023
86fb604
Merge branch 'master' into feature/csharp_-wrapper
bbujak Sep 12, 2023
48d04f5
use IntPtr to free memory
Sep 12, 2023
5d16c7e
Merge remote-tracking branch 'origin/feature/csharp_-wrapper' into fe…
Sep 12, 2023
1d0fc9e
PR comments resolved
Sep 13, 2023
48e83d0
fixes
Sep 13, 2023
0c344f0
PR comments
Sep 13, 2023
e446d93
PR comments
Sep 13, 2023
2f2f404
reformat code
Sep 13, 2023
12eb511
small name fix
Sep 13, 2023
0890640
fixes
Sep 13, 2023
883b499
Merge branch 'master' into feature/csharp_-wrapper
bbujak Sep 13, 2023
c2becb7
make get only properties
Sep 15, 2023
466bccf
Merge branch 'master' into feature/csharp_-wrapper
bbujak Sep 15, 2023
0c433c3
Merge branch 'master' into feature/csharp_-wrapper
bbujak Sep 18, 2023
3d5f1e5
PR comments
Sep 18, 2023
2202db1
PR comments
Sep 19, 2023
34f8a36
PR comment fix
Sep 19, 2023
884e8dd
update readme
Sep 19, 2023
76039dd
reformat code
Sep 21, 2023
1717a56
Merge branch 'master' into feature/csharp_-wrapper
bbujak Sep 21, 2023
7eaf25c
Merge branch 'master' into feature/csharp_-wrapper
Hinton Sep 21, 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
77 changes: 77 additions & 0 deletions .github/workflows/build-dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Build .NET SDK

on:
pull_request:
branches:
- master

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

build_rust:
name: Building ${{matrix.package}} for - ${{ matrix.os }}

runs-on: ${{ matrix.settings.os || 'ubuntu-latest' }}

strategy:
fail-fast: false
matrix:
os:
- macos-latest
- ubuntu-latest
- windows-latest

package:
- bitwarden
- bitwarden-api-api
- bitwarden-api-identity

steps:
- name: Checkout
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

- name: Install rust
uses: dtolnay/rust-toolchain@f361669954a8ecfc00a3443f35f9ac8e610ffc06 # stable
with:
toolchain: stable
targets: ${{ matrix.settings.target }}

- name: Cache cargo registry
uses: Swatinem/rust-cache@e207df5d269b42b69c8bc5101da26f7d31feddb4 # v2.6.2

- name: Build
run: cargo build -p ${{ matrix.package }} --release
env:
RUSTFLAGS: "-D warnings"

- name: Build Internal
if: ${{ matrix.package == 'bitwarden' }}
run: cargo build -p ${{ matrix.package }} --features internal --release
env:
RUSTFLAGS: "-D warnings"

build_dotnet:
runs-on: ubuntu-22.04
needs: [generate_schemas, build_rust]

bbujak marked this conversation as resolved.
Show resolved Hide resolved
steps:
- name: Checkout Repository
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

- name: Download c# schemas artifact
bbujak marked this conversation as resolved.
Show resolved Hide resolved
uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
with:
name: schemas.cs
path: languages/csharp

- name: Set up .NET Core
uses: actions/setup-dotnet@v3
bbujak marked this conversation as resolved.
Show resolved Hide resolved
with:
dotnet-version: "6.0.x"
bbujak marked this conversation as resolved.
Show resolved Hide resolved

- name: Build .NET 6 Project
working-directory: languages/csharp
run: |
dotnet restore
dotnet build --configuration Release
bbujak marked this conversation as resolved.
Show resolved Hide resolved
99 changes: 99 additions & 0 deletions .github/workflows/publish-dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
name: Deploy NuGet Package

on:
workflow_dispatch:
version_number:
description: "New Version"
required: true

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

build_rust:
name: Building ${{matrix.package}} for - ${{ matrix.os }}

runs-on: ${{ matrix.settings.os || 'ubuntu-latest' }}

strategy:
fail-fast: false
matrix:
os:
- macos-latest
- ubuntu-latest
- windows-latest

package:
- bitwarden
- bitwarden-api-api
- bitwarden-api-identity

steps:
- name: Checkout
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

- name: Install rust
uses: dtolnay/rust-toolchain@f361669954a8ecfc00a3443f35f9ac8e610ffc06 # stable
with:
toolchain: stable
targets: ${{ matrix.settings.target }}

- name: Cache cargo registry
uses: Swatinem/rust-cache@e207df5d269b42b69c8bc5101da26f7d31feddb4 # v2.6.2

- name: Build
run: cargo build -p ${{ matrix.package }} --release
env:
RUSTFLAGS: "-D warnings"

- name: Build Internal
if: ${{ matrix.package == 'bitwarden' }}
run: cargo build -p ${{ matrix.package }} --features internal --release
env:
RUSTFLAGS: "-D warnings"

deploy:
runs-on: ubuntu-22.04
needs: [generate_schemas, build_rust]

bbujak marked this conversation as resolved.
Show resolved Hide resolved
steps:
- name: Checkout Repository
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

- name: Download c# schemas artifact
bbujak marked this conversation as resolved.
Show resolved Hide resolved
uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
with:
name: schemas.cs
path: languages/csharp

- name: Set up .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: "6.0.x"

- name: Get Package Version
id: version
env:
VERSION: ${{ github.event.inputs.version_number }}
run: |
cd languages/csharp
package_version=$(dotnet pack --configuration Release --no-build /property:Version=${VERSION} --output ./nuget-output /nologo /v:n)
echo "Package Version: $package_version"
echo "::set-output name=version::$package_version"

- name: Check Existing Version on NuGet.org
id: check_version
run: |
existing_version=$(dotnet nuget list source-package-name -s https://api.nuget.org/v3/index.json --version ${{ steps.version.outputs.version }} | grep -oP '(?<=Version: )[^,]+')
echo "Existing Version: $existing_version"
if [ -n "$existing_version" ]; then
echo "Package version already exists on NuGet.org."
echo "::set-output name=exists::true"
else
echo "Package version does not exist on NuGet.org."
echo "::set-output name=exists::false"
fi

- name: Publish NuGet Package
if: steps.check_version.outputs.exists == 'false'
run: dotnet nuget push ./languages/csharp/nuget-output/*.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"cSpell.words": [
"bindgen",
"Bitwarden",
"Cdecl",
"chrono",
"cloc",
Expand Down
74 changes: 74 additions & 0 deletions languages/csharp/BitwardenClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Newtonsoft.Json;

namespace Bit.Sdk
{
public sealed class BitwardenClient : IDisposable
bbujak marked this conversation as resolved.
Show resolved Hide resolved
{
private readonly Projects _projects;
private readonly Secrets _secrets;
private readonly CommandRunner _commandRunner;
private readonly IntPtr _ptr;
private bool _disposedValue;

public BitwardenClient(Settings? settings = null)
{
var clientSettings = new ClientSettings
{
ApiUrl = settings is { ApiUrl: not null } ? settings.ApiUrl : "https://api.bitwarden.com",
IdentityUrl = settings is { IdentityUrl: not null } ? settings.IdentityUrl : "https://identity.bitwarden.com",
DeviceType = DeviceType.Sdk,
UserAgent = "Bitwarden DOTNET-SDK"
};

_ptr = BitwardenLibrary.init(clientSettings.ToJson());
_commandRunner = new CommandRunner(_ptr);
_projects = new Projects(_commandRunner);
_secrets = new Secrets(_commandRunner);
}

public ResponseForApiKeyLoginResponse AccessTokenLogin(string accessToken) {
var command = new Command();
var accessTokenLoginRequest = new AccessTokenLoginRequest
{
AccessToken = accessToken
};
command.AccessTokenLogin = accessTokenLoginRequest;
return _commandRunner.RunCommand(command, JsonConvert.DeserializeObject<ResponseForApiKeyLoginResponse>);
}

public Projects Projects() {
return _projects;
}

public Secrets Secrets() {
return _secrets;
}

private void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects)
}

BitwardenLibrary.free_mem(_ptr);
_disposedValue = true;
}
}

~BitwardenClient()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: false);
}
bbujak marked this conversation as resolved.
Show resolved Hide resolved

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
15 changes: 15 additions & 0 deletions languages/csharp/BitwardenLibrary.cs
bbujak marked this conversation as resolved.
Show resolved Hide resolved
Hinton marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Runtime.InteropServices;

namespace Bit.Sdk;

internal static class BitwardenLibrary {

[DllImport("bitwarden_c", CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr init(string settings);

[DllImport("bitwarden_c", CallingConvention = CallingConvention.Cdecl)]
internal static extern void free_mem(IntPtr clientPtr);

[DllImport("bitwarden_c", CallingConvention = CallingConvention.Cdecl)]
internal static extern string run_command(string loginRequest, IntPtr clientPtr);
}
75 changes: 0 additions & 75 deletions languages/csharp/BitwardenSdk.cs

This file was deleted.

17 changes: 17 additions & 0 deletions languages/csharp/CommandRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Bit.Sdk;

internal class CommandRunner {

private readonly IntPtr _client;

internal CommandRunner(IntPtr client) {
_client = client;
}

internal TReturn? RunCommand<TReturn>(Command command, Func<string, TReturn> deserializer)
{
var req = command.ToJson();
bbujak marked this conversation as resolved.
Show resolved Hide resolved
var result = BitwardenLibrary.run_command(req, _client);
return deserializer(result);
}
}
Loading