func GenerateCertificates(config *CertificatesConfig) error {
	var privateKey interface{}
	var err error

	switch config.EcdsaCurve {
	case "":
		privateKey, err = rsa.GenerateKey(rand.Reader, config.RsaBits)
	case "P224":
		privateKey, err = ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
	case "P256":
		privateKey, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
	case "P384":
		privateKey, err = ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
	case "P521":
		privateKey, err = ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
	default:
		return fmt.Errorf("unrecognized elliptic curve: %q", config.EcdsaCurve)
	}

	if err != nil {
		return fmt.Errorf("failed to generate private key: %s", err)
	}

	notBefore := time.Now()
	notAfter := notBefore.Add(config.ValidFor)

	serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
	serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
	if err != nil {
		return fmt.Errorf("failed to generate serial number: %s", err)
	}

	template := x509.Certificate{
		SerialNumber: serialNumber,
		Subject: pkix.Name{
			Organization: []string{"datadog"},
		},
		NotBefore: notBefore,
		NotAfter:  notAfter,

		KeyUsage:              x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
		ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
		BasicConstraintsValid: true,
	}

	hosts := strings.Split(config.Hosts, ",")
	for _, h := range hosts {
		ip := net.ParseIP(h)
		if ip != nil {
			template.IPAddresses = append(template.IPAddresses, ip)
			continue
		}
		template.DNSNames = append(template.DNSNames, h)
	}

	template.IsCA = true
	template.KeyUsage = x509.KeyUsageCertSign

	derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey(privateKey), privateKey)
	if err != nil {
		return fmt.Errorf("failed to create certificate: %s", err)
	}

	certOut, err := os.Create(config.CertFilePath)
	if err != nil {
		return fmt.Errorf("failed to open %s for writing: %s", config.CertFilePath, err)
	}

	pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
	defer certOut.Close()

	keyOut, err := os.OpenFile(config.KeyFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
	if err != nil {
		return fmt.Errorf("failed to open %s for writing: %s", config.KeyFilePath, err)
	}
	defer keyOut.Close()

	p, err := pemBlockForKey(privateKey)
	if err != nil {
		return err
	}
	pem.Encode(keyOut, p)
	return nil
}
// GenerateCertificates create a self-signed certificate with a private key according the config in parameter
GenerateCertificates create a self-signed certificate with a private key according the config in parameter
// GenerateCertificates create a self - signed certificate with a private key according the config in parameter
func GenerateCertificates(config *CertificatesConfig) error {
	var privateKey interface{}
	var err error
	switch config.EcdsaCurve {
	case "":
		privateKey, err = rsa.GenerateKey(rand.Reader, config.RsaBits)
	case "P224":
		privateKey, err = ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
	case "P256":
		privateKey, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
	case "P384":
		privateKey, err = ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
	case "P521":
		privateKey, err = ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
	default:
		return fmt.Errorf("unrecognized elliptic curve: %q", config.EcdsaCurve)
	}
	if err != nil {
		return fmt.Errorf("failed to generate private key: %s", err)
	}
	notBefore := time.Now()
	notAfter := notBefore.Add(config.ValidFor)
	serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
	serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
	if err != nil {
		return fmt.Errorf("failed to generate serial number: %s", err)
	}
	template := x509.Certificate{
		SerialNumber: serialNumber,
		Subject: pkix.Name{
			Organization: []string{"datadog"},
		},
		NotBefore: notBefore,
		NotAfter:  notAfter,
		KeyUsage:              x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
		ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
		BasicConstraintsValid: true,
	}
	hosts := strings.Split(config.Hosts, ",")
	for _, h := range hosts {
		ip := net.ParseIP(h)
		if ip != nil {
			template.IPAddresses = append(template.IPAddresses, ip)
			continue
		}
		template.DNSNames = append(template.DNSNames, h)
	}
	template.IsCA = true
	template.KeyUsage = x509.KeyUsageCertSign
	derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey(privateKey), privateKey)
	if err != nil {
		return fmt.Errorf("failed to create certificate: %s", err)
	}
	certOut, err := os.Create(config.CertFilePath)
	if err != nil {
		return fmt.Errorf("failed to open %s for writing: %s", config.CertFilePath, err)
	}
	pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
	defer certOut.Close()
	keyOut, err := os.OpenFile(config.KeyFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
	if err != nil {
		return fmt.Errorf("failed to open %s for writing: %s", config.KeyFilePath, err)
	}
	defer keyOut.Close()
	p, err := pemBlockForKey(privateKey)
	if err != nil {
		return err
	}
	pem.Encode(keyOut, p)
	return nil
}
func GenerateCertificates ( config * CertificatesConfig ) error { var privateKey interface { } var err error switch config . EcdsaCurve { case " " : privateKey , err = rsa . GenerateKey ( rand . Reader , config . RsaBits ) case " " : privateKey , err = ecdsa . GenerateKey ( elliptic . P224 ( ) , rand . Reader ) case " " : privateKey , err = ecdsa . GenerateKey ( elliptic . P256 ( ) , rand . Reader ) case " " : privateKey , err = ecdsa . GenerateKey ( elliptic . P384 ( ) , rand . Reader ) case " " : privateKey , err = ecdsa . GenerateKey ( elliptic . P521 ( ) , rand . Reader ) default : return fmt . Errorf ( " " , config . EcdsaCurve ) } if err != nil { return fmt . Errorf ( " " , err ) } notBefore := time . Now ( ) notAfter := notBefore . Add ( config . ValidFor ) serialNumberLimit := new ( big . Int ) . Lsh ( big . NewInt ( 1 ) , 128 ) serialNumber , err := rand . Int ( rand . Reader , serialNumberLimit ) if err != nil { return fmt . Errorf ( " " , err ) } template := x509 . Certificate { SerialNumber : serialNumber , Subject : pkix . Name { Organization : [ ] string { " " } , } , NotBefore : notBefore , NotAfter : notAfter , KeyUsage : x509 . KeyUsageKeyEncipherment | x509 . KeyUsageDigitalSignature , ExtKeyUsage : [ ] x509 . ExtKeyUsage { x509 . ExtKeyUsageServerAuth } , BasicConstraintsValid : true , } hosts := strings . Split ( config . Hosts , " " ) for _ , h := range hosts { ip := net . ParseIP ( h ) if ip != nil { template . IPAddresses = append ( template . IPAddresses , ip ) continue } template . DNSNames = append ( template . DNSNames , h ) } template . IsCA = true template . KeyUsage = x509 . KeyUsageCertSign derBytes , err := x509 . CreateCertificate ( rand . Reader , & template , & template , publicKey ( privateKey ) , privateKey ) if err != nil { return fmt . Errorf ( " " , err ) } certOut , err := os . Create ( config . CertFilePath ) if err != nil { return fmt . Errorf ( " " , config . CertFilePath , err ) } pem . Encode ( certOut , & pem . Block { Type : " " , Bytes : derBytes } ) defer certOut . Close ( ) keyOut , err := os . OpenFile ( config . KeyFilePath , os . O_WRONLY | os . O_CREATE | os . O_TRUNC , 0600 ) if err != nil { return fmt . Errorf ( " " , config . KeyFilePath , err ) } defer keyOut . Close ( ) p , err := pemBlockForKey ( privateKey ) if err != nil { return err } pem . Encode ( keyOut , p ) return nil }
GenerateCertificates create a self-signed certificate with a private key according the config in parameter
GenerateCertificates create a self - signed certificate with a private key according the config in parameter