Search in sources :

Example 6 with RegistryPartner

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;
    }
}
Also used : RemotePartner(org.jbei.ice.storage.model.RemotePartner) RegistryPartner(org.jbei.ice.lib.dto.web.RegistryPartner) IceRestClient(org.jbei.ice.services.rest.IceRestClient) AccountController(org.jbei.ice.lib.account.AccountController)

Example 7 with RegistryPartner

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);
    }
}
Also used : RemotePartner(org.jbei.ice.storage.model.RemotePartner) RegistryPartner(org.jbei.ice.lib.dto.web.RegistryPartner)

Example 8 with 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;
}
Also used : RemotePartner(org.jbei.ice.storage.model.RemotePartner) RegistryPartner(org.jbei.ice.lib.dto.web.RegistryPartner)

Example 9 with RegistryPartner

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();
}
Also used : PermissionException(org.jbei.ice.lib.access.PermissionException) RemotePartner(org.jbei.ice.storage.model.RemotePartner) RegistryPartner(org.jbei.ice.lib.dto.web.RegistryPartner)

Example 10 with RegistryPartner

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();
}
Also used : RemotePartner(org.jbei.ice.storage.model.RemotePartner) RegistryPartner(org.jbei.ice.lib.dto.web.RegistryPartner)

Aggregations

RegistryPartner (org.jbei.ice.lib.dto.web.RegistryPartner)33 RemotePartner (org.jbei.ice.storage.model.RemotePartner)11 PermissionException (org.jbei.ice.lib.access.PermissionException)6 AccountTransfer (org.jbei.ice.lib.account.AccountTransfer)3 Account (org.jbei.ice.storage.model.Account)3 ArrayList (java.util.ArrayList)2 RemoteUser (org.jbei.ice.lib.dto.web.RemoteUser)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 RemoteAccess (org.jbei.ice.lib.access.RemoteAccess)1 TokenVerification (org.jbei.ice.lib.access.TokenVerification)1 AccountController (org.jbei.ice.lib.account.AccountController)1 FeaturedDNASequence (org.jbei.ice.lib.dto.FeaturedDNASequence)1 AccessPermission (org.jbei.ice.lib.dto.access.AccessPermission)1