Repository: aquasecurity/starboard
Pull request #77 (2020-07-03 12:47:31)

Added check for empty server lines in docker config

codecov[bot]: # [Codecov](https://codecov.io/gh/aquasecurity/starboard/pull/77?src=pr&el=h1) Report
> Merging [#77](https://codecov.io/gh/aquasecurity/starboard/pull/77?src=pr&el=desc) into [master](https://codecov.io/gh/aquasecurity/starboard/commit/97d9b98f0f8fb8644053fc2e0f36a363d4d278ad&el=desc) will **increase** coverage by `0.14%`.
> The diff coverage is `15.38%`.

[![Impacted file tree graph](https://codecov.io/gh/aquasecurity/starboard/pull/77/graphs/tree.svg?width=650&height=150&src=pr&token=qqTGKAJ0Wb)](https://codecov.io/gh/aquasecurity/starboard/pull/77?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##           master     #77      +/-   ##
=========================================
+ Coverage    9.10%   9.25%   +0.14%     
=========================================
  Files          26      26              
  Lines        1153    1156       +3     
=========================================
+ Hits          105     107       +2     
- Misses       1043    1044       +1     
  Partials        5       5              
```


| [Impacted Files](https://codecov.io/gh/aquasecurity/starboard/pull/77?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/find/vulnerabilities/trivy/scanner.go](https://codecov.io/gh/aquasecurity/starboard/pull/77/diff?src=pr&el=tree#diff-cGtnL2ZpbmQvdnVsbmVyYWJpbGl0aWVzL3RyaXZ5L3NjYW5uZXIuZ28=) | `0.00% <0.00%> (ø)` | |
| [pkg/docker/config.go](https://codecov.io/gh/aquasecurity/starboard/pull/77/diff?src=pr&el=tree#diff-cGtnL2RvY2tlci9jb25maWcuZ28=) | `90.00% <100.00%> (+1.11%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/aquasecurity/starboard/pull/77?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/aquasecurity/starboard/pull/77?src=pr&el=footer). Last update [980af79...dd454a4](https://codecov.io/gh/aquasecurity/starboard/pull/77?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/pkg/docker/config.go b/pkg/docker/config.go
index 44c7e8f..b5b4d04 100644
--- a/pkg/docker/config.go
+++ b/pkg/docker/config.go
@@ -29,13 +29,15 @@ func ReadCredentialsFromBytes(contents []byte) (cfg map[string]ServerCredentials
 func encodeAuth(config map[string]ServerCredentials) (encodedConfig map[string]ServerCredentials, err error) {
 	encodedConfig = make(map[string]ServerCredentials)
 	for server, entry := range config {
+		if (ServerCredentials{}) == entry {
+			continue
+		}
 		var decodedAuth []byte
 		decodedAuth, err = base64.StdEncoding.DecodeString(entry.Auth)
 		if err != nil {
 			return
 		}
 		splitDecodedAuth := strings.Split(string(decodedAuth), ":")
-
 		encodedConfig[server] = ServerCredentials{
 			Auth:     entry.Auth,
 			Username: splitDecodedAuth[0],
@@ -45,3 +47,4 @@ func encodeAuth(config map[string]ServerCredentials) (encodedConfig map[string]S
 	}
 	return
 }
+


Test patch:
diff --git a/pkg/docker/config_test.go b/pkg/docker/config_test.go
index 6d383c2..2c2b841 100644
--- a/pkg/docker/config_test.go
+++ b/pkg/docker/config_test.go
@@ -34,15 +34,15 @@ func TestReadCredentialsFromBytes(t *testing.T) {
 		{
 			name: "Should return server credentials with encoded username and password",
 			givenJSON: `{
-  "auths": {
-    "https://index.docker.io/v1/": {
-      "auth": "ZG9ja2VyOmh1Yg=="
-    },
-    "harbor.domain": {
-      "auth": "YWRtaW46SGFyYm9yMTIzNDU="
-    }
-  }
-}`,
+						"auths": {
+							"https://index.docker.io/v1/": {
+							"auth": "ZG9ja2VyOmh1Yg=="
+							},
+							"harbor.domain": {
+							"auth": "YWRtaW46SGFyYm9yMTIzNDU="
+							}
+						}
+						}`,
 			expectedCredentials: map[string]ServerCredentials{
 				"harbor.domain": {
 					Auth:     "YWRtaW46SGFyYm9yMTIzNDU=",
@@ -56,6 +56,26 @@ func TestReadCredentialsFromBytes(t *testing.T) {
 				},
 			},
 		},
+		{
+			name: "Should skip empty server entries",
+			givenJSON: `{
+						"auths": {
+						"https://index.docker.io/v1/": {
+							
+						},
+						"harbor.domain": {
+							"auth": "YWRtaW46SGFyYm9yMTIzNDU="
+						}
+						}
+					}`,
+			expectedCredentials: map[string]ServerCredentials{
+				"harbor.domain": {
+					Auth:     "YWRtaW46SGFyYm9yMTIzNDU=",
+					Username: "admin",
+					Password: "Harbor12345",
+				},
+			},
+		},
 	}
 
 	for _, tc := range testCases {
@@ -72,3 +92,4 @@ func TestReadCredentialsFromBytes(t *testing.T) {
 
 	}
 }
+