Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remote_port in the audit logs when it is available #12790

Merged
merged 5 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions audit/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func (f *AuditFormatter) FormatRequest(ctx context.Context, w io.Writer, config
Data: req.Data,
PolicyOverride: req.PolicyOverride,
RemoteAddr: getRemoteAddr(req),
RemotePort: getRemotePort(req),
ReplicationCluster: req.ReplicationCluster,
Headers: req.Headers,
ClientCertificateSerialNumber: getClientCertificateSerialNumber(connState),
Expand Down Expand Up @@ -284,6 +285,7 @@ func (f *AuditFormatter) FormatResponse(ctx context.Context, w io.Writer, config
Data: req.Data,
PolicyOverride: req.PolicyOverride,
RemoteAddr: getRemoteAddr(req),
RemotePort: getRemotePort(req),
ClientCertificateSerialNumber: getClientCertificateSerialNumber(connState),
ReplicationCluster: req.ReplicationCluster,
Headers: req.Headers,
Expand Down Expand Up @@ -346,6 +348,7 @@ type AuditRequest struct {
Data map[string]interface{} `json:"data,omitempty"`
PolicyOverride bool `json:"policy_override,omitempty"`
RemoteAddr string `json:"remote_address,omitempty"`
RemotePort int `json:"remote_port,omitempty"`
HridoyRoy marked this conversation as resolved.
Show resolved Hide resolved
WrapTTL int `json:"wrap_ttl,omitempty"`
Headers map[string][]string `json:"headers,omitempty"`
ClientCertificateSerialNumber string `json:"client_certificate_serial_number,omitempty"`
Expand Down Expand Up @@ -406,6 +409,14 @@ func getRemoteAddr(req *logical.Request) string {
return ""
}

// getRemotePort safely gets the remote port avoiding a nil pointer
func getRemotePort(req *logical.Request) int {
HridoyRoy marked this conversation as resolved.
Show resolved Hide resolved
if req != nil && req.Connection != nil {
return req.Connection.RemotePort
}
return 0
}

func getClientCertificateSerialNumber(connState *tls.ConnectionState) string {
if connState == nil || len(connState.VerifiedChains) == 0 || len(connState.VerifiedChains[0]) == 0 {
return ""
Expand Down
3 changes: 3 additions & 0 deletions changelog/12790.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
audit: The audit logs now contain the port used by the client
```
9 changes: 8 additions & 1 deletion http/logical.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,14 +491,21 @@ WRITE_RESPONSE:
// attaching to a logical request
func getConnection(r *http.Request) (connection *logical.Connection) {
var remoteAddr string
var remotePort int

remoteAddr, _, err := net.SplitHostPort(r.RemoteAddr)
remoteAddr, port, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
remoteAddr = ""
} else {
remotePort, err = strconv.Atoi(port)
HridoyRoy marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
remotePort = 0
}
}

connection = &logical.Connection{
RemoteAddr: remoteAddr,
RemotePort: remotePort,
ConnState: r.TLS,
}
return
Expand Down
90 changes: 90 additions & 0 deletions http/logical_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
"testing"
"time"

kv "github.com/hashicorp/vault-plugin-secrets-kv"
"github.com/hashicorp/vault/api"
auditFile "github.com/hashicorp/vault/builtin/audit/file"
"github.com/hashicorp/vault/internalshared/configutil"
"github.com/hashicorp/vault/sdk/helper/consts"
"github.com/hashicorp/vault/sdk/helper/logging"
Expand Down Expand Up @@ -499,3 +502,90 @@ func TestLogical_ShouldParseForm(t *testing.T) {
}
}
}

func TestLogical_AuditPort(t *testing.T) {
HridoyRoy marked this conversation as resolved.
Show resolved Hide resolved
coreConfig := &vault.CoreConfig{
LogicalBackends: map[string]logical.Factory{
"kv": kv.VersionedKVFactory,
},
AuditBackends: map[string]audit.Factory{
"file": auditFile.Factory,
},
}

cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
HandlerFunc: Handler,
})

cluster.Start()
defer cluster.Cleanup()

cores := cluster.Cores

core := cores[0].Core
c := cluster.Cores[0].Client
vault.TestWaitActive(t, core)

if err := c.Sys().Mount("kv/", &api.MountInput{
Type: "kv-v2",
}); err != nil {
t.Fatalf("kv-v2 mount attempt failed - err: %#v\n", err)
}

auditLogFile, err := ioutil.TempFile("", "httppatch")
remilapeyre marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Fatal(err)
}

err = c.Sys().EnableAuditWithOptions("file", &api.EnableAuditOptions{
Type: "file",
Options: map[string]string{
"file_path": auditLogFile.Name(),
},
})
if err != nil {
t.Fatalf("failed to enable audit file, err: %#v\n", err)
}

writeData := map[string]interface{}{
"data": map[string]interface{}{
"bar": "a",
},
}

resp, err := c.Logical().Write("kv/data/foo", writeData)
if err != nil {
t.Fatalf("write request failed, err: %#v, resp: %#v\n", err, resp)
}

decoder := json.NewDecoder(auditLogFile)

var auditRecord map[string]interface{}
count := 0
for decoder.Decode(&auditRecord) == nil {
count += 1

// Skip the first line
if count == 1 {
continue
}

auditRequest := map[string]interface{}{}

if req, ok := auditRecord["request"]; ok {
auditRequest = req.(map[string]interface{})
}

if _, ok := auditRequest["remote_address"].(string); !ok {
t.Fatalf("remote_port should be a number, not %T", auditRequest["remote_address"])
}

if _, ok := auditRequest["remote_port"].(float64); !ok {
t.Fatalf("remote_port should be a number, not %T", auditRequest["remote_port"])
}
}

if count != 4 {
t.Fatalf("wrong number of audit entries: %d", count)
}
}
3 changes: 3 additions & 0 deletions sdk/logical/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ type Connection struct {
// RemoteAddr is the network address that sent the request.
RemoteAddr string `json:"remote_addr"`

// RemotePort is the network port that sent the request.
RemotePort int `json:"remote_port"`

// ConnState is the TLS connection state if applicable.
ConnState *tls.ConnectionState `sentinel:""`
}
Loading