Skip to content

Commit

Permalink
#59 Add new parameter to provide credentials as a base64 encoded string
Browse files Browse the repository at this point in the history
  • Loading branch information
cosic committed Oct 13, 2024
1 parent 66df372 commit 24566e4
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 58 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# 1.4.3

##### Add
* [issue#58] Add `publishSocketTimeoutInSeconds` param to change the socket timeout for publish requests in seconds.
* [issue#58] Add new `publishSocketTimeoutInSeconds` parameter to change the socket timeout for publish requests in seconds.
* [issue#59] Add new `credentials` parameter to provide credentials as a base64 encoded string.

##### Breaking Changes
* Remove support of Sonatype. It means that you can't use the plugin from Maven Central. You must to use the Gradle Portal.
Expand All @@ -13,6 +14,7 @@
}
}
```
* Remove support of `clientId` and `clientSecret` CLI params. Use `credentials` param instead.

# 1.4.2

Expand Down
50 changes: 36 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,21 @@ huaweiPublish {
instances {
create("release") {
/**
* Path to json file with AppGallery credentials params (`client_id` and `client_secret`).
* Description: The AppGallery credentials params (`client_id` and `client_secret`) in json format witch encoded to Base64.
* How to get credentials see [AppGallery Connect API Getting Started](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agcapi-getstarted).
* Plugin credential json example:
*
* Credential json example:
* {
* "client_id": "<CLIENT_ID>",
* "client_secret": "<CLIENT_SECRET>"
* }
* Base64 encoded value example: "ewogICAgImNsaWVudF9pZCI6ICI8Q0xJRU5UX0lEPiIsCiAgICAiY2xpZW50X3NlY3JldCI6ICI8Q0xJRU5UX1NFQ1JFVD4iCn0="
*
* Type: String (Optional)
* Default value: `null` (but plugin wait that you provide credentials by CLI params)
* CLI: `--credentialsPath`
*/
credentialsPath = "$rootDir/huawei-credentials-release.json"
* CLI: `--credentials`
*/
credentials = "<BASE64_ENCODED_CREDENTIALS>"

/**
* 'apk' or 'aab' for corresponding build format.
Expand Down Expand Up @@ -160,9 +162,28 @@ huaweiPublish {
huaweiPublish {
instances {
create("release") {
/**
* Description: The AppGallery credentials params (`client_id` and `client_secret`) in json format witch encoded to Base64.
* How to get credentials see [AppGallery Connect API Getting Started](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agcapi-getstarted).
* High priority than `credentialsPath` parameter.
*
* Credential json example:
* {
* "client_id": "<CLIENT_ID>",
* "client_secret": "<CLIENT_SECRET>"
* }
* Base64 encoded value example: "ewogICAgImNsaWVudF9pZCI6ICI8Q0xJRU5UX0lEPiIsCiAgICAiY2xpZW50X3NlY3JldCI6ICI8Q0xJRU5UX1NFQ1JFVD4iCn0="
*
* Type: String (Optional)
* Default value: `null` (but plugin wait that you provide credentials by CLI params)
* CLI: `--credentials`
*/
credentials = "<BASE64_ENCODED_CREDENTIALS>"

/**
* Description: Path to json file with AppGallery credentials params (`client_id` and `client_secret`).
* How to get credentials see [AppGallery Connect API Getting Started](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agcapi-getstarted).
* Low priority than `credentials` parameter.
*
* Plugin credential json example:
* {
Expand Down Expand Up @@ -350,7 +371,8 @@ huaweiPublish {
huaweiPublish {
instances {
release {
credentialsPath = "$rootDir/huawei-credentials-release.json"
credentials = "<BASE64_ENCODED_CREDENTIALS>" // High priority than `credentialsPath`;
credentialsPath = "$rootDir/huawei-credentials-release.json" // Low priority than `credentials`;
deployType = "publish"
buildFormat = "apk"
buildFile = "${buildDir}/app/outputs/apk/release/app-release.apk"
Expand Down Expand Up @@ -427,7 +449,8 @@ CLI params are more priority than gradle configuration params.

```bash
./gradlew assembleRelease publishHuaweiAppGalleryRelease \
--credentialsPath="/sample-kotlin/huawei-credentials.json" \
--credentials = "<BASE64_ENCODED_CREDENTIALS>" \ # High priority than `credentialsPath`;
--credentialsPath="./sample-kotlin/huawei-credentials.json" \ # Low priority than `credentials`;
--deployType=publish \
--buildFormat=apk \
--buildFile="./app/outputs/apk/release/app-release.apk"
Expand All @@ -454,7 +477,7 @@ From gradle build script:
huaweiPublish {
instances {
release {
credentialsPath = "$rootDir/sample1/huawei-credentials.json"
credentials = "<BASE64_ENCODED_CREDENTIALS>"
deployType = "draft"
}
}
Expand All @@ -465,7 +488,7 @@ or execute from command line:

```bash
./gradlew assembleRelease publishHuaweiAppGalleryRelease \
--credentialsPath="$rootDir/sample1/huawei-credentials.json" \
--credentials = "<BASE64_ENCODED_CREDENTIALS>" \
--deployType=draft
```

Expand All @@ -481,7 +504,7 @@ From gradle build script:
huaweiPublish {
instances {
release {
credentialsPath = "$rootDir/sample1/huawei-credentials.json"
credentials = "<BASE64_ENCODED_CREDENTIALS>"
buildFormat = "aab"
}
}
Expand All @@ -491,7 +514,7 @@ or execute from command line:

```bash
./gradlew assembleRelease publishHuaweiAppGalleryRelease \
--credentialsPath="$rootDir/sample1/huawei-credentials.json" \
--credentials = "<BASE64_ENCODED_CREDENTIALS>" \
--buildFormat=aab
```

Expand Down Expand Up @@ -522,7 +545,7 @@ From gradle build script:
huaweiPublish {
instances {
release {
credentialsPath = "$rootDir/sample1/huawei-credentials.json"
credentials = "<BASE64_ENCODED_CREDENTIALS>"
releasePhase {
startTime = "2020-11-13T08:01:02+0300"
endTime = "2020-11-20T15:30:00+0300"
Expand All @@ -537,8 +560,7 @@ or execute from command line:

```bash
./gradlew assembleRelease publishHuaweiAppGalleryRelease \
--clientId=<CLIENT_ID> \
--clientSecret=<CLIENT_SECRET> \
--credentials = "<BASE64_ENCODED_CREDENTIALS>" \
--releasePhaseStartTime=2020-11-13T08:01:02+0300 \
--releasePhaseEndTime=2020-11-20T15:30:00+0300 \
--releasePhasePercent=10.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ internal data class HuaweiPublishCliParam(
val publishTimeoutMs: String? = null,
val publishPeriodMs: String? = null,
val publishSocketTimeoutInSeconds: String? = null,
val credentials: String? = null,
val credentialsPath: String? = null,
val clientId: String? = null,
val clientSecret: String? = null,
val buildFormat: BuildFormat? = null,
val buildFile: String? = null,
val releaseTime: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class HuaweiPublishExtensionConfig(
* For example:
* var param by GradleProperty(project, String::class.java)
*/
var credentials: String? = null
var credentialsPath: String? = null
var deployType = DeployType.PUBLISH
var publishTimeoutMs: Long = DEFAULT_PUBLISH_TIMEOUT_MS
Expand Down Expand Up @@ -64,6 +65,7 @@ class HuaweiPublishExtensionConfig(
override fun toString(): String {
return "HuaweiPublishExtensionConfig(" +
"name='$name', " +
"credentials='$credentials', " +
"credentialsPath='$credentialsPath', " +
"deployType='$deployType', " +
"publishTimeoutMs='$publishTimeoutMs', " +
Expand Down
26 changes: 9 additions & 17 deletions plugin/src/main/kotlin/ru/cian/huawei/publish/HuaweiPublishTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,19 @@ open class HuaweiPublishTask

@get:Internal
@set:Option(
option = "credentialsPath",
description = "File path with AppGallery credentials params ('client_id' and 'client_secret')"
)
var credentialsPath: String? = null

@get:Internal
@set:Option(
option = "clientId",
description = "'client_id' param from AppGallery credentials. " +
"The key more priority than value from 'credentialsPath'"
option = "credentials",
description = "AppGallery credentials in Base64 format. " +
"Decoded json example: {\"client_id\": \"<CLIENT_ID>\", \"client_secret\": \"<CLIENT_SECRET>\"})"
)
var clientId: String? = null
var credentials: String? = null

@get:Internal
@set:Option(
option = "clientSecret",
description = "'client_secret' param from AppGallery credentials. " +
"The key more priority than value from 'credentialsPath'"
option = "credentialsPath",
description = "File path with AppGallery credentials params ('client_id' and 'client_secret') in json format. " +
"Json example: {\"client_id\": \"<CLIENT_ID>\", \"client_secret\": \"<CLIENT_SECRET>\"})"
)
var clientSecret: String? = null
var credentialsPath: String? = null

@get:Internal
@set:Option(
Expand Down Expand Up @@ -199,9 +192,8 @@ open class HuaweiPublishTask
publishTimeoutMs = publishTimeoutMs,
publishPeriodMs = publishPeriodMs,
publishSocketTimeoutInSeconds = publishSocketTimeoutInSeconds,
credentials = credentials,
credentialsPath = credentialsPath,
clientId = clientId,
clientSecret = clientSecret,
buildFormat = buildFormat,
buildFile = buildFile,
releaseTime = releaseTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import ru.cian.huawei.publish.HuaweiPublishExtensionConfig
import ru.cian.huawei.publish.ReleaseNotesConfig
import ru.cian.huawei.publish.ReleaseNotesDescriptionsConfig
import ru.cian.huawei.publish.ReleasePhaseConfig
import ru.cian.huawei.publish.models.Credential
import java.util.Base64

internal class ConfigProvider(
private val extension: HuaweiPublishExtensionConfig,
Expand Down Expand Up @@ -89,10 +91,17 @@ internal class ConfigProvider(

@Suppress("ThrowsCount")
fun getCredentialsConfig(): Credentials {
val credentialsBase64 = cli.credentials ?: extension.credentials
val credentialsFromBase64 = lazy {
if (credentialsBase64 != null) {
decodeCredentials(credentialsBase64)
} else {
null
}
}

val credentialsFilePath = cli.credentialsPath ?: extension.credentialsPath
val clientIdPriority: String? = cli.clientId
val clientSecretPriority: String? = cli.clientSecret
val credentials = lazy {
val credentialsResult = lazy {
if (credentialsFilePath.isNullOrBlank()) {
throw FileNotFoundException(
"$extension (File path for credentials is null or empty. " +
Expand All @@ -106,21 +115,31 @@ internal class ConfigProvider(
"with 'client_id' and 'client_secret' for access to Huawei Publish API is not found)"
)
}
CredentialHelper.getCredentials(credentialsFile)
CredentialHelper.getCredentialsFromFile(credentialsFile)
}
val clientId = clientIdPriority ?: credentials.value.clientId.nullIfBlank()

val clientId = credentialsFromBase64.value?.clientId
?: credentialsResult.value.clientId.nullIfBlank()
?: throw IllegalArgumentException(
"(Huawei credential `clientId` param is null or empty). " +
"Please check your credentials file content or as single parameter."
)
val clientSecret = clientSecretPriority ?: credentials.value.clientSecret.nullIfBlank()
val clientSecret = credentialsFromBase64.value?.clientSecret
?: credentialsResult.value.clientSecret.nullIfBlank()
?: throw IllegalArgumentException(
"(Huawei credential `clientSecret` param is null or empty). " +
"Please check your credentials file content or as single parameter."
)
return Credentials(clientId, clientSecret)
}

@Throws(IllegalArgumentException::class)
fun decodeCredentials(encodedCredentials: String): Credential {
val decodedBytes = Base64.getDecoder().decode(encodedCredentials)
val decodedString = String(decodedBytes, Charsets.UTF_8)
return CredentialHelper.getCredentialsFromJson(decodedString)
}

@Suppress("ThrowsCount")
fun getReleasePhaseConfig(): ReleasePhaseConfig? {
val releasePhaseStartTime = cli.releasePhaseStartTime ?: extension.releasePhase?.startTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ import java.io.File
import java.io.FileReader

internal object CredentialHelper {
fun getCredentials(credentialsFile: File): Credential {
fun getCredentialsFromFile(credentialsFile: File): Credential {
val reader = JsonReader(FileReader(credentialsFile.absolutePath))
val type = object : TypeToken<Credential>() {}.type
return Gson().fromJson(reader, type)
}

fun getCredentialsFromJson(json: String): Credential {
val type = object : TypeToken<Credential>() {}.type
return Gson().fromJson(json, type)
}
}
Loading

0 comments on commit 24566e4

Please sign in to comment.