Skip to content

Commit 8706a12

Browse files
authored
Merge pull request #221 from jacobstr/koobz/fix-gcr-sa
Fix whitespace handling with gcr service accounts in mind.
2 parents b44e208 + e33c4e2 commit 8706a12

23 files changed

+1859
-19
lines changed

docker/config/config.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package config
33
import (
44
"encoding/base64"
55
"encoding/json"
6-
"errors"
76
"fmt"
87
"os"
98
"strings"
109

1110
"github.com/ivanilves/lstags/docker/config/credhelper"
11+
"github.com/moby/moby/api/types"
1212

1313
"github.com/ivanilves/lstags/util/fix"
1414
)
@@ -55,15 +55,19 @@ func (c *Config) GetCredentials(registry string) (string, string, bool) {
5555
}
5656

5757
func getAuthJSONString(username, password string) string {
58-
if username == "_json_key" {
59-
return fmt.Sprintf("%s:%s", username, password)
58+
b, err := json.Marshal(types.AuthConfig{
59+
Username: "_json_key",
60+
Password: password,
61+
})
62+
63+
// Because of the shape of the struct and inputs involved, this should never
64+
// happen. We preserve the non error-propagating API for callers, but want
65+
// some visibility into this that's better than simply swallowing the error.
66+
if err != nil {
67+
panic(err)
6068
}
6169

62-
return fmt.Sprintf(
63-
`{ "username": "%s", "password": "%s" }`,
64-
username,
65-
password,
66-
)
70+
return string(b)
6771
}
6872

6973
// GetRegistryAuth gets per-registry base64 authentication string
@@ -103,7 +107,7 @@ func Load(fileName string) (*Config, error) {
103107
}
104108

105109
authenticationToken := string(b)
106-
usernameAndPassword := strings.Split(authenticationToken, ":")
110+
usernameAndPassword := strings.SplitN(authenticationToken, ":", 2)
107111

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

119123
if fileName != DefaultDockerJSON {
120124
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-
),
125+
return nil, fmt.Errorf(
126+
errStr,
127+
registry,
128+
a.B64Auth,
129+
authenticationToken,
128130
)
129131
}
130132
}

docker/config/config_test.go

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

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

79
var configFile = "../../fixtures/docker/config.json"
810

911
func TestGetRegistryAuth(t *testing.T) {
1012
examples := map[string]string{
11-
"registry.company.io": "eyAidXNlcm5hbWUiOiAidXNlcjEiLCAicGFzc3dvcmQiOiAicGFzczEiIH0=",
12-
"registry.hub.docker.com": "eyAidXNlcm5hbWUiOiAidXNlcjIiLCAicGFzc3dvcmQiOiAicGFzczIiIH0=",
13+
"registry.company.io": "eyJ1c2VybmFtZSI6Il9qc29uX2tleSIsInBhc3N3b3JkIjoicGFzczEifQ==",
14+
"registry.hub.docker.com": "eyJ1c2VybmFtZSI6Il9qc29uX2tleSIsInBhc3N3b3JkIjoicGFzczIifQ==",
1315
"registry.mindundi.org": "",
1416
}
1517

@@ -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": "[email protected]",
7+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/test%40example.iam.gserviceaccount.com"
8+
}

vendor/github.com/moby/moby/api/types/auth.go

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)