Search in sources :

Example 1 with OpalCredential

use of org.obiba.mica.micaConfig.domain.OpalCredential in project mica2 by obiba.

the class OpalCredentialService method saveOrUpdateOpalCertificateCredential.

public void saveOrUpdateOpalCertificateCredential(String opalUrl) {
    OpalCredential credential = Optional.ofNullable(repository.findOne(opalUrl)).map(c -> {
        c.setAuthType(AuthType.CERTIFICATE);
        c.setUsername(null);
        c.setPassword(null);
        c.setToken(null);
        return c;
    }).orElse(new OpalCredential(opalUrl, AuthType.CERTIFICATE));
    repository.save(credential);
}
Also used : NoSuchOpalCredential(org.obiba.mica.micaConfig.NoSuchOpalCredential) KeyStoreService(org.obiba.mica.dataset.service.KeyStoreService) Validated(org.springframework.validation.annotation.Validated) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) NotNull(javax.validation.constraints.NotNull) Inject(javax.inject.Inject) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) OpalCredentialRepository(org.obiba.mica.micaConfig.repository.OpalCredentialRepository) Service(org.springframework.stereotype.Service) Optional(java.util.Optional) AuthType(org.obiba.mica.micaConfig.AuthType) OpalCredential(org.obiba.mica.micaConfig.domain.OpalCredential) NoSuchOpalCredential(org.obiba.mica.micaConfig.NoSuchOpalCredential) OpalCredential(org.obiba.mica.micaConfig.domain.OpalCredential)

Example 2 with OpalCredential

use of org.obiba.mica.micaConfig.domain.OpalCredential in project mica2 by obiba.

the class OpalCredentialService method createOrUpdateOpalCredential.

public void createOrUpdateOpalCredential(String opalUrl, String token) {
    OpalCredential credential = Optional.ofNullable(repository.findOne(opalUrl)).map(c -> {
        if (c.getAuthType() == AuthType.CERTIFICATE)
            keyStoreService.deleteKeyPair(OpalService.OPAL_KEYSTORE, opalUrl);
        c.setAuthType(AuthType.TOKEN);
        c.setUsername(null);
        c.setPassword(null);
        c.setToken(micaConfigService.encrypt(token));
        return c;
    }).orElse(new OpalCredential(opalUrl, AuthType.TOKEN, micaConfigService.encrypt(token)));
    repository.save(credential);
}
Also used : NoSuchOpalCredential(org.obiba.mica.micaConfig.NoSuchOpalCredential) KeyStoreService(org.obiba.mica.dataset.service.KeyStoreService) Validated(org.springframework.validation.annotation.Validated) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) NotNull(javax.validation.constraints.NotNull) Inject(javax.inject.Inject) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) OpalCredentialRepository(org.obiba.mica.micaConfig.repository.OpalCredentialRepository) Service(org.springframework.stereotype.Service) Optional(java.util.Optional) AuthType(org.obiba.mica.micaConfig.AuthType) OpalCredential(org.obiba.mica.micaConfig.domain.OpalCredential) NoSuchOpalCredential(org.obiba.mica.micaConfig.NoSuchOpalCredential) OpalCredential(org.obiba.mica.micaConfig.domain.OpalCredential)

Example 3 with OpalCredential

use of org.obiba.mica.micaConfig.domain.OpalCredential in project mica2 by obiba.

the class OpalService method getDatasource.

/**
 * Get the datasource from the provided Opal server url.
 *
 * @param opalUrl if null, default Opal server url will be used.
 * @param project
 * @return
 */
public synchronized RestDatasource getDatasource(@Nullable String opalUrl, String project) {
    final String projectUrl = getOpalProjectUrl(opalUrl, project);
    opalUrl = Strings.isNullOrEmpty(opalUrl) ? getDefaultOpal() : opalUrl;
    OpalCredential opalCredential = getOpalCredential(opalUrl);
    if (cachedDatasources.containsKey(projectUrl)) {
        Pair<OpalCredential, RestDatasource> p = cachedDatasources.get(projectUrl);
        if (p.getKey().equals(opalCredential)) {
            log.debug("Using cached rest datasource to " + projectUrl);
            return p.getValue();
        }
        log.debug("Opal credential changed, evicting rest datasource for " + projectUrl);
        // opal credential changed
        cachedDatasources.remove(projectUrl);
    }
    RestDatasource datasource = createRestDatasource(opalCredential, projectUrl, opalUrl, project);
    Initialisables.initialise(datasource);
    cachedDatasources.put(projectUrl, Pair.create(opalCredential, datasource));
    log.debug("Initialized rest datasource for " + projectUrl);
    return datasource;
}
Also used : RestDatasource(org.obiba.opal.rest.client.magma.RestDatasource) OpalCredential(org.obiba.mica.micaConfig.domain.OpalCredential)

Example 4 with OpalCredential

use of org.obiba.mica.micaConfig.domain.OpalCredential in project mica2 by obiba.

the class OpalService method getOpalJavaClient.

private OpalJavaClient getOpalJavaClient(String opalUrl) throws URISyntaxException {
    String alias = opalUrl;
    OpalCredential opalCredential = getOpalCredential(opalUrl);
    if (opalCredential.getAuthType() == AuthType.CERTIFICATE) {
        KeyStoreManager kms = keyStoreService.getKeyStore(OPAL_KEYSTORE);
        if (!kms.aliasExists(alias))
            throw new IllegalStateException("Trying to use opal certificate credential but could not be found in keystore.");
        return new OpalJavaClient(cleanupOpalUrl(opalUrl), kms.getKeyStore(), alias, micaConfigService.getConfig().getSecretKey());
    } else if (opalCredential.getAuthType() == AuthType.TOKEN)
        return new OpalJavaClient(cleanupOpalUrl(opalCredential.getOpalUrl()), opalCredential.getToken());
    return new OpalJavaClient(cleanupOpalUrl(opalCredential.getOpalUrl()), opalCredential.getUsername(), opalCredential.getPassword());
}
Also used : KeyStoreManager(org.obiba.security.KeyStoreManager) OpalCredential(org.obiba.mica.micaConfig.domain.OpalCredential) OpalJavaClient(org.obiba.opal.rest.client.magma.OpalJavaClient)

Example 5 with OpalCredential

use of org.obiba.mica.micaConfig.domain.OpalCredential in project mica2 by obiba.

the class OpalCredentialServiceTests method testDeleteCertificateCredential.

@Test
public void testDeleteCertificateCredential() throws KeyStoreException {
    OpalCredential credential = new OpalCredential("https://opal", AuthType.CERTIFICATE);
    when(opalCredentialRepository.findOne("https://opal")).thenReturn(credential);
    KeyStoreManager keyStore = mock(KeyStoreManager.class);
    doNothing().when(keyStore).deleteKey("https://opal");
    when(keyStoreService.getKeyStore("opal")).thenReturn(keyStore);
    opalCredentialService.deleteOpalCredential("https://opal");
    verify(opalCredentialRepository).delete(any(OpalCredential.class));
}
Also used : KeyStoreManager(org.obiba.security.KeyStoreManager) OpalCredential(org.obiba.mica.micaConfig.domain.OpalCredential) Test(org.junit.Test)

Aggregations

OpalCredential (org.obiba.mica.micaConfig.domain.OpalCredential)8 Test (org.junit.Test)3 NoSuchOpalCredential (org.obiba.mica.micaConfig.NoSuchOpalCredential)3 IOException (java.io.IOException)2 KeyStoreException (java.security.KeyStoreException)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors.toList (java.util.stream.Collectors.toList)2 Inject (javax.inject.Inject)2 NotNull (javax.validation.constraints.NotNull)2 KeyStoreService (org.obiba.mica.dataset.service.KeyStoreService)2 AuthType (org.obiba.mica.micaConfig.AuthType)2 OpalCredentialRepository (org.obiba.mica.micaConfig.repository.OpalCredentialRepository)2 KeyStoreManager (org.obiba.security.KeyStoreManager)2 Service (org.springframework.stereotype.Service)2 Validated (org.springframework.validation.annotation.Validated)2 OpalJavaClient (org.obiba.opal.rest.client.magma.OpalJavaClient)1 RestDatasource (org.obiba.opal.rest.client.magma.RestDatasource)1