Skip to content

Commit 328f987

Browse files
feat: [IDP-693]: Remove empty paths in allow list (#49349)
1 parent 0c3a021 commit 328f987

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

idp-service/src/main/java/io/harness/idp/allowlist/services/AllowListServiceImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class AllowListServiceImpl implements AllowListService {
4444
private static final String ALLOW_LIST = "allow-list";
4545
private static final String READING_PROPERTY = "reading";
4646
private static final String ALLOW_PROPERTY = "allow";
47+
private static final String PATHS_PROPERTY = "paths";
4748

4849
@Override
4950
public List<HostInfo> getAllowList(String harnessAccount) throws Exception {
@@ -59,6 +60,7 @@ public List<HostInfo> getAllowList(String harnessAccount) throws Exception {
5960
@Override
6061
public List<HostInfo> saveAllowList(List<HostInfo> hostInfoList, String harnessAccount) throws Exception {
6162
JsonNode allowListNode = asJsonNode(YamlUtils.writeObjectAsYaml(hostInfoList));
63+
removePathsNodeIfEmpty(allowListNode);
6264
String yamlString = CommonUtils.readFileFromClassPath(ALLOW_LIST_CONFIG_FILE);
6365
JsonNode rootNode = asJsonNode(yamlString);
6466
JsonNode readingNode = ConfigManagerUtils.getNodeByName(rootNode, READING_PROPERTY);
@@ -68,6 +70,15 @@ public List<HostInfo> saveAllowList(List<HostInfo> hostInfoList, String harnessA
6870
return hostInfoList;
6971
}
7072

73+
private void removePathsNodeIfEmpty(JsonNode jsonNode) {
74+
for (JsonNode allowNode : jsonNode) {
75+
JsonNode pathsNode = allowNode.get(PATHS_PROPERTY);
76+
if (pathsNode.isArray() && pathsNode.isEmpty()) {
77+
((ObjectNode) allowNode).remove(PATHS_PROPERTY);
78+
}
79+
}
80+
}
81+
7182
private void createOrUpdateAllowListAppConfig(String config, String accountIdentifier) throws Exception {
7283
String schema = CommonUtils.readFileFromClassPath(ALLOW_LIST_JSON_SCHEMA_FILE);
7384
if (!isValidSchema(config, schema)) {

idp-service/src/main/resources/configs/allowlist/allow-list-json-schema.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
"type": "object",
3939
"additionalProperties": false,
4040
"required": [
41-
"host",
42-
"paths"
41+
"host"
4342
],
4443
"properties": {
4544
"host": {

idp-service/src/test/java/io/harness/idp/allowlist/services/AllowListServiceImplTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,19 @@ public void testGetAllowList() throws Exception {
7272
@Category(UnitTests.class)
7373
public void testSaveAllowList() throws Exception {
7474
List<HostInfo> hostInfoList = new ArrayList<>();
75-
HostInfo hostInfo = new HostInfo();
76-
hostInfo.setHost("stress.harness.io");
77-
hostInfo.setPaths(new ArrayList<>());
75+
HostInfo hostInfo1 = new HostInfo();
76+
hostInfo1.setHost("stress.harness.io");
77+
hostInfo1.setPaths(new ArrayList<>());
78+
hostInfoList.add(hostInfo1);
79+
HostInfo hostInfo2 = new HostInfo();
80+
hostInfo2.setHost("qa.harness.io");
81+
hostInfo2.setPaths(List.of("/v1/secrets"));
82+
hostInfoList.add(hostInfo2);
7883
MockedStatic<YamlUtils> yamlUtilsMockedStatic = Mockito.mockStatic(YamlUtils.class);
7984
MockedStatic<CommonUtils> commonUtilsMockedStatic = Mockito.mockStatic(CommonUtils.class);
8085

8186
String yamlString = "backend:\n reading:\n allow: []";
82-
String allowListString = "- host: stress.harness.io\n paths: []";
87+
String allowListString = "- host: stress.harness.io\n paths: []\n- host: qa.harness.io\n paths:\n - /v1/secrets";
8388
when(YamlUtils.writeObjectAsYaml(any())).thenReturn(allowListString);
8489
when(CommonUtils.readFileFromClassPath(any())).thenReturn(yamlString).thenReturn(schema);
8590

0 commit comments

Comments
 (0)