Skip to content

Commit

Permalink
refactor: simpler docker secret code (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
abuchanan-airbyte authored Sep 19, 2024
1 parent 00ab459 commit 5da58fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
36 changes: 11 additions & 25 deletions internal/cmd/local/docker/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,14 @@ import (
// }
func Secret(server, user, pass, email string) ([]byte, error) {
// map of the server to the credentials
servers := map[string]credential{
server: newCredential(user, pass, email),
}
auths := map[string]map[string]credential{
"auths": servers,
}

return json.Marshal(auths)
}

type credential struct {
Username string `json:"username"`
Password string `json:"password"`
Email string `json:"email"`
Auth string `json:"auth"`
}

func newCredential(user, pass, email string) credential {
return credential{
Username: user,
Password: pass,
Email: email,
Auth: base64.StdEncoding.EncodeToString([]byte(user + ":" + pass)),
}
}
return json.Marshal(map[string]any{
"auths": map[string]any {
server: map[string]any {
"username": user,
"password": pass,
"email": email,
"auth": base64.StdEncoding.EncodeToString([]byte(user + ":" + pass)),
},
},
})
}
8 changes: 3 additions & 5 deletions internal/cmd/local/docker/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package docker

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func Test_Secret(t *testing.T) {
exp := `{"auths":{"my-registry.example:5000":{"username":"tiger","password":"pass1234","email":"[email protected]","auth":"dGlnZXI6cGFzczEyMzQ="}}}`
exp := `{"auths":{"my-registry.example:5000":{"auth":"dGlnZXI6cGFzczEyMzQ=","email":"[email protected]","password":"pass1234","username":"tiger"}}}`
act, err := Secret("my-registry.example:5000", "tiger", "pass1234", "[email protected]")
if err != nil {
t.Fatal(err)
}
if d := cmp.Diff(exp, string(act)); d != "" {
t.Errorf("Secret mismatch (-want +got):\n%s", d)
if exp != string(act) {
t.Errorf("Secret mismatch:\nwant: %s\ngot: %s", exp, act)
}
}

0 comments on commit 5da58fd

Please sign in to comment.