Skip to content

Commit c59592f

Browse files
feat: Add schema validation for configs
1 parent 931972d commit c59592f

File tree

6 files changed

+113
-6
lines changed

6 files changed

+113
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Access/access_modules/

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
schema_validate:
2+
@echo $(shell python3 scripts/clone_access_modules.py && python3 scripts/validator.py)
3+
14
run_semgrep:
25
$(shell semgrep --error --config "p/cwe-top-25" --config "p/owasp-top-ten" --config "p/r2c-security-audit")

config.json

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
{
2-
"googleapi": {
3-
"SOCIAL_AUTH_GOOGLE_OAUTH2_KEY": "",
4-
"SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET": "",
5-
"SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS": ""
6-
}
7-
}
2+
"googleapi": {
3+
"SOCIAL_AUTH_GOOGLE_OAUTH2_KEY": "",
4+
"SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET": "",
5+
"SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS": ""
6+
},
7+
"access_modules": {
8+
"git_urls": [
9+
"https://github.com/browserstack/enigma-public-access-modules.git"
10+
]
11+
},
12+
"enigmaGroup": {
13+
"MAIL_APPROVER_GROUPS": [
14+
"devnull@browserstack.com"
15+
]
16+
}
17+
}

schema.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"$id": "/schemas/central",
4+
"title": "Config",
5+
"description": "The config file schema",
6+
"type": "object",
7+
"properties": {
8+
"googleapi": {
9+
"description": "Config keys related to Google SSO",
10+
"type": "object",
11+
"properties": {
12+
"SOCIAL_AUTH_GOOGLE_OAUTH2_KEY": {
13+
"description": "Google OAuth2 Key",
14+
"type": "string"
15+
},
16+
"SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET": {
17+
"description": "Google OAuth2 secret",
18+
"type": "string"
19+
},
20+
"SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS": {
21+
"description": "Google OAuth2 whitelisted domains",
22+
"type": "string"
23+
}
24+
}
25+
},
26+
"access_modules": {
27+
"description": "List of access modules attached to this tool",
28+
"type": "object",
29+
"properties": {
30+
"git_urls": {
31+
"description": "List of git URLs of access modules",
32+
"type": "array"
33+
}
34+
}
35+
},
36+
"enigmaGroup":{
37+
"description":"Config related of enigma groups",
38+
"type":"object",
39+
"properties":{
40+
"MAIL_APPROVER_GROUPS":{
41+
"description":"List of mail approvers",
42+
"type":"array"
43+
}
44+
}
45+
}
46+
},
47+
"required": [
48+
"googleapi"
49+
]
50+
}

scripts/clone_access_modules.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import json
2+
import sys
3+
from git import Repo
4+
5+
try:
6+
f = open("./config.json","r")
7+
config = json.load(f)
8+
urls = config["access_modules"]["git_urls"]
9+
for url in urls:
10+
folder_name = url.split("/").pop()[:-4]
11+
try:
12+
Repo.clone_from(url, "./Access/access_modules/"+folder_name)
13+
except Exception as e:
14+
print("failed cloning "+folder_name+".")
15+
except Exception as e:
16+
print("Access module cloning failed!")
17+
print(str(e))
18+
sys.exit(1)

scripts/validator.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import json
2+
import os
3+
import sys
4+
from jsonschema import validate
5+
6+
try:
7+
f = open("./schema.json","r")
8+
schema = json.load(f)
9+
f = open("./config.json", "r")
10+
config = json.load(f)
11+
root_folders = [ f.path for f in os.scandir("./Access/access_modules") if f.is_dir() ]
12+
for folder in root_folders:
13+
modules = [ f.path for f in os.scandir(folder) if f.is_dir() ]
14+
for module in modules:
15+
if os.path.exists(module+"/schema.json"):
16+
f = open(module+"/schema.json")
17+
module_schema = json.load(f)
18+
schema["properties"].update(module_schema["properties"])
19+
schema["required"] += module_schema["required"]
20+
validate(instance=config, schema=schema)
21+
print("Schema validation passed!")
22+
except Exception as e:
23+
print("Schema validation failed!")
24+
print(e)
25+
sys.exit(1)

0 commit comments

Comments
 (0)