added functionality to hide repos/roles from beta to production and vice versa

This commit is contained in:
Konstantinos Spyrou 2021-07-16 09:25:15 +00:00
parent 940fd326ad
commit 6d492ae919
3 changed files with 28 additions and 15 deletions

View File

@ -14,6 +14,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -29,7 +30,7 @@ public class RegistryCalls implements AaiRegistryService {
public final RegistryUtils jsonUtils; public final RegistryUtils jsonUtils;
@Autowired @Autowired
RegistryCalls(@Value("${registry.coid:2}") String coid, RegistryCalls(@Value("${aai.registry.coid:2}") String coid,
HttpUtils httpUtils, RegistryUtils registryUtils) { HttpUtils httpUtils, RegistryUtils registryUtils) {
this.coid = coid; this.coid = coid;
this.httpUtils = httpUtils; this.httpUtils = httpUtils;
@ -121,7 +122,7 @@ public class RegistryCalls implements AaiRegistryService {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("coid", coid); params.put("coid", coid);
if (name != null) { if (name != null) {
params.put("name", name.toLowerCase()); params.put("name", URLEncoder.encode(name).toLowerCase());
} }
JsonElement response = httpUtils.get("cous.json", params); JsonElement response = httpUtils.get("cous.json", params);
return (response != null) ? response.getAsJsonObject().get("Cous").getAsJsonArray() : new JsonArray(); return (response != null) ? response.getAsJsonObject().get("Cous").getAsJsonArray() : new JsonArray();
@ -245,6 +246,9 @@ public class RegistryCalls implements AaiRegistryService {
@Override @Override
public JsonArray getUserEmailByCouId(Integer couId, boolean admin) { public JsonArray getUserEmailByCouId(Integer couId, boolean admin) {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
if (couId == null) {
throw new IllegalArgumentException("Provided 'couId' is null");
}
params.put("couid", couId.toString()); params.put("couid", couId.toString());
if (admin) { if (admin) {
params.put("admin", "true"); params.put("admin", "true");

View File

@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.Collection; import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Service("roleMappingService") @Service("roleMappingService")
@ -15,7 +16,7 @@ public class AaiRoleMappingService implements RoleMappingService {
private static final Logger logger = Logger.getLogger(AaiRoleMappingService.class); private static final Logger logger = Logger.getLogger(AaiRoleMappingService.class);
@Value("${registry.production:true}") @Value("${aai.registry.production:true}")
private boolean production; private boolean production;
@ -33,6 +34,9 @@ public class AaiRoleMappingService implements RoleMappingService {
@Override @Override
public String getRepoIdByRoleId(String roleId) { public String getRepoIdByRoleId(String roleId) {
if (!roleActive(roleId)) {
return null;
}
return roleId.replaceFirst(".*datasource\\.", "").replace("$", ":"); return roleId.replaceFirst(".*datasource\\.", "").replace("$", ":");
} }
@ -40,7 +44,9 @@ public class AaiRoleMappingService implements RoleMappingService {
public Collection<String> getRepoIdsByRoleIds(Collection<String> roleIds) { public Collection<String> getRepoIdsByRoleIds(Collection<String> roleIds) {
return roleIds return roleIds
.stream() .stream()
//.filter(this::roleActive) // implicitly executed in the next statement
.map(this::getRepoIdByRoleId) .map(this::getRepoIdByRoleId)
.filter(Objects::nonNull)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@ -62,13 +68,14 @@ public class AaiRoleMappingService implements RoleMappingService {
return repoIds return repoIds
.stream() .stream()
.map(this::getRoleIdByRepoId) .map(this::getRoleIdByRepoId)
.filter(Objects::nonNull)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@Override @Override
public String convertAuthorityIdToRepoId(String authorityId) { public String convertAuthorityIdToRepoId(String authorityId) {
String repo = ""; String repo = "";
if (authorityId != null) { if (authorityId != null && roleActive(authorityId)) {
repo = authorityId repo = authorityId
.replaceFirst(".*datasource\\.", "") .replaceFirst(".*datasource\\.", "")
.replace("$", ":") .replace("$", ":")
@ -105,4 +112,9 @@ public class AaiRoleMappingService implements RoleMappingService {
String role = convertRepoIdToEncodedAuthorityId(repoId); String role = convertRepoIdToEncodedAuthorityId(repoId);
return new SimpleGrantedAuthority(role); return new SimpleGrantedAuthority(role);
} }
private boolean roleActive(String roleId) {
return (production && !roleId.toLowerCase().startsWith("beta."))
|| (!production && roleId.toLowerCase().startsWith("beta."));
}
} }

View File

@ -18,16 +18,13 @@ public class HttpUtils {
private static final Logger logger = Logger.getLogger(HttpUtils.class); private static final Logger logger = Logger.getLogger(HttpUtils.class);
//TODO: refactor @Value("${aai.registry.url}")
// TODO: To be changed the values private String registryUrl;
// @Value("https://aai.openaire.eu/registry/")
@Value("https://openaire-dev.aai-dev.grnet.gr/registry/")
private String issuer;
@Value("kostis30fylloy") @Value("${aai.registry.authentication.username}")
private String user; private String user;
@Value("fynhWc7F*2y4me4U") @Value("${aai.registry.authentication.password}")
private String password; private String password;
public JsonElement post(String path, JsonObject body) { public JsonElement post(String path, JsonObject body) {
@ -35,7 +32,7 @@ public class HttpUtils {
HttpHeaders headers = createHeaders(user, password); HttpHeaders headers = createHeaders(user, password);
headers.setContentType(MediaType.APPLICATION_JSON); headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>(body.toString(), headers); HttpEntity<String> request = new HttpEntity<>(body.toString(), headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(issuer + path, HttpMethod.POST, request, String.class); ResponseEntity<String> responseEntity = restTemplate.exchange(registryUrl + path, HttpMethod.POST, request, String.class);
return getResponseEntityAsJsonElement(responseEntity); return getResponseEntityAsJsonElement(responseEntity);
} }
@ -44,13 +41,13 @@ public class HttpUtils {
HttpHeaders headers = createHeaders(user, password); HttpHeaders headers = createHeaders(user, password);
headers.setContentType(MediaType.APPLICATION_JSON); headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>(body.toString(), headers); HttpEntity<String> request = new HttpEntity<>(body.toString(), headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(issuer + path, HttpMethod.PUT, request, String.class); ResponseEntity<String> responseEntity = restTemplate.exchange(registryUrl + path, HttpMethod.PUT, request, String.class);
return getResponseEntityAsJsonElement(responseEntity); return getResponseEntityAsJsonElement(responseEntity);
} }
public JsonElement get(String path, Map<String, String> params) { public JsonElement get(String path, Map<String, String> params) {
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
String url = issuer + path + ((params != null) ? createParams(params) : null); String url = registryUrl + path + ((params != null) ? createParams(params) : null);
ResponseEntity<String> responseEntity = restTemplate.exchange ResponseEntity<String> responseEntity = restTemplate.exchange
(url, HttpMethod.GET, new HttpEntity<>(createHeaders(user, password)), String.class); (url, HttpMethod.GET, new HttpEntity<>(createHeaders(user, password)), String.class);
return getResponseEntityAsJsonElement(responseEntity); return getResponseEntityAsJsonElement(responseEntity);
@ -58,7 +55,7 @@ public class HttpUtils {
public JsonElement delete(String path) { public JsonElement delete(String path) {
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
String url = issuer + path; String url = registryUrl + path;
ResponseEntity<String> responseEntity = restTemplate.exchange ResponseEntity<String> responseEntity = restTemplate.exchange
(url, HttpMethod.DELETE, new HttpEntity<>(createHeaders(user, password)), String.class); (url, HttpMethod.DELETE, new HttpEntity<>(createHeaders(user, password)), String.class);
return getResponseEntityAsJsonElement(responseEntity); return getResponseEntityAsJsonElement(responseEntity);