Search in sources :

Example 1 with AuthData

use of org.datatransferproject.types.transfer.auth.AuthData in project data-transfer-project by google.

the class JobProcessor method processJob.

/**
 * Process our job, whose metadata is available via {@link JobMetadata}.
 */
void processJob() {
    boolean success = false;
    UUID jobId = JobMetadata.getJobId();
    monitor.debug(() -> format("Begin processing jobId: %s", jobId), EventCode.WORKER_JOB_STARTED);
    Collection<ErrorDetail> errors = null;
    try {
        markJobStarted(jobId);
        hooks.jobStarted(jobId);
        PortabilityJob job = store.findJob(jobId);
        JobAuthorization jobAuthorization = job.jobAuthorization();
        monitor.debug(() -> format("Starting copy job, id: %s, source: %s, destination: %s", jobId, job.exportService(), job.importService()));
        String scheme = jobAuthorization.encryptionScheme();
        AuthDataDecryptService decryptService = getAuthDecryptService(scheme);
        if (decryptService == null) {
            monitor.severe(() -> format("No auth decrypter found for scheme %s while processing job: %s", scheme, jobId));
            return;
        }
        String encrypted = jobAuthorization.encryptedAuthData();
        byte[] encodedPrivateKey = JobMetadata.getPrivateKey();
        AuthDataPair pair = decryptService.decrypt(encrypted, encodedPrivateKey);
        AuthData exportAuthData = objectMapper.readValue(pair.getExportAuthData(), AuthData.class);
        AuthData importAuthData = objectMapper.readValue(pair.getImportAuthData(), AuthData.class);
        String exportInfoStr = job.exportInformation();
        Optional<ExportInformation> exportInfo = Optional.empty();
        if (!Strings.isNullOrEmpty(exportInfoStr)) {
            exportInfo = Optional.of(objectMapper.readValue(exportInfoStr, ExportInformation.class));
        }
        // Copy the data
        dtpInternalMetricRecorder.startedJob(JobMetadata.getDataType(), JobMetadata.getExportService(), JobMetadata.getImportService());
        JobMetadata.getStopWatch().start();
        errors = copier.copy(exportAuthData, importAuthData, jobId, exportInfo);
        final int numErrors = errors.size();
        monitor.debug(() -> format("Finished copy for jobId: %s with %d error(s).", jobId, numErrors));
        success = errors.isEmpty();
    } catch (CopyExceptionWithFailureReason e) {
        String failureReason = e.getFailureReason();
        if (failureReason.contains(FailureReasons.DESTINATION_FULL.toString())) {
            monitor.info(() -> "The remaining storage in the user's account is not enough to perform this operation.", e);
        } else if (failureReason.contains(FailureReasons.INVALID_TOKEN.toString()) || failureReason.contains(FailureReasons.SESSION_INVALIDATED.toString()) || failureReason.contains(FailureReasons.UNCONFIRMED_USER.toString()) || failureReason.contains(FailureReasons.USER_CHECKPOINTED.toString())) {
            monitor.info(() -> "Got token error", e);
        } else {
            monitor.severe(() -> format("Error with failure code '%s' while processing jobId: %s", failureReason, jobId), e, EventCode.WORKER_JOB_ERRORED);
        }
        addFailureReasonToJob(jobId, failureReason);
    } catch (IOException | CopyException | RuntimeException e) {
        monitor.severe(() -> "Error processing jobId: " + jobId, e, EventCode.WORKER_JOB_ERRORED);
    } finally {
        monitor.debug(() -> "Finished processing jobId: " + jobId, EventCode.WORKER_JOB_FINISHED);
        addErrorsAndMarkJobFinished(jobId, success, errors);
        hooks.jobFinished(jobId, success);
        dtpInternalMetricRecorder.finishedJob(JobMetadata.getDataType(), JobMetadata.getExportService(), JobMetadata.getImportService(), success, JobMetadata.getStopWatch().elapsed());
        monitor.flushLogs();
        JobMetadata.reset();
    }
}
Also used : JobAuthorization(org.datatransferproject.spi.cloud.types.JobAuthorization) CopyException(org.datatransferproject.spi.transfer.types.CopyException) AuthData(org.datatransferproject.types.transfer.auth.AuthData) CopyExceptionWithFailureReason(org.datatransferproject.spi.transfer.types.CopyExceptionWithFailureReason) AuthDataDecryptService(org.datatransferproject.spi.transfer.security.AuthDataDecryptService) IOException(java.io.IOException) ErrorDetail(org.datatransferproject.types.transfer.errors.ErrorDetail) PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) ExportInformation(org.datatransferproject.types.common.ExportInformation) UUID(java.util.UUID) AuthDataPair(org.datatransferproject.types.transfer.auth.AuthDataPair)

Example 2 with AuthData

use of org.datatransferproject.types.transfer.auth.AuthData in project data-transfer-project by google.

the class GenerateServiceAuthDataAction method handle.

public ServiceAuthData handle(GenerateServiceAuthData request) {
    try {
        String id = request.getId();
        Preconditions.checkNotNull(id, "transfer job ID required for GenerateServiceAuthDataAction");
        UUID jobId = decodeJobId(id);
        Preconditions.checkNotNull(request.getAuthToken(), "Auth token required for GenerateServiceAuthDataAction, transfer job ID: %s", jobId);
        PortabilityJob job = jobStore.findJob(jobId);
        Preconditions.checkNotNull(job, "existing job not found for transfer job ID: %s", jobId);
        // TODO: Determine service from job or from authUrl path?
        AuthMode authMode = GenerateServiceAuthData.Mode.EXPORT == request.getMode() ? AuthMode.EXPORT : AuthMode.IMPORT;
        String service = (authMode == AuthMode.EXPORT) ? job.exportService() : job.importService();
        AuthDataGenerator generator = registry.getAuthDataGenerator(service, job.transferDataType(), authMode);
        // Obtain the session key for this job
        String encodedSessionKey = job.jobAuthorization().sessionSecretKey();
        SecretKey key = symmetricKeyGenerator.parse(BaseEncoding.base64Url().decode(encodedSessionKey));
        // Retrieve initial auth data, if it existed
        AuthData initialAuthData = null;
        String encryptedInitialAuthData = (authMode == AuthMode.EXPORT) ? job.jobAuthorization().encryptedInitialExportAuthData() : job.jobAuthorization().encryptedInitialImportAuthData();
        if (encryptedInitialAuthData != null) {
            // Retrieve and parse the session key from the job
            // Decrypt and deserialize the object
            String serialized = decrypterFactory.create(key).decrypt(encryptedInitialAuthData);
            initialAuthData = objectMapper.readValue(serialized, AuthData.class);
        }
        // TODO: Use UUID instead of UUID.toString()
        // Generate auth data
        AuthData authData = generator.generateAuthData(request.getCallbackUrl(), request.getAuthToken(), jobId.toString(), initialAuthData, null);
        Preconditions.checkNotNull(authData, "Auth data should not be null");
        monitor.debug(() -> format("Generated auth data in mode '%s' for job: %s", authMode, jobId), jobId, EventCode.API_GENERATED_AUTH_DATA);
        // Serialize the auth data
        String serialized = objectMapper.writeValueAsString(authData);
        return new ServiceAuthData(serialized);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) AuthDataGenerator(org.datatransferproject.spi.api.auth.AuthDataGenerator) SecretKey(javax.crypto.SecretKey) ServiceAuthData(org.datatransferproject.types.client.transfer.ServiceAuthData) GenerateServiceAuthData(org.datatransferproject.types.client.transfer.GenerateServiceAuthData) AuthData(org.datatransferproject.types.transfer.auth.AuthData) ServiceAuthData(org.datatransferproject.types.client.transfer.ServiceAuthData) GenerateServiceAuthData(org.datatransferproject.types.client.transfer.GenerateServiceAuthData) IOException(java.io.IOException) UUID(java.util.UUID) AuthMode(org.datatransferproject.spi.api.auth.AuthServiceProviderRegistry.AuthMode)

Example 3 with AuthData

use of org.datatransferproject.types.transfer.auth.AuthData in project data-transfer-project by google.

the class FlickrPhotosExporterTest method exportAlbumInitial.

@Test
public void exportAlbumInitial() throws FlickrException {
    // set up auth, flickr service
    when(user.getId()).thenReturn("userId");
    when(authInterface.checkToken(any(Token.class))).thenReturn(auth);
    when(flickr.getPhotosetsInterface()).thenReturn(photosetsInterface);
    when(flickr.getPhotosInterface()).thenReturn(photosInterface);
    when(flickr.getAuthInterface()).thenReturn(authInterface);
    // setup photoset
    Photoset photoset = FlickrTestUtils.initializePhotoset("photosetId", "title", "description");
    // setup photoset list (aka album view)
    int page = 1;
    Photosets photosetsList = new Photosets();
    photosetsList.setPage(page);
    photosetsList.setPages(page + 1);
    photosetsList.setPhotosets(Collections.singletonList(photoset));
    when(photosetsInterface.getList(anyString(), anyInt(), anyInt(), anyString())).thenReturn(photosetsList);
    // run test
    FlickrPhotosExporter exporter = new FlickrPhotosExporter(flickr, TransferServiceConfig.getDefaultInstance());
    AuthData authData = new TokenSecretAuthData("token", "secret");
    ExportResult<PhotosContainerResource> result = exporter.export(UUID.randomUUID(), authData, Optional.empty());
    // make sure album and photo information is correct
    assertThat(result.getExportedData().getPhotos()).isEmpty();
    Collection<PhotoAlbum> albums = result.getExportedData().getAlbums();
    assertThat(albums.size()).isEqualTo(1);
    assertThat(albums).containsExactly(new PhotoAlbum("photosetId", "title", "description"));
    // check continuation information
    ContinuationData continuationData = (ContinuationData) result.getContinuationData();
    assertThat(continuationData.getPaginationData()).isInstanceOf(IntPaginationToken.class);
    assertThat(((IntPaginationToken) continuationData.getPaginationData()).getStart()).isEqualTo(page + 1);
    Collection<? extends ContainerResource> subResources = continuationData.getContainerResources();
    assertThat(subResources.size()).isEqualTo(1);
    assertThat(subResources).containsExactly(new IdOnlyContainerResource("photosetId"));
}
Also used : IntPaginationToken(org.datatransferproject.types.common.IntPaginationToken) TokenSecretAuthData(org.datatransferproject.types.transfer.auth.TokenSecretAuthData) AuthData(org.datatransferproject.types.transfer.auth.AuthData) IntPaginationToken(org.datatransferproject.types.common.IntPaginationToken) Token(org.scribe.model.Token) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) TokenSecretAuthData(org.datatransferproject.types.transfer.auth.TokenSecretAuthData) Photoset(com.flickr4java.flickr.photosets.Photoset) Photosets(com.flickr4java.flickr.photosets.Photosets) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) Test(org.junit.Test)

Aggregations

AuthData (org.datatransferproject.types.transfer.auth.AuthData)3 IOException (java.io.IOException)2 UUID (java.util.UUID)2 PortabilityJob (org.datatransferproject.spi.cloud.types.PortabilityJob)2 Photoset (com.flickr4java.flickr.photosets.Photoset)1 Photosets (com.flickr4java.flickr.photosets.Photosets)1 SecretKey (javax.crypto.SecretKey)1 AuthDataGenerator (org.datatransferproject.spi.api.auth.AuthDataGenerator)1 AuthMode (org.datatransferproject.spi.api.auth.AuthServiceProviderRegistry.AuthMode)1 JobAuthorization (org.datatransferproject.spi.cloud.types.JobAuthorization)1 AuthDataDecryptService (org.datatransferproject.spi.transfer.security.AuthDataDecryptService)1 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)1 CopyException (org.datatransferproject.spi.transfer.types.CopyException)1 CopyExceptionWithFailureReason (org.datatransferproject.spi.transfer.types.CopyExceptionWithFailureReason)1 GenerateServiceAuthData (org.datatransferproject.types.client.transfer.GenerateServiceAuthData)1 ServiceAuthData (org.datatransferproject.types.client.transfer.ServiceAuthData)1 ExportInformation (org.datatransferproject.types.common.ExportInformation)1 IntPaginationToken (org.datatransferproject.types.common.IntPaginationToken)1 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)1 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)1