Ejemplo n.º 1
0
func (cm *ConfigManager) RootCAs() *x509.CertPool {
	if cm.DestProtocol() == "tls" && cm.DestHost() == "logs.papertrailapp.com" {
		return papertrail.RootCA()
	} else {
		return nil
	}
}
Ejemplo n.º 2
0
func TestRawConfig(t *testing.T) {
	assert := assert.New(t)
	initConfigAndFlags()

	// pretend like some things were passed on the command line
	flags.Set("configfile", "test/config.yaml")
	flags.Set("tls", "true")

	c, err := NewConfigFromEnv()
	if err != nil {
		t.Fatal(err)
	}

	assert.Equal(c.Destination.Host, "logs.papertrailapp.com")
	assert.Equal(c.Destination.Port, 514)
	assert.Equal(c.Destination.Protocol, "tls")
	assert.Equal(c.ExcludePatterns, []*regexp.Regexp{regexp.MustCompile("don't log on me"), regexp.MustCompile(`do \w+ on me`)})
	assert.Equal(c.ExcludeFiles, []*regexp.Regexp{regexp.MustCompile(`\.DS_Store`)})
	assert.Equal(c.Files, []LogFile{
		{
			Path: "locallog.txt",
		},
		{
			Path: "/var/log/**/*.log",
		},
		{
			Tag:  "nginx",
			Path: "/var/log/nginx/nginx.log",
		},
		{
			Tag:  "apache",
			Path: "/var/log/httpd/access_log",
		},
	})
	assert.Equal(c.TcpMaxLineLength, 99991)
	assert.Equal(c.NewFileCheckInterval, 10*time.Second)
	assert.Equal(c.ConnectTimeout, 5*time.Second)
	assert.Equal(c.WriteTimeout, 30*time.Second)
	assert.Equal(c.TCP, false)
	assert.Equal(c.TLS, true)
	assert.Equal(c.LogLevels, "<root>=INFO")
	assert.Equal(c.PidFile, "/var/run/rs2.pid")
	assert.Equal(c.DebugLogFile, "/dev/null")
	assert.Equal(c.NoDetach, false)
	sev, err := syslog.Severity("notice")
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(c.Severity, sev)
	fac, err := syslog.Facility("user")
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(c.Facility, fac)
	assert.NotEqual(c.Hostname, "")
	assert.Equal(c.Poll, false)
	assert.Equal(c.RootCAs, papertrail.RootCA())
}
Ejemplo n.º 3
0
// Read in configuration from environment, flags, and specified or default config file.
func NewConfigFromEnv() (*Config, error) {
	if err := flags.Parse(os.Args[1:]); err != nil {
		return nil, err
	}

	if h, _ := flags.GetBool("help"); h {
		usage()
		return nil, ErrUsage
	}

	if v, _ := flags.GetBool("version"); v {
		version()
		return nil, ErrUsage
	}

	c := &Config{}

	// read in config file if it's there
	configFile := config.GetString("config_file")
	config.SetConfigFile(configFile)
	if err := config.ReadInConfig(); err != nil && configFile != defaultConfigFile {
		return nil, err
	}

	// override daemonize setting for platforms that don't support it
	if !utils.CanDaemonize {
		config.Set("no_daemonize", true)
	}

	// unmarshal environment config into our Config object here
	decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
		Result:           c,
		WeaklyTypedInput: true,
		DecodeHook:       decodeHook,
	})
	if err != nil {
		return nil, err
	}

	if err = decoder.Decode(config.AllSettings()); err != nil {
		return nil, err
	}

	// explicitly set destination fields since they are nested
	c.Destination.Host = config.GetString("destination.host")
	c.Destination.Port = config.GetInt("destination.port")
	c.Destination.Protocol = config.GetString("destination.protocol")

	// explicitly set destination protocol if we've asked for tcp or tls
	if c.TLS {
		c.Destination.Protocol = "tls"
	}
	if c.TCP {
		c.Destination.Protocol = "tcp"
	}

	// add the papertrail root CA if necessary
	if c.Destination.Protocol == "tls" && c.Destination.Host == "logs.papertrailapp.com" {
		c.RootCAs = papertrail.RootCA()
	}

	// figure out where to create a pidfile if none was configured
	if c.PidFile == "" {
		c.PidFile = getPidFile()
	}

	// collect any extra args passed on the command line and add them to our file list
	for _, file := range flags.Args() {
		files, err := decodeLogFiles([]interface{}{file})
		if err != nil {
			return nil, err
		}

		c.Files = append(c.Files, files...)
	}

	return c, nil
}