-
Notifications
You must be signed in to change notification settings - Fork 906
[BWA-182] Add mTLS support for Glide image loading #6125
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
Draft
aj-rosado
wants to merge
3
commits into
main
Choose a base branch
from
BWA-182/support-mtls-icon-loading
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
app/src/main/kotlin/com/x8bit/bitwarden/ui/platform/glide/BitwardenAppGlideModule.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| package com.x8bit.bitwarden.ui.platform.glide | ||
|
|
||
| import android.content.Context | ||
| import com.bitwarden.network.ssl.createMtlsOkHttpClient | ||
| import com.bumptech.glide.Glide | ||
| import com.bumptech.glide.Priority | ||
| import com.bumptech.glide.Registry | ||
| import com.bumptech.glide.annotation.GlideModule | ||
| import com.bumptech.glide.load.DataSource | ||
| import com.bumptech.glide.load.HttpException | ||
| import com.bumptech.glide.load.Options | ||
| import com.bumptech.glide.load.data.DataFetcher | ||
| import com.bumptech.glide.load.model.GlideUrl | ||
| import com.bumptech.glide.load.model.ModelLoader | ||
| import com.bumptech.glide.load.model.ModelLoaderFactory | ||
| import com.bumptech.glide.load.model.MultiModelLoaderFactory | ||
| import com.bumptech.glide.module.AppGlideModule | ||
| import com.x8bit.bitwarden.data.platform.manager.CertificateManager | ||
| import dagger.hilt.EntryPoint | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.android.EntryPointAccessors | ||
| import dagger.hilt.components.SingletonComponent | ||
| import okhttp3.Call | ||
| import okhttp3.OkHttpClient | ||
| import okhttp3.Request | ||
| import java.io.IOException | ||
| import java.io.InputStream | ||
|
|
||
| /** | ||
| * Custom Glide module for the Bitwarden app that configures Glide to use an OkHttpClient | ||
| * with mTLS (mutual TLS) support. | ||
| * | ||
| * This ensures that all icon/image loading requests through Glide present the client certificate | ||
| * for mutual TLS authentication, allowing them to pass through Cloudflare's mTLS checks. | ||
| * | ||
| * The configuration mirrors the SSL setup used in RetrofitsImpl for API calls. | ||
| */ | ||
| @GlideModule | ||
| class BitwardenAppGlideModule : AppGlideModule() { | ||
|
|
||
| /** | ||
| * Entry point to access Hilt-provided dependencies from non-Hilt managed classes. | ||
| */ | ||
| @EntryPoint | ||
| @InstallIn(SingletonComponent::class) | ||
| interface BitwardenGlideEntryPoint { | ||
| /** | ||
| * Provides access to [CertificateManager] for mTLS certificate management. | ||
| */ | ||
| fun certificateManager(): CertificateManager | ||
| } | ||
|
|
||
| override fun registerComponents(context: Context, glide: Glide, registry: Registry) { | ||
| // Get CertificateManager from Hilt | ||
| val entryPoint = EntryPointAccessors.fromApplication( | ||
| context.applicationContext, | ||
| BitwardenGlideEntryPoint::class.java, | ||
| ) | ||
| val certificateManager = entryPoint.certificateManager() | ||
|
|
||
| val okHttpClient = certificateManager.createMtlsOkHttpClient() | ||
|
|
||
| // Register custom ModelLoader that uses our mTLS OkHttpClient | ||
| registry.replace( | ||
| GlideUrl::class.java, | ||
| InputStream::class.java, | ||
| OkHttpModelLoaderFactory(okHttpClient), | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Custom ModelLoaderFactory for Glide 5.x that uses our mTLS-configured OkHttpClient. | ||
| */ | ||
| private class OkHttpModelLoaderFactory( | ||
| private val client: OkHttpClient, | ||
| ) : ModelLoaderFactory<GlideUrl, InputStream> { | ||
|
|
||
| override fun build( | ||
| multiFactory: MultiModelLoaderFactory, | ||
| ): ModelLoader<GlideUrl, InputStream> = OkHttpModelLoader(client) | ||
|
|
||
| override fun teardown() { | ||
| // No-op | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Custom ModelLoader that uses OkHttpClient to load images. | ||
| */ | ||
| private class OkHttpModelLoader( | ||
| private val client: OkHttpClient, | ||
| ) : ModelLoader<GlideUrl, InputStream> { | ||
|
|
||
| override fun buildLoadData( | ||
| model: GlideUrl, | ||
| width: Int, | ||
| height: Int, | ||
| options: Options, | ||
| ): ModelLoader.LoadData<InputStream> { | ||
| return ModelLoader.LoadData(model, OkHttpDataFetcher(client, model)) | ||
| } | ||
|
|
||
| override fun handles(model: GlideUrl): Boolean = true | ||
| } | ||
|
|
||
| /** | ||
| * DataFetcher that uses OkHttpClient to execute HTTP requests. | ||
| */ | ||
| private class OkHttpDataFetcher( | ||
| private val client: OkHttpClient, | ||
| private val url: GlideUrl, | ||
| ) : DataFetcher<InputStream> { | ||
|
|
||
| private var call: Call? = null | ||
|
|
||
| override fun loadData( | ||
| priority: Priority, | ||
| callback: DataFetcher.DataCallback<in InputStream>, | ||
| ) { | ||
| val request = Request.Builder() | ||
| .url(url.toStringUrl()) | ||
| .build() | ||
|
|
||
| call = client.newCall(request) | ||
|
|
||
| val localCall = client.newCall(request).also { call = it } | ||
|
|
||
| val response = try { | ||
| localCall.execute() | ||
| } catch (e: IOException) { | ||
| callback.onLoadFailed(e) | ||
| return | ||
| } | ||
|
|
||
| if (response.isSuccessful) { | ||
| callback.onDataReady(response.body.byteStream()) | ||
| } else { | ||
| callback.onLoadFailed(HttpException(response.message, response.code)) | ||
| } | ||
| } | ||
|
|
||
| override fun cleanup() { | ||
| // Response body cleanup is handled by Glide | ||
| } | ||
|
|
||
| override fun cancel() { | ||
| call?.cancel() | ||
| } | ||
|
|
||
| override fun getDataClass(): Class<InputStream> = InputStream::class.java | ||
|
|
||
| override fun getDataSource(): DataSource = DataSource.REMOTE | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
app/src/test/kotlin/com/x8bit/bitwarden/ui/platform/glide/BitwardenAppGlideModuleTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package com.x8bit.bitwarden.ui.platform.glide | ||
|
|
||
| import org.junit.jupiter.api.Assertions.assertNotNull | ||
| import org.junit.jupiter.api.Assertions.assertTrue | ||
| import org.junit.jupiter.api.Test | ||
|
|
||
| /** | ||
| * Test class for [BitwardenAppGlideModule] to verify mTLS configuration is properly applied | ||
| * to Glide without requiring a real mTLS server. | ||
| * | ||
| * These tests verify the module's structure and that it can be instantiated. | ||
| * Full integration testing requires running the app and checking logcat for | ||
| * "BitwardenGlide" logs when images are loaded. | ||
| */ | ||
| class BitwardenAppGlideModuleTest { | ||
|
|
||
| @Test | ||
| fun `BitwardenAppGlideModule should be instantiable`() { | ||
| // Verify the module can be created | ||
| val module = BitwardenAppGlideModule() | ||
|
|
||
| assertNotNull(module) | ||
| } | ||
|
|
||
| @Test | ||
| fun `BitwardenAppGlideModule should have EntryPoint interface for Hilt dependency injection`() { | ||
| // Verify the Hilt EntryPoint interface exists for accessing CertificateManager | ||
| val entryPointInterface = BitwardenAppGlideModule::class.java | ||
| .declaredClasses | ||
| .firstOrNull { it.simpleName == "BitwardenGlideEntryPoint" } | ||
|
|
||
| assertNotNull(entryPointInterface) | ||
| } | ||
|
|
||
| @Test | ||
| fun `BitwardenGlideEntryPoint should declare certificateManager method`() { | ||
| // Verify the EntryPoint has the required method to access CertificateManager | ||
| val entryPointInterface = BitwardenAppGlideModule::class.java | ||
| .declaredClasses | ||
| .firstOrNull { it.simpleName == "BitwardenGlideEntryPoint" } | ||
|
|
||
| assertNotNull(entryPointInterface, "BitwardenGlideEntryPoint must exist") | ||
|
|
||
| val methods = entryPointInterface!!.declaredMethods | ||
| val hasCertificateManagerMethod = methods.any { it.name == "certificateManager" } | ||
|
|
||
| assertTrue(hasCertificateManagerMethod) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
network/src/main/kotlin/com/bitwarden/network/ssl/CertificateProviderExtensions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package com.bitwarden.network.ssl | ||
|
|
||
| import okhttp3.OkHttpClient | ||
| import java.security.KeyStore | ||
| import javax.net.ssl.SSLContext | ||
| import javax.net.ssl.TrustManager | ||
| import javax.net.ssl.TrustManagerFactory | ||
| import javax.net.ssl.X509TrustManager | ||
|
|
||
| /** | ||
| * Creates an [SSLContext] configured with mTLS support using this [CertificateProvider]. | ||
| * | ||
| * The returned SSLContext will present the client certificate from this provider during | ||
| * TLS handshakes, enabling mutual TLS authentication. | ||
| */ | ||
| fun CertificateProvider.createSslContext(): SSLContext = | ||
| SSLContext.getInstance("TLS").apply { | ||
| init( | ||
| arrayOf( | ||
| BitwardenX509ExtendedKeyManager(certificateProvider = this@createSslContext), | ||
| ), | ||
| createSslTrustManagers(), | ||
| null, | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Creates an [OkHttpClient] configured with mTLS support using this [CertificateProvider]. | ||
| * | ||
| * The returned client will present the client certificate from this provider during TLS | ||
| * handshakes, allowing requests to pass through mTLS checks. | ||
| */ | ||
| fun CertificateProvider.createMtlsOkHttpClient(): OkHttpClient { | ||
| val sslContext = createSslContext() | ||
| val trustManagers = createSslTrustManagers() | ||
|
|
||
| return OkHttpClient.Builder() | ||
| .sslSocketFactory( | ||
| sslContext.socketFactory, | ||
| trustManagers.first() as X509TrustManager, | ||
| ) | ||
| .build() | ||
| } | ||
|
|
||
| /** | ||
| * Creates default [TrustManager]s for verifying server certificates. | ||
| * | ||
| * Uses the system's default trust anchors (trusted CA certificates). | ||
| */ | ||
| private fun createSslTrustManagers(): Array<TrustManager> = | ||
| TrustManagerFactory | ||
| .getInstance(TrustManagerFactory.getDefaultAlgorithm()) | ||
| .apply { init(null as KeyStore?) } | ||
| .trustManagers | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๐