use of org.jbei.ice.lib.dto.web.RegistryPartner in project ice by JBEI.
the class WoRController method updateWebPartner.
public boolean updateWebPartner(String userId, String url, RegistryPartner partner) {
if (!new AccountController().isAdministrator(userId))
return false;
Logger.info(userId + ": updating partner (" + url + ") to " + partner.toString());
RemotePartner existing = dao.getByUrl(url);
if (existing == null)
return false;
if (partner.getStatus() == existing.getPartnerStatus())
return true;
// contact remote with new api key that allows them to contact this instance
String apiKey = Utils.generateToken();
String myURL = Utils.getConfigValue(ConfigurationKey.URI_PREFIX);
String myName = Utils.getConfigValue(ConfigurationKey.PROJECT_NAME);
RegistryPartner thisPartner = new RegistryPartner();
thisPartner.setUrl(myURL);
thisPartner.setName(myName);
// key to use in contacting this instance
thisPartner.setApiKey(apiKey);
IceRestClient client = IceRestClient.getInstance();
try {
client.post(partner.getUrl(), "/rest/web/partner/remote", thisPartner, RegistryPartner.class, null);
existing.setPartnerStatus(partner.getStatus());
existing.setAuthenticationToken(apiKey);
dao.update(existing);
return true;
} catch (Exception e) {
Logger.error(e);
return false;
}
}
use of org.jbei.ice.lib.dto.web.RegistryPartner in project ice by JBEI.
the class WebOfRegistriesTask method execute.
@Override
public void execute() {
if (!UrlValidator.getInstance().isValid("https://" + this.myUrl)) {
Logger.warn("Invalid url (" + this.myUrl + "). Aborting run of web of registries task");
return;
}
final String NODE_MASTER = Utils.getConfigValue(ConfigurationKey.WEB_OF_REGISTRIES_MASTER);
if (NODE_MASTER.equalsIgnoreCase(this.myUrl) || StringUtils.isEmpty(NODE_MASTER)) {
Logger.warn("Aborting contact of node master.");
return;
}
if (!this.enable) {
// delete from the node master
RemotePartner masterPartner = DAOFactory.getRemotePartnerDAO().getByUrl(NODE_MASTER);
if (masterPartner != null)
this.remoteContact.deleteInstanceFromMaster(NODE_MASTER, masterPartner.getApiKey(), this.myUrl);
return;
}
// exchange key information with the master registry
WebPartners webPartners = new WebPartners();
RegistryPartner masterPartner = new RegistryPartner();
masterPartner.setUrl(NODE_MASTER);
masterPartner = webPartners.addNewPartner(this.userId, masterPartner);
if (masterPartner == null) {
Logger.error("Could not connect to master node");
return;
}
// get partners from master (this call requires that this ice instance already be a partner, so
// the requestToJoin call above must succeed)
List<RegistryPartner> partners = this.remoteContact.getPartners(masterPartner.getUrl(), masterPartner.getApiKey());
if (partners == null) {
Logger.error("Could not retrieve list of partners from master node");
return;
}
Logger.info("Received " + partners.size() + " partner(s) from master");
// for potential, check already in partner list and add if not by performing exchange
for (RegistryPartner registryPartner : partners) {
if (registryPartner.getUrl().equalsIgnoreCase(myUrl))
continue;
// perform exchange with partners
webPartners.addNewPartner(userId, registryPartner);
}
}
use of org.jbei.ice.lib.dto.web.RegistryPartner in project ice by JBEI.
the class WebPartners method updateRemoteAPIKey.
/**
* Updates the api token of a remote partner using information sent by that partner
*
* @param url URL of partner making request. This is obtained from the old api key
* @param remotePartner information sent by remote partner
* @return information about this partner including a new api token
*/
public RegistryPartner updateRemoteAPIKey(String url, RegistryPartner remotePartner) {
RemotePartner remotePartnerModel = dao.getByUrl(url);
if (remotePartnerModel == null) {
Logger.error("Could not find a local record of partner with url " + url);
// todo : so create a new one?
return null;
}
Logger.info("Refreshing local api key for " + url);
RegistryPartner thisInstance = getThisInstanceWithNewApiKey();
remotePartnerModel.setUrl(remotePartner.getUrl());
if (!StringUtils.isEmpty(remotePartner.getName()))
remotePartnerModel.setName(remotePartner.getName());
// todo : no need to validate since url is authenticated
remotePartnerModel.setApiKey(remotePartner.getApiKey());
String salt = tokenHash.generateSalt();
remotePartnerModel.setSalt(salt);
String hash = tokenHash.encrypt(thisInstance.getApiKey() + remotePartner.getUrl(), salt);
remotePartnerModel.setAuthenticationToken(hash);
dao.update(remotePartnerModel);
return thisInstance;
}
use of org.jbei.ice.lib.dto.web.RegistryPartner in project ice by JBEI.
the class WebPartners method updateAPIKey.
/**
* Refreshes the API key for the referenced partner
*
* @param userId identifier for user making request. Must have administrative privileges
* @param id unique (local) identifier for remote partner whose API key is being refreshed
* @return null if this instance is not in web of registries
* @throws PermissionException if user making request does not have administrative privileges
* @throws IllegalArgumentException if the partner identifier is invalid (cannot be used to retrieve a valid
* partner)
*/
public RegistryPartner updateAPIKey(String userId, long id) {
if (!isInWebOfRegistries())
return null;
if (!accountController.isAdministrator(userId))
throw new PermissionException(userId + " is not an admin");
RemotePartner partner = dao.get(id);
if (partner == null) {
throw new IllegalArgumentException("Cannot retrieve partner with id " + id);
}
RegistryPartner thisPartner = getThisInstanceWithNewApiKey();
if (thisPartner == null) {
Logger.error("Cannot exchange api token with remote host due to invalid local url");
return null;
}
// contact partner (with new key) to refresh its api key for this partner
RegistryPartner remotePartner = remoteContact.refreshPartnerKey(thisPartner, partner.getUrl(), partner.getApiKey());
if (remotePartner == null) {
// contact failed (keeping existing key)
Logger.error("Remote contact of partner " + partner.getUrl() + " to update api key failed");
return null;
}
// contact succeeded with return of api key, generate new salt
partner.setSalt(tokenHash.generateSalt());
String hash = tokenHash.encrypt(thisPartner.getApiKey() + remotePartner.getUrl(), partner.getSalt());
partner.setAuthenticationToken(hash);
// todo : check api key (validate?)
partner.setApiKey(remotePartner.getApiKey());
partner = dao.update(partner);
return partner.toDataTransferObject();
}
use of org.jbei.ice.lib.dto.web.RegistryPartner in project ice by JBEI.
the class WebPartners method get.
/**
* Retrieve a partner based on partner token and unique identifier
* This is mostly intended to validate the token associated with url
*
* @param token partner token generated by this ICE instance and sent to other instance
* @param url unique identifier for partner instance
* @return found partner
*/
public RegistryPartner get(String token, String url) {
String urlToken = AccessTokens.getUrlToken(url);
if (urlToken == null || token == null || !token.equalsIgnoreCase(urlToken))
return null;
RemotePartner remotePartner = dao.getByUrl(url);
if (remotePartner == null) {
// likely scenario
RegistryPartner partner = new RegistryPartner();
partner.setUrl(url);
return partner;
}
return remotePartner.toDataTransferObject();
}
Aggregations