Skip to content

Commit d5d2646

Browse files
fix(auth) improve error when passing a PEM key as ServiceAccountKey (#3878)
* fix(auth) improve error when passing a PEM key as ServiceAccountKey * fix(auth) add changelog message * fix(auth) bump version * fix(auth) increase patch instead of minor version
1 parent bfbd70e commit d5d2646

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

core/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v0.20.1
2+
- **Improvement:** Improve error message when passing a PEM encoded file to as service account key
3+
14
## v0.20.0
25
- **New:** Added new `GetTraceId` function
36

core/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.20.0
1+
v0.20.1

core/auth/auth.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"os"
88
"path/filepath"
9+
"strings"
910

1011
"github.com/stackitcloud/stackit-sdk-go/core/clients"
1112
"github.com/stackitcloud/stackit-sdk-go/core/config"
@@ -172,7 +173,11 @@ func KeyAuth(cfg *config.Configuration) (http.RoundTripper, error) {
172173
var serviceAccountKey = &clients.ServiceAccountKeyResponse{}
173174
err = json.Unmarshal([]byte(cfg.ServiceAccountKey), serviceAccountKey)
174175
if err != nil {
175-
return nil, fmt.Errorf("unmarshalling service account key: %w", err)
176+
var errorSuffix string
177+
if strings.HasPrefix(cfg.ServiceAccountKey, "-----BEGIN") {
178+
errorSuffix = " - it seems like the provided service account key is in PEM format. Please provide it in JSON format."
179+
}
180+
return nil, fmt.Errorf("unmarshalling service account key: %w%s", err, errorSuffix)
176181
}
177182

178183
// Try to get private key from configuration, environment or credentials file

core/auth/auth_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"os"
1111
"reflect"
12+
"strings"
1213
"testing"
1314
"time"
1415

@@ -668,6 +669,19 @@ func TestKeyAuth(t *testing.T) {
668669
}
669670
}
670671

672+
func TestKeyAuthPemInsteadOfJsonKeyErrorHandling(t *testing.T) {
673+
cfg := &config.Configuration{
674+
ServiceAccountKey: "-----BEGIN PRIVATE KEY",
675+
}
676+
_, err := KeyAuth(cfg)
677+
if err == nil {
678+
t.Fatalf("error expected")
679+
}
680+
if !strings.HasSuffix(err.Error(), "Please provide it in JSON format.") {
681+
t.Fatalf("expected to end with JSON format hint: %s", err)
682+
}
683+
}
684+
671685
func TestNoAuth(t *testing.T) {
672686
for _, test := range []struct {
673687
desc string

0 commit comments

Comments
 (0)