-
Notifications
You must be signed in to change notification settings - Fork 39
/
HiddenSecretsPlugin.kt
282 lines (253 loc) Β· 9.66 KB
/
HiddenSecretsPlugin.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package com.klaxit.hiddensecrets
import com.android.build.gradle.AppExtension
import org.gradle.api.Action
import org.gradle.api.InvalidUserDataException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import java.io.File
import java.nio.charset.Charset
/**
* Available gradle tasks from HiddenSecretsPlugin
*/
open class HiddenSecretsPlugin : Plugin<Project> {
companion object {
private const val APP_MAIN_FOLDER = "src/main/"
//Tasks
private const val TASK_UNZIP_HIDDEN_SECRETS = "unzipHiddenSecrets"
private const val TASK_COPY_CPP = "copyCpp"
private const val TASK_COPY_KOTLIN = "copyKotlin"
private const val TASK_OBFUSCATE = "obfuscate"
private const val TASK_HIDE_SECRET = "hideSecret"
private const val TASK_PACKAGE_NAME = "packageName"
//Errors
private const val ERROR_EMPTY_KEY = "No key provided, use argument '-Pkey=yourKey'"
private const val ERROR_EMPTY_PACKAGE = "Empty package name, use argument '-Ppackage=your.package.name'"
}
override fun apply(project: Project) {
val tmpFolder = java.lang.String.format("%s/hidden-secrets-tmp", project.buildDir)
/**
* Get the package name of the Android app on which this plugin is used
*/
fun getAppPackageName(): String? {
val androidExtension = project.extensions.getByName("android")
if (androidExtension is AppExtension) {
return androidExtension.defaultConfig.applicationId
}
return null
}
/**
* Get key param from command line
*/
@Input
fun getKeyParam(): String {
val key: String
if (project.hasProperty("key")) {
//From command line
key = project.property("key") as String
} else {
throw InvalidUserDataException(ERROR_EMPTY_KEY)
}
return key
}
/**
* Get package name param from command line
*/
@Input
fun getPackageNameParam(): String {
//From config
var packageName: String? = null
if (project.hasProperty("package")) {
//From command line
packageName = project.property("package") as String?
}
if (packageName.isNullOrEmpty()) {
//From Android app
packageName = getAppPackageName()
}
if (packageName.isNullOrEmpty()) {
throw InvalidUserDataException(ERROR_EMPTY_PACKAGE)
}
return packageName
}
/**
* Get key name param from command line
*/
@Input
fun getKeyNameParam(): String {
val chars = ('a'..'Z') + ('A'..'Z')
var keyName = List(8) { chars.random() }.joinToString("")
if (project.hasProperty("keyName")) {
//From command line
keyName = project.property("keyName") as String
} else {
println("Key name has been randomized, chose your own key name by adding argument '-PkeyName=yourName'")
}
println("### KEY NAME ###\n$keyName\n")
return keyName
}
/**
* Generate en encoded key from command line params
*/
fun getObfuscatedKey(): String {
val key = getKeyParam()
println("### SECRET ###\n$key\n")
val packageName = getPackageNameParam()
println("### PACKAGE NAME ###\n$packageName\n")
val encodedKey = Utils.encodeSecret(key, packageName)
println("### OBFUSCATED SECRET ###\n$encodedKey")
return encodedKey
}
@OutputFile
fun getCppDestination(fileName: String): File {
return project.file(APP_MAIN_FOLDER + "cpp/$fileName")
}
@OutputFile
fun getKotlinDestination(packageName: String, fileName: String): File {
var path = APP_MAIN_FOLDER + "java/"
packageName.split(".").forEach {
path += "$it/"
}
val directory = project.file(path)
if (!directory.exists()) {
error("Directory $path does not exist in the project, you might have selected a wrong package.")
}
path += fileName
return project.file(path)
}
/**
* Copy Cpp files from the lib to the Android project if they don't exist yet
*/
fun copyCppFiles() {
project.file("$tmpFolder/cpp/").listFiles()?.forEach {
val destination = getCppDestination(it.name)
if (destination.exists()) {
println(it.name + " already exists")
} else {
println("Copy $it.name to\n$destination")
it.copyTo(destination, true)
}
}
}
/**
* Copy Kotlin files from the lib to the Android project if they don't exist yet
*/
fun copyKotlinFiles() {
val packageName = getPackageNameParam()
project.file("$tmpFolder/kotlin/").listFiles()?.forEach {
val destination = getKotlinDestination(packageName, it.name)
if (destination.exists()) {
println(it.name + " already exists")
} else {
println("Copy $it.name to\n$destination")
it.copyTo(destination, true)
}
}
}
/**
* Unzip plugin into tmp directory
*/
project.tasks.create(TASK_UNZIP_HIDDEN_SECRETS, Copy::class.java,
object : Action<Copy?> {
@TaskAction
override fun execute(copy: Copy) {
// in the case of buildSrc dir
copy.from(project.zipTree(javaClass.protectionDomain.codeSource.location!!.toExternalForm()))
println("Unzip jar to $tmpFolder")
copy.into(tmpFolder)
}
})
/**
* Copy C++ files to your project
*/
project.task(TASK_COPY_CPP)
{
doLast {
copyCppFiles()
}
}
/**
* Copy Kotlin file to your project
*/
project.task(TASK_COPY_KOTLIN)
{
doLast {
copyKotlinFiles()
}
}
/**
* Get an obfuscated key from command line
*/
project.task(TASK_OBFUSCATE)
{
doLast {
getObfuscatedKey()
}
}
/**
* Obfuscate a key and add it to your Android project
*/
project.task(TASK_HIDE_SECRET)
{
dependsOn(TASK_UNZIP_HIDDEN_SECRETS)
doLast {
//Copy files if they do not exist
copyCppFiles()
copyKotlinFiles()
val keyName = getKeyNameParam()
val packageName = getPackageNameParam()
val obfuscatedKey = getObfuscatedKey()
//Add obfuscated key in C++ code
val secretsCpp = getCppDestination("secrets.cpp")
if (secretsCpp.exists()) {
var text = secretsCpp.readText(Charset.defaultCharset())
if (text.contains(obfuscatedKey)) {
println("Key already added in C++ !")
}
if (text.contains("YOUR_KEY_GOES_HERE")) {
//Replace package name
text = text.replace("YOUR_PACKAGE_GOES_HERE", Utils.getUnderScoredPackageName(packageName))
//Replace key name
text = text.replace("YOUR_KEY_NAME_GOES_HERE", keyName)
//Replace demo key
text = text.replace("{YOUR_KEY_GOES_HERE}", obfuscatedKey)
secretsCpp.writeText(text)
} else {
//Add new key
text += CodeGenerator.getCppCode(packageName, keyName, obfuscatedKey)
secretsCpp.writeText(text)
}
} else {
error("Missing C++ file, please run gradle task : $TASK_COPY_CPP")
}
//Add method in Kotlin code
val secretsKotlin = getKotlinDestination(packageName, "Secrets.kt")
if (secretsKotlin.exists()) {
var text = secretsKotlin.readText(Charset.defaultCharset())
text = text.replace("YOUR_PACKAGE_GOES_HERE", packageName)
if (text.contains(keyName)) {
println("Method already added in Kotlin !")
}
text = text.dropLast(1)
text += CodeGenerator.getKotlinCode(keyName)
secretsKotlin.writeText(text)
} else {
error("Missing Kotlin file, please run gradle task : $TASK_COPY_KOTLIN")
}
println("You can now get your secret key by calling : Secrets().get$keyName(packageName)")
}
}
/**
* Print the package name of the app
*/
project.task(TASK_PACKAGE_NAME)
{
doLast {
println("APP PACKAGE NAME = " + getPackageNameParam())
}
}
}
}