Skip to content

Instantly share code, notes, and snippets.

@athoune
Created May 23, 2017
Embed
What would you like to do?
Steal certificates from Traefik
package acme
import (
"crypto/tls"
"encoding/base64"
"encoding/json"
"io/ioutil"
)
type Cert struct {
Domains struct {
Main string
SANs interface{}
}
Certificate struct {
Domain string
CertURL string
CertStableURL string
PrivateKey string
Certificate string
}
}
type Acme struct {
DomainsCertificate struct {
Certs []*Cert
}
}
func (c *Cert) GetCertificate() (*tls.Certificate, error) {
public, err := base64.StdEncoding.DecodeString(c.Certificate.Certificate)
if err != nil {
return nil, err
}
private, err := base64.StdEncoding.DecodeString(c.Certificate.PrivateKey)
if err != nil {
return nil, err
}
cert, err := tls.X509KeyPair(public, private)
return &cert, err
}
func (a *Acme) CertificateByDomain(domain string) (*Cert, bool) {
for _, cert := range a.DomainsCertificate.Certs {
if cert.Certificate.Domain == domain {
return cert, true
}
}
return nil, false
}
func ReadFile(path string) (*Acme, error) {
raw, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var acme Acme
err = json.Unmarshal(raw, &acme)
if err != nil {
return nil, err
}
return &acme, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment