Skip to content

Commit

Permalink
Merge pull request #77 from ivanilves/ivancito-strikes-back
Browse files Browse the repository at this point in the history
Moar cleaning!
  • Loading branch information
vonrabbe authored Oct 17, 2017
2 parents 38d46b7 + 42850d6 commit 738071f
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 196 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ You could use `lstags`, if you ...
## How do I use it myself?
I run `lstags` inside a Cron Job on my Kubernetes worker nodes to poll my own Docker registry for a new [stable] images.
```
lstags --pull -u myuser -p mypass registry.ivanilves.local/tools/sicario~/v1\\.[0-9]+$/
lstags --pull registry.ivanilves.local/tools/sicario~/v1\\.[0-9]+$/
```
**NB!** In case you use private registry with authentication, make sure your Docker client knows how to authenticate against it!
`lstags` will reuse credentials saved by Docker client in its `config.json` file, one usually found at `~/.docker/config.json`

... and following cronjob runs on my CI server to ensure I always have latest Ubuntu 14.04 and 16.04 images to play with:
```
lstags --pull ubuntu~/^1[46]\\.04$/
Expand Down
114 changes: 0 additions & 114 deletions app/app.go

This file was deleted.

2 changes: 1 addition & 1 deletion auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// WebSchema defines how do we connect to remote web servers
var WebSchema = "https://"
const WebSchema = "https://"

// TokenResponse is an abstraction for aggregated token-related information we get from authentication services
type TokenResponse interface {
Expand Down
4 changes: 2 additions & 2 deletions docker/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func buildImageListOptions(repo string) (types.ImageListOptions, error) {

// Pull pulls Docker image specified
func (dc *DockerClient) Pull(ref string) error {
registryAuth, _ := dc.cnf.GetRegistryAuth(
registryAuth := dc.cnf.GetRegistryAuth(
docker.GetRegistry(ref),
)

Expand All @@ -83,7 +83,7 @@ func (dc *DockerClient) Pull(ref string) error {

// Push pushes Docker image specified
func (dc *DockerClient) Push(ref string) error {
registryAuth, _ := dc.cnf.GetRegistryAuth(
registryAuth := dc.cnf.GetRegistryAuth(
docker.GetRegistry(ref),
)

Expand Down
25 changes: 8 additions & 17 deletions docker/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import (
"strings"
)

// DefaultUsername is the username we use if none is defined in config
var DefaultUsername string

// DefaultPassword is the password we use if none is defined in config
var DefaultPassword string

// DefaultDockerJSON is the defalt path for Docker JSON config file
var DefaultDockerJSON = "~/.docker/config.json"

Expand All @@ -29,33 +23,30 @@ type Auth struct {
B64Auth string `json:"auth"`
}

// AreDefaultCredentialsDefined tells if default username & password are defined
func AreDefaultCredentialsDefined() bool {
return DefaultUsername != "" || DefaultPassword != ""
}

// IsEmpty return true if structure has no relevant data inside
func (c *Config) IsEmpty() bool {
return len(c.Auths) == 0
}

// GetCredentials gets per-registry credentials from loaded Docker config
func (c *Config) GetCredentials(registry string) (string, string, bool) {
_, defined := c.usernames[registry]
if !defined {
return DefaultUsername, DefaultUsername, false
if _, defined := c.usernames[registry]; !defined {
return "", "", false
}

return c.usernames[registry], c.passwords[registry], true
}

// GetRegistryAuth gets per-registry base64 authentication string
func (c *Config) GetRegistryAuth(registry string) (string, bool) {
func (c *Config) GetRegistryAuth(registry string) string {
username, password, defined := c.GetCredentials(registry)
if !defined {
return ""
}

jsonString := fmt.Sprintf("{ \"username\": \"%s\", \"password\": \"%s\" }", username, password)
jsonString := fmt.Sprintf(`{ "username": "%s", "password": "%s" }`, username, password)

return base64.StdEncoding.EncodeToString([]byte(jsonString)), defined
return base64.StdEncoding.EncodeToString([]byte(jsonString))
}

// Load loads a Config object from Docker JSON configuration file specified
Expand Down
21 changes: 0 additions & 21 deletions docker/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,6 @@ const configFile = "../../fixtures/docker/config.json"
const irrelevantConfigFile = "../../fixtures/docker/config.json.irrelevant"
const invalidConfigFile = "../../fixtures/docker/config.json.invalid"

func TestAreDefaultCredentialsDefined(t *testing.T) {
if AreDefaultCredentialsDefined() {
t.Fatalf(
"By default no credentials should be defined, but they are: username '%s', password '%s'",
DefaultUsername,
DefaultPassword,
)
}

DefaultUsername = "user"
DefaultPassword = "pass"

if !AreDefaultCredentialsDefined() {
t.Fatalf(
"Credentials should be defined now, but we get contrary: username '%s', password '%s'",
DefaultUsername,
DefaultPassword,
)
}
}

func TestLoad(t *testing.T) {
examples := map[string]string{
"registry.company.io": "user1:pass1",
Expand Down
Loading

0 comments on commit 738071f

Please sign in to comment.