forked from TrueCloudLab/certificates
api/log: initial implementation of the package (#859)
* api/log: initial implementation of the package * api: refactored to support api/log * scep/api: refactored to support api/log * api/log: documented the package * api: moved log-related tests to api/log
This commit is contained in:
parent
9d027c17d0
commit
17d7fd70cd
6 changed files with 112 additions and 87 deletions
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/smallstep/certificates/acme"
|
"github.com/smallstep/certificates/acme"
|
||||||
|
"github.com/smallstep/certificates/api/log"
|
||||||
"github.com/smallstep/certificates/authority/admin"
|
"github.com/smallstep/certificates/authority/admin"
|
||||||
"github.com/smallstep/certificates/errs"
|
"github.com/smallstep/certificates/errs"
|
||||||
"github.com/smallstep/certificates/logging"
|
"github.com/smallstep/certificates/logging"
|
||||||
|
@ -60,6 +61,6 @@ func WriteError(w http.ResponseWriter, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(err); err != nil {
|
if err := json.NewEncoder(w).Encode(err); err != nil {
|
||||||
LogError(w, err)
|
log.Error(w, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
47
api/log/log.go
Normal file
47
api/log/log.go
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
// Package log implements API-related logging helpers.
|
||||||
|
package log
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/smallstep/certificates/logging"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Error adds to the response writer the given error if it implements
|
||||||
|
// logging.ResponseLogger. If it does not implement it, then writes the error
|
||||||
|
// using the log package.
|
||||||
|
func Error(rw http.ResponseWriter, err error) {
|
||||||
|
if rl, ok := rw.(logging.ResponseLogger); ok {
|
||||||
|
rl.WithFields(map[string]interface{}{
|
||||||
|
"error": err,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnabledResponse log the response object if it implements the EnableLogger
|
||||||
|
// interface.
|
||||||
|
func EnabledResponse(rw http.ResponseWriter, v interface{}) {
|
||||||
|
type enableLogger interface {
|
||||||
|
ToLog() (interface{}, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
if el, ok := v.(enableLogger); ok {
|
||||||
|
out, err := el.ToLog()
|
||||||
|
if err != nil {
|
||||||
|
Error(rw, err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if rl, ok := rw.(logging.ResponseLogger); ok {
|
||||||
|
rl.WithFields(map[string]interface{}{
|
||||||
|
"response": out,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
log.Println(out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
44
api/log/log_test.go
Normal file
44
api/log/log_test.go
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
package log
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/smallstep/certificates/logging"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestError(t *testing.T) {
|
||||||
|
theError := errors.New("the error")
|
||||||
|
|
||||||
|
type args struct {
|
||||||
|
rw http.ResponseWriter
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
withFields bool
|
||||||
|
}{
|
||||||
|
{"normalLogger", args{httptest.NewRecorder(), theError}, false},
|
||||||
|
{"responseLogger", args{logging.NewResponseLogger(httptest.NewRecorder()), theError}, true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
Error(tt.args.rw, tt.args.err)
|
||||||
|
if tt.withFields {
|
||||||
|
if rl, ok := tt.args.rw.(logging.ResponseLogger); ok {
|
||||||
|
fields := rl.Fields()
|
||||||
|
if !reflect.DeepEqual(fields["error"], theError) {
|
||||||
|
t.Errorf("ResponseLogger[\"error\"] = %s, wants %s", fields["error"], theError)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.Error("ResponseWriter does not implement logging.ResponseLogger")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
56
api/utils.go
56
api/utils.go
|
@ -2,52 +2,14 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"google.golang.org/protobuf/encoding/protojson"
|
"google.golang.org/protobuf/encoding/protojson"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
|
|
||||||
"github.com/smallstep/certificates/logging"
|
"github.com/smallstep/certificates/api/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// EnableLogger is an interface that enables response logging for an object.
|
|
||||||
type EnableLogger interface {
|
|
||||||
ToLog() (interface{}, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// LogError adds to the response writer the given error if it implements
|
|
||||||
// logging.ResponseLogger. If it does not implement it, then writes the error
|
|
||||||
// using the log package.
|
|
||||||
func LogError(rw http.ResponseWriter, err error) {
|
|
||||||
if rl, ok := rw.(logging.ResponseLogger); ok {
|
|
||||||
rl.WithFields(map[string]interface{}{
|
|
||||||
"error": err,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// LogEnabledResponse log the response object if it implements the EnableLogger
|
|
||||||
// interface.
|
|
||||||
func LogEnabledResponse(rw http.ResponseWriter, v interface{}) {
|
|
||||||
if el, ok := v.(EnableLogger); ok {
|
|
||||||
out, err := el.ToLog()
|
|
||||||
if err != nil {
|
|
||||||
LogError(rw, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if rl, ok := rw.(logging.ResponseLogger); ok {
|
|
||||||
rl.WithFields(map[string]interface{}{
|
|
||||||
"response": out,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
log.Println(out)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// JSON writes the passed value into the http.ResponseWriter.
|
// JSON writes the passed value into the http.ResponseWriter.
|
||||||
func JSON(w http.ResponseWriter, v interface{}) {
|
func JSON(w http.ResponseWriter, v interface{}) {
|
||||||
JSONStatus(w, v, http.StatusOK)
|
JSONStatus(w, v, http.StatusOK)
|
||||||
|
@ -59,10 +21,12 @@ func JSONStatus(w http.ResponseWriter, v interface{}, status int) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(status)
|
w.WriteHeader(status)
|
||||||
if err := json.NewEncoder(w).Encode(v); err != nil {
|
if err := json.NewEncoder(w).Encode(v); err != nil {
|
||||||
LogError(w, err)
|
log.Error(w, err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
LogEnabledResponse(w, v)
|
|
||||||
|
log.EnabledResponse(w, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProtoJSON writes the passed value into the http.ResponseWriter.
|
// ProtoJSON writes the passed value into the http.ResponseWriter.
|
||||||
|
@ -78,12 +42,16 @@ func ProtoJSONStatus(w http.ResponseWriter, m proto.Message, status int) {
|
||||||
|
|
||||||
b, err := protojson.Marshal(m)
|
b, err := protojson.Marshal(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogError(w, err)
|
log.Error(w, err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := w.Write(b); err != nil {
|
if _, err := w.Write(b); err != nil {
|
||||||
LogError(w, err)
|
log.Error(w, err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//LogEnabledResponse(w, v)
|
|
||||||
|
// log.EnabledResponse(w, v)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,46 +3,11 @@ package api
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
|
|
||||||
"github.com/smallstep/certificates/logging"
|
"github.com/smallstep/certificates/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLogError(t *testing.T) {
|
|
||||||
theError := errors.New("the error")
|
|
||||||
type args struct {
|
|
||||||
rw http.ResponseWriter
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
args args
|
|
||||||
withFields bool
|
|
||||||
}{
|
|
||||||
{"normalLogger", args{httptest.NewRecorder(), theError}, false},
|
|
||||||
{"responseLogger", args{logging.NewResponseLogger(httptest.NewRecorder()), theError}, true},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
LogError(tt.args.rw, tt.args.err)
|
|
||||||
if tt.withFields {
|
|
||||||
if rl, ok := tt.args.rw.(logging.ResponseLogger); ok {
|
|
||||||
fields := rl.Fields()
|
|
||||||
if !reflect.DeepEqual(fields["error"], theError) {
|
|
||||||
t.Errorf("ResponseLogger[\"error\"] = %s, wants %s", fields["error"], theError)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
t.Error("ResponseWriter does not implement logging.ResponseLogger")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestJSON(t *testing.T) {
|
func TestJSON(t *testing.T) {
|
||||||
type args struct {
|
type args struct {
|
||||||
rw http.ResponseWriter
|
rw http.ResponseWriter
|
||||||
|
|
|
@ -10,14 +10,14 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"github.com/smallstep/certificates/api"
|
microscep "github.com/micromdm/scep/v2/scep"
|
||||||
"github.com/smallstep/certificates/authority/provisioner"
|
"github.com/pkg/errors"
|
||||||
"github.com/smallstep/certificates/scep"
|
|
||||||
"go.mozilla.org/pkcs7"
|
"go.mozilla.org/pkcs7"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/smallstep/certificates/api"
|
||||||
|
"github.com/smallstep/certificates/api/log"
|
||||||
microscep "github.com/micromdm/scep/v2/scep"
|
"github.com/smallstep/certificates/authority/provisioner"
|
||||||
|
"github.com/smallstep/certificates/scep"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -337,7 +337,7 @@ func formatCapabilities(caps []string) []byte {
|
||||||
func writeSCEPResponse(w http.ResponseWriter, response SCEPResponse) {
|
func writeSCEPResponse(w http.ResponseWriter, response SCEPResponse) {
|
||||||
|
|
||||||
if response.Error != nil {
|
if response.Error != nil {
|
||||||
api.LogError(w, response.Error)
|
log.Error(w, response.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
if response.Certificate != nil {
|
if response.Certificate != nil {
|
||||||
|
|
Loading…
Reference in a new issue