Skip to content

Commit a10732b

Browse files
committed
fix: base64 encoded json service accounts
When encoding json service accounts, the base64 encoded json may contain newlines. These resulted in > 2 elements in the strings.Split. This modification makes the function resilient to differences whitespace within the embedded service account.json. --- drive-by fix to error string generation We're using a formatting string, but not the `f` variant of `Sprint`. --- address linter complaint -> use fmt.Errorf
1 parent b44e208 commit a10732b

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

docker/config/config.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package config
33
import (
44
"encoding/base64"
55
"encoding/json"
6-
"errors"
76
"fmt"
87
"os"
98
"strings"
@@ -103,7 +102,7 @@ func Load(fileName string) (*Config, error) {
103102
}
104103

105104
authenticationToken := string(b)
106-
usernameAndPassword := strings.Split(authenticationToken, ":")
105+
usernameAndPassword := strings.SplitN(authenticationToken, ":", 2)
107106

108107
if len(usernameAndPassword) == 2 {
109108
c.usernames[registry] = usernameAndPassword[0]
@@ -118,13 +117,11 @@ func Load(fileName string) (*Config, error) {
118117

119118
if fileName != DefaultDockerJSON {
120119
errStr := "Invalid auth for Docker registry: %s\nBase64-encoded string is wrong: %s (%s)\n"
121-
return nil, errors.New(
122-
fmt.Sprint(
123-
errStr,
124-
registry,
125-
a.B64Auth,
126-
authenticationToken,
127-
),
120+
return nil, fmt.Errorf(
121+
errStr,
122+
registry,
123+
a.B64Auth,
124+
authenticationToken,
128125
)
129126
}
130127
}

docker/config/config_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package config
22

33
import (
4+
"fmt"
5+
"io/ioutil"
46
"testing"
57
)
68

@@ -34,9 +36,13 @@ func TestGetRegistryAuth(t *testing.T) {
3436
}
3537

3638
func TestLoad(t *testing.T) {
39+
40+
gcrJSONKey, _ := ioutil.ReadFile("../../fixtures/docker/gcr-serviceaccount.json")
41+
3742
examples := map[string]string{
3843
"registry.company.io": "user1:pass1",
3944
"registry.hub.docker.com": "user2:pass2",
45+
"us.gcr.io": fmt.Sprintf("%s:%s", "_json_key", string(gcrJSONKey)),
4046
}
4147

4248
c, err := Load(configFile)

fixtures/docker/config.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"registry.hub.docker.com": {
77
"auth": "dXNlcjI6cGFzczI="
88
},
9-
"registry.credhelper.com": {
9+
"registry.credhelper.com": {},
10+
"us.gcr.io": {
11+
"auth": "X2pzb25fa2V5OnsKICAidHlwZSI6ICJzZXJ2aWNlX2FjY291bnQiLAogICJwcm9qZWN0X2lkIjogImxzdGFncy1leGFtcGxlIiwKICAicHJpdmF0ZV9rZXlfaWQiOiAibHN0YWdzLWV4YW1wbGUta2V5IiwKICAicHJpdmF0ZV9rZXkiOiAiLS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tXG5cbi0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS1cbiIsCiAgImNsaWVudF9lbWFpbCI6ICJleGFtcGxlQGV4YW1wbGUuaWFtLmdzZXJ2aWNlYWNjb3VudC5jb20iLAogICJjbGllbnRfeDUwOV9jZXJ0X3VybCI6ICJodHRwczovL3d3dy5nb29nbGVhcGlzLmNvbS9yb2JvdC92MS9tZXRhZGF0YS94NTA5L3Rlc3QlNDBleGFtcGxlLmlhbS5nc2VydmljZWFjY291bnQuY29tIgp9Cg=="
1012
}
1113
}
1214
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "service_account",
3+
"project_id": "lstags-example",
4+
"private_key_id": "lstags-example-key",
5+
"private_key": "-----BEGIN PRIVATE KEY-----\n\n-----END PRIVATE KEY-----\n",
6+
"client_email": "example@example.iam.gserviceaccount.com",
7+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/test%40example.iam.gserviceaccount.com"
8+
}

0 commit comments

Comments
 (0)