Skip to content

Commit

Permalink
Bundle platform-verifier (#1039)
Browse files Browse the repository at this point in the history
We've run into some issues related to rustls-platform-verifier
distributing an android library through their crate, which works great
if you use platform-verifier within an application. However since we
distribute an sdk, the application have difficulties locating the
android specific platform-verifier library.

To resolve this we follow the method established in
rustls/rustls-platform-verifier#115 and bundle
the classes with the library.
  • Loading branch information
Hinton authored Sep 20, 2024
1 parent 2e506f7 commit 88537a8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
14 changes: 13 additions & 1 deletion crates/bitwarden-uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use vault::ClientVault;
#[derive(uniffi::Object)]
pub struct Client(bitwarden::Client);

#[uniffi::export]
#[uniffi::export(async_runtime = "tokio")]
impl Client {
/// Initialize a new instance of the SDK client
#[uniffi::constructor]
Expand Down Expand Up @@ -79,6 +79,18 @@ impl Client {
pub fn echo(&self, msg: String) -> String {
msg
}

/// Test method, calls http endpoint
pub async fn http_get(&self, url: String) -> Result<String> {
let client = self.0.internal.get_http_client();
let res = client
.get(&url)
.send()
.await
.map_err(bitwarden::Error::Reqwest)?;

Ok(res.text().await.map_err(bitwarden::Error::Reqwest)?)
}
}

fn init_logger() {
Expand Down
39 changes: 29 additions & 10 deletions languages/kotlin/sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
minSdk 28
targetSdk 34

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
consumerProguardFiles 'consumer-rules.pro'
}

buildTypes {
Expand All @@ -31,7 +31,7 @@ android {
}

lint {
baseline = file("lint-baseline.xml")
baseline = file('lint-baseline.xml')
}

publishing {
Expand All @@ -52,9 +52,9 @@ publishing {
// PRs: use the branch name.
// Main: Grab it from `crates/bitwarden/Cargo.toml`

def branchName = "git branch --show-current".execute().text.trim()
def branchName = 'git branch --show-current'.execute().text.trim()

if (branchName == "main") {
if (branchName == 'main') {
def content = ['grep', '-o', '^version = ".*"', '../../Cargo.toml'].execute().text.trim()
def match = ~/version = "(.*)"/
def matcher = match.matcher(content)
Expand All @@ -73,19 +73,38 @@ publishing {
}
repositories {
maven {
name = "GitHubPackages"
url = "https://maven.pkg.github.com/bitwarden/sdk"
name = 'GitHubPackages'
url = 'https://maven.pkg.github.com/bitwarden/sdk'
credentials {
username = System.getenv("GITHUB_ACTOR")
password = System.getenv("GITHUB_TOKEN")
username = System.getenv('GITHUB_ACTOR')
password = System.getenv('GITHUB_TOKEN')
}
}
}
}

// Find and include the classes.jar from platform-verifier.
//
// Based on the instructions from the readme in https://github.com/rustls/rustls-platform-verifier
// and issue details from https://github.com/rustls/rustls-platform-verifier/issues/115.
File findRustlsPlatformVerifierClassesJar() {
def dependencyText = providers.exec {
it.workingDir = new File('../../')
commandLine('cargo', 'metadata', '--format-version', '1', '--manifest-path', 'crates/bitwarden-uniffi/Cargo.toml')
}.standardOutput.asText.get()

def dependencyJson = new groovy.json.JsonSlurper().parseText(dependencyText)
def manifestPath = file(dependencyJson.packages.find { it.name == "rustls-platform-verifier-android" }.manifest_path)

def aar = fileTree(manifestPath.parentFile).matching {
include "maven/rustls/rustls-platform-verifier/*/rustls-platform-verifier-*.aar"
}.getSingleFile()
return zipTree(aar).matching { include 'classes.jar'}.getSingleFile()
}

dependencies {
implementation 'net.java.dev.jna:jna:5.14.0@aar'
implementation 'rustls:rustls-platform-verifier:latest.release'
implementation files(findRustlsPlatformVerifierClassesJar())

implementation 'androidx.core:core-ktx:1.13.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3'
Expand Down
18 changes: 1 addition & 17 deletions languages/kotlin/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,9 @@ dependencyResolutionManagement {
repositories {
google()
mavenCentral()

maven {
url = findRustlsPlatformVerifierProject()
metadataSources.artifact()
}
}
}

String findRustlsPlatformVerifierProject() {
def dependencyText = providers.exec {
it.workingDir = new File("../../")
commandLine("cargo", "metadata", "--format-version", "1", "--manifest-path", "crates/bitwarden-uniffi/Cargo.toml")
}.standardOutput.asText.get()

def dependencyJson = new groovy.json.JsonSlurper().parseText(dependencyText)
def manifestPath = file(dependencyJson.packages.find { it.name == "rustls-platform-verifier-android" }.manifest_path)
return new File(manifestPath.parentFile, "maven").path
}

rootProject.name = "My Application"
rootProject.name = 'My Application'
include ':app'
include ':sdk'

0 comments on commit 88537a8

Please sign in to comment.