Skip to content

Commit

Permalink
Added go report card
Browse files Browse the repository at this point in the history
Fixed several issues that were shown in the report card.
  • Loading branch information
dkumor committed Jul 16, 2016
1 parent 02a182a commit 284efe4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -3,6 +3,7 @@
Add Let's Encrypt support to your golang server in 10 lines of code.

[![GoDoc](https://godoc.org/github.com/dkumor/acmewrapper?status.svg)](https://godoc.org/github.com/dkumor/acmewrapper)
[![Go Report Card](https://goreportcard.com/badge/github.com/dkumor/acmewrapper)](https://goreportcard.com/report/github.com/dkumor/acmewrapper)
[![Build Status](https://travis-ci.org/dkumor/acmewrapper.svg?branch=master)](https://travis-ci.org/dkumor/acmewrapper)

```go
Expand Down Expand Up @@ -155,7 +156,7 @@ w, err := acmewrapper.New(acmewrapper.Config{
PrivateKeyFile: "PRIVATEKEY",

TOSCallback: acmewrapper.TOSAgree,

SaveFileCallback: func(path string, contents []byte) error {
// the path is the file name as set up in the configuration - the certificate will be "CERTIFICATE", etc.
},
Expand Down
35 changes: 21 additions & 14 deletions acme.go
@@ -1,6 +1,7 @@
package acmewrapper

import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
Expand All @@ -12,6 +13,24 @@ import (
"github.com/xenolf/lego/acme"
)

// generateKey generates a key to use for registration in acme
func generateKey(keytype acme.KeyType) (crypto.PrivateKey, error) {
switch keytype {
case acme.RSA2048:
return rsa.GenerateKey(rand.Reader, 2048)
case acme.RSA4096:
return rsa.GenerateKey(rand.Reader, 4096)
case acme.RSA8192:
return rsa.GenerateKey(rand.Reader, 8192)
case acme.EC256:
return ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
case acme.EC384:
return ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
default:
return nil, errors.New("Unrecognized key type")
}
}

// initACME initailizes the acme client - it does everything from reading/writing the
// user private key and registration files, to ensuring that the user is registered
// on the ACME server and has accepted the TOS.
Expand Down Expand Up @@ -79,19 +98,7 @@ func (w *AcmeWrapper) initACME(serverRunning bool) (err error) {
// Whatever the case, we generate our acme user

// Generate the key
if w.Config.PrivateKeyType == acme.RSA2048 {
w.privatekey, err = rsa.GenerateKey(rand.Reader, 2048)
} else if w.Config.PrivateKeyType == acme.RSA4096 {
w.privatekey, err = rsa.GenerateKey(rand.Reader, 4096)
} else if w.Config.PrivateKeyType == acme.RSA8192 {
w.privatekey, err = rsa.GenerateKey(rand.Reader, 8192)
} else if w.Config.PrivateKeyType == acme.EC256 {
w.privatekey, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
} else if w.Config.PrivateKeyType == acme.EC384 {
w.privatekey, err = ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
} else {
return errors.New("Unrecognized key type")
}
w.privatekey, err = generateKey(w.Config.PrivateKeyType)
if err != nil {
return err
}
Expand Down Expand Up @@ -157,7 +164,7 @@ func (w *AcmeWrapper) initACME(serverRunning bool) (err error) {
}
}

// Now that the user and client basics are intialized, we set up the client
// Now that the user and client basics are initialized, we set up the client
// so that it uses our custom SNI provider. We don't want
// to start custom servers, but rather plug into our certificate updater once
// we are running. This allows cert updates to be transparent.
Expand Down
3 changes: 2 additions & 1 deletion acmewrapper.go
Expand Up @@ -10,11 +10,12 @@ import (
"github.com/xenolf/lego/acme"
)

// LoggerInterface represents anything that can Printf.
type LoggerInterface interface {
Printf(format string, v ...interface{})
}

// Allows to use a custom logger for logging purposes
// Logger allows to use a custom logger for logging purposes
var Logger LoggerInterface

func logf(s string, v ...interface{}) {
Expand Down
19 changes: 11 additions & 8 deletions configuration.go
Expand Up @@ -7,18 +7,21 @@ import (
)

const (
// The server to use by default
// DefaultServer is the server to use by default
DefaultServer = "https://acme-v01.api.letsencrypt.org/directory"
// Default type for the private key
// DefaultKeyType represents the type for the private key
DefaultKeyType = acme.RSA2048

// The default port to use for initializing certs on startup
// DefaultAddress is thedefault port to use for initializing certs on startup
DefaultAddress = ":443"

// DefaultRenewTime is the time period before cert expiration to attempt renewal
DefaultRenewTime = 30 * 24 * time.Hour
DefaultRetryDelay = 1 * 24 * time.Hour // Retry once a day
DefaultRenewCheck = 12 * time.Hour // The time between checks for renewal
DefaultRenewTime = 30 * 24 * time.Hour
// DefaultRetryDelay is the time between renew attempts after a previous attempt failed
DefaultRetryDelay = 1 * 24 * time.Hour
// DefaultRenewCheck is the time between checks of the certificate to make sure it doesn't
// need to be renewed
DefaultRenewCheck = 12 * time.Hour
)

// TOSAgree always agrees to the terms of service. This should only be really used if
Expand Down Expand Up @@ -73,13 +76,13 @@ type Config struct {

RenewTime time.Duration // The time in seconds until expiration of current cert that renew is attempted. If not set, default is 30d
RetryDelay time.Duration // The time in seconds to delay between attempts at renewing if renewal fails. (1 day)
RenewCheck time.Duration // The time inbetween checks for renewal. Default is 12h
RenewCheck time.Duration // The time between checks for renewal. Default is 12h

// The callback to use prompting the user to agree to the terms of service. A special Agree is built in, so
// you can set TOSCallback: TOSAgree
TOSCallback TOSCallback

// If there is no certificate set up at all, we need to generate an inital one
// If there is no certificate set up at all, we need to generate an initial one
// to jump-start the server. Therefore, you should input the port that you
// will use when running listen. If there are no certs, it runs a temporary mini
// server at that location to generate initial certificates. Once that is done,
Expand Down
3 changes: 2 additions & 1 deletion rw.go
Expand Up @@ -5,6 +5,8 @@ import (
"io/ioutil"
)

// ErrNotHandled is returned by read and write file callbacks if the file should be
// read from filesystem.
var ErrNotHandled = errors.New("not handled")

func (w *AcmeWrapper) loadFile(path string) ([]byte, error) {
Expand Down Expand Up @@ -39,4 +41,3 @@ func (w *AcmeWrapper) saveFile(path string, contents []byte) error {
}
return nil
}

0 comments on commit 284efe4

Please sign in to comment.