react-native-secreton 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +44 -0
- package/android/build.gradle +77 -0
- package/android/gradle.properties +5 -0
- package/android/secreton.gradle +156 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/com/secreton/SecretonModule.kt +23 -0
- package/android/src/main/java/com/secreton/SecretonPackage.kt +33 -0
- package/dist/CLISecreton.js +40 -0
- package/dist/NodeSecreton.js +70 -0
- package/ios/Secreton.h +5 -0
- package/ios/Secreton.mm +21 -0
- package/lib/module/CLISecreton.js +38 -0
- package/lib/module/CLISecreton.js.map +1 -0
- package/lib/module/NativeSecreton.js +5 -0
- package/lib/module/NativeSecreton.js.map +1 -0
- package/lib/module/NodeSecreton.js +78 -0
- package/lib/module/NodeSecreton.js.map +1 -0
- package/lib/module/index.js +7 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/CLISecreton.d.ts +3 -0
- package/lib/typescript/src/CLISecreton.d.ts.map +1 -0
- package/lib/typescript/src/NativeSecreton.d.ts +7 -0
- package/lib/typescript/src/NativeSecreton.d.ts.map +1 -0
- package/lib/typescript/src/NodeSecreton.d.ts +29 -0
- package/lib/typescript/src/NodeSecreton.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +2 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/package.json +178 -0
- package/react-native-secreton.podspec +27 -0
- package/src/CLISecreton.ts +44 -0
- package/src/NativeSecreton.ts +7 -0
- package/src/NodeSecreton.ts +126 -0
- package/src/index.tsx +5 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sarli Wariali
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# react-native-secreton
|
|
2
|
+
Config secret variables for React Native apps
|
|
3
|
+
|
|
4
|
+
## Installation
|
|
5
|
+
```sh
|
|
6
|
+
npm install react-native-secreton
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Generate secret key
|
|
10
|
+
Command line random key:
|
|
11
|
+
```
|
|
12
|
+
openssl rand -hex 32
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Native Usage
|
|
16
|
+
|
|
17
|
+
### Android
|
|
18
|
+
Config **android/app/build.gradle**
|
|
19
|
+
```groovy
|
|
20
|
+
...
|
|
21
|
+
apply from: new File(rootProject.projectDir.parentFile.parentFile, "android/secreton.gradle")
|
|
22
|
+
loadEnvFromSecretonCliSync(project)
|
|
23
|
+
...
|
|
24
|
+
react {
|
|
25
|
+
...
|
|
26
|
+
nodeExecutableAndArgs = [
|
|
27
|
+
project.findProperty("NODE_BINARY") ?: "/usr/local/bin/node"
|
|
28
|
+
]
|
|
29
|
+
...
|
|
30
|
+
}
|
|
31
|
+
android {
|
|
32
|
+
defaultConfig {
|
|
33
|
+
manifestPlaceholders = [
|
|
34
|
+
geoApiKey : project.ext.env?.get('GEO_APK_API_KEY')
|
|
35
|
+
]
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### iOS / macOS
|
|
41
|
+
- Manual Link
|
|
42
|
+
```
|
|
43
|
+
pod 'react-native-secreton', :path => '../node_modules/react-native-secreton'
|
|
44
|
+
```
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
buildscript {
|
|
2
|
+
ext.getExtOrDefault = {name ->
|
|
3
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['Secreton_' + name]
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
repositories {
|
|
7
|
+
google()
|
|
8
|
+
mavenCentral()
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
dependencies {
|
|
12
|
+
classpath "com.android.tools.build:gradle:8.7.2"
|
|
13
|
+
// noinspection DifferentKotlinGradleVersion
|
|
14
|
+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
apply plugin: "com.android.library"
|
|
20
|
+
apply plugin: "kotlin-android"
|
|
21
|
+
|
|
22
|
+
apply plugin: "com.facebook.react"
|
|
23
|
+
|
|
24
|
+
def getExtOrIntegerDefault(name) {
|
|
25
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Secreton_" + name]).toInteger()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
android {
|
|
29
|
+
namespace "com.secreton"
|
|
30
|
+
|
|
31
|
+
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
|
|
32
|
+
|
|
33
|
+
defaultConfig {
|
|
34
|
+
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
|
|
35
|
+
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
buildFeatures {
|
|
39
|
+
buildConfig true
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
buildTypes {
|
|
43
|
+
release {
|
|
44
|
+
minifyEnabled false
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
lintOptions {
|
|
49
|
+
disable "GradleCompatible"
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
compileOptions {
|
|
53
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
|
54
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
sourceSets {
|
|
58
|
+
main {
|
|
59
|
+
java.srcDirs += [
|
|
60
|
+
"generated/java",
|
|
61
|
+
"generated/jni"
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
repositories {
|
|
68
|
+
mavenCentral()
|
|
69
|
+
google()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
def kotlin_version = getExtOrDefault("kotlinVersion")
|
|
73
|
+
|
|
74
|
+
dependencies {
|
|
75
|
+
implementation "com.facebook.react:react-android"
|
|
76
|
+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
77
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* =========================
|
|
3
|
+
* Secreton bootstrap
|
|
4
|
+
* =========================
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
ext.findWorkspaceRoot = { File startDir ->
|
|
8
|
+
File current = startDir
|
|
9
|
+
while (current != null) {
|
|
10
|
+
if (new File(current, "package.json").exists()) {
|
|
11
|
+
return current
|
|
12
|
+
}
|
|
13
|
+
current = current.parentFile
|
|
14
|
+
}
|
|
15
|
+
throw new RuntimeException("❌ package.json not found")
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
ext.loadEnvFile = { File file ->
|
|
19
|
+
if (!file.exists()) return [:]
|
|
20
|
+
|
|
21
|
+
def map = [:]
|
|
22
|
+
file.eachLine { line ->
|
|
23
|
+
line = line.trim()
|
|
24
|
+
if (!line || line.startsWith("#")) return
|
|
25
|
+
|
|
26
|
+
def idx = line.indexOf("=")
|
|
27
|
+
if (idx <= 0) return
|
|
28
|
+
|
|
29
|
+
def key = line.substring(0, idx)
|
|
30
|
+
def value = line.substring(idx + 1)
|
|
31
|
+
|
|
32
|
+
if (value.startsWith("\"") && value.endsWith("\"")) {
|
|
33
|
+
value = value.substring(1, value.length() - 1)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
map[key] = value
|
|
37
|
+
}
|
|
38
|
+
return map
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
ext.ensureLocalEnv = { appProject ->
|
|
42
|
+
if (appProject.ext.has("localEnv")) return
|
|
43
|
+
|
|
44
|
+
appProject.ext.localEnv = [:]
|
|
45
|
+
appProject.ext.env = [:]
|
|
46
|
+
|
|
47
|
+
def androidDir = appProject.rootProject.projectDir
|
|
48
|
+
def workspaceRoot = findWorkspaceRoot(androidDir)
|
|
49
|
+
def rootDir = workspaceRoot
|
|
50
|
+
|
|
51
|
+
def envFileName = appProject.findProperty("ENVFILE") ?: System.getenv("ENVFILE") ?: ".env"
|
|
52
|
+
def envFile = new File(rootDir, envFileName)
|
|
53
|
+
|
|
54
|
+
if (envFile.exists()) {
|
|
55
|
+
appProject.ext.localEnv.putAll(loadEnvFile(envFile))
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
ext.getSecretKey = { appProject ->
|
|
60
|
+
ensureLocalEnv(appProject)
|
|
61
|
+
return appProject.ext.localEnv.get("ENV_SECRET_KEY")
|
|
62
|
+
?: appProject.findProperty("ENV_SECRET_KEY")
|
|
63
|
+
?: System.getenv("ENV_SECRET_KEY")
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
ext.decryptValue = { String encrypted, String secretKey ->
|
|
67
|
+
if (!encrypted) return encrypted
|
|
68
|
+
if (!secretKey) throw new RuntimeException("❌ ENV_SECRET_KEY is required for decryption")
|
|
69
|
+
|
|
70
|
+
def cmd = """printf "%s" '${encrypted}' | openssl enc -aes-256-cbc -a -A -d -salt -pbkdf2 -iter 100000 -pass pass:${secretKey}"""
|
|
71
|
+
def proc = ["bash", "-c", cmd].execute()
|
|
72
|
+
proc.waitFor()
|
|
73
|
+
|
|
74
|
+
if (proc.exitValue() != 0) {
|
|
75
|
+
throw new RuntimeException(proc.err.text)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return proc.in.text.trim()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
ext.loadAndDecryptEnv = { appProject ->
|
|
82
|
+
ensureLocalEnv(appProject)
|
|
83
|
+
|
|
84
|
+
def androidDir = appProject.rootProject.projectDir
|
|
85
|
+
def rootDir = findWorkspaceRoot(androidDir)
|
|
86
|
+
|
|
87
|
+
def envFileName =
|
|
88
|
+
appProject.findProperty("ENVFILE")
|
|
89
|
+
?: System.getenv("ENVFILE")
|
|
90
|
+
?: ".env"
|
|
91
|
+
def envFile = new File(rootDir, envFileName)
|
|
92
|
+
if (!envFile.exists()) return
|
|
93
|
+
|
|
94
|
+
def secretKey = getSecretKey(appProject)
|
|
95
|
+
if (!secretKey) return
|
|
96
|
+
|
|
97
|
+
def envMap = loadEnvFile(envFile)
|
|
98
|
+
envMap.each { k, v ->
|
|
99
|
+
def value = v
|
|
100
|
+
if (v instanceof String && v.startsWith("enc:")) {
|
|
101
|
+
value = decryptValue(v.substring(4), secretKey)
|
|
102
|
+
}
|
|
103
|
+
appProject.ext.env[k] = value
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
ext.findRnSecretonCli = { appProject ->
|
|
108
|
+
def androidDir = appProject.rootProject.rootDir
|
|
109
|
+
def workspaceRoot = findWorkspaceRoot(androidDir)
|
|
110
|
+
def possibleCliBins = [
|
|
111
|
+
new File(workspaceRoot, "node_modules/.bin/rn-secreton-cli"),
|
|
112
|
+
new File(workspaceRoot.parentFile, "node_modules/.bin/rn-secreton-cli"),
|
|
113
|
+
new File(workspaceRoot, "node_modules/.bin/rn-secreton-cli.cmd"),
|
|
114
|
+
new File(workspaceRoot.parentFile, "node_modules/.bin/rn-secreton-cli.cmd")
|
|
115
|
+
].collect { it.canonicalFile }
|
|
116
|
+
|
|
117
|
+
def cliBin = possibleCliBins.find { it.exists() }
|
|
118
|
+
if (!cliBin) {
|
|
119
|
+
throw new RuntimeException("❌ rn-secreton-cli not found in expected paths: ${possibleCliBins*.absolutePath}")
|
|
120
|
+
}
|
|
121
|
+
return cliBin
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
ext.loadEnvFromSecretonCliSync = { appProject ->
|
|
125
|
+
def cliBin = findRnSecretonCli(appProject)
|
|
126
|
+
def workspaceRoot = findWorkspaceRoot(appProject.rootProject.rootDir)
|
|
127
|
+
def envFileName = appProject.findProperty("ENVFILE") ?: System.getenv("ENVFILE") ?: ".env"
|
|
128
|
+
def envFile = new File(workspaceRoot, envFileName)
|
|
129
|
+
|
|
130
|
+
ensureLocalEnv(appProject)
|
|
131
|
+
def secretKey = getSecretKey(appProject)
|
|
132
|
+
if (!secretKey) throw new RuntimeException("❌ ENV_SECRET_KEY not found")
|
|
133
|
+
if (!envFile.exists()) throw new RuntimeException("❌ ENV file not found: ${envFile.absolutePath}")
|
|
134
|
+
|
|
135
|
+
println "🔐 rn-secreton generating env (sync)..."
|
|
136
|
+
println " ENV_FILE : ${envFileName}"
|
|
137
|
+
println " CLI path : ${cliBin.absolutePath}"
|
|
138
|
+
|
|
139
|
+
def envMap = loadEnvFile(envFile)
|
|
140
|
+
def envFromFile = envMap.collect { k, v -> "${k}=${v}" }
|
|
141
|
+
def envArray = [
|
|
142
|
+
"ENV_SECRET_KEY=${secretKey}",
|
|
143
|
+
"ENV_FILE=${envFileName}",
|
|
144
|
+
"PATH=${System.getenv("PATH")}"
|
|
145
|
+
]
|
|
146
|
+
envArray += envFromFile
|
|
147
|
+
envArray = envArray as String[]
|
|
148
|
+
|
|
149
|
+
def proc = [cliBin.absolutePath, envFile.absolutePath].execute(envArray, workspaceRoot)
|
|
150
|
+
proc.waitForProcessOutput(System.out, System.err)
|
|
151
|
+
if (proc.exitValue() != 0) {
|
|
152
|
+
throw new RuntimeException("❌ rn-secreton-cli failed")
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
loadAndDecryptEnv(appProject)
|
|
156
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
package com.secreton
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
4
|
+
import com.facebook.react.module.annotations.ReactModule
|
|
5
|
+
|
|
6
|
+
@ReactModule(name = SecretonModule.NAME)
|
|
7
|
+
class SecretonModule(reactContext: ReactApplicationContext) :
|
|
8
|
+
NativeSecretonSpec(reactContext) {
|
|
9
|
+
|
|
10
|
+
override fun getName(): String {
|
|
11
|
+
return NAME
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Example method
|
|
15
|
+
// See https://reactnative.dev/docs/native-modules-android
|
|
16
|
+
override fun multiply(a: Double, b: Double): Double {
|
|
17
|
+
return a * b
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
companion object {
|
|
21
|
+
const val NAME = "Secreton"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
package com.secreton
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.BaseReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfo
|
|
7
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
8
|
+
import java.util.HashMap
|
|
9
|
+
|
|
10
|
+
class SecretonPackage : BaseReactPackage() {
|
|
11
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
|
|
12
|
+
return if (name == SecretonModule.NAME) {
|
|
13
|
+
SecretonModule(reactContext)
|
|
14
|
+
} else {
|
|
15
|
+
null
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
|
|
20
|
+
return ReactModuleInfoProvider {
|
|
21
|
+
val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
|
|
22
|
+
moduleInfos[SecretonModule.NAME] = ReactModuleInfo(
|
|
23
|
+
SecretonModule.NAME,
|
|
24
|
+
SecretonModule.NAME,
|
|
25
|
+
false, // canOverrideExistingModule
|
|
26
|
+
false, // needsEagerInit
|
|
27
|
+
false, // isCxxModule
|
|
28
|
+
true // isTurboModule
|
|
29
|
+
)
|
|
30
|
+
moduleInfos
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { generateEnv } from './NodeSecreton.js';
|
|
4
|
+
const args = process.argv.slice(2);
|
|
5
|
+
const envFile = args[0] || process.env.ENV_FILE;
|
|
6
|
+
const fileName = path.basename(envFile);
|
|
7
|
+
(async () => {
|
|
8
|
+
try {
|
|
9
|
+
const secretKey = process.env.ENV_SECRET_KEY;
|
|
10
|
+
if (!secretKey) {
|
|
11
|
+
throw new Error('ENV_SECRET_KEY is required');
|
|
12
|
+
}
|
|
13
|
+
const fetchEnv = process.env.FETCH_ENV || 'consul';
|
|
14
|
+
await generateEnv({
|
|
15
|
+
envFile,
|
|
16
|
+
secretKey,
|
|
17
|
+
fetchEnv,
|
|
18
|
+
consul: fetchEnv === 'consul'
|
|
19
|
+
? {
|
|
20
|
+
addr: process.env.CONSUL_ADDR || 'http://localhost:8500',
|
|
21
|
+
path: process.env.CONSUL_PATH || 'mobile/project-example',
|
|
22
|
+
token: process.env.CONSUL_TOKEN,
|
|
23
|
+
}
|
|
24
|
+
: undefined,
|
|
25
|
+
vault: fetchEnv === 'vault'
|
|
26
|
+
? {
|
|
27
|
+
addr: process.env.VAULT_ADDR || 'http://localhost:8200',
|
|
28
|
+
path: process.env.VAULT_PATH || 'secret/data/mobile/project-example',
|
|
29
|
+
token: process.env.VAULT_TOKEN || '',
|
|
30
|
+
}
|
|
31
|
+
: undefined,
|
|
32
|
+
});
|
|
33
|
+
console.log(`✅ [CLI] Secreton updated safely → ${fileName}`);
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
const message = err instanceof Error ? err.message : JSON.stringify(err, null, 2);
|
|
37
|
+
console.error(`❌ [CLI] Secreton failed: ${message}`);
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
40
|
+
})();
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import { execSync } from 'node:child_process';
|
|
4
|
+
export function encrypt(value, key) {
|
|
5
|
+
const cmd = `printf "%s" "${value}" | openssl enc -aes-256-cbc -a -A -salt -pbkdf2 -iter 100000 -pass pass:${key}`;
|
|
6
|
+
return execSync(cmd).toString().trim();
|
|
7
|
+
}
|
|
8
|
+
export function readExistingEnv(envFile) {
|
|
9
|
+
if (!fs.existsSync(envFile))
|
|
10
|
+
return new Set();
|
|
11
|
+
const content = fs.readFileSync(envFile, 'utf8');
|
|
12
|
+
return new Set(content
|
|
13
|
+
.split('\n')
|
|
14
|
+
.map((line) => line.trim())
|
|
15
|
+
.filter((line) => Boolean(line))
|
|
16
|
+
.filter((line) => !line.startsWith('#'))
|
|
17
|
+
.map((line) => line.split('=')[0])
|
|
18
|
+
.filter((key) => typeof key === 'string' && key.length > 0));
|
|
19
|
+
}
|
|
20
|
+
async function fetchConsul({ addr, path, token, }) {
|
|
21
|
+
const headers = {};
|
|
22
|
+
if (token)
|
|
23
|
+
headers['X-Consul-Token'] = token;
|
|
24
|
+
const res = await fetch(`${addr}/v1/kv/${path}?recurse=true`, { headers });
|
|
25
|
+
if (!res.ok)
|
|
26
|
+
throw new Error('Consul fetch failed');
|
|
27
|
+
const data = await res.json();
|
|
28
|
+
return data
|
|
29
|
+
.filter((item) => typeof item.Value === 'string')
|
|
30
|
+
.map((item) => ({
|
|
31
|
+
key: item.Key.replace(`${path}/`, ''),
|
|
32
|
+
value: Buffer.from(item.Value, 'base64').toString('utf8'),
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
async function fetchVault({ addr, path, token, }) {
|
|
36
|
+
const res = await fetch(`${addr}/v1/${path}`, {
|
|
37
|
+
headers: { 'X-Vault-Token': token },
|
|
38
|
+
});
|
|
39
|
+
if (!res.ok)
|
|
40
|
+
throw new Error('Vault fetch failed');
|
|
41
|
+
const json = await res.json();
|
|
42
|
+
return Object.entries(json.data.data).map(([key, value]) => ({
|
|
43
|
+
key,
|
|
44
|
+
value: String(value),
|
|
45
|
+
}));
|
|
46
|
+
}
|
|
47
|
+
export async function generateEnv(options) {
|
|
48
|
+
const { envFile, secretKey, fetchEnv = 'consul', } = options;
|
|
49
|
+
const fileName = path.basename(envFile);
|
|
50
|
+
const existingKeys = readExistingEnv(envFile);
|
|
51
|
+
let entries = [];
|
|
52
|
+
if (fetchEnv === 'vault' && options.vault) {
|
|
53
|
+
entries = await fetchVault(options.vault);
|
|
54
|
+
}
|
|
55
|
+
else if (options.consul) {
|
|
56
|
+
entries = await fetchConsul(options.consul);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
throw new Error('No config source provided');
|
|
60
|
+
}
|
|
61
|
+
const newLines = entries
|
|
62
|
+
.filter(({ key }) => !existingKeys.has(key))
|
|
63
|
+
.map(({ key, value }) => `${key}=enc:${encrypt(value, secretKey)}`);
|
|
64
|
+
if (newLines.length === 0) {
|
|
65
|
+
console.log('ℹ️ [Node] Secreton no new env keys to add');
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
fs.appendFileSync(envFile, (fs.existsSync(envFile) ? '\n' : '') + newLines.join('\n'));
|
|
69
|
+
console.log(`✅ [Node] Secreton updated safely → ${fileName}`);
|
|
70
|
+
}
|
package/ios/Secreton.h
ADDED
package/ios/Secreton.mm
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#import "Secreton.h"
|
|
2
|
+
|
|
3
|
+
@implementation Secreton
|
|
4
|
+
- (NSNumber *)multiply:(double)a b:(double)b {
|
|
5
|
+
NSNumber *result = @(a * b);
|
|
6
|
+
|
|
7
|
+
return result;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
|
11
|
+
(const facebook::react::ObjCTurboModule::InitParams &)params
|
|
12
|
+
{
|
|
13
|
+
return std::make_shared<facebook::react::NativeSecretonSpecJSI>(params);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
+ (NSString *)moduleName
|
|
17
|
+
{
|
|
18
|
+
return @"Secreton";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { generateEnv } from './NodeSecreton.js';
|
|
6
|
+
const args = process.argv.slice(2);
|
|
7
|
+
const envFile = args[0] || process.env.ENV_FILE;
|
|
8
|
+
const fileName = path.basename(envFile);
|
|
9
|
+
(async () => {
|
|
10
|
+
try {
|
|
11
|
+
const secretKey = process.env.ENV_SECRET_KEY;
|
|
12
|
+
if (!secretKey) {
|
|
13
|
+
throw new Error('ENV_SECRET_KEY is required');
|
|
14
|
+
}
|
|
15
|
+
const fetchEnv = process.env.FETCH_ENV || 'consul';
|
|
16
|
+
await generateEnv({
|
|
17
|
+
envFile,
|
|
18
|
+
secretKey,
|
|
19
|
+
fetchEnv,
|
|
20
|
+
consul: fetchEnv === 'consul' ? {
|
|
21
|
+
addr: process.env.CONSUL_ADDR || 'http://localhost:8500',
|
|
22
|
+
path: process.env.CONSUL_PATH || 'mobile/project-example',
|
|
23
|
+
token: process.env.CONSUL_TOKEN
|
|
24
|
+
} : undefined,
|
|
25
|
+
vault: fetchEnv === 'vault' ? {
|
|
26
|
+
addr: process.env.VAULT_ADDR || 'http://localhost:8200',
|
|
27
|
+
path: process.env.VAULT_PATH || 'secret/data/mobile/project-example',
|
|
28
|
+
token: process.env.VAULT_TOKEN || ''
|
|
29
|
+
} : undefined
|
|
30
|
+
});
|
|
31
|
+
console.log(`✅ [CLI] Secreton updated safely → ${fileName}`);
|
|
32
|
+
} catch (err) {
|
|
33
|
+
const message = err instanceof Error ? err.message : JSON.stringify(err, null, 2);
|
|
34
|
+
console.error(`❌ [CLI] Secreton failed: ${message}`);
|
|
35
|
+
process.exit(1);
|
|
36
|
+
}
|
|
37
|
+
})();
|
|
38
|
+
//# sourceMappingURL=CLISecreton.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["path","generateEnv","args","process","argv","slice","envFile","env","ENV_FILE","fileName","basename","secretKey","ENV_SECRET_KEY","Error","fetchEnv","FETCH_ENV","consul","addr","CONSUL_ADDR","CONSUL_PATH","token","CONSUL_TOKEN","undefined","vault","VAULT_ADDR","VAULT_PATH","VAULT_TOKEN","console","log","err","message","JSON","stringify","error","exit"],"sourceRoot":"../../src","sources":["CLISecreton.ts"],"mappings":"AAAA;AAAmB;;AACnB,OAAOA,IAAI,MAAM,MAAM;AACvB,SAASC,WAAW,QAAQ,mBAAmB;AAE/C,MAAMC,IAAI,GAAGC,OAAO,CAACC,IAAI,CAACC,KAAK,CAAC,CAAC,CAAC;AAClC,MAA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K,CAAC,4BAA4BH,OAAO,EAAE,CAAC;IACpD3B,OAAO,CAAC+B,IAAI,CAAC,CAAC,CAAC;EACjB;AACF,CAAC,EAAE,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["TurboModuleRegistry","getEnforcing"],"sourceRoot":"../../src","sources":["NativeSecreton.ts"],"mappings":";;AAAA,SAASA,mBAAmB,QAA0B,cAAc;AAMpE,eAAeA,mBAAmB,CAACC,YAAY,CAAO,UAAU,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import { execSync } from 'node:child_process';
|
|
6
|
+
export function encrypt(value, key) {
|
|
7
|
+
const cmd = `printf "%s" "${value}" | openssl enc -aes-256-cbc -a -A -salt -pbkdf2 -iter 100000 -pass pass:${key}`;
|
|
8
|
+
return execSync(cmd).toString().trim();
|
|
9
|
+
}
|
|
10
|
+
export function readExistingEnv(envFile) {
|
|
11
|
+
if (!fs.existsSync(envFile)) return new Set();
|
|
12
|
+
const content = fs.readFileSync(envFile, 'utf8');
|
|
13
|
+
return new Set(content.split('\n').map(line => line.trim()).filter(line => Boolean(line)).filter(line => !line.startsWith('#')).map(line => line.split('=')[0]).filter(key => typeof key === 'string' && key.length > 0));
|
|
14
|
+
}
|
|
15
|
+
async function fetchConsul({
|
|
16
|
+
addr,
|
|
17
|
+
path,
|
|
18
|
+
token
|
|
19
|
+
}) {
|
|
20
|
+
const headers = {};
|
|
21
|
+
if (token) headers['X-Consul-Token'] = token;
|
|
22
|
+
const res = await fetch(`${addr}/v1/kv/${path}?recurse=true`, {
|
|
23
|
+
headers
|
|
24
|
+
});
|
|
25
|
+
if (!res.ok) throw new Error('Consul fetch failed');
|
|
26
|
+
const data = await res.json();
|
|
27
|
+
return data.filter(item => typeof item.Value === 'string').map(item => ({
|
|
28
|
+
key: item.Key.replace(`${path}/`, ''),
|
|
29
|
+
value: Buffer.from(item.Value, 'base64').toString('utf8')
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
async function fetchVault({
|
|
33
|
+
addr,
|
|
34
|
+
path,
|
|
35
|
+
token
|
|
36
|
+
}) {
|
|
37
|
+
const res = await fetch(`${addr}/v1/${path}`, {
|
|
38
|
+
headers: {
|
|
39
|
+
'X-Vault-Token': token
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
if (!res.ok) throw new Error('Vault fetch failed');
|
|
43
|
+
const json = await res.json();
|
|
44
|
+
return Object.entries(json.data.data).map(([key, value]) => ({
|
|
45
|
+
key,
|
|
46
|
+
value: String(value)
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
export async function generateEnv(options) {
|
|
50
|
+
const {
|
|
51
|
+
envFile,
|
|
52
|
+
secretKey,
|
|
53
|
+
fetchEnv = 'consul'
|
|
54
|
+
} = options;
|
|
55
|
+
const fileName = path.basename(envFile);
|
|
56
|
+
const existingKeys = readExistingEnv(envFile);
|
|
57
|
+
let entries = [];
|
|
58
|
+
if (fetchEnv === 'vault' && options.vault) {
|
|
59
|
+
entries = await fetchVault(options.vault);
|
|
60
|
+
} else if (options.consul) {
|
|
61
|
+
entries = await fetchConsul(options.consul);
|
|
62
|
+
} else {
|
|
63
|
+
throw new Error('No config source provided');
|
|
64
|
+
}
|
|
65
|
+
const newLines = entries.filter(({
|
|
66
|
+
key
|
|
67
|
+
}) => !existingKeys.has(key)).map(({
|
|
68
|
+
key,
|
|
69
|
+
value
|
|
70
|
+
}) => `${key}=enc:${encrypt(value, secretKey)}`);
|
|
71
|
+
if (newLines.length === 0) {
|
|
72
|
+
console.log('ℹ️ [Node] Secreton no new env keys to add');
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
fs.appendFileSync(envFile, (fs.existsSync(envFile) ? '\n' : '') + newLines.join('\n'));
|
|
76
|
+
console.log(`✅ [Node] Secreton updated safely → ${fileName}`);
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=NodeSecreton.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["path","fs","execSync","encrypt","value","key","cmd","toString","trim","readExistingEnv","envFile","existsSync","Set","content","readFileSync","split","map","line","filter","Boolean","startsWith","length","fetchConsul","addr","token","headers","res","fetch","ok","Error","data","json","item","Value","Key","replace","Buffer","from","fetchVault","Object","entries","String","generateEnv","options","secretKey","fetchEnv","fileName","basename","existingKeys","vault","consul","newLines","has","console","log","appendFileSync","join"],"sourceRoot":"../../src","sources":["NodeSecreton.ts"],"mappings":";;AAAA,OAAOA,IAAI,MAAM,WAAW;AAC5B,OAAOC,EAAE,MAAM,SAAS;AACxB,SAASC,QAAQ,QAAQ,oBAAoB;AAgC7C,OAAO,SAASC,OAAOA,CAACC,KAAa,EAAEC,GAAW,EAAE;EAClD,MAAMC,GAAG,GAAG,gBAAgBF,KAAK,4EAA4EC,GAAG,EAAE;EAClH,OAAOH,QAAQ,CAACI,GAAG,CAAC,CAACC,QAAQ,CAAC,CAAC,CAACC,IAAI,CAAC,CAAC;AACxC;AAEA,OAAO,SAASC,eAAeA,CAACC,OAAe,EAAe;EAC5D,IAAI,CAACT,EAAE,CAACU,UAAU,CAACD,OAAO,CAAC,EAAE,OAAO,IAAIE,GAAG,CAAC,CAAC;EAE7C,MAAMC,OAAO,GAAGZ,EAAE,CAACa,YAAY,CAACJ,OAAO,EAAE,MAAM,CAAC;EAEhD,OAAO,IAAIE,GAAG,CACZC,OAAO,CACJE,KAAK,CAAC,IAAI,CAAC,CACXC,GAAG,CAAEC,IAAI,IAAKA,IAAI,CAACT,IAAI,CAAC,CAAC,CAAC,CAC1BU,MAAM,CAAED,IAAI,IAAqBE,OAAO,CAACF,IAAI,CAAC,CAAC,CAC/CC,MAAM,CAAED,IAAI,IAAK,CAACA,IAAI,CAACG,UAAU,CAAC,GAAG,CAAC,CAAC,CACvCJ,GAAG,CAAEC,IAA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;EAErE,IAAIO,QAAQ,CAAC9B,MAAM,KAAK,CAAC,EAAE;IACzBgC,OAAO,CAACC,GAAG,CAAC,2CAA2C,CAAC;IACxD;EACF;EAEArD,EAAE,CAACsD,cAAc,CAAC7C,OAAO,EAAE,CAACT,EAAE,CAACU,UAAU,CAACD,OAAO,CAAC,GAAG,IAAI,GAAG,EAAE,IAAIyC,QAAQ,CAACK,IAAI,CAAC,IAAI,CAAC,CAAC;EAEtFH,OAAO,CAACC,GAAG,CAAC,sCAAsCR,QAAQ,EAAE,CAAC;AAC/D","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["Secreton","multiply","a","b"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,OAAOA,QAAQ,MAAM,qBAAkB;AAEvC,OAAO,SAASC,QAAQA,CAACC,CAAS,EAAEC,CAAS,EAAU;EACrD,OAAOH,QAAQ,CAACC,QAAQ,CAACC,CAAC,EAAEC,CAAC,CAAC;AAChC","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CLISecreton.d.ts","sourceRoot":"","sources":["../../../src/CLISecreton.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NativeSecreton.d.ts","sourceRoot":"","sources":["../../../src/NativeSecreton.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAErE,MAAM,WAAW,IAAK,SAAQ,WAAW;IACvC,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACxC;;AAED,wBAAkE"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export interface EnvEntry {
|
|
2
|
+
key: string;
|
|
3
|
+
value: string;
|
|
4
|
+
}
|
|
5
|
+
export interface ConsulConfig {
|
|
6
|
+
addr: string;
|
|
7
|
+
path: string;
|
|
8
|
+
token?: string;
|
|
9
|
+
}
|
|
10
|
+
export type ConsulKV = {
|
|
11
|
+
Key: string;
|
|
12
|
+
Value: string | null;
|
|
13
|
+
};
|
|
14
|
+
export interface VaultConfig {
|
|
15
|
+
addr: string;
|
|
16
|
+
path: string;
|
|
17
|
+
token: string;
|
|
18
|
+
}
|
|
19
|
+
export interface GenerateEnvOptions {
|
|
20
|
+
envFile: string;
|
|
21
|
+
secretKey: string;
|
|
22
|
+
consul?: ConsulConfig;
|
|
23
|
+
vault?: VaultConfig;
|
|
24
|
+
fetchEnv?: 'consul' | 'vault';
|
|
25
|
+
}
|
|
26
|
+
export declare function encrypt(value: string, key: string): string;
|
|
27
|
+
export declare function readExistingEnv(envFile: string): Set<string>;
|
|
28
|
+
export declare function generateEnv(options: GenerateEnvOptions): Promise<void>;
|
|
29
|
+
//# sourceMappingURL=NodeSecreton.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NodeSecreton.d.ts","sourceRoot":"","sources":["../../../src/NodeSecreton.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,QAAQ;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,MAAM,QAAQ,GAAG;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB,CAAC;AAEF,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,QAAQ,CAAC,EAAE,QAAQ,GAAG,OAAO,CAAC;CAC/B;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,UAGjD;AAED,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,CAc5D;AAwCD,wBAAsB,WAAW,CAAC,OAAO,EAAE,kBAAkB,iBAgC5D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAEA,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAErD"}
|
package/package.json
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-native-secreton",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Config secret variables for React Native apps",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react-native",
|
|
7
|
+
"ios",
|
|
8
|
+
"android",
|
|
9
|
+
"secrets",
|
|
10
|
+
"env"
|
|
11
|
+
],
|
|
12
|
+
"main": "./lib/module/index.js",
|
|
13
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"source": "./src/index.tsx",
|
|
17
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
18
|
+
"default": "./lib/module/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"src",
|
|
24
|
+
"lib",
|
|
25
|
+
"dist",
|
|
26
|
+
"android",
|
|
27
|
+
"ios",
|
|
28
|
+
"cpp",
|
|
29
|
+
"*.podspec",
|
|
30
|
+
"react-native.config.js",
|
|
31
|
+
"!ios/build",
|
|
32
|
+
"!android/build",
|
|
33
|
+
"!android/gradle",
|
|
34
|
+
"!android/gradlew",
|
|
35
|
+
"!android/gradlew.bat",
|
|
36
|
+
"!android/local.properties",
|
|
37
|
+
"!**/__tests__",
|
|
38
|
+
"!**/__fixtures__",
|
|
39
|
+
"!**/__mocks__",
|
|
40
|
+
"!**/.*"
|
|
41
|
+
],
|
|
42
|
+
"type": "module",
|
|
43
|
+
"scripts": {
|
|
44
|
+
"clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib dist",
|
|
45
|
+
"build:cli": "tsc -p tsconfig.cli.json",
|
|
46
|
+
"prepare": "bob build && npm run build:cli",
|
|
47
|
+
"nitrogen": "nitrogen",
|
|
48
|
+
"typecheck": "tsc",
|
|
49
|
+
"lint": "eslint \"src/**/*.{js,ts,tsx}\"",
|
|
50
|
+
"test": "jest",
|
|
51
|
+
"release": "release-it --only-version"
|
|
52
|
+
},
|
|
53
|
+
"bin": {
|
|
54
|
+
"rn-secreton-cli": "dist/CLISecreton.js"
|
|
55
|
+
},
|
|
56
|
+
"repository": {
|
|
57
|
+
"type": "git",
|
|
58
|
+
"url": "git+https://github.com/serldna86/react-native-secreton.git"
|
|
59
|
+
},
|
|
60
|
+
"author": "Sarli Wariali",
|
|
61
|
+
"license": "MIT",
|
|
62
|
+
"bugs": {
|
|
63
|
+
"url": "https://github.com/serldna86/react-native-secreton/issues"
|
|
64
|
+
},
|
|
65
|
+
"homepage": "https://github.com/serldna86/react-native-secreton#readme",
|
|
66
|
+
"publishConfig": {
|
|
67
|
+
"registry": "https://registry.npmjs.org/"
|
|
68
|
+
},
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"@commitlint/config-conventional": "^19.8.1",
|
|
71
|
+
"@eslint/compat": "^1.3.2",
|
|
72
|
+
"@eslint/eslintrc": "^3.3.1",
|
|
73
|
+
"@eslint/js": "^9.35.0",
|
|
74
|
+
"@react-native/babel-preset": "0.83.0",
|
|
75
|
+
"@react-native/eslint-config": "0.83.0",
|
|
76
|
+
"@release-it/conventional-changelog": "^10.0.1",
|
|
77
|
+
"@types/jest": "^29.5.14",
|
|
78
|
+
"@types/react": "^19.2.0",
|
|
79
|
+
"commitlint": "^19.8.1",
|
|
80
|
+
"del-cli": "^6.0.0",
|
|
81
|
+
"eslint": "^9.35.0",
|
|
82
|
+
"eslint-config-prettier": "^10.1.8",
|
|
83
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
84
|
+
"globals": "^17.0.0",
|
|
85
|
+
"jest": "^29.7.0",
|
|
86
|
+
"lefthook": "^2.0.3",
|
|
87
|
+
"prettier": "^2.8.8",
|
|
88
|
+
"react": "19.2.0",
|
|
89
|
+
"react-native": "0.83.0",
|
|
90
|
+
"react-native-builder-bob": "^0.40.17",
|
|
91
|
+
"release-it": "^19.0.4",
|
|
92
|
+
"turbo": "^2.5.6",
|
|
93
|
+
"typescript": "^5.9.2"
|
|
94
|
+
},
|
|
95
|
+
"peerDependencies": {
|
|
96
|
+
"react": "*",
|
|
97
|
+
"react-native": "*"
|
|
98
|
+
},
|
|
99
|
+
"workspaces": [
|
|
100
|
+
"example"
|
|
101
|
+
],
|
|
102
|
+
"packageManager": "yarn@4.11.0",
|
|
103
|
+
"react-native-builder-bob": {
|
|
104
|
+
"source": "src",
|
|
105
|
+
"output": "lib",
|
|
106
|
+
"targets": [
|
|
107
|
+
[
|
|
108
|
+
"module",
|
|
109
|
+
{
|
|
110
|
+
"esm": true
|
|
111
|
+
}
|
|
112
|
+
],
|
|
113
|
+
[
|
|
114
|
+
"typescript",
|
|
115
|
+
{
|
|
116
|
+
"project": "tsconfig.build.json"
|
|
117
|
+
}
|
|
118
|
+
]
|
|
119
|
+
]
|
|
120
|
+
},
|
|
121
|
+
"codegenConfig": {
|
|
122
|
+
"name": "SecretonSpec",
|
|
123
|
+
"type": "modules",
|
|
124
|
+
"jsSrcsDir": "src",
|
|
125
|
+
"android": {
|
|
126
|
+
"javaPackageName": "com.secreton"
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
"prettier": {
|
|
130
|
+
"quoteProps": "consistent",
|
|
131
|
+
"singleQuote": true,
|
|
132
|
+
"tabWidth": 2,
|
|
133
|
+
"trailingComma": "es5",
|
|
134
|
+
"useTabs": false
|
|
135
|
+
},
|
|
136
|
+
"jest": {
|
|
137
|
+
"preset": "react-native",
|
|
138
|
+
"modulePathIgnorePatterns": [
|
|
139
|
+
"<rootDir>/example/node_modules",
|
|
140
|
+
"<rootDir>/lib/"
|
|
141
|
+
]
|
|
142
|
+
},
|
|
143
|
+
"commitlint": {
|
|
144
|
+
"extends": [
|
|
145
|
+
"@commitlint/config-conventional"
|
|
146
|
+
]
|
|
147
|
+
},
|
|
148
|
+
"release-it": {
|
|
149
|
+
"git": {
|
|
150
|
+
"commitMessage": "chore: release ${version}",
|
|
151
|
+
"tagName": "v${version}"
|
|
152
|
+
},
|
|
153
|
+
"npm": {
|
|
154
|
+
"publish": true
|
|
155
|
+
},
|
|
156
|
+
"github": {
|
|
157
|
+
"release": true
|
|
158
|
+
},
|
|
159
|
+
"plugins": {
|
|
160
|
+
"@release-it/conventional-changelog": {
|
|
161
|
+
"preset": {
|
|
162
|
+
"name": "angular"
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
"create-react-native-library": {
|
|
168
|
+
"type": "turbo-module",
|
|
169
|
+
"languages": "kotlin-objc",
|
|
170
|
+
"tools": [
|
|
171
|
+
"eslint",
|
|
172
|
+
"jest",
|
|
173
|
+
"lefthook",
|
|
174
|
+
"release-it"
|
|
175
|
+
],
|
|
176
|
+
"version": "0.56.0"
|
|
177
|
+
}
|
|
178
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = package["name"]
|
|
7
|
+
s.version = package["version"]
|
|
8
|
+
s.summary = package["description"]
|
|
9
|
+
s.homepage = package["homepage"]
|
|
10
|
+
s.license = package["license"]
|
|
11
|
+
s.authors = package["author"]
|
|
12
|
+
|
|
13
|
+
s.platforms = { :ios => min_ios_version_supported }
|
|
14
|
+
s.source = { :git => "https://github.com/serldna86/react-native-secreton.git", :tag => "#{s.version}" }
|
|
15
|
+
s.requires_arc = true
|
|
16
|
+
|
|
17
|
+
s.source_files = "ios/**/*.{h,m,mm,swift,cpp}"
|
|
18
|
+
s.private_header_files = "ios/**/*.h"
|
|
19
|
+
|
|
20
|
+
s.script_phase = {
|
|
21
|
+
:name => 'Secreton Env',
|
|
22
|
+
:script => File.read(File.join(__dir__, 'scripts/secreton-ios.sh')),
|
|
23
|
+
:execution_position => :before_compile
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
install_modules_dependencies(s)
|
|
27
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { generateEnv } from './NodeSecreton.js';
|
|
4
|
+
|
|
5
|
+
const args = process.argv.slice(2);
|
|
6
|
+
const envFile: any = args[0] || process.env.ENV_FILE;
|
|
7
|
+
const fileName = path.basename(envFile);
|
|
8
|
+
|
|
9
|
+
(async () => {
|
|
10
|
+
try {
|
|
11
|
+
const secretKey = process.env.ENV_SECRET_KEY;
|
|
12
|
+
if (!secretKey) {
|
|
13
|
+
throw new Error('ENV_SECRET_KEY is required');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const fetchEnv: any = process.env.FETCH_ENV || 'consul';
|
|
17
|
+
|
|
18
|
+
await generateEnv({
|
|
19
|
+
envFile,
|
|
20
|
+
secretKey,
|
|
21
|
+
fetchEnv,
|
|
22
|
+
consul: fetchEnv === 'consul'
|
|
23
|
+
? {
|
|
24
|
+
addr: process.env.CONSUL_ADDR || 'http://localhost:8500',
|
|
25
|
+
path: process.env.CONSUL_PATH || 'mobile/project-example',
|
|
26
|
+
token: process.env.CONSUL_TOKEN,
|
|
27
|
+
}
|
|
28
|
+
: undefined,
|
|
29
|
+
vault: fetchEnv === 'vault'
|
|
30
|
+
? {
|
|
31
|
+
addr: process.env.VAULT_ADDR || 'http://localhost:8200',
|
|
32
|
+
path: process.env.VAULT_PATH || 'secret/data/mobile/project-example',
|
|
33
|
+
token: process.env.VAULT_TOKEN || '',
|
|
34
|
+
}
|
|
35
|
+
: undefined,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
console.log(`✅ [CLI] Secreton updated safely → ${fileName}`);
|
|
39
|
+
} catch (err: unknown) {
|
|
40
|
+
const message = err instanceof Error ? err.message : JSON.stringify(err, null, 2);
|
|
41
|
+
console.error(`❌ [CLI] Secreton failed: ${message}`);
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
})();
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import { execSync } from 'node:child_process';
|
|
4
|
+
|
|
5
|
+
export interface EnvEntry {
|
|
6
|
+
key: string;
|
|
7
|
+
value: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface ConsulConfig {
|
|
11
|
+
addr: string;
|
|
12
|
+
path: string;
|
|
13
|
+
token?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type ConsulKV = {
|
|
17
|
+
Key: string;
|
|
18
|
+
Value: string | null;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export interface VaultConfig {
|
|
22
|
+
addr: string;
|
|
23
|
+
path: string;
|
|
24
|
+
token: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface GenerateEnvOptions {
|
|
28
|
+
envFile: string;
|
|
29
|
+
secretKey: string;
|
|
30
|
+
consul?: ConsulConfig;
|
|
31
|
+
vault?: VaultConfig;
|
|
32
|
+
fetchEnv?: 'consul' | 'vault';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function encrypt(value: string, key: string) {
|
|
36
|
+
const cmd = `printf "%s" "${value}" | openssl enc -aes-256-cbc -a -A -salt -pbkdf2 -iter 100000 -pass pass:${key}`;
|
|
37
|
+
return execSync(cmd).toString().trim();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function readExistingEnv(envFile: string): Set<string> {
|
|
41
|
+
if (!fs.existsSync(envFile)) return new Set();
|
|
42
|
+
|
|
43
|
+
const content = fs.readFileSync(envFile, 'utf8');
|
|
44
|
+
|
|
45
|
+
return new Set(
|
|
46
|
+
content
|
|
47
|
+
.split('\n')
|
|
48
|
+
.map((line) => line.trim())
|
|
49
|
+
.filter((line): line is string => Boolean(line))
|
|
50
|
+
.filter((line) => !line.startsWith('#'))
|
|
51
|
+
.map((line) => line.split('=')[0])
|
|
52
|
+
.filter((key): key is string => typeof key === 'string' && key.length > 0)
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function fetchConsul({
|
|
57
|
+
addr,
|
|
58
|
+
path,
|
|
59
|
+
token,
|
|
60
|
+
}: ConsulConfig): Promise<EnvEntry[]> {
|
|
61
|
+
const headers: Record<string, string> = {};
|
|
62
|
+
if (token) headers['X-Consul-Token'] = token;
|
|
63
|
+
|
|
64
|
+
const res = await fetch(`${addr}/v1/kv/${path}?recurse=true`, { headers });
|
|
65
|
+
if (!res.ok) throw new Error('Consul fetch failed');
|
|
66
|
+
|
|
67
|
+
const data: ConsulKV[] = await res.json();
|
|
68
|
+
return data
|
|
69
|
+
.filter((item: any) => typeof item.Value === 'string')
|
|
70
|
+
.map((item: any) => ({
|
|
71
|
+
key: item.Key.replace(`${path}/`, ''),
|
|
72
|
+
value: Buffer.from(item.Value, 'base64').toString('utf8'),
|
|
73
|
+
}));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function fetchVault({
|
|
77
|
+
addr,
|
|
78
|
+
path,
|
|
79
|
+
token,
|
|
80
|
+
}: VaultConfig): Promise<EnvEntry[]> {
|
|
81
|
+
const res = await fetch(`${addr}/v1/${path}`, {
|
|
82
|
+
headers: { 'X-Vault-Token': token },
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
if (!res.ok) throw new Error('Vault fetch failed');
|
|
86
|
+
|
|
87
|
+
const json = await res.json();
|
|
88
|
+
return Object.entries(json.data.data).map(([key, value]) => ({
|
|
89
|
+
key,
|
|
90
|
+
value: String(value),
|
|
91
|
+
}));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export async function generateEnv(options: GenerateEnvOptions) {
|
|
95
|
+
const {
|
|
96
|
+
envFile,
|
|
97
|
+
secretKey,
|
|
98
|
+
fetchEnv = 'consul',
|
|
99
|
+
} = options;
|
|
100
|
+
|
|
101
|
+
const fileName = path.basename(envFile);
|
|
102
|
+
const existingKeys = readExistingEnv(envFile);
|
|
103
|
+
|
|
104
|
+
let entries: EnvEntry[] = [];
|
|
105
|
+
|
|
106
|
+
if (fetchEnv === 'vault' && options.vault) {
|
|
107
|
+
entries = await fetchVault(options.vault);
|
|
108
|
+
} else if (options.consul) {
|
|
109
|
+
entries = await fetchConsul(options.consul);
|
|
110
|
+
} else {
|
|
111
|
+
throw new Error('No config source provided');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const newLines = entries
|
|
115
|
+
.filter(({ key }) => !existingKeys.has(key))
|
|
116
|
+
.map(({ key, value }) => `${key}=enc:${encrypt(value, secretKey)}`);
|
|
117
|
+
|
|
118
|
+
if (newLines.length === 0) {
|
|
119
|
+
console.log('ℹ️ [Node] Secreton no new env keys to add');
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
fs.appendFileSync(envFile, (fs.existsSync(envFile) ? '\n' : '') + newLines.join('\n'));
|
|
124
|
+
|
|
125
|
+
console.log(`✅ [Node] Secreton updated safely → ${fileName}`);
|
|
126
|
+
}
|