github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/logfwd/syslog/config_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package syslog_test
     5  
     6  import (
     7  	"github.com/juju/testing"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/cert"
    12  	"github.com/juju/juju/logfwd/syslog"
    13  	coretesting "github.com/juju/juju/testing"
    14  )
    15  
    16  type ConfigSuite struct {
    17  	testing.IsolationSuite
    18  }
    19  
    20  var _ = gc.Suite(&ConfigSuite{})
    21  
    22  func (s *ConfigSuite) TestRawValidateFull(c *gc.C) {
    23  	cfg := syslog.RawConfig{
    24  		Host:       "a.b.c:9876",
    25  		CACert:     coretesting.CACert,
    26  		ClientCert: coretesting.ServerCert,
    27  		ClientKey:  coretesting.ServerKey,
    28  	}
    29  
    30  	err := cfg.Validate()
    31  
    32  	c.Check(err, jc.ErrorIsNil)
    33  }
    34  
    35  func (s *ConfigSuite) TestRawValidateWithoutPort(c *gc.C) {
    36  	cfg := syslog.RawConfig{
    37  		Host:       "a.b.c",
    38  		CACert:     coretesting.CACert,
    39  		ClientCert: coretesting.ServerCert,
    40  		ClientKey:  coretesting.ServerKey,
    41  	}
    42  
    43  	err := cfg.Validate()
    44  
    45  	c.Check(err, jc.ErrorIsNil)
    46  }
    47  
    48  func (s *ConfigSuite) TestRawValidateZeroValue(c *gc.C) {
    49  	var cfg syslog.RawConfig
    50  	err := cfg.Validate()
    51  	c.Check(err, jc.ErrorIsNil)
    52  }
    53  
    54  func (s *ConfigSuite) TestRawValidateMissingHost(c *gc.C) {
    55  	cfg := syslog.RawConfig{
    56  		Enabled:    true,
    57  		Host:       "",
    58  		CACert:     coretesting.CACert,
    59  		ClientCert: coretesting.ServerCert,
    60  		ClientKey:  coretesting.ServerKey,
    61  	}
    62  
    63  	err := cfg.Validate()
    64  
    65  	c.Check(err, gc.ErrorMatches, `Host "" not valid`)
    66  }
    67  
    68  func (s *ConfigSuite) TestRawValidateMissingHostNotEnabled(c *gc.C) {
    69  	cfg := syslog.RawConfig{
    70  		Host:       "",
    71  		CACert:     coretesting.CACert,
    72  		ClientCert: coretesting.ServerCert,
    73  		ClientKey:  coretesting.ServerKey,
    74  	}
    75  
    76  	err := cfg.Validate()
    77  	c.Check(err, jc.ErrorIsNil)
    78  }
    79  
    80  func (s *ConfigSuite) TestRawValidateMissingHostname(c *gc.C) {
    81  	cfg := syslog.RawConfig{
    82  		Enabled:    true,
    83  		Host:       ":9876",
    84  		CACert:     coretesting.CACert,
    85  		ClientCert: coretesting.ServerCert,
    86  		ClientKey:  coretesting.ServerKey,
    87  	}
    88  
    89  	err := cfg.Validate()
    90  
    91  	c.Check(err, gc.ErrorMatches, `Host ":9876" not valid`)
    92  }
    93  
    94  func (s *ConfigSuite) TestRawValidateMissingCACert(c *gc.C) {
    95  	cfg := syslog.RawConfig{
    96  		Host:       "a.b.c:9876",
    97  		CACert:     "",
    98  		ClientCert: coretesting.ServerCert,
    99  		ClientKey:  coretesting.ServerKey,
   100  	}
   101  
   102  	err := cfg.Validate()
   103  
   104  	c.Check(err, gc.ErrorMatches, `validating TLS config: parsing CA certificate: no certificates found`)
   105  }
   106  
   107  func (s *ConfigSuite) TestRawValidateBadCACert(c *gc.C) {
   108  	cfg := syslog.RawConfig{
   109  		Host:       "a.b.c:9876",
   110  		CACert:     invalidCert,
   111  		ClientCert: coretesting.ServerCert,
   112  		ClientKey:  coretesting.ServerKey,
   113  	}
   114  
   115  	err := cfg.Validate()
   116  
   117  	c.Check(err, gc.ErrorMatches, `validating TLS config: parsing CA certificate: asn1: syntax error: data truncated`)
   118  }
   119  
   120  func (s *ConfigSuite) TestRawValidateBadCACertFormat(c *gc.C) {
   121  	cfg := syslog.RawConfig{
   122  		Host:       "a.b.c:9876",
   123  		CACert:     "abc",
   124  		ClientCert: coretesting.ServerCert,
   125  		ClientKey:  coretesting.ServerKey,
   126  	}
   127  
   128  	err := cfg.Validate()
   129  
   130  	c.Check(err, gc.ErrorMatches, `validating TLS config: parsing CA certificate: no certificates found`)
   131  }
   132  
   133  func (s *ConfigSuite) TestRawValidateMissingCert(c *gc.C) {
   134  	cfg := syslog.RawConfig{
   135  		Host:       "a.b.c:9876",
   136  		CACert:     coretesting.CACert,
   137  		ClientCert: "",
   138  		ClientKey:  coretesting.ServerKey,
   139  	}
   140  
   141  	err := cfg.Validate()
   142  
   143  	c.Check(err, gc.ErrorMatches, `validating TLS config: parsing client key pair: (crypto/)?tls: failed to find any PEM data in certificate input`)
   144  }
   145  
   146  func (s *ConfigSuite) TestRawValidateBadCert(c *gc.C) {
   147  	cfg := syslog.RawConfig{
   148  		Host:       "a.b.c:9876",
   149  		CACert:     coretesting.CACert,
   150  		ClientCert: invalidCert,
   151  		ClientKey:  coretesting.ServerKey,
   152  	}
   153  
   154  	err := cfg.Validate()
   155  
   156  	c.Check(err, gc.ErrorMatches, `validating TLS config: parsing client key pair: asn1: syntax error: data truncated`)
   157  }
   158  
   159  func (s *ConfigSuite) TestRawValidateBadCertFormat(c *gc.C) {
   160  	cfg := syslog.RawConfig{
   161  		Host:       "a.b.c:9876",
   162  		CACert:     coretesting.CACert,
   163  		ClientCert: "abc",
   164  		ClientKey:  coretesting.ServerKey,
   165  	}
   166  
   167  	err := cfg.Validate()
   168  
   169  	c.Check(err, gc.ErrorMatches, `validating TLS config: parsing client key pair: (crypto/)?tls: failed to find any PEM data in certificate input`)
   170  }
   171  
   172  func (s *ConfigSuite) TestRawValidateMissingKey(c *gc.C) {
   173  	cfg := syslog.RawConfig{
   174  		Host:       "a.b.c:9876",
   175  		CACert:     coretesting.CACert,
   176  		ClientCert: coretesting.ServerCert,
   177  		ClientKey:  "",
   178  	}
   179  
   180  	err := cfg.Validate()
   181  
   182  	c.Check(err, gc.ErrorMatches, `validating TLS config: parsing client key pair: (crypto/)?tls: failed to find any PEM data in key input`)
   183  }
   184  
   185  func (s *ConfigSuite) TestRawValidateBadKey(c *gc.C) {
   186  	cfg := syslog.RawConfig{
   187  		Host:       "a.b.c:9876",
   188  		CACert:     coretesting.CACert,
   189  		ClientCert: coretesting.ServerCert,
   190  		ClientKey:  invalidKey,
   191  	}
   192  
   193  	err := cfg.Validate()
   194  
   195  	c.Check(err, gc.ErrorMatches, `validating TLS config: parsing client key pair: (crypto/)?tls: failed to parse private key`)
   196  }
   197  
   198  func (s *ConfigSuite) TestRawValidateBadKeyFormat(c *gc.C) {
   199  	cfg := syslog.RawConfig{
   200  		Host:       "a.b.c:9876",
   201  		CACert:     coretesting.CACert,
   202  		ClientCert: coretesting.ServerCert,
   203  		ClientKey:  "abc",
   204  	}
   205  
   206  	err := cfg.Validate()
   207  
   208  	c.Check(err, gc.ErrorMatches, `validating TLS config: parsing client key pair: (crypto/)?tls: failed to find any PEM data in key input`)
   209  }
   210  
   211  func (s *ConfigSuite) TestRawValidateCertKeyMismatch(c *gc.C) {
   212  	_, key, err := cert.NewDefaultServer(coretesting.CACert, coretesting.CAKey, nil)
   213  	c.Assert(err, jc.ErrorIsNil)
   214  	cfg := syslog.RawConfig{
   215  		Host:       "a.b.c:9876",
   216  		CACert:     coretesting.CACert,
   217  		ClientCert: coretesting.ServerCert,
   218  		ClientKey:  key,
   219  	}
   220  
   221  	err = cfg.Validate()
   222  	c.Check(err, gc.ErrorMatches, `validating TLS config: parsing client key pair: (crypto/)?tls: private key does not match public key`)
   223  }
   224  
   225  var invalidCert = `
   226  -----BEGIN CERTIFICATE-----
   227  MIIBOgIBAAJAZabKgKInuOxj5vDWLwHHQtK3/45KB+32D15w94Nt83BmuGxo90lw
   228  -----END CERTIFICATE-----
   229  `[1:]
   230  
   231  var invalidKey = `
   232  -----BEGIN RSA PRIVATE KEY-----
   233  MIIBOgIBAAJAZabKgKInuOxj5vDWLwHHQtK3/45KB+32D15w94Nt83BmuGxo90lw
   234  -----END RSA PRIVATE KEY-----
   235  `[1:]