Skip to content

Commit

Permalink
feat: add generate docker auth func. (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
kycheng authored Mar 1, 2024
1 parent 3db6315 commit 56b7e5b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions apis/artifacts/v1alpha1/registry_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1

import (
"encoding/base64"
"encoding/json"
"fmt"
"net/url"
Expand Down Expand Up @@ -50,6 +51,12 @@ type DockerAuthItem struct {
Scope string `json:"scope"`
}

// GenerateDockerAuth generate docker auth by input.
func GenerateDockerAuth(username, password []byte) string {
authData := make([]byte, 0, len(username)+len(password)+1)
return base64.StdEncoding.EncodeToString(fmt.Appendf(authData, "%s:%s", username, password))
}

// GetAuthFromDockerConfigJson get docker credential information from docker config json
func GetAuthFromDockerConfigJson(registry string, dockerConfigJsonBytes []byte) (username, password string, err error) {
var dockerConfig DockerConfigJson
Expand Down
23 changes: 23 additions & 0 deletions apis/artifacts/v1alpha1/registry_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1alpha1

import (
"errors"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -100,3 +101,25 @@ var _ = Describe("Test.GetAuthFromDockerConfigJson", func() {
),
)
})

func TestDockerAuthItem_SetStringAuth(t *testing.T) {
tests := map[string]struct {
username string
password string
want string
}{
"generate auth": {
username: "test-user",
password: "test-password",
want: "dGVzdC11c2VyOnRlc3QtcGFzc3dvcmQ=",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := GenerateDockerAuth([]byte(tt.username), []byte(tt.password))
if tt.want != got {
t.Errorf("generate failed")
}
})
}
}

0 comments on commit 56b7e5b

Please sign in to comment.