| 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/tls" |
| 21 | "errors" |
| 22 | "time" |
| 23 | |
| 24 | "github.com/streadway/amqp" |
| 25 | |
| 26 | "github.com/sassoftware/relic/v7/config" |
| 27 | "github.com/sassoftware/relic/v7/lib/certloader" |
| 28 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 29 | ) |
| 30 | |
| 31 | // Publish audit record to a AMQP exchange |
| 32 | func (info *Info) Publish(aconf *config.AmqpConfig) error { |
| 33 | blob, err := info.Marshal() |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | msg := amqp.Publishing{ |
| 38 | DeliveryMode: amqp.Persistent, |
| 39 | Timestamp: time.Now(), |
| 40 | ContentType: "application/json", |
| 41 | Body: blob, |
| 42 | } |
| 43 | conn, err := Connect(aconf) |
| 44 | if err != nil { |
| 45 | return err |
| 46 | } |
| 47 | defer conn.Close() |
| 48 | ch, err := conn.Channel() |
| 49 | if err != nil { |
| 50 | return err |
| 51 | } |
| 52 | defer ch.Close() |
| 53 | if err := ch.ExchangeDeclare(aconf.ExchangeName(), amqp.ExchangeFanout, true, false, false, false, nil); err != nil { |
| 54 | return err |
| 55 | } |
| 56 | if err := ch.Confirm(false); err != nil { |
| 57 | return err |
| 58 | } |
| 59 | notify := ch.NotifyPublish(make(chan amqp.Confirmation, 1)) |
| 60 | if err := ch.Publish(aconf.ExchangeName(), aconf.RoutingKey(), false, false, msg); err != nil { |
| 61 | return err |
| 62 | } |
| 63 | confirm := <-notify |
| 64 | if !confirm.Ack { |
| 65 | return errors.New("message was NACKed") |
| 66 | } |
| 67 | return nil |
| 68 | } |
| 69 | |
| 70 | // Connect to the configured AMQP broker |
| 71 | func Connect(aconf *config.AmqpConfig) (*amqp.Connection, error) { |
| 72 | uri, err := amqp.ParseURI(aconf.URL) |
| 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | var tconf *tls.Config |
| 77 | var auth []amqp.Authentication |
| 78 | if uri.Scheme == "amqps" { |
| 79 | tconf = &tls.Config{} |
| 80 | if aconf.CaCert != "" { |
| 81 | if err := x509tools.LoadCertPool(aconf.CaCert, tconf); err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | } |
| 85 | if aconf.CertFile != "" { |
| 86 | cert, err := certloader.LoadX509KeyPair(aconf.CertFile, aconf.KeyFile) |
| 87 | if err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | tconf.Certificates = []tls.Certificate{cert.TLS()} |
| 91 | } |
| 92 | x509tools.SetKeyLogFile(tconf) |
| 93 | if len(tconf.Certificates) != 0 { |
| 94 | auth = append(auth, externalAuth{}) |
| 95 | } |
| 96 | } |
| 97 | if uri.Password != "" { |
| 98 | auth = append(auth, uri.PlainAuth()) |
| 99 | } |
| 100 | qconf := amqp.Config{SASL: auth, TLSClientConfig: tconf} |
| 101 | return amqp.DialConfig(aconf.URL, qconf) |
| 102 | } |
| 103 | |
| 104 | type externalAuth struct{} |
| 105 | |
| 106 | func (externalAuth) Mechanism() string { return "EXTERNAL" } |
| 107 | func (externalAuth) Response() string { return "" } |