Skip to content

Commit

Permalink
User zerolog for structured logs
Browse files Browse the repository at this point in the history
  • Loading branch information
project0 committed May 27, 2022
1 parent 424953c commit 97f39ea
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 20 deletions.
5 changes: 3 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package api

import (
"log"
"net/http"
"os"

"github.com/rs/zerolog/log"

"github.com/gorilla/handlers"
"github.com/gorilla/mux"

Expand All @@ -28,7 +29,7 @@ func NewApiServer(listen string, store *certstore.CertStore) {
go func() {
err := http.ListenAndServe(listen, handlers.LoggingHandler(os.Stdout, r))
if err != nil {
log.Fatalf("Failed to setup the http server: %s\n", err.Error())
log.Fatal().Err(err).Msg("Failed to setup the http server")
}
}()
}
13 changes: 7 additions & 6 deletions certstore/certstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"crypto/rsa"
"crypto/x509"
"encoding/json"
"log"
"sync"

"github.com/rs/zerolog/log"

"github.com/docker/libkv/store"
"github.com/go-acme/lego/v4/certcrypto"
"github.com/go-acme/lego/v4/certificate"
Expand All @@ -34,7 +35,7 @@ func (u User) GetRegistration() *registration.Resource {
func (u User) GetPrivateKey() crypto.PrivateKey {
key, err := x509.ParsePKCS1PrivateKey(u.Key)
if err != nil {
log.Fatal("Could not decode stored user private key")
log.Err(err).Msg("Cannot decode stored user private key")
}
return key
}
Expand Down Expand Up @@ -146,16 +147,16 @@ func (c *CertStore) GetCertificate(request *CertRequest) (*CertificateResource,

// check user first....
if c.user.Registration == nil {
log.Println("New Registration of user", c.client)
log.Info().Msg("register new user")
reg, err := c.client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
if err != nil {
log.Println(err)
log.Err(err).Msg("registration failed")
return nil, err
}
// save this
c.user.Registration = reg
if err := c.SaveUser(c.user); err != nil {
log.Printf("could not save user registration %v", err)
log.Err(err).Msg("could not save user registration")
return nil, err
}
}
Expand Down Expand Up @@ -184,7 +185,7 @@ func (c *CertStore) GetCertificate(request *CertRequest) (*CertificateResource,
val, _ := json.Marshal(cert)
err = c.storage.Put(request.pathCert(), val, nil)
if err != nil {
log.Printf("could not save cert for %s to storage %v", request.Domain, err)
log.Err(err).Str("domain", request.Domain).Msg("cannot save certificate in storage")
}

return cert, nil
Expand Down
3 changes: 2 additions & 1 deletion certstore/request.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package certstore

import (
"log"
"strings"
"time"

"github.com/rs/zerolog/log"
)

// CertRequest contains information about the requested cert
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pquerna/otp v1.3.0 // indirect
github.com/rs/zerolog v1.26.1
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sacloud/libsacloud v1.36.2 // indirect
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkE
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpu/goacmedns v0.1.1 h1:DM3H2NiN2oam7QljgGY5ygy4yDXhK5Z4JUnqaugs2C4=
github.com/cpu/goacmedns v0.1.1/go.mod h1:MuaouqEhPAHxsbqjgnck5zeghuwBP1dLnPoobeGqugQ=
Expand Down Expand Up @@ -239,6 +240,7 @@ github.com/gobs/pretty v0.0.0-20180724170744-09732c25a95b h1:/vQ+oYKu+JoyaMPDsv5
github.com/gobs/pretty v0.0.0-20180724170744-09732c25a95b/go.mod h1:Xo4aNUOrJnVruqWQJBtW6+bTBDTniY8yZum5rF3b5jw=
github.com/goccy/go-json v0.7.8/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0=
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
Expand Down Expand Up @@ -598,6 +600,9 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.26.1 h1:/ihwxqH+4z8UxyI70wM1z9yCvkWcfz/a3mj48k/Zngc=
github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+tmc=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down Expand Up @@ -697,6 +702,7 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand Down Expand Up @@ -739,6 +745,7 @@ golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM=
Expand Down Expand Up @@ -826,6 +833,7 @@ golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210913180222-943fd674d43e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
Expand Down Expand Up @@ -941,6 +949,7 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down Expand Up @@ -1038,6 +1047,7 @@ golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
golang.org/x/tools v0.1.10 h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20=
golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
56 changes: 47 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package main

import (
"fmt"
"log"
"errors"
"os"
"os/signal"
"strings"
Expand All @@ -16,6 +15,9 @@ import (
"github.com/project0/certjunkie/certstore"
"github.com/project0/certjunkie/certstore/libkv/local"
"github.com/project0/certjunkie/provider"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"

"github.com/urfave/cli/v2"
)

Expand All @@ -38,6 +40,36 @@ func main() {
app.HideVersion = true
app.Usage = "issue certificate with ACME as a REST"

app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "log.debug",
Category: "log",
Usage: "Enable debug logs",
EnvVars: flagSetHelperEnvKey("LOG_DEBUG"),
},
&cli.StringFlag{
Name: "log.format",
Category: "log",
Usage: "Log format (console,json)",
Value: "console",
EnvVars: flagSetHelperEnvKey("LOG_FORMAT"),
},
}
app.Before = func(ctx *cli.Context) error {
// Default level for this example is info, unless debug flag is present
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if ctx.Bool("log.debug") {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}

// output format
if ctx.String("log.format") == "console" {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
}

return nil
}

app.Commands = []*cli.Command{
{
Name: "server",
Expand Down Expand Up @@ -108,33 +140,37 @@ func main() {
email := c.String("email")
challengeProvider := c.String("provider")
if email == "" {
return fmt.Errorf("Email is not set")
log.Error().Str("email", email).Msg("you need to provide a valid email address")
return errors.New("cannot initialize server")
}

local.Register()
storage, err := libkv.NewStore(store.Backend(c.String("storage")), []string{}, &store.Config{
Bucket: c.String("storage.path"),
})
if err != nil {
log.Fatal(err)
log.Err(err).Msg("failed to initialize storage")
return errors.New("cannot initialize server")
}

var dnsprovider challenge.Provider
if challengeProvider == provider.Name {
// use built in dns server for cname redirect

dnsprovider = provider.NewDNSCnameChallengeProvider(c.String("dns.zone"), c.String("dns.domain"), c.String("dns.listen"))
} else {
// one of the shipped lego providers
dnsprovider, err = dns.NewDNSChallengeProviderByName(challengeProvider)
if err != nil {
log.Fatal(err)
log.Err(err).Msg("failed to initialize DNS challenge provider")
return errors.New("cannot initialize server")

}
}

certStore, err = certstore.NewCertStore(c.String("server"), email, dnsprovider, storage, c.String("preferred-chain"))
if err != nil {
log.Fatal(err)
log.Err(err).Msg("failed to initialize certificate storage")
return errors.New("cannot initialize server")
}

api.NewApiServer(c.String("listen"), certStore)
Expand Down Expand Up @@ -200,7 +236,7 @@ func main() {
Action: func(c *cli.Context) error {
domain := c.String("domain")
if domain == "" {
return fmt.Errorf("Domain is not set")
return errors.New("domain is not set")
}

client := &api.Client{
Expand Down Expand Up @@ -246,6 +282,8 @@ func main() {
},
},
}
app.RunAndExitOnError()

if err := app.Run(os.Args); err != nil {
log.Fatal().Err(err).Msg("execution failed")
}
}
6 changes: 4 additions & 2 deletions provider/dnscname.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package provider

import (
"fmt"
"log"
"strings"
"sync"
"time"

"github.com/rs/zerolog/log"

"github.com/go-acme/lego/v4/challenge"
"github.com/go-acme/lego/v4/challenge/dns01"
"github.com/miekg/dns"
Expand All @@ -33,6 +34,7 @@ func NewDNSCnameChallengeProvider(zone string, nsdomain string, listen string) c
// start the internal dns server
dns.HandleFunc(zone+".", provider.handleDnsRequests)
log.Printf("Start listening DNS server on %s", listen)
// TODO: Handle properly with signal shutdowns
go serveDns("tcp", listen)
go serveDns("udp", listen)
return provider
Expand Down Expand Up @@ -147,6 +149,6 @@ func (d *DnsCnameProviderAcme) handleDnsRequests(w dns.ResponseWriter, r *dns.Ms
func serveDns(net string, listen string) {
server := &dns.Server{Addr: listen, Net: net, TsigSecret: nil}
if err := server.ListenAndServe(); err != nil {
log.Fatalf("Failed to setup the %s server: %s\n", net, err.Error())
log.Fatal().Err(err).Msgf("Failed to setup the %s server", net)
}
}

0 comments on commit 97f39ea

Please sign in to comment.