Skip to content

Commit f649b01

Browse files
authored
Merge pull request #23 from apenella/fix-generate-auth
allow to generate user-pass auth when user and password are not provided
2 parents 995bfd1 + 6844da0 commit f649b01

File tree

5 files changed

+64
-79
lines changed

5 files changed

+64
-79
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## [0.7.3]
4+
5+
### Fixed
6+
- Generate auth config when username and password are empty, instead of returning an error
7+
38
## [0.7.2]
49

510
### Fixed

README.md

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,45 @@ go-docker-builder
33

44
`go-docker-builder` library is a wrapper over docker client SDK that provides a set of packages that help to manage the most common docker use cases such as build or push images.
55

6-
It also manages docker registry autentication, prepares docker build context to be used by docker client SDK and supports docker build context either from local path or git repository.
6+
It also manages docker registry authentication, prepares docker build context to be used by docker client SDK and supports docker build context either from the local path or git repository.
77

88

99
<!-- @import "[TOC]" {cmd="toc" depthFrom=1 depthTo=6 orderedList=false} -->
1010

1111
<!-- code_chunk_output -->
1212

13-
- [Use cases](#use-cases)
14-
- [Build](#build)
15-
- [Context](#context)
16-
- [Path](#path)
17-
- [Git](#git)
18-
- [Context filesystem](#context-filesystem)
19-
- [Push](#push)
20-
- [Copy](#copy)
21-
- [Authentication](#authentication)
22-
- [Docker registry](#docker-registry)
23-
- [Git server](#git-server)
24-
- [Basic auth](#basic-auth)
25-
- [SSH key](#ssh-key)
26-
- [SSH agent](#ssh-agent)
27-
- [Response](#response)
28-
- [Examples](#examples)
29-
- [References](#references)
30-
- [License](#license)
13+
- [go-docker-builder](#go-docker-builder)
14+
- [Use cases](#use-cases)
15+
- [Build](#build)
16+
- [Context](#context)
17+
- [Path](#path)
18+
- [Git](#git)
19+
- [Context filesystem](#context-filesystem)
20+
- [Push](#push)
21+
- [Copy](#copy)
22+
- [Authentication](#authentication)
23+
- [Docker registry](#docker-registry)
24+
- [Git server](#git-server)
25+
- [Basic auth](#basic-auth)
26+
- [SSH key](#ssh-key)
27+
- [SSH agent](#ssh-agent)
28+
- [Response](#response)
29+
- [Examples](#examples)
30+
- [References](#references)
31+
- [License](#license)
3132

3233
<!-- /code_chunk_output -->
3334

3435
## Use cases
35-
`go-docker-builder` library has been written to provide an easy way to interactuate with docker client SDK on the use cases listed below:
36+
`go-docker-builder` library is a wrapper for Docker client SDK that simplifies its usage for the most common use cases. The cover use cases are:
3637

3738
- **Build**: build a docker images
3839
- **Push**: push a docker image to a docker registry
3940
- **Copy**: copy a docker image from one Docker registry to another one
4041

4142
### Build
42-
Package `build` purpose is to build docker images.
43-
To perform a build action, must be created a `DockerBuildCmd` instance.
43+
The `build` package's purpose is to build docker images.
44+
To perform a build action you must create a `DockerBuildCmd` instance.
4445

4546
```go
4647
// DockerBuilderCmd
@@ -66,7 +67,7 @@ type DockerBuildCmd struct {
6667

6768
Below there is a recipe to build docker images using `go-docker-builder`:
6869

69-
1. Create a docker client from docker client SDK
70+
1. Create a docker client from the Docker client SDK
7071
```go
7172
dockerCli, err = client.NewClientWithOpts(client.FromEnv)
7273
if err != nil {
@@ -114,28 +115,28 @@ if err != nil {
114115
}
115116
```
116117

117-
7. Include extra docker image tags, in case are needed
118+
7. Include extra docker image tags, when you need them
118119
```go
119120
dockerBuilder.AddTags(strings.Join([]string{imageName, "tag1"}, ":"))
120121
```
121122

122-
8. Include authorization either for pull, push or both, when is required.
123+
8. Include authorization either for pull, push or both
123124
```go
124125
err = dockerBuilder.AddAuth(username, password, registry)
125126
if err != nil {
126127
return err
127128
}
128129
```
129130

130-
9. Include build arguments, in case are needed
131+
9. Include build arguments, when you need them
131132
```go
132133
err = dockerBuilder.AddBuildArgs("key", "value")
133134
if err != nil {
134135
return err
135136
}
136137
```
137138

138-
10. Start the build
139+
10. Start the Docker image build
139140
```go
140141
err = dockerBuilder.Run(context.TODO())
141142
if err != nil {
@@ -144,12 +145,12 @@ if err != nil {
144145
```
145146

146147
#### Context
147-
Docker build context is a set of files required to build a docker image. `go-docker-builder` library supports two kind of sources to create the Docker build context: `path` and `git`
148+
The Docker build context is a set of files required to create a docker image. `go-docker-builder` library supports two kinds of sources to create the Docker build context: `path` and `git`
148149

149150
##### Path
150-
When files are located on local host, Docker build context must be created as `path`, and it is only required to define the local folder where files are located.
151+
When files are located on your localhost, the Docker build context must be created as `path`, and it is only required to define the local folder where files are located.
151152

152-
A `PathBuildContext` instance represents a Docker build context as path.
153+
A `PathBuildContext` instance represents a Docker build context as the path.
153154
```go
154155
// PathBuildContext creates a build context from path
155156
type PathBuildContext struct {
@@ -159,7 +160,7 @@ type PathBuildContext struct {
159160
```
160161

161162
##### Git
162-
When files are located on a git repository, Docker build context must be created as `git`.
163+
When files are located on a git repository, the Docker build context must be created as `git`.
163164

164165
A `GitBuildContext` instance represents a Docker build context as git.
165166
```go
@@ -176,18 +177,18 @@ type GitBuildContext struct {
176177
}
177178
```
178179

179-
To define a `git` Docker build context, the only required attribute is `Repository`, although it accepts other configurations such as the `Reference` name (branch, commit or tag), `Auth` which is used to be authenticated over git server or `Path` which let you to define as Docker build context base a subfolder inside the repository.
180+
To define a `git` Docker build context, the only required attribute is `Repository`, although it accepts other configurations such as the `Reference` name (branch, commit or tag), `Auth` which is used to be authenticated over the git server or `Path` which let you define as Docker build context base a subfolder inside the repository.
180181

181182
`go-docker-builder` uses [go-git](https://github.com/go-git/go-git) library to manage `git` Docker build context.
182183

183184
#### Context filesystem
184185
Context filesystem has been created as an intermediate filesystem between the source files and Docker build context.
185186

186-
Context filesystem, is build on top of [afero](https://github.com/spf13/afero) filesystem. It supports to **tar** the entire filesystem and also to **join** multiple filesystems.
187+
Context filesystem is built on top of an [afero](https://github.com/spf13/afero) filesystem. It supports **taring** the entire filesystem and also **joining** multiple filesystems.
187188

188189
### Push
189190
Package `push` purpose is to push Docker images to a Docker registry.
190-
To perform a push action, must be created a `DockerPushBuildCmd` instance.
191+
To perform a push action you must create a `DockerPushBuildCmd` instance.
191192

192193
```go
193194
// DockerPushCmd is used to push images to docker registry
@@ -211,7 +212,7 @@ type DockerPushCmd struct {
211212

212213
Below there is a recipe to build docker images using `go-docker-builder`:
213214

214-
1. Create a docker client from docker client SDK
215+
1. Create a docker client from Docker client SDK
215216
```go
216217
dockerCli, err = client.NewClientWithOpts(client.FromEnv)
217218
if err != nil {
@@ -240,7 +241,7 @@ pass := "myregistrypass"
240241
dockerPush.AddAuth(user, pass)
241242
```
242243

243-
5. Push the image to Docker registry
244+
5. Push the image to the Docker registry
244245
```go
245246
err = dockerPush.Run(context.TODO())
246247
if err != nil {
@@ -250,8 +251,8 @@ if err != nil {
250251

251252

252253
### Copy
253-
Package `copy` can be understand such a `push` use case variation. Its purpose is to push images either from local host or from a Docker registry to another Docker registry. It can be also used to copy images from one Docker registry namespace to another namespace.
254-
To perform a copy action, must be created a `DockerImageCopyCmd` instance.
254+
The `copy` package can be understood as such a `push` use case variation. Its purpose is to push images either from your localhost or from a Docker registry to another Docker registry. It can be also used to copy images from one Docker registry namespace to another namespace.
255+
To perform a copy action you must create a `DockerImageCopyCmd` instance.
255256

256257
```go
257258
// DockerCopyImageCmd is used to copy images to docker registry. Copy image is understood as tag an existing image and push it to a docker registry
@@ -279,27 +280,27 @@ type DockerImageCopyCmd struct {
279280
}
280281
```
281282

282-
Some cases where copy can be used are:
283-
- Copy one image from [dockerhub](https://hub.docker.com/) to a private Docker registry
284-
- When Docker images used on your staging environments needs to be promoted somewhere else before use them on production environment.
283+
You can use the `copy` package when:
284+
- You need to copy one image from [dockerhub](https://hub.docker.com/) to a private Docker registry
285+
- The Docker images you use on your staging environments need to be promoted somewhere else before using them in the production environment.
285286

286287
## Authentication
287-
`go-docker-builder` library contains a bunch of packages to manage authentication either to docker registry or git server.
288+
`go-docker-builder` library contains a bunch of packages to manage authentication either to the Docker registry or the git server.
288289

289290
### Docker registry
290-
Package `github.com/apenella/go-docker-builder/pkg/auth/docker` provides a set of functions to create the authentication items required by Docker registry. Docker registy may require authentication either for `push` or `pull` operations.
291+
Package `github.com/apenella/go-docker-builder/pkg/auth/docker` provides a set of functions to create the authentication items required by the Docker registry. The Docker registry may require authentication either for `push` or `pull` operations.
291292

292-
Packages [build](#build), [push](#push) or [copy](#copy) already uses that package on its authentication methods, and is not necessary to use it directly.
293+
Packages [build](#build), [push](#push) or [copy](#copy) already use that package on its authentication methods, and is not necessary to use it directly.
293294

294295
### Git server
295-
Git server authentication is needed when Docker build context is located on a git repository and the git server requires it authorize you for cloning any repository.
296+
Git server authentication is needed when the Docker build context is located on a git repository and the git server requires it to authorize you for cloning any repository.
296297

297298
`go-docker-builder` supports several git authentication methods such as `basic auth`, `ssh-key` or `ssh-agent`.
298299

299-
All this authentication methods generates an `AuthMethod` (github.com/go-git/go-git/v5/plumbing/transport).
300+
All these authorization methods generate an `AuthMethod` (github.com/go-git/go-git/v5/plumbing/transport).
300301

301302
#### Basic auth
302-
`Basic auth` requires a `username` and `password`. It have to be used when comunication is done through `http/s`.
303+
`Basic auth` requires a `username` and `password`. It must be used when communication is done through `http/s`.
303304

304305
```go
305306
type BasicAuth struct {
@@ -309,7 +310,7 @@ type BasicAuth struct {
309310
```
310311

311312
#### SSH key
312-
You can use an `ssh key` when comunication to git server is done over ssh. It requires your privete key location (`PkFile`). In case your key is being protected by password, you have to set it on `PkPassword` attribute. Finally, when git user is not `git`, you can define it on `GitSSHUser` attribute.
313+
You can use an `ssh key` when want to connect to the git server over SSH. It requires your private key location (`PkFile`). In case your key is being protected by a password, you have to set it on `PkPassword` attribute. Finally, when the git user is not `git`, you can define it on `GitSSHUser` attribute.
313314
```go
314315
type KeyAuth struct {
315316
GitSSHUser string
@@ -319,7 +320,7 @@ type KeyAuth struct {
319320
```
320321

321322
#### SSH agent
322-
To authenticate to git server, `ssh-agent` method uses the ssh agent running on you host. When git user is not `git`, you can define it on `GitSSHUser` attribute.
323+
To authenticate to the git server, `ssh-agent` method uses the SSH agent running on your host. When your SSH user is not `git`, you can define it on `GitSSHUser` attribute.
323324
```go
324325
type SSHAgentAuth struct {
325326
GitSSHUser string
@@ -328,10 +329,10 @@ type SSHAgentAuth struct {
328329

329330
## Response
330331
To control how Docker output is sent to the user, `go-docker-build` provides `response` package.
331-
By default, [DockerBuildCmd](#build), [DockerPushCmd](#push) and [DockerCopyCmd](#copy) instances use a basic configuration of `response` that prints Docker output to `os.Stdout`. But you could customize docker client SDK response, defining your own `response` instance, and pass it to them.
332+
By default, [DockerBuildCmd](#build), [DockerPushCmd](#push) and [DockerCopyCmd](#copy) instances use a basic configuration of `response` that prints Docker output to `os.Stdout`. But you could customize the Docker client SDK response, define your `response` instance, and pass it to them.
332333

333-
`response` receives `ImageBuildResponse` or `ImagePushResponse` items, unmarshals into an struct and finally prepares a user-frendly output.
334-
To customize the Docker output coming from response items, it could receive a list of transfromer functions. That function must complain the type `TransformerFunc` defined on `github.com/apenella/go-common-utils/transformer/string`.
334+
`response` receives `ImageBuildResponse` or `ImagePushResponse` items, unmarshals into a struct and finally prepares a user-friendly output.
335+
To customize the Docker output coming from response items, it could receive a list of transformer functions. That function must complain the type `TransformerFunc` defined on `github.com/apenella/go-common-utils/transformer/string`.
335336

336337
```go
337338
// TransformerFunc is used to enrich or update messages before to be printed out
@@ -359,11 +360,10 @@ dockerBuilder := &build.DockerBuildCmd{
359360
```
360361

361362
## Examples
362-
On folder [examples](https://github.com/apenella/go-docker-builder/tree/master/examples), you could find some `go-docker-build` examples. Among those examples you could find how to build images using distinct Docker build context, how to authenticate to Docker registry or git server, etc.
363+
On folder [examples](https://github.com/apenella/go-docker-builder/tree/master/examples), you could find some `go-docker-build` examples. Among those examples, you could find how to build images using distinct Docker build context, how to authenticate to Docker registry or git server, etc.
363364

364-
To run any example, the repository is provided with some resources that let you to start an ephemeral environment where examples can run. Each environments run on `docker compose` and starts a Docker registry, a git server and a client container where example runs. That environments are also used to run functional test.
365-
366-
Each example is also provide by a `Makefile` which helps you to start the examples or tests.
365+
To run any example, the repository is provided with some resources that let you start an ephemeral environment where examples can run. Each environment runs on `docker compose` and starts a Docker registry, a git server and a client container where the example runs. Those environments are also used to run the functional test.
366+
Each example is also provided by a `Makefile` which helps you to start the examples or tests.
367367

368368
```shell
369369
❯ make help
@@ -383,7 +383,7 @@ Each example is also provide by a `Makefile` which helps you to start the exampl
383383

384384
## References
385385
- Docker engine API specifications for building an image: https://docs.docker.com/engine/api/v1.39/#operation/ImageBuild
386-
- Taring files strategy was inspired by: https://medium.com/@skdomino/taring-untaring-files-in-go-6b07cf56bc07
386+
- The used taring files strategy is inspired by: https://medium.com/@skdomino/taring-untaring-files-in-go-6b07cf56bc07
387387

388388
## License
389389
`go-docker-builder` is available under [MIT](https://github.com/apenella/go-docker-builder/blob/master/LICENSE) license.

RELEASE_NOTES.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# RELEASE NOTES
22

3-
## [0.7.2]
3+
## [0.7.3]
44

55
### Fixed
6-
- In docker build command, before adding a new tag validate that is normalized
6+
- Generate auth config when username and password are empty, instead of returning an error

pkg/auth/docker/auth.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ func GenerateAuthConfig(username, password string) (*dockertypes.AuthConfig, err
3030
// GenerateUserPasswordAuthConfig return an AuthConfig to identify to docker registry using user-password credentials
3131
func GenerateUserPasswordAuthConfig(username, password string) (*dockertypes.AuthConfig, error) {
3232

33-
if username == "" {
34-
return nil, errors.New("(auth::GenerateUserPasswordAuthConfig)", "Username must be provided")
35-
}
36-
37-
if password == "" {
38-
return nil, errors.New("(auth::GenerateUserPasswordAuthConfig)", "Password must be provided")
39-
}
40-
4133
authConfig := &dockertypes.AuthConfig{
4234
Username: username,
4335
Password: password,

pkg/build/build_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,6 @@ func TestAddAuth(t *testing.T) {
141141
},
142142
},
143143
},
144-
{
145-
desc: "Testing add invalid user-password auth",
146-
dockerBuildCmd: &DockerBuildCmd{},
147-
args: &args{
148-
username: "",
149-
password: "AqSwd3Fr",
150-
registry: "registry",
151-
},
152-
err: errors.New("(build::AddAuth)", "Error generation user password auth configuration", errors.New(
153-
"(auth::GenerateUserPasswordAuthConfig)", "Username must be provided")),
154-
res: map[string]dockertypes.AuthConfig{},
155-
},
156144
}
157145
for _, test := range tests {
158146
t.Run(test.desc, func(t *testing.T) {

0 commit comments

Comments
 (0)