Skip to content

Commit 35baf8d

Browse files
committed
feat: add secrets script
1 parent c8b4591 commit 35baf8d

File tree

5 files changed

+179
-4
lines changed

5 files changed

+179
-4
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*.test.tsx
55
.eslintrc.js
66
vite.config.mts
7+
secrets.js
78

89
# The following files have eslint errors/warnings
910
src/Pages/GlobalConfigurations/Authorization/APITokens/__tests__/ApiTokens.test.tsx

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,5 @@ yalc.lock
5252
!.yarn/sdks
5353
!.yarn/versions
5454
.pnp.*
55+
56+
.env.secrets

secrets.js

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
const fs = require('fs');
2+
3+
const ENV_FILE_PATH = '.env.secrets';
4+
const DEVTRON_HOST = 'https://dev-staging.devtron.info';
5+
6+
// Function to read environment variables from .env.secrets file
7+
const readEnvFile = () => {
8+
try {
9+
const envContent = fs.readFileSync(ENV_FILE_PATH, 'utf8');
10+
const envVars = {};
11+
envContent.split('\n').forEach(line => {
12+
const [key, value] = line.split('=');
13+
if (key && value) {
14+
envVars[key.trim()] = value.trim();
15+
}
16+
});
17+
return envVars;
18+
} catch (error) {
19+
console.error(`Error reading ${ENV_FILE_PATH}:`, error.message);
20+
return {};
21+
}
22+
};
23+
24+
const writeIngressHostToEnv = async (token, namespace) => {
25+
const cookie = `argocd.token=${token}`;
26+
27+
const ingressBody = {
28+
appId: "",
29+
clusterId: 1,
30+
k8sRequest: {
31+
resourceIdentifier: {
32+
groupVersionKind: {
33+
Group: "networking.k8s.io",
34+
Version: "v1",
35+
Kind: "Ingress"
36+
},
37+
namespace,
38+
}
39+
}
40+
};
41+
42+
const { result: { data: [ingress] } } = await fetch(`${DEVTRON_HOST}/orchestrator/k8s/resource/list`, {
43+
"headers": {
44+
"cookie": cookie,
45+
},
46+
"body": JSON.stringify(ingressBody),
47+
"method": "POST"
48+
}).then(response => response.json());
49+
50+
const ingressDetailBody = {
51+
appId: "",
52+
clusterId: 1,
53+
k8sRequest: {
54+
resourceIdentifier: {
55+
groupVersionKind: {
56+
Group: "networking.k8s.io",
57+
Version: "v1",
58+
Kind: "Ingress"
59+
},
60+
namespace,
61+
name: ingress.name
62+
}
63+
}
64+
};
65+
66+
const { result: { manifestResponse: { manifest: { spec: { rules: [ { host } ] } } } }} = await fetch(`${DEVTRON_HOST}/orchestrator/k8s/resource`, {
67+
"headers": {
68+
"cookie": cookie,
69+
},
70+
"body": JSON.stringify(ingressDetailBody),
71+
"method": "POST"
72+
}).then(response => response.json());
73+
74+
// Read existing .env file or create empty string if it doesn't exist
75+
let envContent = '';
76+
try {
77+
envContent = fs.readFileSync(ENV_FILE_PATH, 'utf8');
78+
} catch (error) {
79+
// File doesn't exist, will create it
80+
}
81+
82+
// Check if VITE_TARGET_URL already exists and update or add it
83+
if (envContent.includes('VITE_TARGET_URL=')) {
84+
// Replace existing value
85+
envContent = envContent.replace(/VITE_TARGET_URL=.*$/m, `VITE_TARGET_URL=https://${host}`);
86+
console.log('VITE_TARGET_URL updated in .env.secrets file');
87+
} else {
88+
// Add new entry
89+
const newLine = envContent.length > 0 && !envContent.endsWith('\n') ? '\n' : '';
90+
envContent += `${newLine}VITE_TARGET_URL=https://${host}\n`;
91+
console.log('VITE_TARGET_URL added to .env.secrets file');
92+
}
93+
94+
fs.writeFileSync(ENV_FILE_PATH, envContent);
95+
};
96+
97+
const main = async () => {
98+
if (!process.argv[2]) {
99+
console.error('Please provide a namespace as an argument.');
100+
process.exit(1);
101+
}
102+
103+
const namespace = process.argv[2];
104+
105+
// Read token from .env.secrets file
106+
const envVars = readEnvFile();
107+
const token = envVars.DEV_STAGING_TOKEN;
108+
109+
if (!token) {
110+
console.error('DEV_STAGING_TOKEN not found in .env.secrets file');
111+
process.exit(1);
112+
}
113+
114+
const cookie = `argocd.token=${token}`
115+
116+
const secretsBody = {
117+
appId: "",
118+
clusterId: 1,
119+
k8sRequest: {
120+
resourceIdentifier: {
121+
groupVersionKind: {
122+
Group: "",
123+
Version: "v1",
124+
Kind: "Secret"
125+
},
126+
namespace,
127+
name: "orchestrator-secret"
128+
}
129+
}
130+
}
131+
132+
const { result: { manifestResponse: { manifest: { data: { ADMIN_PASSWORD } } } }} = await fetch(`${DEVTRON_HOST}/orchestrator/k8s/resource`, {
133+
"headers": {
134+
"cookie": cookie,
135+
},
136+
"body": JSON.stringify(secretsBody),
137+
"method": "POST"
138+
}).then(response => response.json());
139+
140+
// Read existing .env file or create empty string if it doesn't exist
141+
let envContent = '';
142+
try {
143+
envContent = fs.readFileSync(ENV_FILE_PATH, 'utf8');
144+
} catch (error) {
145+
// File doesn't exist, will create it
146+
}
147+
148+
// Decode the base64 encoded ADMIN_PASSWORD
149+
const decodedPassword = Buffer.from(ADMIN_PASSWORD, 'base64').toString('utf8');
150+
151+
// Check if VITE_ADMIN_PASSWORD already exists and update or add it
152+
if (envContent.includes('VITE_ADMIN_PASSWORD=')) {
153+
// Replace existing value
154+
envContent = envContent.replace(/VITE_ADMIN_PASSWORD=.*$/m, `VITE_ADMIN_PASSWORD=${decodedPassword}`);
155+
console.log('VITE_ADMIN_PASSWORD updated in .env.secrets file');
156+
} else {
157+
// Add new entry
158+
const newLine = envContent.length > 0 && !envContent.endsWith('\n') ? '\n' : '';
159+
envContent += `${newLine}VITE_ADMIN_PASSWORD=${decodedPassword}\n`;
160+
console.log('VITE_ADMIN_PASSWORD added to .env.secrets file');
161+
}
162+
163+
fs.writeFileSync(ENV_FILE_PATH, envContent);
164+
165+
await writeIngressHostToEnv(token, namespace);
166+
}
167+
168+
main()

src/components/login/LoginForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const LoginForm = ({ loginList }: LoginFormType) => {
4343
const [loading, setLoading] = useState(false)
4444
const [form, setForm] = useState({
4545
username: 'admin',
46-
password: '',
46+
password: import.meta.env.VITE_ADMIN_PASSWORD ?? '',
4747
})
4848
const [errorMessage, setErrorMessage] = useState({
4949
username: {

vite.config.mts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ const jsToBottomNoModule = () => {
9494

9595
// https://vitejs.dev/config/
9696
export default defineConfig(({ mode }) => {
97-
process.env = { ...process.env, ...loadEnv(mode, process.cwd(), '') }
97+
const secretsEnv = loadEnv('secrets', process.cwd(), '')
98+
const targetUrl = secretsEnv.VITE_TARGET_URL ?? TARGET_URL
99+
100+
process.env = { ...process.env, ...loadEnv(mode, process.cwd(), ''), ...secretsEnv }
101+
98102
const baseConfig: UserConfig = {
99103
base: '/dashboard',
100104
preview: {
@@ -268,10 +272,10 @@ export default defineConfig(({ mode }) => {
268272
port: 3000,
269273
proxy: {
270274
'/orchestrator': {
271-
target: TARGET_URL,
275+
target: targetUrl,
272276
changeOrigin: true,
273277
},
274-
'/grafana': TARGET_URL,
278+
'/grafana': targetUrl,
275279
},
276280
},
277281
}

0 commit comments

Comments
 (0)