github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/state/internal/audit/audit.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package audit
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/audit"
    10  	"github.com/juju/version"
    11  
    12  	"github.com/juju/juju/mongo/utils"
    13  )
    14  
    15  // auditEntryDoc is the doc that is persisted to the audit collection.
    16  type auditEntryDoc struct {
    17  
    18  	// JujuServerVersion is the version of jujud that recorded this
    19  	// entry.
    20  	JujuServerVersion version.Number `bson:"juju-server-version"`
    21  
    22  	// ModelID is the ID of the model the audit entry was written on.
    23  	ModelUUID string `bson:"model-uuid"`
    24  
    25  	// Timestamp is when the audit entry was written. It is marshaled
    26  	// to a bytestream via time.Time::MarshalText and can be
    27  	// unmarshaled via time.Time::UnmarshalText.
    28  	Timestamp string `bson:"timestamp"`
    29  
    30  	// RemoteAddress is the IP of the machine from which the
    31  	// audit-event was triggered.
    32  	RemoteAddress string `bson:"remote-address"`
    33  
    34  	// OriginType is the type of entity (e.g. model, user, action)
    35  	// which triggered the audit event.
    36  	OriginType string `bson:"origin-type"`
    37  
    38  	// OriginName is the name of the origin which triggered the
    39  	// audit-event.
    40  	OriginName string `bson:"origin-name"`
    41  
    42  	// Operation is the operation that was performed that triggered
    43  	// the audit event.
    44  	Operation string `bson:"operation"`
    45  
    46  	// Data is a catch-all for storing random data.
    47  	Data map[string]interface{} `bson:"data"`
    48  }
    49  
    50  // PutAuditEntryFn creates a closure which when passed an AuditEntry
    51  // will write it to the audit collection.
    52  func PutAuditEntryFn(
    53  	collectionName string,
    54  	insertDoc func(string, ...interface{}) error,
    55  ) func(audit.AuditEntry) error {
    56  	return func(auditEntry audit.AuditEntry) error {
    57  		if err := auditEntry.Validate(); err != nil {
    58  			return errors.Trace(err)
    59  		}
    60  		auditEntryDoc, err := auditEntryDocFromAuditEntry(auditEntry)
    61  		if err != nil {
    62  			return errors.Trace(err)
    63  		}
    64  		return errors.Trace(insertDoc(collectionName, auditEntryDoc))
    65  	}
    66  }
    67  
    68  func auditEntryDocFromAuditEntry(auditEntry audit.AuditEntry) (auditEntryDoc, error) {
    69  
    70  	timeAsBlob, err := auditEntry.Timestamp.MarshalText()
    71  	if err != nil {
    72  		return auditEntryDoc{}, errors.Trace(err)
    73  	}
    74  
    75  	return auditEntryDoc{
    76  		JujuServerVersion: auditEntry.JujuServerVersion,
    77  		ModelUUID:         auditEntry.ModelUUID,
    78  		Timestamp:         string(timeAsBlob),
    79  		RemoteAddress:     auditEntry.RemoteAddress,
    80  		OriginType:        auditEntry.OriginType,
    81  		OriginName:        auditEntry.OriginName,
    82  		Operation:         auditEntry.Operation,
    83  		Data:              utils.EscapeKeys(auditEntry.Data),
    84  	}, nil
    85  }