Skip to content

Commit

Permalink
Merge pull request #12182 from chamilaadhi/admin_portal
Browse files Browse the repository at this point in the history
Admin portal API scopes related fixs
  • Loading branch information
chamilaadhi authored Nov 16, 2023
2 parents 1cfcab5 + 9ddc40f commit 73a1bcb
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

/**
Expand Down Expand Up @@ -176,7 +178,62 @@ public String getTenantConfig(String organization) throws APIManagementException
if (organization == null) {
organization = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
}
return systemConfigurationsDAO.getSystemConfig(organization, ConfigType.TENANT.toString());
return addMissingScopes(systemConfigurationsDAO.getSystemConfig(organization, ConfigType.TENANT.toString()));
}

/*
* This method facilitates the on-the-fly migration of the scope section in the tenant-config.json. This
* checks whether RESTAPIScopes section has newly introduced scopes and add them to the json String if it
* is not available.
*/
private String addMissingScopes(String systemConfig) {
if (systemConfig == null) {
return null;
}
// List of newly introduced scopes
String[] scopesToCheck = {
"apim:admin_tier_view",
"apim:admin_tier_manage",
"apim:keymanagers_manage",
"apim:api_category"
};

ArrayList<String> missingScopesList = new ArrayList<>(Arrays.asList(scopesToCheck));

JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(systemConfig).getAsJsonObject();

// Get the existing rest api scopes
if (jsonObject.has("RESTAPIScopes")) {
JsonObject restApiScopes = jsonObject.getAsJsonObject("RESTAPIScopes");
if (restApiScopes.has("Scope")) {
JsonArray scopeArray = restApiScopes.getAsJsonArray("Scope");
for (int i = 0; i < scopeArray.size(); i++) {
String existingScope = scopeArray.get(i).getAsJsonObject().get("Name").getAsString();
if (missingScopesList.contains(existingScope)) {
missingScopesList.remove(existingScope);
}
}
}
}

// Check if there is no missing scopes in the tenant-conf.json and return the original file
if (missingScopesList.isEmpty()) {
return systemConfig;
}

JsonArray scopeArray = jsonObject.getAsJsonObject("RESTAPIScopes").getAsJsonArray("Scope");
// Add the missing scopes to the tenant-conf
for (String missingScope : missingScopesList) {
JsonObject newScope = new JsonObject();
newScope.addProperty("Name", missingScope);
newScope.addProperty("Roles", "admin");
scopeArray.add(newScope);
}

// Convert the modified JSON back to a string
String modifiedJson = jsonObject.toString();
return modifiedJson;
}

@Override
Expand All @@ -188,6 +245,9 @@ public void updateTenantConfig(String organization, String tenantConfig) throws
Cache tenantConfigCache = CacheProvider.getTenantConfigCache();
String cacheName = organization + "_" + APIConstants.TENANT_CONFIG_CACHE_NAME;
tenantConfigCache.remove(cacheName);

// Clear restapi scope cache
CacheProvider.getRESTAPIScopeCache().remove(organization);
systemConfigurationsDAO.updateSystemConfig(organization, ConfigType.TENANT.toString(), tenantConfig);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,14 @@
{
"Name": "apim:policies_import_export",
"Roles": "admin,Internal/devops"
},
{
"Name": "apim:admin_tier_manage",
"Roles": "admin"
},
{
"Name": "apim:admin_tier_view",
"Roles": "admin"
}
]
},
Expand Down
Loading

0 comments on commit 73a1bcb

Please sign in to comment.