Skip to content

Commit 0dca1bb

Browse files
committed
Refactored path.
Added service account configuration through settings
1 parent 3704b54 commit 0dca1bb

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

GoogleStorageWagon/src/main/java/com/gkatzioura/maven/cloud/gcs/GoogleStorageRepository.java renamed to GoogleStorageWagon/src/main/java/com/gkatzioura/maven/cloud/gcs/wagon/GoogleStorageRepository.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.gkatzioura.maven.cloud.gcs;
17+
package com.gkatzioura.maven.cloud.gcs.wagon;
1818

1919
import java.io.File;
2020
import java.io.IOException;
2121
import java.io.InputStream;
2222
import java.nio.ByteBuffer;
2323
import java.util.ArrayList;
2424
import java.util.List;
25+
import java.util.Optional;
2526
import java.util.logging.Level;
2627
import java.util.logging.Logger;
2728

@@ -34,34 +35,43 @@
3435
import com.google.cloud.storage.Blob;
3536
import com.google.cloud.storage.BlobInfo;
3637
import com.google.cloud.storage.Storage;
37-
import com.google.cloud.storage.StorageOptions;
3838

3939
public class GoogleStorageRepository {
4040

4141
private final String bucket;
4242
private final String baseDirectory;
43-
private final KeyResolver keyResolver;
43+
private final KeyResolver keyResolver = new KeyResolver();
44+
private final StorageFactory storageFactory = new StorageFactory();
45+
private final Optional<String> keyPath;
46+
4447
private Storage storage;
4548

4649
private static final Logger LOGGER = Logger.getLogger(GoogleStorageRepository.class.getName());
4750

48-
public GoogleStorageRepository(String bucket, String directory) {
49-
this.keyResolver = new KeyResolver();
51+
public GoogleStorageRepository(Optional<String> keyPath,String bucket, String directory) {
52+
this.keyPath = keyPath;
5053
this.bucket = bucket;
5154
this.baseDirectory = directory;
5255
}
5356

5457
public void connect() throws AuthenticationException {
55-
5658
try {
57-
storage = StorageOptions.getDefaultInstance().getService();
59+
storage = createStorage();
5860
storage.list(bucket, Storage.BlobListOption.pageSize(1));
5961
} catch (Exception e) {
6062
LOGGER.log(Level.SEVERE,"Could not establish connection with google cloud",e);
6163
throw new AuthenticationException("Please configure you google cloud account by logging using gcloud and specify a default project");
6264
}
6365
}
6466

67+
private final Storage createStorage() throws IOException {
68+
if(keyPath.isPresent()) {
69+
return storageFactory.createWithKeyFile(keyPath.get());
70+
} else {
71+
return storageFactory.createDefault();
72+
}
73+
}
74+
6575
public void copy(String resourceName, File destination) throws ResourceDoesNotExistException {
6676

6777
final String key = resolveKey(resourceName);

GoogleStorageWagon/src/main/java/com/gkatzioura/maven/cloud/gcs/GoogleStorageWagon.java renamed to GoogleStorageWagon/src/main/java/com/gkatzioura/maven/cloud/gcs/wagon/GoogleStorageWagon.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.gkatzioura.maven.cloud.gcs;
17+
package com.gkatzioura.maven.cloud.gcs.wagon;
1818

1919
import java.io.File;
2020
import java.io.FileNotFoundException;
2121
import java.io.IOException;
2222
import java.io.InputStream;
2323
import java.util.List;
24+
import java.util.Optional;
2425
import java.util.logging.Level;
2526
import java.util.logging.Logger;
2627

@@ -43,6 +44,7 @@
4344
public class GoogleStorageWagon extends AbstractStorageWagon {
4445

4546
private GoogleStorageRepository googleStorageRepository;
47+
private Optional<String> keyPath;
4648

4749
private static final Logger LOGGER = Logger.getLogger(GoogleStorageWagon.class.getName());
4850

@@ -131,7 +133,7 @@ public void connect(Repository repository, AuthenticationInfo authenticationInfo
131133

132134
LOGGER.log(Level.FINER,String.format("Opening connection for bucket %s and directory %s",bucket,directory));
133135

134-
googleStorageRepository = new GoogleStorageRepository(bucket, directory);
136+
googleStorageRepository = new GoogleStorageRepository(keyPath ,bucket, directory);
135137
googleStorageRepository.connect();
136138
sessionListenerContainer.fireSessionLoggedIn();
137139
sessionListenerContainer.fireSessionOpened();
@@ -149,4 +151,11 @@ public void disconnect() throws ConnectionException {
149151
sessionListenerContainer.fireSessionDisconnected();
150152
}
151153

154+
public String getKeyPath() {
155+
return keyPath.get();
156+
}
157+
158+
public void setKeyPath(String keyPath) {
159+
this.keyPath = Optional.of(keyPath);
160+
}
152161
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.gkatzioura.maven.cloud.gcs.wagon;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.util.logging.Level;
7+
import java.util.logging.Logger;
8+
9+
import com.google.auth.oauth2.GoogleCredentials;
10+
import com.google.auth.oauth2.ServiceAccountCredentials;
11+
import com.google.cloud.storage.Storage;
12+
import com.google.cloud.storage.StorageOptions;
13+
14+
class StorageFactory {
15+
16+
private static final Logger LOGGER = Logger.getLogger(StorageFactory.class.getName());
17+
18+
Storage createWithKeyFile(String keyPath) throws IOException {
19+
File credentialsPath = new File(keyPath);
20+
try(FileInputStream serviceAccountStream = new FileInputStream(credentialsPath)) {
21+
GoogleCredentials googleCredentials = ServiceAccountCredentials.fromStream(serviceAccountStream);
22+
return StorageOptions.newBuilder().setCredentials(googleCredentials).build().getService();
23+
} catch (IOException e) {
24+
LOGGER.log(Level.SEVERE, "Could not parse properly service account key file", e);
25+
throw e;
26+
}
27+
}
28+
29+
Storage createDefault() {
30+
return StorageOptions.getDefaultInstance().getService();
31+
}
32+
}

GoogleStorageWagon/src/main/resources/META-INF/plexus/components.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<component>
1919
<role>org.apache.maven.wagon.Wagon</role>
2020
<role-hint>gs</role-hint>
21-
<implementation>com.gkatzioura.maven.cloud.gcs.GoogleStorageWagon</implementation>
21+
<implementation>com.gkatzioura.maven.cloud.gcs.wagon.GoogleStorageWagon</implementation>
2222
<instantiation-strategy>per-lookup</instantiation-strategy>
2323
</component>
2424
</components>

0 commit comments

Comments
 (0)