Skip to content

Commit ff194cb

Browse files
authored
Merge pull request #184 from avast/feat/gcp_raw_key_support
feat: GCS backend - Add support for providing credentials file content in the env variable
2 parents 45b744b + 588c75c commit ff194cb

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

gcs/README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,27 @@ compile "com.avast.clients.storage:storage-client-gcs_2.13:x.x.x"
88

99
## Usage
1010

11-
Configuration:
11+
### Configuration:
1212

1313
```hocon
1414
projectId = "my-project-id"
1515
bucketName = "bucket-name"
1616
```
1717

18-
Client init, example for `monix.eval.Task`:
18+
### Authentication
19+
20+
GCS backends supports multiple ways of authentication:
21+
* Providing path to the credentials file in the configuration under `credentialsFile` key
22+
* Using `GOOGLE_APPLICATION_CREDENTIALS_RAW` environment variable with the content of the credentials file
23+
* All native ways of authentication provided by Google Cloud SDK
24+
* Using `GOOGLE_APPLICATION_CREDENTIALS` environment variable
25+
* Reading credential file from default paths (see https://cloud.google.com/docs/authentication/application-default-credentials#personal)
26+
* For all options see https://cloud.google.com/docs/authentication/provide-credentials-adc#how-to
27+
28+
29+
### Client initialization
30+
31+
Example for `monix.eval.Task`:
1932

2033
```scala
2134
import com.avast.clients.storage.gcs.GcsStorageBackend

gcs/src/main/scala/com/avast/clients/storage/gcs/GcsStorageBackend.scala

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import pureconfig.generic.ProductHint
1818
import pureconfig.generic.auto._
1919
import pureconfig.{CamelCase, ConfigFieldMapping}
2020

21-
import java.io.FileInputStream
21+
import java.io.{ByteArrayInputStream, FileInputStream}
22+
import java.nio.charset.StandardCharsets
2223
import java.nio.file.StandardOpenOption
2324
import java.security.{DigestOutputStream, MessageDigest}
2425

@@ -158,10 +159,20 @@ object GcsStorageBackend {
158159
blocker.delay {
159160
Either
160161
.catchNonFatal {
161-
val builder = conf.jsonKeyPath match {
162-
case Some(jsonKeyPath) =>
162+
val credentialsFileContent = conf.credentialsFile
163+
.map { credentialsFilePath =>
164+
new FileInputStream(credentialsFilePath)
165+
}
166+
.orElse {
167+
sys.env.get("GOOGLE_APPLICATION_CREDENTIALS_RAW").map { credentialFileRaw =>
168+
new ByteArrayInputStream(credentialFileRaw.getBytes(StandardCharsets.UTF_8))
169+
}
170+
}
171+
172+
val builder = credentialsFileContent match {
173+
case Some(inputStream) =>
163174
StorageOptions.newBuilder
164-
.setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream(jsonKeyPath)))
175+
.setCredentials(ServiceAccountCredentials.fromStream(inputStream))
165176
case None =>
166177
StorageOptions.getDefaultInstance.toBuilder
167178
}
@@ -206,7 +217,7 @@ object GcsStorageBackend {
206217
}
207218
}
208219

209-
case class GcsBackendConfiguration(projectId: String, bucketName: String, jsonKeyPath: Option[String] = None)
220+
case class GcsBackendConfiguration(projectId: String, bucketName: String, credentialsFile: Option[String] = None)
210221

211222
object GcsBackendConfiguration {
212223
// configure pureconfig:

0 commit comments

Comments
 (0)