From f4d34dfe180c9d8438b12d73ba9cadd36d4a832c Mon Sep 17 00:00:00 2001 From: Aurimas Liutikas Date: Wed, 15 Jun 2022 17:53:44 -0700 Subject: [PATCH] Validate that configured bucket for the project is valid --- README.md | 4 ++-- RELEASE-NOTES.md | 4 ++++ gcpbuildcache/build.gradle.kts | 2 +- .../gradle/gcpbuildcache/FileSystemStorageService.kt | 4 ++++ .../gradle/gcpbuildcache/GcpBuildCacheService.kt | 4 ++++ .../gcpbuildcache/GcpBuildCacheServiceFactory.kt | 4 +++- .../build/gradle/gcpbuildcache/GcpStorageService.kt | 11 ++++++++++- .../build/gradle/gcpbuildcache/StorageService.kt | 5 +++++ 8 files changed, 33 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 09963a2..f4d3036 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ In your `settings.gradle.kts` file add the following ```kotlin plugins { - id("androidx.build.gradle.gcpbuildcache") version "1.0.0-alpha05" + id("androidx.build.gradle.gcpbuildcache") version "1.0.0-alpha06" } import androidx.build.gradle.gcpbuildcache.GcpBuildCache @@ -36,7 +36,7 @@ If you are using Groovy, then you should do the following: ```groovy plugins { - id("androidx.build.gradle.gcpbuildcache") version "1.0.0-alpha05" + id("androidx.build.gradle.gcpbuildcache") version "1.0.0-alpha06" } import androidx.build.gradle.gcpbuildcache.GcpBuildCache diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 9c458ec..386d5f5 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -2,6 +2,10 @@ ## 1.0.0-alpha05 +- Warn when a user incorrectly configures GCP bucket to be used for the cache. + +## 1.0.0-alpha05 + - Downloads `Blob`s to an intermediate `Buffer` or a `File` depending on the size of the blob. - The underlying `FileHandleInputStream` gets cleaned up automatically after the `InputStream` is closed. - This way we can avoid flakes from the build cache, that is caused by intermittent `StorageException`s diff --git a/gcpbuildcache/build.gradle.kts b/gcpbuildcache/build.gradle.kts index 5a1ae1a..80e8c55 100644 --- a/gcpbuildcache/build.gradle.kts +++ b/gcpbuildcache/build.gradle.kts @@ -54,7 +54,7 @@ gradlePlugin { } group = "androidx.build.gradle.gcpbuildcache" -version = "1.0.0-alpha05" +version = "1.0.0-alpha06" testing { suites { diff --git a/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/FileSystemStorageService.kt b/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/FileSystemStorageService.kt index 5315d7d..0fdac29 100644 --- a/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/FileSystemStorageService.kt +++ b/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/FileSystemStorageService.kt @@ -76,6 +76,10 @@ internal class FileSystemStorageService( return file.delete() } + override fun validateConfiguration() { + // There is nothing to validate + } + override fun close() { location.deleteRecursively() } diff --git a/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpBuildCacheService.kt b/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpBuildCacheService.kt index 7bd7ec5..f6546fd 100644 --- a/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpBuildCacheService.kt +++ b/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpBuildCacheService.kt @@ -70,6 +70,10 @@ internal class GcpBuildCacheService( storageService.store(cacheKey, output.toByteArray()) } + fun validateConfiguration() { + storageService.validateConfiguration() + } + companion object { // Build Cache Key Helpers private val SLASHES = """"/+""".toRegex() diff --git a/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpBuildCacheServiceFactory.kt b/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpBuildCacheServiceFactory.kt index 0fbec31..30e2c44 100644 --- a/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpBuildCacheServiceFactory.kt +++ b/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpBuildCacheServiceFactory.kt @@ -39,12 +39,14 @@ class GcpBuildCacheServiceFactory : BuildCacheServiceFactory { "${buildCache.credentials is ExportedKeyGcpCredentials}" ) - return GcpBuildCacheService( + val service = GcpBuildCacheService( buildCache.projectId, buildCache.bucketName, buildCache.credentials, buildCache.isPush, buildCache.isEnabled ) + service.validateConfiguration() + return service } } diff --git a/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpStorageService.kt b/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpStorageService.kt index c4c9d21..0acb529 100644 --- a/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpStorageService.kt +++ b/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpStorageService.kt @@ -45,7 +45,6 @@ internal class GcpStorageService( logger.info("Not Enabled") return null } - val blobId = BlobId.of(bucketName, cacheKey) logger.info("Loading $cacheKey from ${blobId.name}") return load(storageOptions, blobId, sizeThreshold) @@ -80,6 +79,16 @@ internal class GcpStorageService( return Companion.delete(storageOptions, blobId) } + override fun validateConfiguration() { + if (storageOptions?.service?.get(bucketName, Storage.BucketGetOption.fields()) == null) { + throw Exception(""" + Bucket $bucketName under project $projectId cannot be found or it is not accessible using the provided + credentials. + """.trimIndent() + ) + } + } + override fun close() { // Does nothing } diff --git a/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/StorageService.kt b/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/StorageService.kt index 0858a58..f65058f 100644 --- a/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/StorageService.kt +++ b/gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/StorageService.kt @@ -59,4 +59,9 @@ interface StorageService : Closeable { * @param cacheKey is the unique key that can identify a resource that needs to be removed. */ fun delete(cacheKey: String): Boolean + + /** + * Checks of the current configuration is valid. Throws an exception if the state is bad. + */ + fun validateConfiguration() }