-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better Docker Auth lookup #2190
Merged
+3,757
−31
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6342d4f
WIP
dadgar 03f9bba
Better auth lookup
dadgar 3672537
vendor
dadgar 982feb8
undo
dadgar 3c610ec
Remove SSL
dadgar abd0693
Deprecation notice
dadgar 67bec60
Close file
dadgar 77b19f5
Add test and better logs
dadgar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,10 @@ import ( | |
|
||
docker "github.com/fsouza/go-dockerclient" | ||
|
||
"github.com/docker/docker/cli/config/configfile" | ||
"github.com/docker/docker/reference" | ||
"github.com/docker/docker/registry" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"github.com/hashicorp/go-plugin" | ||
"github.com/hashicorp/nomad/client/allocdir" | ||
|
@@ -135,7 +139,6 @@ type DockerDriverConfig struct { | |
LabelsRaw []map[string]string `mapstructure:"labels"` // | ||
Labels map[string]string `mapstructure:"-"` // Labels to set when the container starts up | ||
Auth []DockerDriverAuth `mapstructure:"auth"` // Authentication credentials for a private Docker registry | ||
SSL bool `mapstructure:"ssl"` // Flag indicating repository is served via https | ||
TTY bool `mapstructure:"tty"` // Allocate a Pseudo-TTY | ||
Interactive bool `mapstructure:"interactive"` // Keep STDIN open even if not attached | ||
ShmSize int64 `mapstructure:"shm_size"` // Size of /dev/shm of the container in bytes | ||
|
@@ -164,9 +167,6 @@ func (c *DockerDriverConfig) Validate() error { | |
func NewDockerDriverConfig(task *structs.Task, env *env.TaskEnvironment) (*DockerDriverConfig, error) { | ||
var dconf DockerDriverConfig | ||
|
||
// Default to SSL | ||
dconf.SSL = true | ||
|
||
if err := mapstructure.WeakDecode(task.Config, &dconf); err != nil { | ||
return nil, err | ||
} | ||
|
@@ -315,6 +315,7 @@ func (d *DockerDriver) Validate(config map[string]interface{}) error { | |
"auth": &fields.FieldSchema{ | ||
Type: fields.TypeArray, | ||
}, | ||
// COMPAT: Remove in 0.6.0. SSL is no longer needed | ||
"ssl": &fields.FieldSchema{ | ||
Type: fields.TypeBool, | ||
}, | ||
|
@@ -985,29 +986,17 @@ func (d *DockerDriver) pullImage(driverConfig *DockerDriverConfig, client *docke | |
Email: driverConfig.Auth[0].Email, | ||
ServerAddress: driverConfig.Auth[0].ServerAddress, | ||
} | ||
} | ||
|
||
if authConfigFile := d.config.Read("docker.auth.config"); authConfigFile != "" { | ||
if f, err := os.Open(authConfigFile); err == nil { | ||
defer f.Close() | ||
var authConfigurations *docker.AuthConfigurations | ||
if authConfigurations, err = docker.NewAuthConfigurations(f); err != nil { | ||
return fmt.Errorf("Failed to create docker auth object: %v", err) | ||
} | ||
|
||
authConfigurationKey := "" | ||
if driverConfig.SSL { | ||
authConfigurationKey += "https://" | ||
} | ||
} else if authConfigFile := d.config.Read("docker.auth.config"); authConfigFile != "" { | ||
authOptionsPtr, err := authOptionFrom(authConfigFile, repo) | ||
if err != nil { | ||
d.logger.Printf("[INFO] driver.docker: failed to find docker auth for repo %q: %v", repo, err) | ||
return fmt.Errorf("Failed to find docker auth for repo %q: %v", repo, err) | ||
} | ||
|
||
authConfigurationKey += strings.Split(driverConfig.ImageName, "/")[0] | ||
if authConfiguration, ok := authConfigurations.Configs[authConfigurationKey]; ok { | ||
authOptions = authConfiguration | ||
} else { | ||
d.logger.Printf("[INFO] Failed to find docker auth with key %s", authConfigurationKey) | ||
} | ||
} else { | ||
return fmt.Errorf("Failed to open auth config file: %v, error: %v", authConfigFile, err) | ||
authOptions = *authOptionsPtr | ||
if authOptions.Email == "" && authOptions.Password == "" && | ||
authOptions.ServerAddress == "" && authOptions.Username == "" { | ||
d.logger.Printf("[DEBUG] driver.docker: did not find docker auth for repo %q", repo) | ||
} | ||
} | ||
|
||
|
@@ -1404,3 +1393,41 @@ func calculatePercent(newSample, oldSample, newTotal, oldTotal uint64, cores int | |
|
||
return (float64(numerator) / float64(denom)) * float64(cores) * 100.0 | ||
} | ||
|
||
// authOptionFrom takes the Docker auth config file and the repo being pulled | ||
// and returns an AuthConfiguration or an error if the file/repo could not be | ||
// parsed or looked up. | ||
func authOptionFrom(file, repo string) (*docker.AuthConfiguration, error) { | ||
name, err := reference.ParseNamed(repo) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to parse named repo %q: %v", err) | ||
} | ||
|
||
repoInfo, err := registry.ParseRepositoryInfo(name) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to parse repository: %v", err) | ||
} | ||
|
||
f, err := os.Open(file) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please close the file once you are done using it. |
||
if err != nil { | ||
return nil, fmt.Errorf("Failed to open auth config file: %v, error: %v", file, err) | ||
} | ||
defer f.Close() | ||
|
||
cfile := new(configfile.ConfigFile) | ||
if err := cfile.LoadFromReader(f); err != nil { | ||
return nil, fmt.Errorf("Failed to parse auth config file: %v", err) | ||
} | ||
|
||
dockerAuthConfig := registry.ResolveAuthConfig(cfile.AuthConfigs, repoInfo.Index) | ||
|
||
// Convert to Api version | ||
apiAuthConfig := &docker.AuthConfiguration{ | ||
Username: dockerAuthConfig.Username, | ||
Password: dockerAuthConfig.Password, | ||
Email: dockerAuthConfig.Email, | ||
ServerAddress: dockerAuthConfig.ServerAddress, | ||
} | ||
|
||
return apiAuthConfig, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"auths": { | ||
"https://index.docker.io/v1/": { | ||
"auth": "dGVzdDoxMjM0" | ||
}, | ||
"quay.io": { | ||
"auth": "dGVzdDo1Njc4" | ||
}, | ||
"https://other.io/v1/": { | ||
"auth": "dGVzdDphYmNk" | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to see a test for this. Hard to follow what this method expects to return the right AuthConfiguration.