Skip to content

Commit

Permalink
Logging of client calls
Browse files Browse the repository at this point in the history
  • Loading branch information
evenh committed Jan 9, 2019
1 parent 8eb20ee commit b59bc4d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"time"
)

func StartClient(config *config.ClientConfig) {
func StartClient(config *config.ClientConfig, userAgent string) {
log.Infof("Initializing client")

// Check if the config is valid
Expand All @@ -28,7 +28,7 @@ func StartClient(config *config.ClientConfig) {
log.Infof("Configuring connection to %s for gRPC operations", config.GetDialAddr())

// Configure connection
conn, err := grpc.Dial(config.GetDialAddr(), grpc.WithInsecure()) // TODO: Not run insecure
conn, err := grpc.Dial(config.GetDialAddr(), grpc.WithInsecure(), grpc.WithUserAgent(userAgent+";")) // TODO: Not run insecure

if err != nil {
log.Warnf("Could not configure connection to host: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ var clientCmd = &cobra.Command{
RenewalThreshold: viper.GetDuration("client.renewalThreshold"),
}

client.StartClient(&c)
client.StartClient(&c, UserAgent())
},
}
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd

import (
"fmt"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"runtime"
)

var (
Expand Down Expand Up @@ -55,3 +57,7 @@ func constructDir() string {

return home + "/.intercert"
}

func UserAgent() string {
return fmt.Sprintf("intercert v%s (%s); %s-%s", Version, Commit, runtime.GOOS, runtime.GOARCH)
}
13 changes: 13 additions & 0 deletions server/issuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/xenolf/lego/certcrypto"
"github.com/xenolf/lego/log"
"github.com/xenolf/lego/providers/dns"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"strings"
)

Expand Down Expand Up @@ -58,6 +60,7 @@ func NewIssuerService(config *config.ServerConfig) *IssuerService {

func (s IssuerService) IssueCert(ctx context.Context, req *api.CertificateRequest) (*api.CertificateResponse, error) {
// TODO: Validate auth in context
logClient(ctx, "IssueCert("+req.DnsName+")")

log.Infof("[%s] Received certificate request from client", req.DnsName)

Expand Down Expand Up @@ -107,6 +110,7 @@ func (s IssuerService) IssueCert(ctx context.Context, req *api.CertificateReques
}

func (s IssuerService) Ping(ctx context.Context, req *api.PingRequest) (*api.PingResponse, error) {
logClient(ctx, "Ping")
// TODO: Auth for ping?
return &api.PingResponse{Msg: "pong"}, nil
}
Expand Down Expand Up @@ -149,3 +153,12 @@ func pemEncodeCerts(cert tls.Certificate) (string, error) {

return strings.Join(certificates, ""), nil
}

func logClient(ctx context.Context, operation string) {
md, mdOK := metadata.FromIncomingContext(ctx)
peerInfo, pOK := peer.FromContext(ctx)

if mdOK && pOK {
log.Infof("Call from %s - %s: %s", peerInfo.Addr, md["user-agent"], operation)
}
}

0 comments on commit b59bc4d

Please sign in to comment.