Skip to content

Commit

Permalink
feat: Throw GatewayUnregisteredException when gateway is not regist…
Browse files Browse the repository at this point in the history
…ered (#347)

… with its Internet gateway
  • Loading branch information
gnarea authored Oct 4, 2023
1 parent 8b84e97 commit b3635a1
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/src/main/java/tech/relaycorp/awaladroid/Awala.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public object Awala {
/**
* Set up the endpoint library.
*/
@Throws(GatewayUnregisteredException::class)
public suspend fun setUp(context: Context) {
val keystoreRoot =
FileKeystoreRoot(File(context.filesDir, "awaladroid${File.separator}keystores"))
Expand Down
34 changes: 26 additions & 8 deletions lib/src/main/java/tech/relaycorp/awaladroid/GatewayClientImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ internal constructor(
@Throws(
RegistrationFailedException::class,
GatewayProtocolException::class,
GatewayUnregisteredException::class,
)
internal suspend fun registerEndpoint(keyPair: KeyPair): PrivateNodeRegistration =
withContext(coroutineContext) {
Expand Down Expand Up @@ -119,6 +120,7 @@ internal constructor(
ServiceInteractor.BindFailedException::class,
ServiceInteractor.SendFailedException::class,
GatewayProtocolException::class,
GatewayUnregisteredException::class,
)
private suspend fun preRegister(): ByteArray {
val interactor = serviceInteractorBuilder().apply {
Expand All @@ -132,15 +134,24 @@ internal constructor(
return suspendCoroutine { cont ->
val request = android.os.Message.obtain(null, PREREGISTRATION_REQUEST)
interactor.sendMessage(request) { replyMessage ->
if (replyMessage.what != REGISTRATION_AUTHORIZATION) {
interactor.unbind()
cont.resumeWithException(
GatewayProtocolException("Pre-registration failed, received wrong reply"),
)
return@sendMessage
}
interactor.unbind()
cont.resume(replyMessage.data.getByteArray("auth")!!)
when (replyMessage.what) {
REGISTRATION_AUTHORIZATION -> {
cont.resume(replyMessage.data.getByteArray("auth")!!)
}
GATEWAY_NOT_REGISTERED -> {
cont.resumeWithException(
GatewayUnregisteredException("Gateway not registered"),
)
}
else -> {
cont.resumeWithException(
GatewayProtocolException(
"Pre-registration failed, received wrong reply",
),
)
}
}
}
}
}
Expand Down Expand Up @@ -209,6 +220,7 @@ internal constructor(
internal companion object {
internal const val PREREGISTRATION_REQUEST = 1
internal const val REGISTRATION_AUTHORIZATION = 2
internal const val GATEWAY_NOT_REGISTERED = 4
}
}

Expand All @@ -230,6 +242,12 @@ public open class GatewayProtocolException(message: String, cause: Throwable? =
public class GatewayBindingException(message: String, cause: Throwable? = null) :
GatewayException(message, cause)

/**
* The gateway isn't yet registered with its Internet peer.
*/
public class GatewayUnregisteredException(message: String, cause: Throwable? = null) :
GatewayException(message, cause)

/**
* Failure to register a first-party endpoint.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tech.relaycorp.awaladroid.endpoint
import tech.relaycorp.awaladroid.Awala
import tech.relaycorp.awaladroid.AwaladroidException
import tech.relaycorp.awaladroid.GatewayProtocolException
import tech.relaycorp.awaladroid.GatewayUnregisteredException
import tech.relaycorp.awaladroid.RegistrationFailedException
import tech.relaycorp.awaladroid.SetupPendingException
import tech.relaycorp.awaladroid.common.Logging.logger
Expand Down Expand Up @@ -153,6 +154,7 @@ internal constructor(
@Throws(
RegistrationFailedException::class,
GatewayProtocolException::class,
GatewayUnregisteredException::class,
PersistenceException::class,
SetupPendingException::class,
)
Expand Down Expand Up @@ -228,6 +230,7 @@ internal constructor(
@Throws(
RegistrationFailedException::class,
GatewayProtocolException::class,
GatewayUnregisteredException::class,
PersistenceException::class,
SetupPendingException::class,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package tech.relaycorp.awaladroid.endpoint

import tech.relaycorp.awaladroid.GatewayUnregisteredException
import tech.relaycorp.relaynet.keystores.PrivateKeyStore
import tech.relaycorp.relaynet.wrappers.nodeId

internal class HandleGatewayCertificateChange(
private val privateKeyStore: PrivateKeyStore,
) {

@Throws(GatewayUnregisteredException::class)
suspend operator fun invoke() {
privateKeyStore.retrieveAllIdentityKeys()
.mapNotNull { FirstPartyEndpoint.load(it.nodeId) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.relaycorp.awaladroid.endpoint

import tech.relaycorp.awaladroid.GatewayUnregisteredException
import tech.relaycorp.relaynet.keystores.PrivateKeyStore
import tech.relaycorp.relaynet.wrappers.nodeId
import tech.relaycorp.relaynet.wrappers.x509.Certificate
Expand All @@ -11,6 +12,7 @@ internal class RenewExpiringCertificates(
private val firstPartyEndpointLoader: suspend (String) -> FirstPartyEndpoint?,
) {

@Throws(GatewayUnregisteredException::class)
suspend operator fun invoke() {
privateKeyStore.retrieveAllIdentityKeys()
.mapNotNull { firstPartyEndpointLoader(it.nodeId) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@ internal class GatewayClientImplTest : MockContextTestCase() {
gatewayClient.registerEndpoint(KeyPairSet.PRIVATE_ENDPOINT)
}

@Test(expected = GatewayUnregisteredException::class)
internal fun registerEndpoint_withFailedRegistrationDueToGatewayUnregistered() =
coroutineScope.runTest {
val replyMessage = Message.obtain(null, GatewayClientImpl.GATEWAY_NOT_REGISTERED)
whenever(serviceInteractor.sendMessage(any(), any())).thenAnswer {
it.getArgument<((Message) -> Unit)?>(1)(replyMessage)
}

pdcClient = MockPDCClient()

gatewayClient.registerEndpoint(KeyPairSet.PRIVATE_ENDPOINT)
}

private fun buildPnra() = PrivateNodeRegistrationAuthorization(
ZonedDateTime.now().plusDays(1),
PDACertPath.PRIVATE_GW.serialize(),
Expand Down

0 comments on commit b3635a1

Please sign in to comment.