Skip to content

Commit a3aa146

Browse files
authored
Merge pull request #16 from tikurahul/main
Add the ability to construct a credential from a block
2 parents eb40b53 + 0c9850c commit a3aa146

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpCredentials.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@ sealed interface GcpCredentials
2828
/**
2929
* Use Application Default to authenticate to Google Cloud Platform.
3030
*/
31-
object ApplicationDefaultGcpCredentials: GcpCredentials
31+
object ApplicationDefaultGcpCredentials : GcpCredentials
3232

3333
/**
3434
* Use Service Account to authenticate to Google Cloud Platform.
3535
*
36-
* @param pathToCredentials a file that stores the exported service account credentials.
36+
* @param credentials a block which returns the exported service account credentials payload.
3737
*/
38-
class ExportedKeyGcpCredentials(val pathToCredentials: File): GcpCredentials
38+
class ExportedKeyGcpCredentials(val credentials: () -> String) : GcpCredentials {
39+
/**
40+
* Builds an [ExportedKeyGcpCredentials] from a file containing the exported service account keys.
41+
*/
42+
constructor(file: File) : this({ file.readText() })
43+
}

gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpStorageService.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,13 @@ internal class GcpStorageService(
155155
GoogleCredentials.getApplicationDefault().createScoped(scopes)
156156
}
157157
is ExportedKeyGcpCredentials -> {
158-
val path = gcpCredentials.pathToCredentials
159-
if (!path.exists()) throw GradleException("Credentials path $path does not exist")
160-
if (!path.isFile) throw GradleException("Credentials path $path is not a file")
158+
val contents = gcpCredentials.credentials.invoke()
159+
if (contents.isBlank()) throw GradleException("Credentials are empty.")
161160
// Use the underlying transport factory to ensure logging is disabled.
162-
GoogleCredentials.fromStream(path.inputStream(), transportOptions.httpTransportFactory)
163-
.createScoped(scopes)
161+
GoogleCredentials.fromStream(
162+
contents.byteInputStream(charset = Charsets.UTF_8),
163+
transportOptions.httpTransportFactory
164+
).createScoped(scopes)
164165
}
165166
}
166167
}

0 commit comments

Comments
 (0)