Skip to content

Commit a5ca8bf

Browse files
committed
✨add config update in kyma configmanager pod
1 parent 4364762 commit a5ca8bf

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

configmanager/server.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ app.use(hsts({
1010
}))
1111
app.use(express.json())
1212

13+
app.use((err, req, res, next) => {
14+
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
15+
return res.status(400).send("Invalid JSON")
16+
}
17+
next(err);
18+
});
19+
1320
// Define a GET route that accepts a namespace and application parameter
1421
app.get('/:namespace/:application', (req, res) => {
1522
res.setHeader("Strict-Transport-Security", "max-age=31536000; includeSubDomains");
@@ -141,6 +148,57 @@ app.get('/:namespace/:application', (req, res) => {
141148
});
142149
});
143150

151+
app.post('/:namespace/:application', (req, res) => {
152+
if (req.headers['content-type'] != 'application/json') return res.status(400).send("Invalid JSON");
153+
const { namespace, application } = req.params;
154+
console.log(req.body)
155+
const body = JSON.stringify(req.body)
156+
console.log(body)
157+
try {
158+
const parsedBody = JSON.parse(body);
159+
console.log(parsedBody)
160+
const newDecoys = parsedBody.decoys;
161+
const newConfig = parsedBody.config;
162+
var filePath = '', configFilePath = ''
163+
if (!namespace.match(/^[a-zA-Z0-9-]+$/) || !application.match(/^[a-zA-Z0-9-]+$/)) {
164+
console.warn(`Bad path provided for decoys config file: ${filePath}, ${configFilePath}`);
165+
} else {
166+
filePath = path.resolve(`/data/cad-${namespace}-${application}.json`);
167+
configFilePath = path.resolve(`/data/config-${namespace}-${application}.json`);
168+
}
169+
const defaultFilePath = `/data/cad-default.json`;
170+
const defaultConfigFilePath = `/data/config-default.json`;
171+
172+
if (newDecoys){
173+
fs.access(filePath, fs.constants.F_OK, err => {
174+
if (err) {
175+
fs.access(defaultFilePath, fs.constants.F_OK, err => {
176+
if (err) return res.send("Cannot update decoy config");
177+
fs.writeFileSync(defaultFilePath, JSON.stringify(newDecoys));
178+
})
179+
} else {
180+
fs.writeFileSync(filePath, JSON.stringify(newDecoys));
181+
}
182+
})
183+
}
184+
if (newConfig) {
185+
fs.access(configFilePath, fs.constants.F_OK, err => {
186+
if (err) {
187+
fs.access(defaultConfigFilePath, fs.constants.F_OK, err => {
188+
if (err) return res.send("Cannot update config");
189+
fs.writeFileSync(defaultConfigFilePath, JSON.stringify(newConfig))
190+
})
191+
} else {
192+
fs.writeFileSync(configFilePath, JSON.stringify(newConfig))
193+
}
194+
})
195+
}
196+
return res.send("Config updated");
197+
} catch (err) {
198+
return res.status(400).send("Invalid JSON");
199+
}
200+
});
201+
144202
app.get('/blocklist', (req, res) => {
145203
fs.access("/data/blocklist/blocklist.json", fs.constants.F_OK, err => {
146204
if (err) {

kyma/configmanager/cad-config.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"config": {
3+
"alert": {
4+
"session": {
5+
"in": "cookie",
6+
"key": "SESSION"
7+
},
8+
"username": {
9+
"in": "",
10+
"key": "",
11+
"value": ""
12+
}
13+
},
14+
"server": "",
15+
"respond": [
16+
{
17+
"source": "",
18+
"behavior": "",
19+
"delay": "",
20+
"duration": ""
21+
}
22+
],
23+
"blocklistReload": 1
24+
},
25+
"decoys": {
26+
"filters": [
27+
{
28+
"decoy": {
29+
"key": "x-cloud-active-defense",
30+
"separator": "=",
31+
"value": "ACTIVE"
32+
},
33+
"inject": {
34+
"store": {
35+
"inResponse": ".*",
36+
"as": "header"
37+
}
38+
}
39+
}
40+
]
41+
}
42+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
@echo off
2+
chcp 65001>nul
3+
setlocal enabledelayedexpansion
4+
5+
set dir=%~dp0
6+
set namespace=unknown
7+
set deployment=unknown
8+
9+
if "%1"=="-h" (
10+
echo Edit the config for Cloud Active Defense
11+
echo Specify the NAMESPACE and DEPLOYMENT of your app, if none are specify the default config will be edited
12+
echo Edit the cad-config.json file to send your changes to configmanager ^(Changes will overwrite previous config^)
13+
echo.
14+
echo Usage:
15+
echo ./edit-cad-config.bat NAMESPACE DEPLOYMENT
16+
exit
17+
)
18+
19+
if not defined KUBECONFIG (
20+
echo Please set KUBECONFIG to connect to the cluster
21+
echo Example:
22+
echo $ENV:KUBECONFIG="PATH\TO\FILE"
23+
exit
24+
)
25+
26+
if not "%1"=="" (
27+
for /f "tokens=* delims=" %%A in ('kubectl get ns ^| findstr %1') do set "namespaceResult=%%A"
28+
if not defined namespaceResult (
29+
echo Namespace doesn't exists, exiting... 🚪
30+
exit
31+
)
32+
set namespace=%1
33+
)
34+
if not "%2"=="" (
35+
for /f "tokens=* delims=" %%A in ('kubectl get deployment -n %1 ^| findstr %2') do set "deploymentResult=%%A"
36+
if not defined deploymentResult (
37+
echo Deployment doesn't exists, exiting... 🚪
38+
exit
39+
)
40+
set deployment=%2
41+
) else (
42+
if not "%1"=="" (
43+
echo Deployment name is missing, editing default config... 🔧
44+
)
45+
)
46+
set filename=%dir%\cad-config.json
47+
set "content="
48+
for /f "delims=" %%i in (%filename%) do set "content=!content! %%i"
49+
(
50+
echo apiVersion: batch/v1
51+
echo kind: Job
52+
echo metadata:
53+
echo name: edit-decoys
54+
echo namespace: config-ns
55+
echo spec:
56+
echo template:
57+
echo spec:
58+
echo containers:
59+
echo - name: curl
60+
echo image: curlimages/curl
61+
echo command: ['sh', '-c', 'curl -X POST configmanager-service/namespace/deployment -H "Content-Type: application/json" -d ''!content!''']
62+
echo restartPolicy: Never
63+
) > %dir%\cad-job.yaml
64+
kubectl apply -f %dir%\cad-job.yaml > nul
65+
del %dir%\cad-job.yaml
66+
67+
timeout /t 2 /nobreak > nul
68+
for /f "tokens=*" %%i in ('kubectl logs -l job-name^=edit-decoys -n config-ns ^| findstr "Config updated"') do set editResult=%%i
69+
if not defined editResult (
70+
echo Something went wrong when editing the config ⚠️
71+
) else (
72+
echo Updated decoys configuration 💫
73+
)
74+
kubectl delete job edit-decoys -n config-ns > nul
75+
endlocal

0 commit comments

Comments
 (0)