-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Create DataBrokerConnectorFactory
- Loading branch information
Showing
4 changed files
with
111 additions
and
163 deletions.
There are no files selected for viewing
This file contains 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 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
101 changes: 101 additions & 0 deletions
101
...main/kotlin/org/eclipse/kuksa/testapp/databroker/connection/DataBrokerConnectorFactory.kt
This file contains 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,101 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.eclipse.kuksa.testapp.databroker.connection | ||
|
||
import android.content.Context | ||
import android.net.Uri | ||
import io.grpc.ChannelCredentials | ||
import io.grpc.Grpc | ||
import io.grpc.ManagedChannel | ||
import io.grpc.ManagedChannelBuilder | ||
import io.grpc.TlsChannelCredentials | ||
import org.eclipse.kuksa.DataBrokerConnector | ||
import org.eclipse.kuksa.TimeoutConfig | ||
import org.eclipse.kuksa.authentication.JsonWebToken | ||
import org.eclipse.kuksa.testapp.databroker.model.ConnectionInfo | ||
import org.eclipse.kuksa.testapp.extension.readAsText | ||
import java.io.IOException | ||
import java.util.concurrent.TimeUnit | ||
|
||
class DataBrokerConnectorFactory { | ||
private val timeoutConfig = TimeoutConfig(5, TimeUnit.SECONDS) | ||
|
||
@Throws(IOException::class) | ||
fun create(context: Context, connectionInfo: ConnectionInfo): DataBrokerConnector { | ||
val managedChannel = if (connectionInfo.isTlsEnabled) { | ||
createSecureManagedChannel(context, connectionInfo) | ||
} else { | ||
createInsecureManagedChannel(connectionInfo) | ||
} | ||
|
||
val jsonWebToken = loadJsonWebToken(context, connectionInfo) | ||
|
||
return DataBrokerConnector(managedChannel, jsonWebToken).apply { | ||
timeoutConfig = this@DataBrokerConnectorFactory.timeoutConfig | ||
} | ||
} | ||
|
||
private fun createInsecureManagedChannel(connectionInfo: ConnectionInfo): ManagedChannel { | ||
val host = connectionInfo.host.trim() | ||
val port = connectionInfo.port | ||
|
||
return ManagedChannelBuilder | ||
.forAddress(host, port) | ||
.usePlaintext() | ||
.build() | ||
} | ||
|
||
@Throws(IOException::class) | ||
private fun createSecureManagedChannel(context: Context, connectionInfo: ConnectionInfo): ManagedChannel { | ||
val certificate = connectionInfo.certificate | ||
val rootCertFile = context.contentResolver.openInputStream(certificate.uri) | ||
|
||
val tlsCredentials: ChannelCredentials = TlsChannelCredentials.newBuilder() | ||
.trustManager(rootCertFile) | ||
.build() | ||
|
||
val host = connectionInfo.host.trim() | ||
val port = connectionInfo.port | ||
val channelBuilder = Grpc | ||
.newChannelBuilderForAddress(host, port, tlsCredentials) | ||
|
||
val overrideAuthority = certificate.overrideAuthority.trim() | ||
val hasOverrideAuthority = overrideAuthority.isNotEmpty() | ||
if (hasOverrideAuthority) { | ||
channelBuilder.overrideAuthority(overrideAuthority) | ||
} | ||
|
||
return channelBuilder.build() | ||
} | ||
|
||
@Throws(IOException::class) | ||
private fun loadJsonWebToken(context: Context, connectionInfo: ConnectionInfo): JsonWebToken? { | ||
val isAuthenticationDisabled = !connectionInfo.isAuthenticationEnabled | ||
val jwtUriPath = connectionInfo.jwtUriPath | ||
if (isAuthenticationDisabled || jwtUriPath.isNullOrEmpty()) { | ||
return null | ||
} | ||
|
||
val uri: Uri = Uri.parse(jwtUriPath) | ||
val token = uri.readAsText(context) | ||
|
||
return JsonWebToken(token) | ||
} | ||
} |
74 changes: 0 additions & 74 deletions
74
app/src/main/kotlin/org/eclipse/kuksa/testapp/extension/ConnectionInfoExtension.kt
This file was deleted.
Oops, something went wrong.