-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanitnya.go
138 lines (121 loc) · 3.11 KB
/
anitnya.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"crypto/tls"
"crypto/x509"
"io"
"log"
"os"
"github.com/streadway/amqp"
)
var l = log.New(os.Stdout, "[anitnya] ", 0)
type FedoraAuth struct {
}
func (auth *FedoraAuth) Mechanism() string {
return "EXTERNAL"
}
func (auth *FedoraAuth) Response() string {
return ""
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil { return true, nil }
if os.IsNotExist(err) { return false, nil }
return false, err
}
func readf(path string) chan []byte {
ch := make(chan []byte)
go func() {
f, err := os.Open(path)
if err != nil {
l.Fatal(err)
}
defer f.Close()
s, err := io.ReadAll(f)
if err != nil {
l.Fatal(err)
}
ch <- s
}()
return ch
}
func init_tls() (tls.Certificate, *x509.CertPool) {
x, err := exists("/etc/fedora-messaging/")
if err != nil {
l.Println("Fail to check /etc/fedora-messaging/: " + err.Error())
}
if x {
l.Println("using keys and certs in /etc/fedora-messaging/")
f1 := readf("/etc/fedora-messaging/fedora-cert.pem")
f2 := readf("/etc/fedora-messaging/fedora-key.pem")
f3 := readf("/etc/fedora-messaging/cacert.pem")
cert, err := tls.X509KeyPair(<-f1, <-f2)
if err != nil {
l.Fatal(err)
}
l.Println("cfg cacert")
certpool := x509.NewCertPool()
certpool.AppendCertsFromPEM(<-f3)
return cert, certpool
}
l.Println("dl cert, prikey, cacert")
dl1 := dl("https://raw.githubusercontent.com/fedora-infra/fedora-messaging/stable/configs/fedora-cert.pem")
dl2 := dl("https://raw.githubusercontent.com/fedora-infra/fedora-messaging/stable/configs/fedora-key.pem")
dl3 := dl("https://raw.githubusercontent.com/fedora-infra/fedora-messaging/stable/configs/cacert.pem")
cert, err := tls.X509KeyPair([]byte(<-dl1), []byte(<-dl2))
if err != nil {
l.Fatal(err)
}
l.Println("cfg cacert")
certpool := x509.NewCertPool()
certpool.AppendCertsFromPEM([]byte(<-dl3))
return cert, certpool
}
func anitnya_conn() {
cert, certpool := init_tls()
l.Println("conn")
conn, err := amqp.DialConfig("amqps://rabbitmq.fedoraproject.org/%2Fpublic_pubsub", amqp.Config{
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: certpool,
},
SASL: []amqp.Authentication{
&FedoraAuth{},
},
})
if err != nil {
l.Fatal(err)
}
l.Println("ch grab")
ch, err := conn.Channel()
if err != nil {
l.Fatal(err)
}
defer conn.Close()
// "org.release-monitoring.prod.anitya.project.version.update.v2", "org.fedoraproject.prod.copr"
for _, topic := range []string{"org.fedoraproject.prod.ci.productmd-compose.test.complete"} {
l.Println("xdcl " + topic)
err = ch.ExchangeDeclare(topic, "topic", true, false, false, false, nil)
if err != nil {
l.Fatal(err)
}
}
qname := os.Getenv("QUEUE_UUID")
l.Println("qdcl " + qname)
_, err = ch.QueueDeclare(qname, true, false, true, false, nil)
if err != nil {
l.Fatal(err)
}
l.Println("ch consume")
msgs, err := ch.Consume(qname, "", true, false, false, false, nil)
if err != nil {
l.Fatal(err)
}
forever := make(chan bool, 1)
go func() {
for d := range msgs {
l.Printf("| %s\n", d.Body)
}
}()
l.Println("conn ok nya~")
<-forever
}