Skip to content

Commit d84105c

Browse files
committed
Added public repo functionality to google cloud
1 parent 0dca1bb commit d84105c

File tree

8 files changed

+79
-13
lines changed

8 files changed

+79
-13
lines changed

S3StorageWagon/src/main/java/com/gkatzioura/maven/cloud/s3/PublicReadProperty.java renamed to CloudStorageCore/src/main/java/com/gkatzioura/maven/cloud/wagon/PublicReadProperty.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

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

1919
public class PublicReadProperty {
2020

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
By executing this file certain resources on google cloud are going to be created.
2+
3+
A bucket in order to use as a repository
4+
5+
A in order to read, write to the bucket and list the files available
6+
7+
A role in order to attach it to your ec2-instance or ci/cd jobs
8+
9+
A group to attach to the users in order to be able to execute requests

GoogleStorageWagon/deploy/gcs_repository_bucket.tf

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2018 Emmanouil Gkatziouras
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
variable "bucket_name" {
218
}
319

@@ -23,18 +39,18 @@ resource "google_project_iam_custom_role" "cloud_storage_maven_role" {
2339
role_id = "${replace(var.bucket_name,"-","_")}Role"
2440
title = "${var.bucket_name}Role"
2541
description = "Cloud Storage Maven Repository Bucket Role"
26-
permissions = ["storage.objects.create","storage.objects.get","storage.objects.list"]
42+
permissions = ["storage.objects.create","storage.objects.get","storage.objects.list","storage.objects.delete"]
2743
}
2844

2945
resource "google_service_account" "cloud_storage_service_account" {
3046
account_id = "${var.bucket_name}-sa"
3147
display_name = "${var.bucket_name}ServiceAccount"
3248
}
3349

34-
resource "google_service_account_iam_binding" "cloud_storage_maven_service_account_iam_policy" {
35-
service_account_id = "${google_service_account.cloud_storage_service_account.id}"
36-
role = "${google_project_iam_custom_role.cloud_storage_maven_role.id}"
50+
resource "google_storage_bucket_iam_binding" "cloud_storage_bucket_service_account_iam_binding" {
51+
bucket = "${google_storage_bucket.cloud_storage_maven_repo.id}"
3752
members = [
3853
"serviceAccount:${google_service_account.cloud_storage_service_account.email}"
3954
]
40-
}
55+
role = "${google_project_iam_custom_role.cloud_storage_maven_role.id}"
56+
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.InputStream;
2222
import java.nio.ByteBuffer;
2323
import java.util.ArrayList;
24+
import java.util.Collections;
2425
import java.util.List;
2526
import java.util.Optional;
2627
import java.util.logging.Level;
@@ -30,8 +31,10 @@
3031
import org.apache.maven.wagon.authentication.AuthenticationException;
3132

3233
import com.gkatzioura.maven.cloud.resolver.KeyResolver;
34+
import com.gkatzioura.maven.cloud.wagon.PublicReadProperty;
3335
import com.google.api.gax.paging.Page;
3436
import com.google.cloud.WriteChannel;
37+
import com.google.cloud.storage.Acl;
3538
import com.google.cloud.storage.Blob;
3639
import com.google.cloud.storage.BlobInfo;
3740
import com.google.cloud.storage.Storage;
@@ -43,15 +46,17 @@ public class GoogleStorageRepository {
4346
private final KeyResolver keyResolver = new KeyResolver();
4447
private final StorageFactory storageFactory = new StorageFactory();
4548
private final Optional<String> keyPath;
49+
private final PublicReadProperty publicReadProperty;
4650

4751
private Storage storage;
4852

4953
private static final Logger LOGGER = Logger.getLogger(GoogleStorageRepository.class.getName());
5054

51-
public GoogleStorageRepository(Optional<String> keyPath,String bucket, String directory) {
55+
public GoogleStorageRepository(Optional<String> keyPath,String bucket, String directory, PublicReadProperty publicReadProperty) {
5256
this.keyPath = keyPath;
5357
this.bucket = bucket;
5458
this.baseDirectory = directory;
59+
this.publicReadProperty = publicReadProperty;
5560
}
5661

5762
public void connect() throws AuthenticationException {
@@ -108,7 +113,7 @@ public void put(InputStream inputStream,String destination) throws IOException {
108113

109114
LOGGER.log(Level.FINER,String.format("Uploading key %s ",key));
110115

111-
BlobInfo blobInfo = BlobInfo.newBuilder(bucket,key).build();
116+
BlobInfo blobInfo = applyPublicRead(BlobInfo.newBuilder(bucket,key)).build();
112117

113118
try(WriteChannel writeChannel = storage.writer(blobInfo)) {
114119

@@ -121,6 +126,17 @@ public void put(InputStream inputStream,String destination) throws IOException {
121126
}
122127
}
123128

129+
private BlobInfo.Builder applyPublicRead(BlobInfo.Builder builder) {
130+
if(publicReadProperty.get()) {
131+
Acl acl = Acl.newBuilder(Acl.User.ofAllUsers(), Acl.Role.READER).build();
132+
LOGGER.info("Public read was set to true");
133+
return builder.setAcl(Collections.singletonList(acl));
134+
135+
} else {
136+
return builder;
137+
}
138+
}
139+
124140
public List<String> list(String path) {
125141

126142
String key = resolveKey(path);

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@
4040
import com.gkatzioura.maven.cloud.transfer.TransferProgressFileInputStream;
4141
import com.gkatzioura.maven.cloud.transfer.TransferProgressImpl;
4242
import com.gkatzioura.maven.cloud.wagon.AbstractStorageWagon;
43+
import com.gkatzioura.maven.cloud.wagon.PublicReadProperty;
4344

4445
public class GoogleStorageWagon extends AbstractStorageWagon {
4546

4647
private GoogleStorageRepository googleStorageRepository;
4748
private Optional<String> keyPath;
49+
private Boolean publicRepository;
4850

4951
private static final Logger LOGGER = Logger.getLogger(GoogleStorageWagon.class.getName());
5052

@@ -133,7 +135,7 @@ public void connect(Repository repository, AuthenticationInfo authenticationInfo
133135

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

136-
googleStorageRepository = new GoogleStorageRepository(keyPath ,bucket, directory);
138+
googleStorageRepository = new GoogleStorageRepository(keyPath ,bucket, directory, new PublicReadProperty(publicRepository));
137139
googleStorageRepository.connect();
138140
sessionListenerContainer.fireSessionLoggedIn();
139141
sessionListenerContainer.fireSessionOpened();
@@ -158,4 +160,13 @@ public String getKeyPath() {
158160
public void setKeyPath(String keyPath) {
159161
this.keyPath = Optional.of(keyPath);
160162
}
163+
164+
public Boolean getPublicRepository() {
165+
return publicRepository;
166+
}
167+
168+
public void setPublicRepository(Boolean publicRepository) {
169+
this.publicRepository = publicRepository;
170+
}
171+
161172
}

S3StorageWagon/deploy/s3_repository_bucket.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2018 Emmanouil Gkatziouras
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
variable "bucket_name" {
218
}
319

S3StorageWagon/src/main/java/com/gkatzioura/maven/cloud/s3/S3StorageRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.gkatzioura.maven.cloud.transfer.TransferProgress;
4545
import com.gkatzioura.maven.cloud.transfer.TransferProgressFileInputStream;
4646
import com.gkatzioura.maven.cloud.transfer.TransferProgressFileOutputStream;
47+
import com.gkatzioura.maven.cloud.wagon.PublicReadProperty;
4748

4849
public class S3StorageRepository {
4950

S3StorageWagon/src/main/java/com/gkatzioura/maven/cloud/s3/S3StorageWagon.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
package com.gkatzioura.maven.cloud.s3;
1818

1919
import java.io.File;
20-
import java.nio.file.Path;
21-
import java.nio.file.Paths;
22-
import java.util.ArrayList;
2320
import java.util.Collection;
2421
import java.util.HashSet;
2522
import java.util.List;
@@ -46,13 +43,13 @@
4643
import com.gkatzioura.maven.cloud.transfer.TransferProgress;
4744
import com.gkatzioura.maven.cloud.transfer.TransferProgressImpl;
4845
import com.gkatzioura.maven.cloud.wagon.AbstractStorageWagon;
46+
import com.gkatzioura.maven.cloud.wagon.PublicReadProperty;
4947

5048
public class S3StorageWagon extends AbstractStorageWagon {
5149

5250
private S3StorageRepository s3StorageRepository;
5351
private final KeyResolver keyResolver = new KeyResolver();
5452

55-
5653
private String region;
5754
private Boolean publicRepository;
5855

0 commit comments

Comments
 (0)