-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted the combined recorder from rpc.Conn
rpc.Conn now accepts a RecorderFactory rather than an ObserverFactory. The Recorder interface has the same methods as Observer, but they can return errors to stop the handling of the request, which is needed for auditing. I've kept the Observer interface, since the lack of errors makes multiplexing simpler. The observer is now embedded in a combined recorder that forwards messages to it but also passes them on to the auditlog recorder, which has the opportunity to interrupt the request.
- Loading branch information
1 parent
c388ea9
commit bae76b8
Showing
12 changed files
with
157 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2017 Canonical Ltd. | ||
// Licensed under the AGPLv3, see LICENCE file for details. | ||
|
||
package observer | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/juju/errors" | ||
|
||
"github.com/juju/juju/core/auditlog" | ||
"github.com/juju/juju/rpc" | ||
) | ||
|
||
// NewRecorderFactory makes a new rpc.RecorderFactory to make | ||
// recorders that that will update the observer and the auditlog | ||
// recorder when it records a request or reply. The auditlog recorder | ||
// can be nil. | ||
func NewRecorderFactory(observerFactory rpc.ObserverFactory, recorder *auditlog.Recorder) rpc.RecorderFactory { | ||
return func() rpc.Recorder { | ||
return &combinedRecorder{ | ||
observer: observerFactory.RPCObserver(), | ||
recorder: recorder, | ||
} | ||
} | ||
} | ||
|
||
// combinedRecorder wraps an observer (which might be a multiplexer) | ||
// up with an auditlog recorder into an rpc.Recorder. | ||
type combinedRecorder struct { | ||
observer rpc.Observer | ||
recorder *auditlog.Recorder | ||
} | ||
|
||
// ServerRequest implements rpc.Recorder. | ||
func (cr *combinedRecorder) ServerRequest(hdr *rpc.Header, body interface{}) error { | ||
cr.observer.ServerRequest(hdr, body) | ||
if cr.recorder == nil { | ||
return nil | ||
} | ||
// TODO(babbageclunk): make this configurable. | ||
jsonArgs, err := json.Marshal(body) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
return errors.Trace(cr.recorder.AddRequest(auditlog.RequestArgs{ | ||
RequestID: hdr.RequestId, | ||
Facade: hdr.Request.Type, | ||
Method: hdr.Request.Action, | ||
Version: hdr.Request.Version, | ||
Args: string(jsonArgs), | ||
})) | ||
} | ||
|
||
// ServerReply implements rpc.Recorder. | ||
func (cr *combinedRecorder) ServerReply(req rpc.Request, replyHdr *rpc.Header, body interface{}) error { | ||
cr.observer.ServerReply(req, replyHdr, body) | ||
if cr.recorder == nil { | ||
return nil | ||
} | ||
var responseErrors []*auditlog.Error | ||
if replyHdr.Error == "" { | ||
var err error | ||
responseErrors, err = extractErrors(body) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
} else { | ||
responseErrors = []*auditlog.Error{{ | ||
Message: replyHdr.Error, | ||
Code: replyHdr.ErrorCode, | ||
}} | ||
} | ||
return errors.Trace(cr.recorder.AddResponse(auditlog.ResponseErrorsArgs{ | ||
RequestID: replyHdr.RequestId, | ||
Errors: responseErrors, | ||
})) | ||
} | ||
|
||
func extractErrors(body interface{}) ([]*auditlog.Error, error) { | ||
// TODO(babbageclunk): use reflection to find errors in the response body. | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.