-
Notifications
You must be signed in to change notification settings - Fork 343
/
deleteoldcerts.go
203 lines (183 loc) · 7.21 KB
/
deleteoldcerts.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package deliveryservice
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import (
"context"
"database/sql"
"errors"
"strings"
"sync"
"time"
"github.com/apache/trafficcontrol/lib/go-log"
"github.com/apache/trafficcontrol/lib/go-tc"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/config"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/riaksvc"
"github.com/basho/riak-go-client"
)
// DeleteOldCerts asynchronously deletes HTTPS certificates in Riak which have no corresponding delivery service in the database.
//
// Note the delivery service may still be in the CRConfig! Therefore, this should only be called immediately after a CRConfig Snapshot.
//
// This creates a goroutine, and immediately returns. It returns an error if there was an error preparing the delete routine, such as an error creating a db transaction.
//
// Note because it is asynchronous, this may return a nil error, but the asynchronous goroutine may error when fetching or deleting the certificates. If such an error occurs, it will be logged to the error log.
//
// If certificate deletion is already being processed by a goroutine, another delete will be queued, and this immediately returns nil. Only one delete will ever be queued.
//
func DeleteOldCerts(db *sql.DB, tx *sql.Tx, cfg *config.Config, cdn tc.CDNName) error {
if !cfg.RiakEnabled {
log.Infoln("deleting old delivery service certificates: Riak is not enabled, returning without cleaning up old certificates.")
return nil
}
if db == nil {
return errors.New("nil db")
}
if cfg == nil {
return errors.New("nil config")
}
startOldCertDeleter(db, tx, time.Duration(cfg.DBQueryTimeoutSeconds)*time.Second, cfg.RiakAuthOptions, cfg.RiakPort, cdn)
cleanupOldCertDeleters(tx)
return nil
}
// deleteOldDSCerts deletes the HTTPS certificates in Riak of delivery services which have been deleted in Traffic Ops.
func deleteOldDSCerts(tx *sql.Tx, authOpts *riak.AuthOptions, riakPort *uint, cdn tc.CDNName) error {
dsKeys, err := riaksvc.GetCDNSSLKeysDSNames(tx, authOpts, riakPort, cdn)
if err != nil {
return errors.New("getting riak ds keys: " + err.Error())
}
dses, err := dbhelpers.GetCDNDSes(tx, cdn)
if err != nil {
return errors.New("getting ds names: " + err.Error())
}
successes := []string{}
failures := []string{}
for ds, riakKeys := range dsKeys {
if _, ok := dses[ds]; ok {
continue
}
for _, riakKey := range riakKeys {
err := riaksvc.DeleteDeliveryServicesSSLKey(tx, authOpts, riakPort, riakKey)
if err != nil {
log.Errorln("deleting Riak SSL keys for Delivery Service '" + string(ds) + "' key '" + riakKey + "': " + err.Error())
failures = append(failures, string(ds))
} else {
log.Infoln("Deleted Riak SSL keys for delivery service which has been deleted in the database '" + string(ds) + "' key '" + riakKey + "'")
successes = append(successes, string(ds))
}
}
}
if len(failures) > 0 {
return errors.New("successfully deleted Riak SSL keys for deleted dses [" + strings.Join(successes, ", ") + "], but failed to delete Riak SSL keys for [" + strings.Join(failures, ", ") + "]; see the error log for details")
}
return nil
}
// deleteOldDSCertsDB takes a db, and creates a transaction to pass to deleteOldDSCerts.
func deleteOldDSCertsDB(db *sql.DB, dbTimeout time.Duration, riakOpts *riak.AuthOptions, riakPort *uint, cdn tc.CDNName) {
dbCtx, cancelTx := context.WithTimeout(context.Background(), dbTimeout)
tx, err := db.BeginTx(dbCtx, nil)
if err != nil {
log.Errorln("Old Cert Deleter Job: beginning tx: " + err.Error())
return
}
defer cancelTx()
txCommit := false
defer dbhelpers.CommitIf(tx, &txCommit)
if err := deleteOldDSCerts(tx, riakOpts, riakPort, cdn); err != nil {
log.Errorln("deleting old DS certificates: " + err.Error())
return
}
txCommit = true
}
var oldCertDeleters = OldCertDeleters{D: map[tc.CDNName]*OldCertDeleter{}}
type OldCertDeleters struct {
D map[tc.CDNName]*OldCertDeleter
M sync.Mutex
}
// startOldCertDeleter tells the old cert deleter goroutine to start another delete job, creating the goroutine if it doesn't exist.
func startOldCertDeleter(db *sql.DB, tx *sql.Tx, dbTimeout time.Duration, riakOpts *riak.AuthOptions, riakPort *uint, cdn tc.CDNName) {
oldCertDeleter := getOrCreateOldCertDeleter(cdn)
oldCertDeleter.Once.Do(func() {
go doOldCertDeleter(oldCertDeleter.Start, oldCertDeleter.Die, db, dbTimeout, riakOpts, riakPort, cdn)
})
select {
case oldCertDeleter.Start <- struct{}{}:
default:
}
}
func getOrCreateOldCertDeleter(cdn tc.CDNName) *OldCertDeleter {
oldCertDeleters.M.Lock()
defer oldCertDeleters.M.Unlock()
oldCertDeleter, ok := oldCertDeleters.D[cdn]
if !ok {
oldCertDeleter = newOldCertDeleter()
oldCertDeleters.D[cdn] = oldCertDeleter
}
return oldCertDeleter
}
// cleanupOldCertDeleters stops all cert deleter goroutines for CDNs which have been deleted in the database.
// Any error is logged, but not returned.
// This is designed to be called when starting a new cert deleter job, to clean up any old cert deleters from deleted CDNs.
// This should only be called immediately when snapshotting, and immediately after startOldCertDeleter, because otherwise a cert deleter may be removed before it can delete old certs for a given CDN.
func cleanupOldCertDeleters(tx *sql.Tx) {
cdns, err := dbhelpers.GetCDNs(tx) // (map[tc.CDNName]struct{}, error) {
if err != nil {
log.Errorln("cleanupOldCertDeleters: getting cdns: " + err.Error())
return
}
oldCertDeleters.M.Lock()
defer oldCertDeleters.M.Unlock()
for cdn, oldCertDeleter := range oldCertDeleters.D {
if _, ok := cdns[cdn]; ok {
continue
}
delete(oldCertDeleters.D, cdn)
select {
case oldCertDeleter.Die <- struct{}{}:
default:
}
}
}
type OldCertDeleter struct {
Start chan struct{}
Die chan struct{}
Once sync.Once
}
func newOldCertDeleter() *OldCertDeleter {
return &OldCertDeleter{
Start: make(chan struct{}, 1),
Die: make(chan struct{}, 1),
}
}
func doOldCertDeleter(do chan struct{}, die chan struct{}, db *sql.DB, dbTimeout time.Duration, riakOpts *riak.AuthOptions, riakPort *uint, cdn tc.CDNName) {
for {
select {
case <-do:
deleteOldDSCertsDB(db, dbTimeout, riakOpts, riakPort, cdn)
case <-die:
// Go selects aren't ordered, so double-check the do chan in case a race happened and a job came in at the same time as the die.
select {
case <-do:
deleteOldDSCertsDB(db, dbTimeout, riakOpts, riakPort, cdn)
default:
}
return
}
}
}