-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Handling broken token issues (#1301)
* fix: Handling bad token issues * add tests
- Loading branch information
1 parent
e97a025
commit ab55fbc
Showing
12 changed files
with
173 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ftl.config | ||
|
||
import com.google.api.client.http.GoogleApiLogger | ||
import com.google.api.client.http.HttpRequestInitializer | ||
import com.google.auth.oauth2.AccessToken | ||
import com.google.auth.oauth2.GoogleCredentials | ||
import com.google.auth.oauth2.ServiceAccountCredentials | ||
import ftl.config.FtlConstants.userHome | ||
import ftl.gc.UserAuth | ||
import ftl.http.HttpTimeoutIncrease | ||
import ftl.run.exception.FlankGeneralError | ||
import java.io.IOException | ||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
import java.util.Date | ||
|
||
val defaultCredentialPath: Path by lazy { | ||
Paths.get(userHome, ".config/gcloud/application_default_credentials.json") | ||
} | ||
|
||
val credential: GoogleCredentials by lazy { | ||
when { | ||
FtlConstants.useMock -> GoogleCredentials.create(AccessToken("mock", Date())) | ||
UserAuth.exists() -> UserAuth.load() | ||
else -> runCatching { | ||
GoogleApiLogger.silenceComputeEngine() | ||
ServiceAccountCredentials.getApplicationDefault() | ||
}.getOrElse { | ||
if (FtlConstants.isWindows) loadGoogleAccountCredentials() | ||
else throw FlankGeneralError("Error: Failed to read service account credential.\n${it.message}") | ||
} | ||
} | ||
} | ||
|
||
private fun loadGoogleAccountCredentials(): GoogleCredentials = try { | ||
GoogleCredentials.fromStream(defaultCredentialPath.toFile().inputStream()) | ||
} catch (e: IOException) { | ||
throw FlankGeneralError("Error: Failed to read service account credential.\n${e.message}") | ||
} | ||
|
||
val httpCredential: HttpRequestInitializer by lazy { | ||
if (FtlConstants.useMock) { | ||
HttpRequestInitializer {} | ||
} else { | ||
// Authenticate with https://github.com/googleapis/google-auth-library-java | ||
// Scope is required. | ||
// https://developers.google.com/identity/protocols/googlescopes | ||
// https://developers.google.com/identity/protocols/application-default-credentials | ||
// https://cloud.google.com/sdk/gcloud/reference/alpha/compute/instances/set-scopes | ||
HttpTimeoutIncrease(credential.createScoped(listOf("https://www.googleapis.com/auth/cloud-platform"))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.