DImuthuUpe commented on a change in pull request #11: URL: https://github.com/apache/airavata-mft/pull/11#discussion_r417023741
########## File path: transport/gdrive-transport/src/main/java/org/apache/airavata/mft/transport/gdrive/GDriveMetadataCollector.java ########## @@ -0,0 +1,151 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.airavata.mft.transport.gdrive; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.drive.Drive; +import com.google.api.services.drive.DriveScopes; +import com.google.api.services.drive.model.File; +import com.google.api.services.drive.model.FileList; +import org.apache.airavata.mft.core.ResourceMetadata; +import org.apache.airavata.mft.core.api.MetadataCollector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDriveResourceGetRequest; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; + +import java.io.ByteArrayInputStream; +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.Collection; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class GDriveMetadataCollector implements MetadataCollector { + + private String resourceServiceHost; + private int resourceServicePort; + private String secretServiceHost; + private int secretServicePort; + boolean initialized = false; + // private static final List<String> SCOPES = Arrays.asList(DriveScopes.DRIVE, Review comment: Is this comment useful? ########## File path: services/secret-service/server/src/main/java/org/apache/airavata/mft/secret/server/backend/file/FileBasedSecretBackend.java ########## @@ -292,5 +292,45 @@ public boolean deleteDropboxSecret(DropboxSecretDeleteRequest request) throws Ex throw new UnsupportedOperationException("Operation is not supported in backend"); } + @Override + public Optional<GDriveSecret> getGDriveSecret(GDriveSecretGetRequest request) throws Exception { + //throw new UnsupportedOperationException("Operation is not supported in backend"); + JSONParser jsonParser = new JSONParser(); + InputStream inputStream = FileBasedSecretBackend.class.getClassLoader().getResourceAsStream(secretFile); + + try (InputStreamReader reader = new InputStreamReader(inputStream)) { + Object obj = jsonParser.parse(reader); + + JSONArray resourceList = (JSONArray) obj; + + List<GDriveSecret> gDriveSecrets = (List<GDriveSecret>) resourceList.stream() + .filter(resource -> "GDRIVE".equals(((JSONObject) resource).get("type").toString())) + .map(resource -> { + JSONObject r = (JSONObject) resource; + + GDriveSecret gDriveSecret = GDriveSecret.newBuilder() + .setSecretId(r.get("secretId").toString()) + .setCredentialsJson(r.get("credentialsJson").toString()).build(); Review comment: Is google drive actually providing a credential json or did you create your own format of json? ########## File path: transport/gdrive-transport/src/main/java/org/apache/airavata/mft/transport/gdrive/GDriveMetadataCollector.java ########## @@ -0,0 +1,151 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.airavata.mft.transport.gdrive; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.drive.Drive; +import com.google.api.services.drive.DriveScopes; +import com.google.api.services.drive.model.File; +import com.google.api.services.drive.model.FileList; +import org.apache.airavata.mft.core.ResourceMetadata; +import org.apache.airavata.mft.core.api.MetadataCollector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDriveResourceGetRequest; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; + +import java.io.ByteArrayInputStream; +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.Collection; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class GDriveMetadataCollector implements MetadataCollector { + + private String resourceServiceHost; + private int resourceServicePort; + private String secretServiceHost; + private int secretServicePort; + boolean initialized = false; + // private static final List<String> SCOPES = Arrays.asList(DriveScopes.DRIVE, + // "https://www.googleapis.com/auth/drive.install"); + private static final Logger logger = LoggerFactory.getLogger(GDriveMetadataCollector.class); + + + @Override + public void init(String resourceServiceHost, int resourceServicePort, String secretServiceHost, int secretServicePort) { + this.resourceServiceHost = resourceServiceHost; + this.resourceServicePort = resourceServicePort; + this.secretServiceHost = secretServiceHost; + this.secretServicePort = secretServicePort; + this.initialized = true; + } + + + private void checkInitialized() { + if (!initialized) { + throw new IllegalStateException("GDrive Metadata Collector is not initialized"); + } + } + + @Override + public ResourceMetadata getGetResourceMetadata(String resourceId, String credentialToken) throws Exception { + checkInitialized(); + ResourceServiceGrpc.ResourceServiceBlockingStub resourceClient = ResourceServiceClient.buildClient(resourceServiceHost, resourceServicePort); + GDriveResource gdriveResource = resourceClient.getGDriveResource(GDriveResourceGetRequest.newBuilder().setResourceId(resourceId).build()); + + SecretServiceGrpc.SecretServiceBlockingStub secretClient = SecretServiceClient.buildClient(secretServiceHost, secretServicePort); + GDriveSecret gdriveSecret = secretClient.getGDriveSecret(GDriveSecretGetRequest.newBuilder().setSecretId(credentialToken).build()); + + + HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = new JacksonFactory(); + String jsonString = gdriveSecret.getCredentialsJson(); + GoogleCredential credential = GoogleCredential.fromStream(new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8)), transport, jsonFactory); + if (credential.createScopedRequired()) { + Collection<String> scopes = DriveScopes.all(); + //Arrays.asList(DriveScopes.DRIVE,"https://www.googleapis.com/auth/drive"); Review comment: Remove this ########## File path: transport/gdrive-transport/src/main/java/org/apache/airavata/mft/transport/gdrive/GDriveMetadataCollector.java ########## @@ -0,0 +1,151 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.airavata.mft.transport.gdrive; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.drive.Drive; +import com.google.api.services.drive.DriveScopes; +import com.google.api.services.drive.model.File; +import com.google.api.services.drive.model.FileList; +import org.apache.airavata.mft.core.ResourceMetadata; +import org.apache.airavata.mft.core.api.MetadataCollector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDriveResourceGetRequest; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; + +import java.io.ByteArrayInputStream; +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.Collection; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class GDriveMetadataCollector implements MetadataCollector { + + private String resourceServiceHost; + private int resourceServicePort; + private String secretServiceHost; + private int secretServicePort; + boolean initialized = false; + // private static final List<String> SCOPES = Arrays.asList(DriveScopes.DRIVE, + // "https://www.googleapis.com/auth/drive.install"); + private static final Logger logger = LoggerFactory.getLogger(GDriveMetadataCollector.class); + + + @Override + public void init(String resourceServiceHost, int resourceServicePort, String secretServiceHost, int secretServicePort) { + this.resourceServiceHost = resourceServiceHost; + this.resourceServicePort = resourceServicePort; + this.secretServiceHost = secretServiceHost; + this.secretServicePort = secretServicePort; + this.initialized = true; + } + + + private void checkInitialized() { + if (!initialized) { + throw new IllegalStateException("GDrive Metadata Collector is not initialized"); + } + } + + @Override + public ResourceMetadata getGetResourceMetadata(String resourceId, String credentialToken) throws Exception { + checkInitialized(); + ResourceServiceGrpc.ResourceServiceBlockingStub resourceClient = ResourceServiceClient.buildClient(resourceServiceHost, resourceServicePort); + GDriveResource gdriveResource = resourceClient.getGDriveResource(GDriveResourceGetRequest.newBuilder().setResourceId(resourceId).build()); + + SecretServiceGrpc.SecretServiceBlockingStub secretClient = SecretServiceClient.buildClient(secretServiceHost, secretServicePort); + GDriveSecret gdriveSecret = secretClient.getGDriveSecret(GDriveSecretGetRequest.newBuilder().setSecretId(credentialToken).build()); + + + HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = new JacksonFactory(); + String jsonString = gdriveSecret.getCredentialsJson(); + GoogleCredential credential = GoogleCredential.fromStream(new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8)), transport, jsonFactory); + if (credential.createScopedRequired()) { + Collection<String> scopes = DriveScopes.all(); + //Arrays.asList(DriveScopes.DRIVE,"https://www.googleapis.com/auth/drive"); + credential = credential.createScoped(scopes); + + } + + + Drive drive = new Drive.Builder(transport, jsonFactory, credential) + .setApplicationName("My Project").build(); + ResourceMetadata metadata = new ResourceMetadata(); + FileList fileList = drive.files().list().setFields("files(id,name,modifiedTime,md5Checksum,size,mimeType)").execute(); + + for (File f : fileList.getFiles()) { + if (f.getName().equalsIgnoreCase(gdriveResource.getResourcePath())) { + metadata.setMd5sum(f.getMd5Checksum()); + metadata.setUpdateTime(f.getModifiedTime().getValue()); + metadata.setResourceSize(f.getSize().longValue()); + } + } + return metadata; + } + + @Override + public Boolean isAvailable(String resourceId, String credentialToken) throws Exception { + checkInitialized(); + ResourceServiceGrpc.ResourceServiceBlockingStub resourceClient = ResourceServiceClient.buildClient(resourceServiceHost, resourceServicePort); + GDriveResource gdriveResource = resourceClient.getGDriveResource(GDriveResourceGetRequest.newBuilder().setResourceId(resourceId).build()); + + SecretServiceGrpc.SecretServiceBlockingStub secretClient = SecretServiceClient.buildClient(secretServiceHost, secretServicePort); + GDriveSecret gdriveSecret = secretClient.getGDriveSecret(GDriveSecretGetRequest.newBuilder().setSecretId(credentialToken).build()); + logger.info("Inside GDRiveMetadata is available()"); Review comment: If you actually need this line, make this a debug log. We don't need it to be printed in Info level ########## File path: transport/gdrive-transport/src/main/java/org/apache/airavata/mft/transport/gdrive/GDriveMetadataCollector.java ########## @@ -0,0 +1,151 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.airavata.mft.transport.gdrive; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.drive.Drive; +import com.google.api.services.drive.DriveScopes; +import com.google.api.services.drive.model.File; +import com.google.api.services.drive.model.FileList; +import org.apache.airavata.mft.core.ResourceMetadata; +import org.apache.airavata.mft.core.api.MetadataCollector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDriveResourceGetRequest; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; + +import java.io.ByteArrayInputStream; +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.Collection; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class GDriveMetadataCollector implements MetadataCollector { + + private String resourceServiceHost; + private int resourceServicePort; + private String secretServiceHost; + private int secretServicePort; + boolean initialized = false; + // private static final List<String> SCOPES = Arrays.asList(DriveScopes.DRIVE, + // "https://www.googleapis.com/auth/drive.install"); + private static final Logger logger = LoggerFactory.getLogger(GDriveMetadataCollector.class); + + + @Override + public void init(String resourceServiceHost, int resourceServicePort, String secretServiceHost, int secretServicePort) { + this.resourceServiceHost = resourceServiceHost; + this.resourceServicePort = resourceServicePort; + this.secretServiceHost = secretServiceHost; + this.secretServicePort = secretServicePort; + this.initialized = true; + } + + + private void checkInitialized() { + if (!initialized) { + throw new IllegalStateException("GDrive Metadata Collector is not initialized"); + } + } + + @Override + public ResourceMetadata getGetResourceMetadata(String resourceId, String credentialToken) throws Exception { + checkInitialized(); + ResourceServiceGrpc.ResourceServiceBlockingStub resourceClient = ResourceServiceClient.buildClient(resourceServiceHost, resourceServicePort); + GDriveResource gdriveResource = resourceClient.getGDriveResource(GDriveResourceGetRequest.newBuilder().setResourceId(resourceId).build()); + + SecretServiceGrpc.SecretServiceBlockingStub secretClient = SecretServiceClient.buildClient(secretServiceHost, secretServicePort); + GDriveSecret gdriveSecret = secretClient.getGDriveSecret(GDriveSecretGetRequest.newBuilder().setSecretId(credentialToken).build()); + + + HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = new JacksonFactory(); + String jsonString = gdriveSecret.getCredentialsJson(); + GoogleCredential credential = GoogleCredential.fromStream(new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8)), transport, jsonFactory); + if (credential.createScopedRequired()) { + Collection<String> scopes = DriveScopes.all(); + //Arrays.asList(DriveScopes.DRIVE,"https://www.googleapis.com/auth/drive"); + credential = credential.createScoped(scopes); + + } + + + Drive drive = new Drive.Builder(transport, jsonFactory, credential) + .setApplicationName("My Project").build(); Review comment: We can't have constant application names like this. It might not work for others' accounts ########## File path: transport/gdrive-transport/src/main/java/org/apache/airavata/mft/transport/gdrive/GDriveMetadataCollector.java ########## @@ -0,0 +1,151 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.airavata.mft.transport.gdrive; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.drive.Drive; +import com.google.api.services.drive.DriveScopes; +import com.google.api.services.drive.model.File; +import com.google.api.services.drive.model.FileList; +import org.apache.airavata.mft.core.ResourceMetadata; +import org.apache.airavata.mft.core.api.MetadataCollector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDriveResourceGetRequest; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; + +import java.io.ByteArrayInputStream; +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.Collection; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class GDriveMetadataCollector implements MetadataCollector { + + private String resourceServiceHost; + private int resourceServicePort; + private String secretServiceHost; + private int secretServicePort; + boolean initialized = false; + // private static final List<String> SCOPES = Arrays.asList(DriveScopes.DRIVE, + // "https://www.googleapis.com/auth/drive.install"); + private static final Logger logger = LoggerFactory.getLogger(GDriveMetadataCollector.class); + + + @Override + public void init(String resourceServiceHost, int resourceServicePort, String secretServiceHost, int secretServicePort) { + this.resourceServiceHost = resourceServiceHost; + this.resourceServicePort = resourceServicePort; + this.secretServiceHost = secretServiceHost; + this.secretServicePort = secretServicePort; + this.initialized = true; + } + + + private void checkInitialized() { + if (!initialized) { + throw new IllegalStateException("GDrive Metadata Collector is not initialized"); + } + } + + @Override + public ResourceMetadata getGetResourceMetadata(String resourceId, String credentialToken) throws Exception { + checkInitialized(); + ResourceServiceGrpc.ResourceServiceBlockingStub resourceClient = ResourceServiceClient.buildClient(resourceServiceHost, resourceServicePort); + GDriveResource gdriveResource = resourceClient.getGDriveResource(GDriveResourceGetRequest.newBuilder().setResourceId(resourceId).build()); + + SecretServiceGrpc.SecretServiceBlockingStub secretClient = SecretServiceClient.buildClient(secretServiceHost, secretServicePort); + GDriveSecret gdriveSecret = secretClient.getGDriveSecret(GDriveSecretGetRequest.newBuilder().setSecretId(credentialToken).build()); + + + HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = new JacksonFactory(); + String jsonString = gdriveSecret.getCredentialsJson(); + GoogleCredential credential = GoogleCredential.fromStream(new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8)), transport, jsonFactory); + if (credential.createScopedRequired()) { + Collection<String> scopes = DriveScopes.all(); + //Arrays.asList(DriveScopes.DRIVE,"https://www.googleapis.com/auth/drive"); + credential = credential.createScoped(scopes); + + } + + + Drive drive = new Drive.Builder(transport, jsonFactory, credential) + .setApplicationName("My Project").build(); + ResourceMetadata metadata = new ResourceMetadata(); + FileList fileList = drive.files().list().setFields("files(id,name,modifiedTime,md5Checksum,size,mimeType)").execute(); + + for (File f : fileList.getFiles()) { + if (f.getName().equalsIgnoreCase(gdriveResource.getResourcePath())) { + metadata.setMd5sum(f.getMd5Checksum()); + metadata.setUpdateTime(f.getModifiedTime().getValue()); + metadata.setResourceSize(f.getSize().longValue()); + } + } + return metadata; + } + + @Override + public Boolean isAvailable(String resourceId, String credentialToken) throws Exception { + checkInitialized(); + ResourceServiceGrpc.ResourceServiceBlockingStub resourceClient = ResourceServiceClient.buildClient(resourceServiceHost, resourceServicePort); + GDriveResource gdriveResource = resourceClient.getGDriveResource(GDriveResourceGetRequest.newBuilder().setResourceId(resourceId).build()); + + SecretServiceGrpc.SecretServiceBlockingStub secretClient = SecretServiceClient.buildClient(secretServiceHost, secretServicePort); + GDriveSecret gdriveSecret = secretClient.getGDriveSecret(GDriveSecretGetRequest.newBuilder().setSecretId(credentialToken).build()); + logger.info("Inside GDRiveMetadata is available()"); + HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = new JacksonFactory(); + String jsonString = gdriveSecret.getCredentialsJson(); + GoogleCredential credential = GoogleCredential.fromStream(new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8)), transport, jsonFactory); + if (credential.createScopedRequired()) { + Collection<String> scopes = DriveScopes.all(); + //Arrays.asList(DriveScopes.DRIVE,"https://www.googleapis.com/auth/drive"); Review comment: Remove this ########## File path: transport/gdrive-transport/src/main/java/org/apache/airavata/mft/transport/gdrive/GDriveReceiver.java ########## @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.airavata.mft.transport.gdrive; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.drive.Drive; +import com.google.api.services.drive.DriveScopes; +import com.google.api.services.drive.model.File; +import com.google.api.services.drive.model.FileList; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.apache.airavata.mft.core.ConnectorContext; +import org.apache.airavata.mft.core.api.Connector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDriveResourceGetRequest; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Base64; +import java.util.Collection; + +import org.apache.airavata.mft.core.ConnectorContext; +import org.apache.airavata.mft.core.api.Connector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDriveResourceGetRequest; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; + +import java.io.*; + + +public class GDriveReceiver implements Connector { + + private static final Logger logger = LoggerFactory.getLogger(GDriveReceiver.class); + + private GDriveResource gdriveResource; + private Drive drive; + + @Override + public void init(String resourceId, String credentialToken, String resourceServiceHost, int resourceServicePort, String secretServiceHost, int secretServicePort) throws Exception { + ResourceServiceGrpc.ResourceServiceBlockingStub resourceClient = ResourceServiceClient.buildClient(resourceServiceHost, resourceServicePort); + this.gdriveResource = resourceClient.getGDriveResource(GDriveResourceGetRequest.newBuilder().setResourceId(resourceId).build()); + + SecretServiceGrpc.SecretServiceBlockingStub secretClient = SecretServiceClient.buildClient(secretServiceHost, secretServicePort); + GDriveSecret gdriveSecret = secretClient.getGDriveSecret(GDriveSecretGetRequest.newBuilder().setSecretId(credentialToken).build()); + + HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = new JacksonFactory(); + String jsonString = gdriveSecret.getCredentialsJson(); + GoogleCredential credential = GoogleCredential.fromStream(new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8)), transport, jsonFactory); + if (credential.createScopedRequired()) { + Collection<String> scopes = DriveScopes.all(); + credential = credential.createScoped(scopes); + } + drive = new Drive.Builder(transport, jsonFactory, credential) + .setApplicationName("My Project").build(); + } + + @Override + public void destroy() { + } + + @Override + public void startStream(ConnectorContext context) throws Exception { + logger.info("Starting GDrive Receiver stream for transfer {}", context.getTransferId()); + + String id = null; + FileList fileList = drive.files().list().setFields("files(id,name,modifiedTime,md5Checksum,size)").execute(); Review comment: Isn't there an api to directly filter the file rather than getting the file list and locally filtering ########## File path: transport/gdrive-transport/src/main/java/org/apache/airavata/mft/transport/gdrive/GDriveReceiver.java ########## @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.airavata.mft.transport.gdrive; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.drive.Drive; +import com.google.api.services.drive.DriveScopes; +import com.google.api.services.drive.model.File; +import com.google.api.services.drive.model.FileList; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.apache.airavata.mft.core.ConnectorContext; +import org.apache.airavata.mft.core.api.Connector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDriveResourceGetRequest; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Base64; +import java.util.Collection; + +import org.apache.airavata.mft.core.ConnectorContext; +import org.apache.airavata.mft.core.api.Connector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDriveResourceGetRequest; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; + +import java.io.*; + + +public class GDriveReceiver implements Connector { + + private static final Logger logger = LoggerFactory.getLogger(GDriveReceiver.class); + + private GDriveResource gdriveResource; + private Drive drive; + + @Override + public void init(String resourceId, String credentialToken, String resourceServiceHost, int resourceServicePort, String secretServiceHost, int secretServicePort) throws Exception { + ResourceServiceGrpc.ResourceServiceBlockingStub resourceClient = ResourceServiceClient.buildClient(resourceServiceHost, resourceServicePort); + this.gdriveResource = resourceClient.getGDriveResource(GDriveResourceGetRequest.newBuilder().setResourceId(resourceId).build()); + + SecretServiceGrpc.SecretServiceBlockingStub secretClient = SecretServiceClient.buildClient(secretServiceHost, secretServicePort); + GDriveSecret gdriveSecret = secretClient.getGDriveSecret(GDriveSecretGetRequest.newBuilder().setSecretId(credentialToken).build()); + + HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = new JacksonFactory(); + String jsonString = gdriveSecret.getCredentialsJson(); + GoogleCredential credential = GoogleCredential.fromStream(new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8)), transport, jsonFactory); + if (credential.createScopedRequired()) { + Collection<String> scopes = DriveScopes.all(); + credential = credential.createScoped(scopes); + } + drive = new Drive.Builder(transport, jsonFactory, credential) + .setApplicationName("My Project").build(); Review comment: Constant application name issue here as well ########## File path: transport/gdrive-transport/src/main/java/org/apache/airavata/mft/transport/gdrive/GDriveReceiver.java ########## @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.airavata.mft.transport.gdrive; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.drive.Drive; +import com.google.api.services.drive.DriveScopes; +import com.google.api.services.drive.model.File; +import com.google.api.services.drive.model.FileList; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.apache.airavata.mft.core.ConnectorContext; +import org.apache.airavata.mft.core.api.Connector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDriveResourceGetRequest; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Base64; +import java.util.Collection; + +import org.apache.airavata.mft.core.ConnectorContext; +import org.apache.airavata.mft.core.api.Connector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDriveResourceGetRequest; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; + +import java.io.*; + + +public class GDriveReceiver implements Connector { + + private static final Logger logger = LoggerFactory.getLogger(GDriveReceiver.class); + + private GDriveResource gdriveResource; + private Drive drive; + + @Override + public void init(String resourceId, String credentialToken, String resourceServiceHost, int resourceServicePort, String secretServiceHost, int secretServicePort) throws Exception { + ResourceServiceGrpc.ResourceServiceBlockingStub resourceClient = ResourceServiceClient.buildClient(resourceServiceHost, resourceServicePort); + this.gdriveResource = resourceClient.getGDriveResource(GDriveResourceGetRequest.newBuilder().setResourceId(resourceId).build()); + + SecretServiceGrpc.SecretServiceBlockingStub secretClient = SecretServiceClient.buildClient(secretServiceHost, secretServicePort); + GDriveSecret gdriveSecret = secretClient.getGDriveSecret(GDriveSecretGetRequest.newBuilder().setSecretId(credentialToken).build()); + + HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = new JacksonFactory(); + String jsonString = gdriveSecret.getCredentialsJson(); + GoogleCredential credential = GoogleCredential.fromStream(new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8)), transport, jsonFactory); + if (credential.createScopedRequired()) { + Collection<String> scopes = DriveScopes.all(); + credential = credential.createScoped(scopes); + } + drive = new Drive.Builder(transport, jsonFactory, credential) + .setApplicationName("My Project").build(); + } + + @Override + public void destroy() { + } + + @Override + public void startStream(ConnectorContext context) throws Exception { + logger.info("Starting GDrive Receiver stream for transfer {}", context.getTransferId()); + + String id = null; + FileList fileList = drive.files().list().setFields("files(id,name,modifiedTime,md5Checksum,size)").execute(); + for (File f : fileList.getFiles()) { + if (f.getName().equalsIgnoreCase(gdriveResource.getResourcePath())) { + logger.info("File matched in receiver" + f.getName()); Review comment: Add a space after "receiver" ########## File path: transport/gdrive-transport/src/main/java/org/apache/airavata/mft/transport/gdrive/GDriveMetadataCollector.java ########## @@ -0,0 +1,151 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.airavata.mft.transport.gdrive; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.drive.Drive; +import com.google.api.services.drive.DriveScopes; +import com.google.api.services.drive.model.File; +import com.google.api.services.drive.model.FileList; +import org.apache.airavata.mft.core.ResourceMetadata; +import org.apache.airavata.mft.core.api.MetadataCollector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDriveResourceGetRequest; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; + +import java.io.ByteArrayInputStream; +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.Collection; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class GDriveMetadataCollector implements MetadataCollector { + + private String resourceServiceHost; + private int resourceServicePort; + private String secretServiceHost; + private int secretServicePort; + boolean initialized = false; + // private static final List<String> SCOPES = Arrays.asList(DriveScopes.DRIVE, + // "https://www.googleapis.com/auth/drive.install"); + private static final Logger logger = LoggerFactory.getLogger(GDriveMetadataCollector.class); + + + @Override + public void init(String resourceServiceHost, int resourceServicePort, String secretServiceHost, int secretServicePort) { + this.resourceServiceHost = resourceServiceHost; + this.resourceServicePort = resourceServicePort; + this.secretServiceHost = secretServiceHost; + this.secretServicePort = secretServicePort; + this.initialized = true; + } + + + private void checkInitialized() { + if (!initialized) { + throw new IllegalStateException("GDrive Metadata Collector is not initialized"); + } + } + + @Override + public ResourceMetadata getGetResourceMetadata(String resourceId, String credentialToken) throws Exception { + checkInitialized(); + ResourceServiceGrpc.ResourceServiceBlockingStub resourceClient = ResourceServiceClient.buildClient(resourceServiceHost, resourceServicePort); + GDriveResource gdriveResource = resourceClient.getGDriveResource(GDriveResourceGetRequest.newBuilder().setResourceId(resourceId).build()); + + SecretServiceGrpc.SecretServiceBlockingStub secretClient = SecretServiceClient.buildClient(secretServiceHost, secretServicePort); + GDriveSecret gdriveSecret = secretClient.getGDriveSecret(GDriveSecretGetRequest.newBuilder().setSecretId(credentialToken).build()); + + + HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = new JacksonFactory(); + String jsonString = gdriveSecret.getCredentialsJson(); + GoogleCredential credential = GoogleCredential.fromStream(new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8)), transport, jsonFactory); + if (credential.createScopedRequired()) { + Collection<String> scopes = DriveScopes.all(); + //Arrays.asList(DriveScopes.DRIVE,"https://www.googleapis.com/auth/drive"); + credential = credential.createScoped(scopes); + + } + + + Drive drive = new Drive.Builder(transport, jsonFactory, credential) + .setApplicationName("My Project").build(); + ResourceMetadata metadata = new ResourceMetadata(); + FileList fileList = drive.files().list().setFields("files(id,name,modifiedTime,md5Checksum,size,mimeType)").execute(); + + for (File f : fileList.getFiles()) { + if (f.getName().equalsIgnoreCase(gdriveResource.getResourcePath())) { + metadata.setMd5sum(f.getMd5Checksum()); + metadata.setUpdateTime(f.getModifiedTime().getValue()); + metadata.setResourceSize(f.getSize().longValue()); + } + } + return metadata; + } + + @Override + public Boolean isAvailable(String resourceId, String credentialToken) throws Exception { + checkInitialized(); + ResourceServiceGrpc.ResourceServiceBlockingStub resourceClient = ResourceServiceClient.buildClient(resourceServiceHost, resourceServicePort); + GDriveResource gdriveResource = resourceClient.getGDriveResource(GDriveResourceGetRequest.newBuilder().setResourceId(resourceId).build()); + + SecretServiceGrpc.SecretServiceBlockingStub secretClient = SecretServiceClient.buildClient(secretServiceHost, secretServicePort); + GDriveSecret gdriveSecret = secretClient.getGDriveSecret(GDriveSecretGetRequest.newBuilder().setSecretId(credentialToken).build()); + logger.info("Inside GDRiveMetadata is available()"); + HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = new JacksonFactory(); + String jsonString = gdriveSecret.getCredentialsJson(); + GoogleCredential credential = GoogleCredential.fromStream(new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8)), transport, jsonFactory); + if (credential.createScopedRequired()) { + Collection<String> scopes = DriveScopes.all(); + //Arrays.asList(DriveScopes.DRIVE,"https://www.googleapis.com/auth/drive"); + credential = credential.createScoped(scopes); + + } + + Drive drive = new Drive.Builder(transport, jsonFactory, credential) + .setApplicationName("My Project").build(); + logger.info("Before getting resource"); + String id = null; + + FileList fileList = drive.files().list().setFields("files(id,name)").execute(); + for (File f : fileList.getFiles()) { + if (f.getName().equalsIgnoreCase(gdriveResource.getResourcePath())) { Review comment: Is google drive case insensitive? ########## File path: transport/gdrive-transport/src/main/java/org/apache/airavata/mft/transport/gdrive/GDriveSender.java ########## @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.airavata.mft.transport.gdrive; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.http.InputStreamContent; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.drive.Drive; +import com.google.api.services.drive.DriveScopes; +import com.google.api.services.drive.model.File; +import com.google.api.services.drive.model.FileList; +import com.google.api.services.drive.model.Permission; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.apache.airavata.mft.core.ConnectorContext; +import org.apache.airavata.mft.core.api.Connector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDRiveResourceGetRequest; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collection; + + +public class GDriveSender implements Connector { + private static final Logger logger = LoggerFactory.getLogger(GDriveSender.class); + + private GDriveResource gdriveResource; + private Drive drive; + private JsonObject jsonObject; + + + @Override + public void init(String resourceId, String credentialToken, String resourceServiceHost, int resourceServicePort, String secretServiceHost, int secretServicePort) throws Exception { + ResourceServiceGrpc.ResourceServiceBlockingStub resourceClient = ResourceServiceClient.buildClient(resourceServiceHost, resourceServicePort); + this.gdriveResource = resourceClient.getGDriveResource(GDriveResourceGetRequest.newBuilder().setResourceId(resourceId).build()); + + SecretServiceGrpc.SecretServiceBlockingStub secretClient = SecretServiceClient.buildClient(secretServiceHost, secretServicePort); + GDriveSecret gdriveSecret = secretClient.getGDriveSecret(GDriveSecretGetRequest.newBuilder().setSecretId(credentialToken).build()); + + HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = new JacksonFactory(); + String jsonString = gdriveSecret.getCredentialsJson(); + jsonObject = new JsonParser().parse(jsonString).getAsJsonObject(); + GoogleCredential credential = GoogleCredential.fromStream(new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8)), transport, jsonFactory); + + if (credential.createScopedRequired()) { + Collection<String> scopes = DriveScopes.all(); + //Arrays.asList(DriveScopes.DRIVE,"https://www.googleapis.com/auth/drive"); + credential = credential.createScoped(scopes); + + } + + drive = new Drive.Builder(transport, jsonFactory, credential) + .setApplicationName("My Project").build(); + + } + + @Override + public void destroy() { + + } + + @Override + public void startStream(ConnectorContext context) throws Exception { + logger.info("Starting GDrive Sender stream for transfer {}", context.getTransferId()); + logger.info("Content length for transfer {} {}", context.getTransferId(), context.getMetadata().getResourceSize()); + String id = null; + + InputStreamContent contentStream = new InputStreamContent( + "", context.getStreamBuffer().getInputStream()); + + String entityUser = jsonObject.get("client_email").getAsString(); + File fileMetadata = new File(); + fileMetadata.setName(this.gdriveResource.getResourcePath()); + + boolean fileupdated = false; + FileList fileList = drive.files().list().setFields("files(id,name)").execute(); + logger.info("gdriveResource.getResourcePath() " + gdriveResource.getResourcePath()); Review comment: Make this a debug / trace log ########## File path: transport/gdrive-transport/src/main/java/org/apache/airavata/mft/transport/gdrive/GDriveSender.java ########## @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.airavata.mft.transport.gdrive; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.http.InputStreamContent; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.drive.Drive; +import com.google.api.services.drive.DriveScopes; +import com.google.api.services.drive.model.File; +import com.google.api.services.drive.model.FileList; +import com.google.api.services.drive.model.Permission; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.apache.airavata.mft.core.ConnectorContext; +import org.apache.airavata.mft.core.api.Connector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDRiveResourceGetRequest; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collection; + + +public class GDriveSender implements Connector { + private static final Logger logger = LoggerFactory.getLogger(GDriveSender.class); + + private GDriveResource gdriveResource; + private Drive drive; + private JsonObject jsonObject; + + + @Override + public void init(String resourceId, String credentialToken, String resourceServiceHost, int resourceServicePort, String secretServiceHost, int secretServicePort) throws Exception { + ResourceServiceGrpc.ResourceServiceBlockingStub resourceClient = ResourceServiceClient.buildClient(resourceServiceHost, resourceServicePort); + this.gdriveResource = resourceClient.getGDriveResource(GDriveResourceGetRequest.newBuilder().setResourceId(resourceId).build()); + + SecretServiceGrpc.SecretServiceBlockingStub secretClient = SecretServiceClient.buildClient(secretServiceHost, secretServicePort); + GDriveSecret gdriveSecret = secretClient.getGDriveSecret(GDriveSecretGetRequest.newBuilder().setSecretId(credentialToken).build()); + + HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = new JacksonFactory(); + String jsonString = gdriveSecret.getCredentialsJson(); + jsonObject = new JsonParser().parse(jsonString).getAsJsonObject(); + GoogleCredential credential = GoogleCredential.fromStream(new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8)), transport, jsonFactory); + + if (credential.createScopedRequired()) { + Collection<String> scopes = DriveScopes.all(); + //Arrays.asList(DriveScopes.DRIVE,"https://www.googleapis.com/auth/drive"); + credential = credential.createScoped(scopes); + + } + + drive = new Drive.Builder(transport, jsonFactory, credential) + .setApplicationName("My Project").build(); + + } + + @Override + public void destroy() { + + } + + @Override + public void startStream(ConnectorContext context) throws Exception { + logger.info("Starting GDrive Sender stream for transfer {}", context.getTransferId()); + logger.info("Content length for transfer {} {}", context.getTransferId(), context.getMetadata().getResourceSize()); + String id = null; + + InputStreamContent contentStream = new InputStreamContent( + "", context.getStreamBuffer().getInputStream()); + + String entityUser = jsonObject.get("client_email").getAsString(); + File fileMetadata = new File(); + fileMetadata.setName(this.gdriveResource.getResourcePath()); + + boolean fileupdated = false; + FileList fileList = drive.files().list().setFields("files(id,name)").execute(); + logger.info("gdriveResource.getResourcePath() " + gdriveResource.getResourcePath()); + logger.info("Listing files in GDRIVE SENDER " + drive.files().list().setFields("files(id,name)").execute()); + for (File f : fileList.getFiles()) { Review comment: Same concern as above ########## File path: transport/gdrive-transport/src/main/java/org/apache/airavata/mft/transport/gdrive/GDriveSender.java ########## @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.airavata.mft.transport.gdrive; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.http.InputStreamContent; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.drive.Drive; +import com.google.api.services.drive.DriveScopes; +import com.google.api.services.drive.model.File; +import com.google.api.services.drive.model.FileList; +import com.google.api.services.drive.model.Permission; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.apache.airavata.mft.core.ConnectorContext; +import org.apache.airavata.mft.core.api.Connector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDRiveResourceGetRequest; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collection; + + +public class GDriveSender implements Connector { + private static final Logger logger = LoggerFactory.getLogger(GDriveSender.class); + + private GDriveResource gdriveResource; + private Drive drive; + private JsonObject jsonObject; + + + @Override + public void init(String resourceId, String credentialToken, String resourceServiceHost, int resourceServicePort, String secretServiceHost, int secretServicePort) throws Exception { + ResourceServiceGrpc.ResourceServiceBlockingStub resourceClient = ResourceServiceClient.buildClient(resourceServiceHost, resourceServicePort); + this.gdriveResource = resourceClient.getGDriveResource(GDriveResourceGetRequest.newBuilder().setResourceId(resourceId).build()); + + SecretServiceGrpc.SecretServiceBlockingStub secretClient = SecretServiceClient.buildClient(secretServiceHost, secretServicePort); + GDriveSecret gdriveSecret = secretClient.getGDriveSecret(GDriveSecretGetRequest.newBuilder().setSecretId(credentialToken).build()); + + HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = new JacksonFactory(); + String jsonString = gdriveSecret.getCredentialsJson(); + jsonObject = new JsonParser().parse(jsonString).getAsJsonObject(); + GoogleCredential credential = GoogleCredential.fromStream(new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8)), transport, jsonFactory); + + if (credential.createScopedRequired()) { + Collection<String> scopes = DriveScopes.all(); + //Arrays.asList(DriveScopes.DRIVE,"https://www.googleapis.com/auth/drive"); + credential = credential.createScoped(scopes); + + } + + drive = new Drive.Builder(transport, jsonFactory, credential) + .setApplicationName("My Project").build(); + + } + + @Override + public void destroy() { + + } + + @Override + public void startStream(ConnectorContext context) throws Exception { + logger.info("Starting GDrive Sender stream for transfer {}", context.getTransferId()); + logger.info("Content length for transfer {} {}", context.getTransferId(), context.getMetadata().getResourceSize()); + String id = null; + + InputStreamContent contentStream = new InputStreamContent( + "", context.getStreamBuffer().getInputStream()); + + String entityUser = jsonObject.get("client_email").getAsString(); + File fileMetadata = new File(); + fileMetadata.setName(this.gdriveResource.getResourcePath()); + + boolean fileupdated = false; + FileList fileList = drive.files().list().setFields("files(id,name)").execute(); + logger.info("gdriveResource.getResourcePath() " + gdriveResource.getResourcePath()); + logger.info("Listing files in GDRIVE SENDER " + drive.files().list().setFields("files(id,name)").execute()); Review comment: Make log lines consistent. Don't use upper cased words in middle if you don't have a very good reason. And drive.files().list().setFields("files(id,name)").execute() is a very expensive call to do in log lines. Can't you use fileList as above? Further I don't see a value in printing all file list in an info log line because it's not relevant to common monitoring. You can make this line as a debug log. ########## File path: transport/gdrive-transport/src/main/java/org/apache/airavata/mft/transport/gdrive/GDriveReceiver.java ########## @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.airavata.mft.transport.gdrive; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.drive.Drive; +import com.google.api.services.drive.DriveScopes; +import com.google.api.services.drive.model.File; +import com.google.api.services.drive.model.FileList; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.apache.airavata.mft.core.ConnectorContext; +import org.apache.airavata.mft.core.api.Connector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDriveResourceGetRequest; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Base64; +import java.util.Collection; + +import org.apache.airavata.mft.core.ConnectorContext; +import org.apache.airavata.mft.core.api.Connector; +import org.apache.airavata.mft.resource.client.ResourceServiceClient; +import org.apache.airavata.mft.resource.service.ResourceServiceGrpc; +import org.apache.airavata.mft.resource.service.GDriveResource; +import org.apache.airavata.mft.resource.service.GDriveResourceGetRequest; +import org.apache.airavata.mft.secret.client.SecretServiceClient; +import org.apache.airavata.mft.secret.service.GDriveSecret; +import org.apache.airavata.mft.secret.service.GDriveSecretGetRequest; +import org.apache.airavata.mft.secret.service.SecretServiceGrpc; + +import java.io.*; + + +public class GDriveReceiver implements Connector { + + private static final Logger logger = LoggerFactory.getLogger(GDriveReceiver.class); + + private GDriveResource gdriveResource; + private Drive drive; + + @Override + public void init(String resourceId, String credentialToken, String resourceServiceHost, int resourceServicePort, String secretServiceHost, int secretServicePort) throws Exception { + ResourceServiceGrpc.ResourceServiceBlockingStub resourceClient = ResourceServiceClient.buildClient(resourceServiceHost, resourceServicePort); + this.gdriveResource = resourceClient.getGDriveResource(GDriveResourceGetRequest.newBuilder().setResourceId(resourceId).build()); + + SecretServiceGrpc.SecretServiceBlockingStub secretClient = SecretServiceClient.buildClient(secretServiceHost, secretServicePort); + GDriveSecret gdriveSecret = secretClient.getGDriveSecret(GDriveSecretGetRequest.newBuilder().setSecretId(credentialToken).build()); + + HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = new JacksonFactory(); + String jsonString = gdriveSecret.getCredentialsJson(); + GoogleCredential credential = GoogleCredential.fromStream(new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8)), transport, jsonFactory); + if (credential.createScopedRequired()) { + Collection<String> scopes = DriveScopes.all(); + credential = credential.createScoped(scopes); + } + drive = new Drive.Builder(transport, jsonFactory, credential) + .setApplicationName("My Project").build(); + } + + @Override + public void destroy() { + } + + @Override + public void startStream(ConnectorContext context) throws Exception { + logger.info("Starting GDrive Receiver stream for transfer {}", context.getTransferId()); + + String id = null; + FileList fileList = drive.files().list().setFields("files(id,name,modifiedTime,md5Checksum,size)").execute(); + for (File f : fileList.getFiles()) { + if (f.getName().equalsIgnoreCase(gdriveResource.getResourcePath())) { + logger.info("File matched in receiver" + f.getName()); + id = f.getId(); + } + } + + InputStream inputStream = drive.files().get(id).executeMediaAsInputStream(); Review comment: What if the id = null here? You should do a null check before and fail fast ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org