Skip to content

Commit

Permalink
Add implementation for GW visibility control feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Naduni Pamudika committed Dec 10, 2024
1 parent a1781ac commit d5ffd60
Show file tree
Hide file tree
Showing 22 changed files with 190 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public class Environment implements Serializable {
private String gatewayType;
private Map<String, String> additionalProperties = new HashMap<>();

private String[] visibilityRoles;
private String visibility;

public boolean isDefault() {
return isDefault;
}
Expand Down Expand Up @@ -159,6 +162,37 @@ public void setName(String name) {
}
}

public String getVisibility() {
return visibility;
}

public void setVisibility(String visibility) {
this.visibility = visibility;
}

public String[] getVisibilityRoles() {
if (visibilityRoles != null) {
return visibilityRoles;
} else if (visibility != null) {
return visibility.split(",");
}
return null;
}

public void setVisibility(String[] visibilityRoles) {
if (visibilityRoles != null && !"".equals(visibilityRoles[0].trim())) {
StringBuilder builder = new StringBuilder();
for (String role : visibilityRoles) {
builder.append(role).append(',');
}
builder.deleteCharAt(builder.length() - 1);
this.visibility = builder.toString();
} else {
this.visibility = "all";
}
this.visibilityRoles = visibilityRoles;
}

public String getDisplayName() {
return displayName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,7 @@ private Permissions() {
public static final String API_GATEWAY = "APIGateway.";
public static final String API_GATEWAY_NAME = "Name";
public static final String API_GATEWAY_DISPLAY_NAME = "DisplayName";
public static final String API_GATEWAY_VISIBILITY = "Visibility";
public static final String API_GATEWAY_SERVER_URL = "ServerURL";
public static final String API_GATEWAY_USERNAME = "Username";
public static final String API_GATEWAY_PASSWORD = "Password";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4035,13 +4035,12 @@ public API getLightweightAPIByUUID(String uuid, String organization) throws APIM
devPortalApi.getVisibleRoles());
API api = APIMapper.INSTANCE.toApi(devPortalApi);

/// populate relavant external info
// environment
String environmentString = null;
// populate relevant external info environment
List<Environment> environments = null;
if (api.getEnvironments() != null) {
environmentString = String.join(",", api.getEnvironments());
environments = APIUtil.getEnvironmentsOfAPI(api);
}
api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environmentString, organization));
api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environments, organization, userNameWithoutChange));
//CORS . if null is returned, set default config from the configuration
if (api.getCorsConfiguration() == null) {
api.setCorsConfiguration(APIUtil.getDefaultCorsConfiguration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,15 @@ void setEnvironmentConfig(OMElement environmentElem) throws APIManagementExcepti
gatewayType = APIConstants.API_GATEWAY_TYPE_REGULAR;
}
environment.setGatewayType(gatewayType);
OMElement visibility = environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_VISIBILITY));
String[] visibilityRoles;
if (visibility == null) {
visibilityRoles = new String[]{"all"};
} else {
String visibilityString = visibility.getText();
visibilityRoles = visibilityString.split(",");
}
environment.setVisibility(visibilityRoles);
if (StringUtils.isEmpty(environment.getDisplayName())) {environment.setDisplayName(environment.getName());}
environment.setServerURL(APIUtil.replaceSystemProperty(environmentElem.getFirstChildWithName(new QName(
APIConstants.API_GATEWAY_SERVER_URL)).getText()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5600,13 +5600,12 @@ public API getLightweightAPIByUUID(String uuid, String organization) throws APIM
if (publisherAPI != null) {
API api = APIMapper.INSTANCE.toApi(publisherAPI);
checkAccessControlPermission(userNameWithoutChange, api.getAccessControl(), api.getAccessControlRoles());
/// populate relavant external info
// environment
String environmentString = null;
// populate relevant external info environment
List<Environment> environments = null;
if (api.getEnvironments() != null) {
environmentString = String.join(",", api.getEnvironments());
environments = APIUtil.getEnvironmentsOfAPI(api);
}
api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environmentString, organization));
api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environments, organization, userNameWithoutChange));
//CORS . if null is returned, set default config from the configuration
if (api.getCorsConfiguration() == null) {
api.setCorsConfiguration(APIUtil.getDefaultCorsConfiguration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1207,11 +1207,11 @@ protected void populateAPIInformation(String uuid, String organization, API api)
Organization org = new Organization(organization);
api.setOrganization(organization);
// environment
String environmentString = null;
List<Environment> environments = null;
if (api.getEnvironments() != null) {
environmentString = String.join(",", api.getEnvironments());
environments = APIUtil.getEnvironmentsOfAPI(api);
}
api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environmentString, organization));
api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environments, organization, username));
// workflow status
APIIdentifier apiId = api.getId();
WorkflowDTO workflow;
Expand Down Expand Up @@ -1382,11 +1382,11 @@ protected void populateDevPortalAPIInformation(String uuid, String organization,
}
api.setOrganization(organization);
// environment
String environmentString = null;
List<Environment> environments = null;
if (api.getEnvironments() != null) {
environmentString = String.join(",", api.getEnvironments());
environments = APIUtil.getEnvironmentsOfAPI(api);
}
api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environmentString, organization));
api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environments, organization, username));
// workflow status
APIIdentifier apiId = api.getId();
String currentApiUuid = uuid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15027,6 +15027,7 @@ public List<Environment> getAllEnvironments(String tenantDomain) throws APIManag
String uuid = rs.getString("UUID");
String name = rs.getString("NAME");
String type = rs.getString("TYPE");
String visibility = rs.getString("VISIBILITY");
String displayName = rs.getString("DISPLAY_NAME");
String description = rs.getString("DESCRIPTION");
String provider = rs.getString("PROVIDER");
Expand All @@ -15037,6 +15038,7 @@ public List<Environment> getAllEnvironments(String tenantDomain) throws APIManag
env.setUuid(uuid);
env.setName(name);
env.setType(type);
env.setVisibility(visibility);
env.setDisplayName(displayName);
env.setDescription(description);
env.setProvider(provider);
Expand Down Expand Up @@ -15071,6 +15073,7 @@ public Environment getEnvironment(String tenantDomain, String uuid) throws APIMa
Integer id = rs.getInt("ID");
String name = rs.getString("NAME");
String displayName = rs.getString("DISPLAY_NAME");
String visibility = rs.getString("VISIBILITY");
String description = rs.getString("DESCRIPTION");
String provider = rs.getString("PROVIDER");

Expand All @@ -15079,6 +15082,7 @@ public Environment getEnvironment(String tenantDomain, String uuid) throws APIMa
env.setUuid(uuid);
env.setName(name);
env.setDisplayName(displayName);
env.setVisibility(visibility);
env.setDescription(description);
env.setProvider(provider);
env.setVhosts(getVhostGatewayEnvironments(connection, id));
Expand Down Expand Up @@ -15111,11 +15115,12 @@ public Environment addEnvironment(String tenantDomain, Environment environment)
prepStmt.setString(1, uuid);
prepStmt.setString(2, environment.getName());
prepStmt.setString(3, environment.getType());
prepStmt.setString(4, environment.getDisplayName());
prepStmt.setString(5, environment.getDescription());
prepStmt.setString(6, environment.getProvider());
prepStmt.setString(7, environment.getGatewayType());
prepStmt.setString(8, tenantDomain);
prepStmt.setString(4, environment.getVisibility());
prepStmt.setString(5, environment.getDisplayName());
prepStmt.setString(6, environment.getDescription());
prepStmt.setString(7, environment.getProvider());
prepStmt.setString(8, environment.getGatewayType());
prepStmt.setString(9, tenantDomain);
prepStmt.executeUpdate();

ResultSet rs = prepStmt.getGeneratedKeys();
Expand Down Expand Up @@ -15273,7 +15278,8 @@ public Environment updateEnvironment(Environment environment) throws APIManageme
try (PreparedStatement prepStmt = connection.prepareStatement(SQLConstants.UPDATE_ENVIRONMENT_SQL)) {
prepStmt.setString(1, environment.getDisplayName());
prepStmt.setString(2, environment.getDescription());
prepStmt.setString(3, environment.getUuid());
prepStmt.setString(3, environment.getVisibility());
prepStmt.setString(4, environment.getUuid());
prepStmt.executeUpdate();
deleteGatewayVhosts(connection, environment.getId());
addGatewayVhosts(connection, environment.getId(), environment.getVhosts());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2732,18 +2732,18 @@ public class SQLConstants {
/** Environment related constants **/

public static final String GET_ENVIRONMENT_BY_ORGANIZATION_SQL =
"SELECT ID, UUID, NAME, TYPE, ORGANIZATION, DISPLAY_NAME, DESCRIPTION, PROVIDER, GATEWAY_TYPE " +
"SELECT ID, UUID, NAME, TYPE, VISIBILITY, ORGANIZATION, DISPLAY_NAME, DESCRIPTION, PROVIDER, GATEWAY_TYPE " +
"FROM AM_GATEWAY_ENVIRONMENT " +
"WHERE ORGANIZATION = ?";

public static final String GET_ENVIRONMENT_BY_ORGANIZATION_AND_UUID_SQL =
"SELECT ID, UUID, NAME, ORGANIZATION, DISPLAY_NAME, DESCRIPTION, PROVIDER, GATEWAY_TYPE " +
"SELECT ID, UUID, NAME, VISIBILITY, ORGANIZATION, DISPLAY_NAME, DESCRIPTION, PROVIDER, GATEWAY_TYPE " +
"FROM AM_GATEWAY_ENVIRONMENT " +
"WHERE ORGANIZATION = ? AND UUID = ?";

public static final String INSERT_ENVIRONMENT_SQL = "INSERT INTO " +
"AM_GATEWAY_ENVIRONMENT (UUID, NAME, TYPE, DISPLAY_NAME, DESCRIPTION, PROVIDER, GATEWAY_TYPE, ORGANIZATION) " +
"VALUES (?,?,?,?,?,?,?,?)";
"AM_GATEWAY_ENVIRONMENT (UUID, NAME, TYPE, VISIBILITY, DISPLAY_NAME, DESCRIPTION, PROVIDER, GATEWAY_TYPE, ORGANIZATION) " +
"VALUES (?,?,?,?,?,?,?,?,?)";

public static final String INSERT_GATEWAY_VHOSTS_SQL = "INSERT INTO " +
"AM_GW_VHOST (GATEWAY_ENV_ID, HOST, HTTP_CONTEXT, HTTP_PORT, HTTPS_PORT, WS_PORT, WSS_PORT) " +
Expand All @@ -2758,7 +2758,7 @@ public class SQLConstants {
public static final String DELETE_ENVIRONMENT_SQL = "DELETE FROM AM_GATEWAY_ENVIRONMENT WHERE UUID = ?";

public static final String UPDATE_ENVIRONMENT_SQL = "UPDATE AM_GATEWAY_ENVIRONMENT " +
"SET DISPLAY_NAME = ?, DESCRIPTION = ? " +
"SET DISPLAY_NAME = ?, DESCRIPTION = ? , VISIBILITY = ?" +
"WHERE UUID = ?";

public static final String INSERT_LLM_PROVIDER_SQL =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5031,23 +5031,84 @@ public static Set<String> extractEnvironmentsForAPI(String environments) throws
return environmentStringSet;
}

public static Set<String> extractVisibleEnvironmentsForUser(List<Environment> environments, String organization, String userName) throws APIManagementException {

Set<String> environmentStringSet = new HashSet<String>();
String[] userRoles = APIUtil.getListOfRoles(userName);
if (environments != null) {
for (Environment environment : environments) {
String[] permittedRoles = environment.getVisibilityRoles();
if (permittedRoles[0].equals("all")) {
environmentStringSet.add(environment.toString());
} else {
for (String role : userRoles) {
for (String permission : permittedRoles) {
if (role.equals(permission)) {
environmentStringSet.add(environment.toString());
}
}
}
}
}
} else {
Map<String, Environment> environmentsMap = getEnvironments(organization);
for (Environment environment : environmentsMap.values()) {
String[] permittedRoles = environment.getVisibilityRoles();
if (permittedRoles[0].equals("all")) {
environmentStringSet.add(environment.toString());
} else {
for (String role : userRoles) {
for (String permission : permittedRoles) {
if (role.equals(permission)) {
environmentStringSet.add(environment.toString());
}
}
}
}
}
}
return environmentStringSet;
}

public static Map<String, Environment> extractVisibleEnvironmentsForUser(String organization, String userName) throws APIManagementException {

Map<String, Environment> returnEnvironments = new LinkedHashMap<>();
String[] userRoles = APIUtil.getListOfRoles(userName);
Map<String, Environment> environmentsMap = getEnvironments(organization);
for (Environment environment : environmentsMap.values()) {
String[] permittedRoles = environment.getVisibilityRoles();
if (permittedRoles[0].equals("all")) {
returnEnvironments.put(environment.getName(), environment);
} else {
for (String role : userRoles) {
for (String permission : permittedRoles) {
if (role.equals(permission)) {
returnEnvironments.put(environment.getName(), environment);
}
}
}
}
}
return returnEnvironments;
}

public static Set<String> extractEnvironmentsForAPI(String environments, String organization) throws APIManagementException {

Set<String> environmentStringSet = null;
if (environments == null) {
environmentStringSet = new HashSet<>(getEnvironments(organization).keySet());
} else {
//handle not to publish to any of the gateways
// Handle not to publish to any of the gateways
if (APIConstants.API_GATEWAY_NONE.equals(environments)) {
environmentStringSet = new HashSet<String>();
}
//handle to set published gateways nto api object
// Handle to set published gateways into api object
else if (!"".equals(environments)) {
String[] publishEnvironmentArray = environments.split(",");
environmentStringSet = new HashSet<String>(Arrays.asList(publishEnvironmentArray));
environmentStringSet.remove(APIConstants.API_GATEWAY_NONE);
}
//handle to publish to any of the gateways when api creating stage
// Handle to publish to any of the gateways when api creating stage
else if ("".equals(environments)) {
environmentStringSet = new HashSet<>(getEnvironments(organization).keySet());
}
Expand All @@ -5056,6 +5117,30 @@ else if ("".equals(environments)) {
return environmentStringSet;
}

public static Set<String> extractEnvironmentsForAPI(List<Environment> environments, String organization, String userName) throws APIManagementException {

Set<String> environmentStringSet = null;
if (environments == null) {
environmentStringSet = extractVisibleEnvironmentsForUser(environments, organization, userName);
} else {
// Handle not to publish to any of the gateways
if (environments.contains(APIConstants.API_GATEWAY_NONE)) {
environmentStringSet = new HashSet<String>();
}
// Handle to set published gateways into api object
else if (!environments.isEmpty()) {
environmentStringSet = extractVisibleEnvironmentsForUser(environments, organization, userName);
environmentStringSet.remove(APIConstants.API_GATEWAY_NONE);
}
// Handle to publish to any of the gateways when api creating stage
else if ("".equals(environments)) {
environmentStringSet = extractVisibleEnvironmentsForUser(environments, organization, userName);
}
}

return environmentStringSet;
}

/**
* This method used to set environment values to governance artifact of API .
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,10 @@
"type" : "string",
"description" : "Context of the API."
},
"contextTemplate" : {
"type" : "string",
"description" : "Context template of the API."
},
"policy" : {
"type" : "string",
"description" : "API level throttling policy."
Expand Down Expand Up @@ -1525,8 +1529,8 @@
"type" : "string",
"example" : "EXCHANGED",
"description" : "The type of the tokens to be used (exchanged or without exchanged). Accepted values are EXCHANGED, DIRECT or BOTH.",
"default" : "DIRECT",
"enum" : [ "EXCHANGED", "DIRECT", "BOTH" ]
"enum" : [ "EXCHANGED", "DIRECT", "BOTH" ],
"default" : "DIRECT"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class EnvironmentsApiServiceImpl implements EnvironmentsApiService {
private static final Log log = LogFactory.getLog(EnvironmentsApiServiceImpl.class);

/**
* Delete gateway envirionment
* Delete gateway environment
*
* @param environmentId environment ID
* @param messageContext message context
Expand Down
Loading

0 comments on commit d5ffd60

Please sign in to comment.