Skip to content

Commit

Permalink
Merge pull request #16 from tikurahul/main
Browse files Browse the repository at this point in the history
Add the ability to construct a credential from a block
  • Loading branch information
tikurahul authored Apr 18, 2022
2 parents eb40b53 + 0c9850c commit a3aa146
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ sealed interface GcpCredentials
/**
* Use Application Default to authenticate to Google Cloud Platform.
*/
object ApplicationDefaultGcpCredentials: GcpCredentials
object ApplicationDefaultGcpCredentials : GcpCredentials

/**
* Use Service Account to authenticate to Google Cloud Platform.
*
* @param pathToCredentials a file that stores the exported service account credentials.
* @param credentials a block which returns the exported service account credentials payload.
*/
class ExportedKeyGcpCredentials(val pathToCredentials: File): GcpCredentials
class ExportedKeyGcpCredentials(val credentials: () -> String) : GcpCredentials {
/**
* Builds an [ExportedKeyGcpCredentials] from a file containing the exported service account keys.
*/
constructor(file: File) : this({ file.readText() })
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,13 @@ internal class GcpStorageService(
GoogleCredentials.getApplicationDefault().createScoped(scopes)
}
is ExportedKeyGcpCredentials -> {
val path = gcpCredentials.pathToCredentials
if (!path.exists()) throw GradleException("Credentials path $path does not exist")
if (!path.isFile) throw GradleException("Credentials path $path is not a file")
val contents = gcpCredentials.credentials.invoke()
if (contents.isBlank()) throw GradleException("Credentials are empty.")
// Use the underlying transport factory to ensure logging is disabled.
GoogleCredentials.fromStream(path.inputStream(), transportOptions.httpTransportFactory)
.createScoped(scopes)
GoogleCredentials.fromStream(
contents.byteInputStream(charset = Charsets.UTF_8),
transportOptions.httpTransportFactory
).createScoped(scopes)
}
}
}
Expand Down

0 comments on commit a3aa146

Please sign in to comment.