mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #15649 from LK4D4/syslog_validation
Syslog validation
This commit is contained in:
commit
8aa520b8c0
3 changed files with 54 additions and 26 deletions
|
@ -41,7 +41,7 @@ func (daemon *Daemon) getInspectData(container *Container) (*types.ContainerJSON
|
|||
hostConfig.LogConfig.Type = daemon.defaultLogConfig.Type
|
||||
}
|
||||
|
||||
if hostConfig.LogConfig.Config == nil {
|
||||
if len(hostConfig.LogConfig.Config) == 0 {
|
||||
hostConfig.LogConfig.Config = daemon.defaultLogConfig.Config
|
||||
}
|
||||
|
||||
|
|
|
@ -107,33 +107,35 @@ func (s *syslogger) Name() string {
|
|||
}
|
||||
|
||||
func parseAddress(address string) (string, string, error) {
|
||||
if urlutil.IsTransportURL(address) {
|
||||
url, err := url.Parse(address)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
// unix socket validation
|
||||
if url.Scheme == "unix" {
|
||||
if _, err := os.Stat(url.Path); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return url.Scheme, url.Path, nil
|
||||
}
|
||||
|
||||
// here we process tcp|udp
|
||||
host := url.Host
|
||||
if _, _, err := net.SplitHostPort(host); err != nil {
|
||||
if !strings.Contains(err.Error(), "missing port in address") {
|
||||
return "", "", err
|
||||
}
|
||||
host = host + ":514"
|
||||
}
|
||||
|
||||
return url.Scheme, host, nil
|
||||
if address == "" {
|
||||
return "", "", nil
|
||||
}
|
||||
if !urlutil.IsTransportURL(address) {
|
||||
return "", "", fmt.Errorf("syslog-address should be in form proto://address, got %v", address)
|
||||
}
|
||||
url, err := url.Parse(address)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
return "", "", nil
|
||||
// unix socket validation
|
||||
if url.Scheme == "unix" {
|
||||
if _, err := os.Stat(url.Path); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return url.Scheme, url.Path, nil
|
||||
}
|
||||
|
||||
// here we process tcp|udp
|
||||
host := url.Host
|
||||
if _, _, err := net.SplitHostPort(host); err != nil {
|
||||
if !strings.Contains(err.Error(), "missing port in address") {
|
||||
return "", "", err
|
||||
}
|
||||
host = host + ":514"
|
||||
}
|
||||
|
||||
return url.Scheme, host, nil
|
||||
}
|
||||
|
||||
// ValidateLogOpt looks for syslog specific log options
|
||||
|
@ -148,6 +150,12 @@ func ValidateLogOpt(cfg map[string]string) error {
|
|||
return fmt.Errorf("unknown log opt '%s' for syslog log driver", key)
|
||||
}
|
||||
}
|
||||
if _, _, err := parseAddress(cfg["syslog-address"]); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := parseFacility(cfg["syslog-facility"]); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -1550,3 +1550,23 @@ func (s *DockerDaemonSuite) TestDaemonRestartWithContainerWithRestartPolicyAlway
|
|||
c.Assert(err, check.IsNil)
|
||||
c.Assert(strings.TrimSpace(out), check.Equals, id[:12])
|
||||
}
|
||||
|
||||
func (s *DockerDaemonSuite) TestDaemonCorruptedSyslogAddress(c *check.C) {
|
||||
c.Assert(s.d.Start("--log-driver=syslog", "--log-opt", "syslog-address=corrupted:1234"), check.NotNil)
|
||||
runCmd := exec.Command("grep", "Failed to set log opts: syslog-address should be in form proto://address", s.d.LogfileName())
|
||||
if out, _, err := runCommandWithOutput(runCmd); err != nil {
|
||||
c.Fatalf("Expected 'Error starting daemon' message; but doesn't exist in log: %q, err: %v", out, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *DockerDaemonSuite) TestDaemonWideLogConfig(c *check.C) {
|
||||
c.Assert(s.d.Start("--log-driver=json-file", "--log-opt=max-size=1k"), check.IsNil)
|
||||
out, err := s.d.Cmd("run", "-d", "--name=logtest", "busybox", "top")
|
||||
c.Assert(err, check.IsNil, check.Commentf("Output: %s, err: %v", out, err))
|
||||
out, err = s.d.Cmd("inspect", "-f", "{{ .HostConfig.LogConfig.Config }}", "logtest")
|
||||
c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
|
||||
cfg := strings.TrimSpace(out)
|
||||
if cfg != "map[max-size:1k]" {
|
||||
c.Fatalf("Unexpected log-opt: %s, expected map[max-size:1k]", cfg)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue