示例#1
0
        //~ func (f *JSONFormatWriter) WriteRequest(w io.Writer, req *AuditRequestEntry) error {
        public void WriteRequest(Stream w, AuditRequestEntry req)
        {
            //~ if req == nil {
            //~     return fmt.Errorf("request entry was nil, cannot encode")
            //~ }
            if (req == null)
            {
                throw new ArgumentNullException(nameof(req));
            }

            //~ if len(f.Prefix) > 0 {
            //~     _, err := w.Write([]byte(f.Prefix))
            //~     if err != nil {
            //~         return err
            //~     }
            //~ }
            if (!string.IsNullOrEmpty(Prefix))
            {
                w.Write(Prefix.ToUtf8Bytes());
            }

            //~ enc := json.NewEncoder(w)
            //~ return enc.Encode(req)
            w.Write(JsonConvert.SerializeObject(req).ToUtf8Bytes());
        }
示例#2
0
 //~ func (n *noopFormatWriter) WriteRequest(_ io.Writer, _ *AuditRequestEntry) error {
 //~     return nil
 //~ }
 public void WriteRequest(Stream s, AuditRequestEntry entry)
 {
 }
示例#3
0
        //~ func (f *AuditFormatter) FormatRequest(
        //~     w io.Writer,
        //~     config FormatterConfig,
        //~     auth *logical.Auth,
        //~     req *logical.Request,
        //~     inErr error) error {
        public void FormatRequest(Stream w, FormatterConfig config,
                                  Logical.Auth auth, Logical.Request req, Exception inErr)
        {
            //~ if req == nil {
            //~     return fmt.Errorf("request to request-audit a nil request")
            //~ }
            //~
            //~ if w == nil {
            //~     return fmt.Errorf("writer for audit request is nil")
            //~ }
            //~
            //~ if f.AuditFormatWriter == nil {
            //~     return fmt.Errorf("no format writer specified")
            //~ }
            if (req == null)
            {
                throw new ArgumentNullException(nameof(req));
            }
            if (w == null)
            {
                throw new ArgumentNullException(nameof(w));
            }
            //if (this.AuditFormatWriter == null)
            //	throw new InvalidOperationException("no format writer present");

            using (var defer = new Util.Defer())
            {
                //~ if !config.Raw {
                if (!config.Raw)
                {
                    // Before we copy the structure we must nil out some data
                    // otherwise we will cause reflection to panic and die
                    //~ if req.Connection != nil && req.Connection.ConnState != nil {
                    //~     origReq := req
                    //~     origState := req.Connection.ConnState
                    //~     req.Connection.ConnState = nil
                    //~     defer func() {
                    //~         origReq.Connection.ConnState = origState
                    //~     }()
                    //~ }
                    if (req?.Connection?.ConnectionState != null)
                    {
                        var origReq   = req;
                        var origState = req.Connection.ConnectionState;
                        req.Connection.ConnectionState = null;
                        defer.Add(() => origReq.Connection.ConnectionState = origState);
                    }

                    // Copy the auth structure
                    //~ if auth != nil {
                    //~     cp, err:= copystructure.Copy(auth)
                    //~     if err != nil {
                    //~         return err
                    //~     }
                    //~     auth = cp.(*logical.Auth)
                    //~ }
                    if (auth != null)
                    {
                        auth = auth.DeepCopy();
                    }

                    //~ cp, err:= copystructure.Copy(req)
                    //~ if err != nil {
                    //~     return err
                    //~ }
                    //~ req = cp.(*logical.Request)
                    req = req.DeepCopy();

                    // Hash any sensitive information
                    //~ if auth != nil {
                    //~     if err := Hash(config.Salt, auth); err != nil {
                    //~         return err
                    //~     }
                    //~ }
                    if (auth != null)
                    {
                        HashStructure.Hash(config.Salt, auth);
                    }

                    // Cache and restore accessor in the request
                    //~ var clientTokenAccessor string
                    //~ if !config.HMACAccessor && req != nil && req.ClientTokenAccessor != "" {
                    //~     clientTokenAccessor = req.ClientTokenAccessor
                    //~ }
                    //~ if err := Hash(config.Salt, req); err != nil {
                    //~     return err
                    //~ }
                    //~ if clientTokenAccessor != "" {
                    //~     req.ClientTokenAccessor = clientTokenAccessor
                    //~ }
                    var clientTokenAccessor = config.HMACAccessor
                                                ? null
                                                : req?.ClientTokenAccessor;
                    HashStructure.Hash(config.Salt, req);
                    if (!string.IsNullOrEmpty(clientTokenAccessor))
                    {
                        req.ClientTokenAccessor = clientTokenAccessor;
                    }
                }

                // If auth is nil, make an empty one
                //~ if auth == nil {
                //~     auth = new(logical.Auth)
                //~ }
                if (auth == null)
                {
                    auth = new Logical.Auth();
                }

                //~ var errString string
                //~ if inErr != nil {
                //~         errString = inErr.Error()
                //~ }
                string errString = null;
                if (inErr != null)
                {
                    errString = inErr.Message;
                }

                //~reqEntry:= &AuditRequestEntry{
                var reqEntry = new AuditRequestEntry
                {
                    //~ Type: "request",
                    //~ Error: errString,
                    //~
                    //~ Auth: AuditAuth{
                    //~     DisplayName: auth.DisplayName,
                    //~     Policies: auth.Policies,
                    //~     Metadata: auth.Metadata,
                    //~ },
                    Type  = "request",
                    Error = errString,
                    Auth  = new AuditAuth
                    {
                        DisplayName = auth.DisplayName,
                        Policies    = auth.Policies,
                        Metadata    = auth.Metadata,
                    },

                    //~ Request: AuditRequest{
                    //~     ID: req.ID,
                    //~     ClientToken: req.ClientToken,
                    //~     ClientTokenAccessor: req.ClientTokenAccessor,
                    //~     Operation: req.Operation,
                    //~     Path: req.Path,
                    //~     Data: req.Data,
                    //~     RemoteAddr: getRemoteAddr(req),
                    //~     ReplicationCluster: req.ReplicationCluster,
                    //~     Headers: req.Headers,
                    //~ },
                    Request = new AuditRequest
                    {
                        ID                  = req.ID,
                        ClientToken         = req.ClientToken,
                        ClientTokenAccessor = req.ClientTokenAccessor,
                        Operation           = req.Operation,
                        Path                = req.Path,
                        Data                = req.Data,
                        RemoteAddr          = GetRemoteAddr(req),
                        ReplicationCluster  = req.ReplicationCluster,
                        Headers             = req.Headers,
                    },
                };

                //~ if req.WrapInfo != nil {
                //~     reqEntry.Request.WrapTTL = int(req.WrapInfo.TTL / time.Second)
                //~ }
                if (req.WrapInfo != null)
                {
                    reqEntry.Request.WrapTTL = (int)req.WrapInfo.TTL.TotalSeconds;
                }

                //~ if !config.OmitTime {
                //~     reqEntry.Time = time.Now().UTC().Format(time.RFC3339)
                //~ }
                if (!config.OmitTime)
                {
                    reqEntry.Time = DateTime.UtcNow.FormatUtcAsRFC3339();
                }

                //~ return f.AuditFormatWriter.WriteRequest(w, reqEntry)
                this.WriteRequest(w, reqEntry);
            }
        }
示例#4
0
 //~ AuditFormatWriter
 public void WriteRequest(Stream s, AuditRequestEntry entry)
 {
     _writer.WriteRequest(s, entry);
 }