From 2cef6a5bb4cb4afb1b1f1b704f83c1565e441825 Mon Sep 17 00:00:00 2001 From: "(´⌣`ʃƪ)" Date: Sun, 3 Oct 2021 01:32:15 -0700 Subject: Replace Timber with logcat (#1509) * Replace Timber with logcat (#1505) * Add extension for asLog which takes a message param Co-authored-by: Aditya Wasan --- .../aps/util/git/sshj/OpenKeychainKeyProvider.kt | 8 +++--- .../java/dev/msfjarvis/aps/util/git/sshj/SshKey.kt | 14 ++++----- .../dev/msfjarvis/aps/util/git/sshj/SshjConfig.kt | 33 +++++++++++++--------- .../aps/util/git/sshj/SshjSessionFactory.kt | 18 ++++++------ 4 files changed, 40 insertions(+), 33 deletions(-) (limited to 'app/src/main/java/dev/msfjarvis/aps/util/git/sshj') diff --git a/app/src/main/java/dev/msfjarvis/aps/util/git/sshj/OpenKeychainKeyProvider.kt b/app/src/main/java/dev/msfjarvis/aps/util/git/sshj/OpenKeychainKeyProvider.kt index d726c353..3a40b8a2 100644 --- a/app/src/main/java/dev/msfjarvis/aps/util/git/sshj/OpenKeychainKeyProvider.kt +++ b/app/src/main/java/dev/msfjarvis/aps/util/git/sshj/OpenKeychainKeyProvider.kt @@ -9,7 +9,6 @@ import android.content.Intent import androidx.activity.result.IntentSenderRequest import androidx.core.content.edit import androidx.lifecycle.lifecycleScope -import com.github.ajalt.timberkt.d import dev.msfjarvis.aps.util.extensions.OPENPGP_PROVIDER import dev.msfjarvis.aps.util.extensions.sharedPrefs import dev.msfjarvis.aps.util.settings.PreferenceKeys @@ -21,6 +20,7 @@ import kotlin.coroutines.suspendCoroutine import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import logcat.logcat import net.schmizz.sshj.common.DisconnectReason import net.schmizz.sshj.common.KeyType import net.schmizz.sshj.userauth.UserAuthException @@ -81,7 +81,7 @@ class OpenKeychainKeyProvider private constructor(val activity: ContinuationCont sshServiceConnection.connect( object : SshAuthenticationConnection.OnBound { override fun onBound(sshAgent: ISshAuthenticationService) { - d { "Bound to SshAuthenticationApi: $OPENPGP_PROVIDER" } + logcat { "Bound to SshAuthenticationApi: $OPENPGP_PROVIDER" } cont.resume(SshAuthenticationApi(context, sshAgent)) } @@ -134,7 +134,7 @@ class OpenKeychainKeyProvider private constructor(val activity: ContinuationCont request: Request, resultOfUserInteraction: Intent? = null ): ApiResponse { - d { "executeRequest($request) called" } + logcat { "executeRequest($request) called" } val result = withContext(Dispatchers.Main) { // If the request required user interaction, the data returned from the @@ -142,7 +142,7 @@ class OpenKeychainKeyProvider private constructor(val activity: ContinuationCont // is used as the real request. sshServiceApi.executeApi(resultOfUserInteraction ?: request.toIntent())!! } - return parseResult(request, result).also { d { "executeRequest($request): $it" } } + return parseResult(request, result).also { logcat { "executeRequest($request): $it" } } } private suspend fun parseResult(request: Request, result: Intent): ApiResponse { diff --git a/app/src/main/java/dev/msfjarvis/aps/util/git/sshj/SshKey.kt b/app/src/main/java/dev/msfjarvis/aps/util/git/sshj/SshKey.kt index 66617a18..f2db6067 100644 --- a/app/src/main/java/dev/msfjarvis/aps/util/git/sshj/SshKey.kt +++ b/app/src/main/java/dev/msfjarvis/aps/util/git/sshj/SshKey.kt @@ -16,8 +16,6 @@ import android.util.Base64 import androidx.core.content.edit import androidx.security.crypto.EncryptedFile import androidx.security.crypto.MasterKey -import com.github.ajalt.timberkt.d -import com.github.ajalt.timberkt.e import com.github.michaelbull.result.getOrElse import com.github.michaelbull.result.runCatching import dev.msfjarvis.aps.Application @@ -39,6 +37,8 @@ import javax.crypto.SecretKeyFactory import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext +import logcat.asLog +import logcat.logcat import net.i2p.crypto.eddsa.EdDSAPrivateKey import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable import net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec @@ -101,7 +101,7 @@ object SshKey { // It is fine to swallow the exception here since it will reappear when the key // is // used for SSH authentication and can then be shown in the UI. - d(error) + logcat { error.asLog() } false } } @@ -317,13 +317,13 @@ object SshKey { override fun getPublic(): PublicKey = runCatching { androidKeystore.sshPublicKey!! }.getOrElse { error -> - e(error) + logcat { error.asLog() } throw IOException("Failed to get public key '$KEYSTORE_ALIAS' from Android Keystore", error) } override fun getPrivate(): PrivateKey = runCatching { androidKeystore.sshPrivateKey!! }.getOrElse { error -> - e(error) + logcat { error.asLog() } throw IOException( "Failed to access private key '$KEYSTORE_ALIAS' from Android Keystore", error @@ -337,7 +337,7 @@ object SshKey { override fun getPublic(): PublicKey = runCatching { parseSshPublicKey(sshPublicKey!!)!! }.getOrElse { error -> - e(error) + logcat { error.asLog() } throw IOException("Failed to get the public key for wrapped ed25519 key", error) } @@ -353,7 +353,7 @@ object SshKey { ) } .getOrElse { error -> - e(error) + logcat { error.asLog() } throw IOException("Failed to unwrap wrapped ed25519 key", error) } diff --git a/app/src/main/java/dev/msfjarvis/aps/util/git/sshj/SshjConfig.kt b/app/src/main/java/dev/msfjarvis/aps/util/git/sshj/SshjConfig.kt index f8053ef3..f0405e04 100644 --- a/app/src/main/java/dev/msfjarvis/aps/util/git/sshj/SshjConfig.kt +++ b/app/src/main/java/dev/msfjarvis/aps/util/git/sshj/SshjConfig.kt @@ -4,8 +4,6 @@ */ package dev.msfjarvis.aps.util.git.sshj -import com.github.ajalt.timberkt.Timber -import com.github.ajalt.timberkt.d import com.github.michaelbull.result.runCatching import com.hierynomus.sshj.key.KeyAlgorithms import com.hierynomus.sshj.transport.cipher.BlockCiphers @@ -14,6 +12,12 @@ import com.hierynomus.sshj.transport.kex.ExtInfoClientFactory import com.hierynomus.sshj.transport.mac.Macs import com.hierynomus.sshj.userauth.keyprovider.OpenSSHKeyV1KeyFile import java.security.Security +import logcat.LogPriority.ERROR +import logcat.LogPriority.INFO +import logcat.LogPriority.VERBOSE +import logcat.LogPriority.WARN +import logcat.asLog +import logcat.logcat import net.schmizz.keepalive.KeepAliveProvider import net.schmizz.sshj.ConfigImpl import net.schmizz.sshj.common.LoggerFactory @@ -49,7 +53,9 @@ fun setUpBouncyCastleForSshj() { runCatching { Class.forName("sun.security.jca.Providers") } Security.insertProviderAt(BouncyCastleProvider(), bcIndex + 1) } - d { "JCE providers: ${Security.getProviders().joinToString { "${it.name} (${it.version})" }}" } + logcat("setUpBouncyCastleForSshj") { + "JCE providers: ${Security.getProviders().joinToString { "${it.name} (${it.version})" }}" + } // Prevent sshj from forwarding all cryptographic operations to BC. SecurityUtils.setRegisterBouncyCastle(false) SecurityUtils.setSecurityProvider(null) @@ -147,10 +153,9 @@ private abstract class AbstractLogger(private val name: String) : Logger { override fun error(marker: Marker?, msg: String, t: Throwable?) = error(msg, t) } -object TimberLoggerFactory : LoggerFactory { - private class TimberLogger(name: String) : AbstractLogger(name) { +object LogcatLoggerFactory : LoggerFactory { + private class LogcatLogger(name: String) : AbstractLogger(name) { - // We defer the log level checks to Timber. override fun isTraceEnabled() = true override fun isDebugEnabled() = true override fun isInfoEnabled() = true @@ -163,39 +168,39 @@ object TimberLoggerFactory : LoggerFactory { private fun String.fix() = replace("""(?!<\\)\{\}""".toRegex(), "%s") override fun t(message: String, t: Throwable?, vararg args: Any?) { - Timber.tag(name).v(t, message.fix(), *args) + logcat(name, VERBOSE) { message.fix().format(*args) + (t?.asLog() ?: "") } } override fun d(message: String, t: Throwable?, vararg args: Any?) { - Timber.tag(name).d(t, message.fix(), *args) + logcat(name) { message.fix().format(*args) + (t?.asLog() ?: "") } } override fun i(message: String, t: Throwable?, vararg args: Any?) { - Timber.tag(name).i(t, message.fix(), *args) + logcat(name, INFO) { message.fix().format(*args) + (t?.asLog() ?: "") } } override fun w(message: String, t: Throwable?, vararg args: Any?) { - Timber.tag(name).w(t, message.fix(), *args) + logcat(name, WARN) { message.fix().format(*args) + (t?.asLog() ?: "") } } override fun e(message: String, t: Throwable?, vararg args: Any?) { - Timber.tag(name).e(t, message.fix(), *args) + logcat(name, ERROR) { message.fix().format(*args) + (t?.asLog() ?: "") } } } override fun getLogger(name: String): Logger { - return TimberLogger(name) + return LogcatLogger(name) } override fun getLogger(clazz: Class<*>): Logger { - return TimberLogger(clazz.name) + return LogcatLogger(clazz.name) } } class SshjConfig : ConfigImpl() { init { - loggerFactory = TimberLoggerFactory + loggerFactory = LogcatLoggerFactory keepAliveProvider = KeepAliveProvider.HEARTBEAT version = "OpenSSH_8.2p1 Ubuntu-4ubuntu0.1" diff --git a/app/src/main/java/dev/msfjarvis/aps/util/git/sshj/SshjSessionFactory.kt b/app/src/main/java/dev/msfjarvis/aps/util/git/sshj/SshjSessionFactory.kt index bc068f52..79438bcc 100644 --- a/app/src/main/java/dev/msfjarvis/aps/util/git/sshj/SshjSessionFactory.kt +++ b/app/src/main/java/dev/msfjarvis/aps/util/git/sshj/SshjSessionFactory.kt @@ -5,8 +5,6 @@ package dev.msfjarvis.aps.util.git.sshj import android.util.Base64 -import com.github.ajalt.timberkt.d -import com.github.ajalt.timberkt.w import com.github.michaelbull.result.getOrElse import com.github.michaelbull.result.runCatching import dev.msfjarvis.aps.util.git.operation.CredentialFinder @@ -20,6 +18,8 @@ import kotlin.coroutines.Continuation import kotlin.coroutines.suspendCoroutine import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking +import logcat.LogPriority.WARN +import logcat.logcat import net.schmizz.sshj.SSHClient import net.schmizz.sshj.common.Buffer.PlainBuffer import net.schmizz.sshj.common.DisconnectReason @@ -76,7 +76,7 @@ class SshjSessionFactory(private val authMethod: SshAuthMethod, private val host ): RemoteSession { return currentSession ?: SshjSession(uri, uri.user, authMethod, hostKeyFile).connect().also { - d { "New SSH connection created" } + logcat { "New SSH connection created" } currentSession = it } } @@ -96,13 +96,15 @@ private fun makeTofuHostKeyVerifier(hostKeyFile: File): HostKeyVerifier { digest.update(PlainBuffer().putPublicKey(key).compactData) val digestData = digest.digest() val hostKeyEntry = "SHA256:${Base64.encodeToString(digestData, Base64.NO_WRAP)}" - d { "Trusting host key on first use: $hostKeyEntry" } + logcat(SshjSessionFactory::class.java.simpleName) { + "Trusting host key on first use: $hostKeyEntry" + } hostKeyFile.writeText(hostKeyEntry) true } } else { val hostKeyEntry = hostKeyFile.readText() - d { "Pinned host key: $hostKeyEntry" } + logcat(SshjSessionFactory::class.java.simpleName) { "Pinned host key: $hostKeyEntry" } return FingerprintVerifier.getInstance(hostKeyEntry) } } @@ -122,12 +124,12 @@ private class SshjSession( // URIish's String constructor cannot handle '@' in the user part of the URI and the URL // constructor can't be used since Java's URL does not recognize the ssh scheme. We thus // need to patch everything up ourselves. - d { "Before fixup: user=${uri.user}, host=${uri.host}" } + logcat { "Before fixup: user=${uri.user}, host=${uri.host}" } val userPlusHost = "${uri.user}@${uri.host}" val realUser = userPlusHost.substringBeforeLast('@') val realHost = userPlusHost.substringAfterLast('@') uri.setUser(realUser).setHost(realHost).also { - d { "After fixup: user=${it.user}, host=${it.host}" } + logcat { "After fixup: user=${it.user}, host=${it.host}" } } } else { uri @@ -162,7 +164,7 @@ private class SshjSession( override fun exec(commandName: String?, timeout: Int): Process { if (currentCommand != null) { - w { "Killing old command" } + logcat(WARN) { "Killing old command" } disconnect() } val session = ssh.startSession() -- cgit v1.2.3