From 348c761bea08f1c9b94de23452560a58b1462cfc Mon Sep 17 00:00:00 2001 From: Megan Bang Date: Thu, 7 Oct 2021 16:28:47 -0500 Subject: [PATCH 1/3] add better error if credentials are invalid --- internal/backend/remote-state/gcs/backend.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/backend/remote-state/gcs/backend.go b/internal/backend/remote-state/gcs/backend.go index af2a667eb..440600491 100644 --- a/internal/backend/remote-state/gcs/backend.go +++ b/internal/backend/remote-state/gcs/backend.go @@ -141,6 +141,10 @@ func (b *Backend) configure(ctx context.Context) error { return fmt.Errorf("Error loading credentials: %s", err) } + if !strings.HasPrefix(contents, "{") { + return fmt.Errorf("contents of credentials are invalid") + } + credOptions = append(credOptions, option.WithCredentialsJSON([]byte(contents))) } From 81201d69a34973f108e3e624c3023cab27d52508 Mon Sep 17 00:00:00 2001 From: Megan Bang Date: Thu, 7 Oct 2021 16:33:21 -0500 Subject: [PATCH 2/3] check valid json --- internal/backend/remote-state/gcs/backend.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/backend/remote-state/gcs/backend.go b/internal/backend/remote-state/gcs/backend.go index 440600491..bb13137cf 100644 --- a/internal/backend/remote-state/gcs/backend.go +++ b/internal/backend/remote-state/gcs/backend.go @@ -4,6 +4,7 @@ package gcs import ( "context" "encoding/base64" + "encoding/json" "fmt" "os" "strings" @@ -141,7 +142,7 @@ func (b *Backend) configure(ctx context.Context) error { return fmt.Errorf("Error loading credentials: %s", err) } - if !strings.HasPrefix(contents, "{") { + if !json.Valid([]byte(contents)) { return fmt.Errorf("contents of credentials are invalid") } From 7dda3366a672507d8fcd480a22990271d6ee26ab Mon Sep 17 00:00:00 2001 From: megan07 Date: Fri, 8 Oct 2021 10:02:05 -0500 Subject: [PATCH 3/3] Update internal/backend/remote-state/gcs/backend.go Co-authored-by: appilon --- internal/backend/remote-state/gcs/backend.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/backend/remote-state/gcs/backend.go b/internal/backend/remote-state/gcs/backend.go index bb13137cf..1f1577799 100644 --- a/internal/backend/remote-state/gcs/backend.go +++ b/internal/backend/remote-state/gcs/backend.go @@ -143,7 +143,7 @@ func (b *Backend) configure(ctx context.Context) error { } if !json.Valid([]byte(contents)) { - return fmt.Errorf("contents of credentials are invalid") + return fmt.Errorf("contents of credentials are invalid json") } credOptions = append(credOptions, option.WithCredentialsJSON([]byte(contents)))