Skip to content

Commit

Permalink
fix: base64 encoded json service accounts
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jacobstr committed May 29, 2020
1 parent b44e208 commit a10732b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
15 changes: 6 additions & 9 deletions docker/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -103,7 +102,7 @@ func Load(fileName string) (*Config, error) {
}

authenticationToken := string(b)
usernameAndPassword := strings.Split(authenticationToken, ":")
usernameAndPassword := strings.SplitN(authenticationToken, ":", 2)

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

if fileName != DefaultDockerJSON {
errStr := "Invalid auth for Docker registry: %s\nBase64-encoded string is wrong: %s (%s)\n"
return nil, errors.New(
fmt.Sprint(
errStr,
registry,
a.B64Auth,
authenticationToken,
),
return nil, fmt.Errorf(
errStr,
registry,
a.B64Auth,
authenticationToken,
)
}
}
Expand Down
6 changes: 6 additions & 0 deletions docker/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import (
"fmt"
"io/ioutil"
"testing"
)

Expand Down Expand Up @@ -34,9 +36,13 @@ func TestGetRegistryAuth(t *testing.T) {
}

func TestLoad(t *testing.T) {

gcrJSONKey, _ := ioutil.ReadFile("../../fixtures/docker/gcr-serviceaccount.json")

examples := map[string]string{
"registry.company.io": "user1:pass1",
"registry.hub.docker.com": "user2:pass2",
"us.gcr.io": fmt.Sprintf("%s:%s", "_json_key", string(gcrJSONKey)),
}

c, err := Load(configFile)
Expand Down
4 changes: 3 additions & 1 deletion fixtures/docker/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"registry.hub.docker.com": {
"auth": "dXNlcjI6cGFzczI="
},
"registry.credhelper.com": {
"registry.credhelper.com": {},
"us.gcr.io": {
"auth": "X2pzb25fa2V5OnsKICAidHlwZSI6ICJzZXJ2aWNlX2FjY291bnQiLAogICJwcm9qZWN0X2lkIjogImxzdGFncy1leGFtcGxlIiwKICAicHJpdmF0ZV9rZXlfaWQiOiAibHN0YWdzLWV4YW1wbGUta2V5IiwKICAicHJpdmF0ZV9rZXkiOiAiLS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tXG5cbi0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS1cbiIsCiAgImNsaWVudF9lbWFpbCI6ICJleGFtcGxlQGV4YW1wbGUuaWFtLmdzZXJ2aWNlYWNjb3VudC5jb20iLAogICJjbGllbnRfeDUwOV9jZXJ0X3VybCI6ICJodHRwczovL3d3dy5nb29nbGVhcGlzLmNvbS9yb2JvdC92MS9tZXRhZGF0YS94NTA5L3Rlc3QlNDBleGFtcGxlLmlhbS5nc2VydmljZWFjY291bnQuY29tIgp9Cg=="
}
}
}
8 changes: 8 additions & 0 deletions fixtures/docker/gcr-serviceaccount.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "service_account",
"project_id": "lstags-example",
"private_key_id": "lstags-example-key",
"private_key": "-----BEGIN PRIVATE KEY-----\n\n-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/test%40example.iam.gserviceaccount.com"
}

0 comments on commit a10732b

Please sign in to comment.