Debian Codegraph @

lib/audit/audit.go

raw symbols SCIP
1 //
2 // Copyright (c) SAS Institute Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 package audit
18
19 import (
20 "crypto"
21 "crypto/x509"
22 "encoding/base64"
23 "encoding/json"
24 "errors"
25 "fmt"
26 "os"
27 "strings"
28 "time"
29
30 "golang.org/x/crypto/openpgp"
31
32 "github.com/rs/zerolog"
33 "github.com/sassoftware/relic/v7/lib/pgptools"
34 "github.com/sassoftware/relic/v7/lib/pkcs9"
35 "github.com/sassoftware/relic/v7/lib/x509tools"
36 )
37
38 type Info struct {
39 Attributes map[string]interface{}
40 StartTime time.Time
41 }
42
43 // Create a new audit record, starting with the given key name, signature type,
44 // and digest
45 func New(keyName, sigType string, hash crypto.Hash) *Info {
46 now := time.Now().UTC()
47 a := make(map[string]interface{})
48 a["sig.type"] = sigType
49 a["sig.keyname"] = keyName
50 a["sig.hash"] = x509tools.HashNames[hash]
51 a["sig.timestamp"] = now
52 if hostname, _ := os.Hostname(); hostname != "" {
53 a["sig.hostname"] = hostname
54 }
55 return &Info{Attributes: a, StartTime: now}
56 }
57
58 // Set a PGP certificate for this audit record
59 func (info *Info) SetPgpCert(entity *openpgp.Entity) {
60 info.Attributes["sig.pgp.fingerprint"] = fmt.Sprintf("%x", entity.PrimaryKey.Fingerprint[:])
61 info.Attributes["sig.pgp.entity"] = pgptools.EntityName(entity)
62 }
63
64 // Set a X509 certificate for this audit record
65 func (info *Info) SetX509Cert(cert *x509.Certificate) {
66 info.Attributes["sig.x509.subject"] = x509tools.FormatSubject(cert)
67 info.Attributes["sig.x509.issuer"] = x509tools.FormatIssuer(cert)
68 d := crypto.SHA1.New()
69 d.Write(cert.Raw)
70 info.Attributes["sig.x509.fingerprint"] = fmt.Sprintf("%x", d.Sum(nil))
71 }
72
73 // Override the default timestamp for this audit record
74 func (info *Info) SetTimestamp(t time.Time) {
75 info.Attributes["sig.timestamp"] = t.UTC()
76 }
77
78 // Add a PKCS#9 timestamp (counter-signature) to this audit record
79 func (info *Info) SetCounterSignature(cs *pkcs9.CounterSignature) {
80 if cs == nil {
81 return
82 }
83 info.Attributes["sig.ts.timestamper"] = x509tools.FormatSubject(cs.Certificate)
84 info.Attributes["sig.ts.timestamp"] = cs.SigningTime
85 info.Attributes["sig.ts.hash"] = x509tools.HashNames[cs.Hash]
86 }
87
88 // Set the MIME type (Content-Type) that the server will use when returning a
89 // result to the client. This is not the MIME type of the package being signed.
90 func (info *Info) SetMimeType(mimeType string) {
91 info.Attributes["content-type"] = mimeType
92 }
93
94 // Get the MIME type that the server will use when returning a result to the
95 // client. This is not the MIME type of the package being signed.
96 func (info *Info) GetMimeType() string {
97 v := info.Attributes["content-type"]
98 if v != nil {
99 return v.(string)
100 }
101 return "application/octet-stream"
102 }
103
104 // Marshal the audit record to JSON
105 func (info *Info) Marshal() ([]byte, error) {
106 if info.Attributes["perf.elapsed.ms"] == nil && !info.StartTime.IsZero() {
107 info.Attributes["perf.elapsed.ms"] = time.Since(info.StartTime).Nanoseconds() / 1e6
108 }
109 return json.Marshal(info.Attributes)
110 }
111
112 func (info *Info) AttrsForLog(prefix string) *zerolog.Event {
113 ev := zerolog.Dict()
114 for name, value := range info.Attributes {
115 if !strings.HasPrefix(name, prefix) {
116 continue
117 }
118 name = name[len(prefix):]
119 if s, ok := value.(string); ok {
120 ev.Str(name, s)
121 } else {
122 ev.Interface(name, s)
123 }
124 }
125 return ev
126 }
127
128 // Parse audit data from a JSON blob
129 func Parse(blob []byte) (*Info, error) {
130 if len(blob) == 0 {
131 return nil, errors.New("missing attributes")
132 }
133 info := new(Info)
134 if err := json.Unmarshal(blob, &info.Attributes); err != nil {
135 return nil, err
136 }
137 if sealed := info.Attributes["attributes"]; sealed != nil {
138 blob, err := base64.StdEncoding.DecodeString(sealed.(string))
139 if err != nil {
140 return nil, err
141 }
142 info.Attributes = nil
143 if err := json.Unmarshal(blob, &info.Attributes); err != nil {
144 return nil, err
145 }
146 }
147 return info, nil
148 }