Skip to content

Commit

Permalink
Merge branch 'release/v2.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
kerren committed May 6, 2024
2 parents 937702f + 115df2d commit 16cf82b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.

## [2.5.0](https://github.com/kerren/dockem/compare/v2.4.0...v2.5.0) (2024-05-06)


### Features

* **auth:** Use the regclient code to extract the docker host authentication from ~/.docker/config.json [#17](https://github.com/kerren/dockem/issues/17) ([6d8a237](https://github.com/kerren/dockem/commit/6d8a237f404bac19af9c84c5a6450d8d358a0129))

## [2.4.0](https://github.com/kerren/dockem/compare/v2.3.0...v2.4.0) (2024-05-05)


Expand Down
68 changes: 50 additions & 18 deletions cli/utils/create_docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,77 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"strings"

"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/client"
"github.com/regclient/regclient/config"
)

func CreateDockerClient(username string, password string, registryName string) (*client.Client, image.PushOptions, error) {
// Create a new docker client
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())

if err != nil {
fmt.Print("ERROR: An error occurred when creating the docker client. Please ensure that the docker daemon is running and that you have the correct permissions to access it.\n")
return nil, image.PushOptions{}, err
}

// Print the version of the docker client
fmt.Printf("Docker client version: %s\n", cli.ClientVersion())

if username != "" && password != "" {
// See https://docs.docker.com/engine/api/sdk/examples/#pull-an-image-with-authentication for details
authConfig := registry.AuthConfig{
Username: username,
Password: password,
ServerAddress: registryName,
}
encodedJSON, err := json.Marshal(authConfig)
return CreateClientWithAuthConfig(authConfig)
} else {
hosts, _ := config.DockerLoad()
for _, h := range hosts {
if strings.Contains(h.Name, registryName) || (registryName == "" && h.Name == config.DockerRegistry) {
// At this point, we can configure the auth
authConfig := registry.AuthConfig{
Username: h.User,
Password: h.Pass,
ServerAddress: h.Hostname,
IdentityToken: h.Token,
}
return CreateClientWithAuthConfig(authConfig)
}
}

// At this point, there is no auth and we can return an empty client
// Create a new docker client
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())

if err != nil {
fmt.Print("ERROR: An error occurred when creating the docker client. Please ensure that the docker daemon is running and that you have the correct permissions to access it.\n")
return nil, image.PushOptions{}, err
}

authStr := base64.URLEncoding.EncodeToString(encodedJSON)
// Print the version of the docker client
fmt.Printf("Docker client version: %s\n", cli.ClientVersion())
registryPushOptions := image.PushOptions{
RegistryAuth: authStr,
All: true,
}

return cli, registryPushOptions, nil

}
}

func CreateClientWithAuthConfig(authConfig registry.AuthConfig) (*client.Client, image.PushOptions, error) {
// Create a new docker client
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())

if err != nil {
fmt.Print("ERROR: An error occurred when creating the docker client. Please ensure that the docker daemon is running and that you have the correct permissions to access it.\n")
return nil, image.PushOptions{}, err
}

// Print the version of the docker client
fmt.Printf("Docker client version: %s\n", cli.ClientVersion())
encodedJSON, err := json.Marshal(authConfig)
if err != nil {
return nil, image.PushOptions{}, err
}

authStr := base64.URLEncoding.EncodeToString(encodedJSON)
registryPushOptions := image.PushOptions{
RegistryAuth: authStr,
}

return cli, image.PushOptions{
All: true,
}, nil
return cli, registryPushOptions, nil
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "2.4.0",
"version": "2.5.0",
"license": "MIT"
}

0 comments on commit 16cf82b

Please sign in to comment.