mirror of
https://github.com/bitwarden/server
synced 2026-02-24 08:33:06 +00:00
SDK integration in db seeder
This commit is contained in:
58
util/RustSdk/NativeMethods.cs
Normal file
58
util/RustSdk/NativeMethods.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Bit.RustSDK;
|
||||
|
||||
public static partial class NativeMethods
|
||||
{
|
||||
// https://docs.microsoft.com/en-us/dotnet/standard/native-interop/cross-platform
|
||||
// Library path will search
|
||||
// win => __DllName, __DllName.dll
|
||||
// linux, osx => __DllName.so, __DllName.dylib
|
||||
|
||||
static NativeMethods()
|
||||
{
|
||||
NativeLibrary.SetDllImportResolver(typeof(NativeMethods).Assembly, DllImportResolver);
|
||||
}
|
||||
|
||||
static IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
|
||||
{
|
||||
if (libraryName != __DllName) return IntPtr.Zero;
|
||||
|
||||
var path = "runtimes/";
|
||||
var extension = "";
|
||||
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
path += "win-";
|
||||
extension = ".dll";
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||
{
|
||||
path += "osx-";
|
||||
extension = ".dylib";
|
||||
}
|
||||
else
|
||||
{
|
||||
path += "linux-";
|
||||
extension = ".so";
|
||||
}
|
||||
|
||||
if (RuntimeInformation.ProcessArchitecture == Architecture.X86)
|
||||
{
|
||||
path += "x86";
|
||||
}
|
||||
else if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
|
||||
{
|
||||
path += "x64";
|
||||
}
|
||||
else if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
|
||||
{
|
||||
path += "arm64";
|
||||
}
|
||||
|
||||
path += "/native/" + __DllName + extension;
|
||||
|
||||
return NativeLibrary.Load(Path.Combine(AppContext.BaseDirectory, path), assembly, searchPath);
|
||||
}
|
||||
}
|
||||
26
util/RustSdk/RustSdk.csproj
Normal file
26
util/RustSdk/RustSdk.csproj
Normal file
@@ -0,0 +1,26 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>Bit.RustSDK</RootNamespace>
|
||||
<UserSecretsId>Bit.RustSDK</UserSecretsId>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="rust/target/debug/libsdk.dylib">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
<PackageCopyToOutput>true</PackageCopyToOutput>
|
||||
<Link>runtimes/osx-arm64/native/libsdk.dylib</Link>
|
||||
</Content>
|
||||
<Content Include="./rust/target/debug/libsdk.so">
|
||||
<PackageCopyToOutput>true</PackageCopyToOutput>
|
||||
</Content>
|
||||
<Content Include="./rust/target/debug/libsdk.dll">
|
||||
<PackageCopyToOutput>true</PackageCopyToOutput>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
2933
util/RustSdk/rust/Cargo.lock
generated
Normal file
2933
util/RustSdk/rust/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
19
util/RustSdk/rust/Cargo.toml
Normal file
19
util/RustSdk/rust/Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "sdk"
|
||||
publish = false
|
||||
|
||||
version = "0.1.0"
|
||||
authors = ["Bitwarden Inc"]
|
||||
edition = "2021"
|
||||
homepage = "https://bitwarden.com"
|
||||
repository = "https://github.com/bitwarden/server"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
bitwarden-core = { git = "https://github.com/bitwarden/sdk-internal.git", rev = "b0c950dad701bc419c76e8a7d37bf5c17a6909d6" }
|
||||
bitwarden-crypto = { git = "https://github.com/bitwarden/sdk-internal.git", rev = "b0c950dad701bc419c76e8a7d37bf5c17a6909d6" }
|
||||
|
||||
[build-dependencies]
|
||||
csbindgen = "=1.9.3"
|
||||
9
util/RustSdk/rust/build.rs
Normal file
9
util/RustSdk/rust/build.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
fn main() {
|
||||
csbindgen::Builder::default()
|
||||
.input_extern_file("src/lib.rs")
|
||||
.csharp_dll_name("libsdk")
|
||||
.csharp_namespace("Bit.RustSDK")
|
||||
.csharp_class_accessibility("public")
|
||||
.generate_csharp_file("../NativeMethods.g.cs")
|
||||
.unwrap();
|
||||
}
|
||||
4
util/RustSdk/rust/src/lib.rs
Normal file
4
util/RustSdk/rust/src/lib.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
#[no_mangle]
|
||||
pub extern "C" fn my_add(x: i32, y: i32) -> i32 {
|
||||
x + y
|
||||
}
|
||||
56
util/Sdk2/dotnet/NativeMethods.cs
Normal file
56
util/Sdk2/dotnet/NativeMethods.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
internal static unsafe partial class NativeMethods
|
||||
{
|
||||
// https://docs.microsoft.com/en-us/dotnet/standard/native-interop/cross-platform
|
||||
// Library path will search
|
||||
// win => __DllName, __DllName.dll
|
||||
// linux, osx => __DllName.so, __DllName.dylib
|
||||
|
||||
static NativeMethods()
|
||||
{
|
||||
NativeLibrary.SetDllImportResolver(typeof(NativeMethods).Assembly, DllImportResolver);
|
||||
}
|
||||
|
||||
static IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
|
||||
{
|
||||
if (libraryName == __DllName)
|
||||
{
|
||||
var path = "runtimes/";
|
||||
var extension = "";
|
||||
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
path += "win-";
|
||||
extension = ".dll";
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||
{
|
||||
path += "osx-";
|
||||
extension = ".dylib";
|
||||
}
|
||||
else
|
||||
{
|
||||
path += "linux-";
|
||||
extension = ".so";
|
||||
}
|
||||
|
||||
if (RuntimeInformation.ProcessArchitecture == Architecture.X86)
|
||||
{
|
||||
path += "x86";
|
||||
}
|
||||
else if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
|
||||
{
|
||||
path += "x64";
|
||||
}
|
||||
else if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
|
||||
{
|
||||
path += "arm64";
|
||||
}
|
||||
|
||||
path += "/native/" + __DllName + extension;
|
||||
|
||||
return NativeLibrary.Load(Path.Combine(AppContext.BaseDirectory, path), assembly, searchPath);
|
||||
}
|
||||
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Bit.RustSDK;
|
||||
|
||||
namespace Bit.Seeder.Factories;
|
||||
|
||||
@@ -7,6 +8,7 @@ public class UserSeeder
|
||||
{
|
||||
public static User CreateUser(string email)
|
||||
{
|
||||
Console.WriteLine(NativeMethods.my_add(2, 3));
|
||||
return new User
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
<ProjectReference Include="..\..\src\Core\Core.csproj" />
|
||||
<ProjectReference Include="..\..\src\Infrastructure.EntityFramework\Infrastructure.EntityFramework.csproj" />
|
||||
<ProjectReference Include="..\..\src\SharedWeb\SharedWeb.csproj" />
|
||||
<ProjectReference Include="..\RustSdk\RustSdk.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user