ocket8888 commented on a change in pull request #3534: TP Delivery Service Generate SSL update, new letsencrypt generate and… URL: https://github.com/apache/trafficcontrol/pull/3534#discussion_r339180240
########## File path: traffic_ops/traffic_ops_golang/deliveryservice/letsencryptcert.go ########## @@ -0,0 +1,305 @@ +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 ( + "bytes" + "context" + "crypto" + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "encoding/pem" + "errors" + "net/http" + "strconv" + "strings" + "time" + + "github.com/apache/trafficcontrol/lib/go-log" + "github.com/apache/trafficcontrol/lib/go-tc" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/auth" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/config" + "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/riaksvc" + "github.com/go-acme/lego/certcrypto" + "github.com/go-acme/lego/certificate" + "github.com/go-acme/lego/challenge" + "github.com/go-acme/lego/challenge/dns01" + "github.com/go-acme/lego/lego" + "github.com/go-acme/lego/registration" + "github.com/jmoiron/sqlx" +) + +type MyUser struct { + Email string + Registration *registration.Resource + key crypto.PrivateKey +} + +const LetsEncryptTimeout = time.Minute * 10 + +func (u *MyUser) GetEmail() string { + return u.Email +} + +func (u MyUser) GetRegistration() *registration.Resource { + return u.Registration +} + +func (u *MyUser) GetPrivateKey() crypto.PrivateKey { + return u.key +} + +type DNSProviderTrafficRouter struct { + db *sqlx.DB +} + +func NewDNSProviderTrafficRouter() *DNSProviderTrafficRouter { + return &DNSProviderTrafficRouter{} +} + +func (d *DNSProviderTrafficRouter) Timeout() (timeout, interval time.Duration) { + return LetsEncryptTimeout, time.Second * 30 +} + +func (d *DNSProviderTrafficRouter) Present(domain, token, keyAuth string) error { + tx, err := d.db.Begin() + fqdn, value := dns01.GetRecord(domain, keyAuth) + + q := `INSERT INTO dnschallenges (fqdn, record) VALUES ($1, $2)` + response, err := tx.Exec(q, fqdn, value) + tx.Commit() + if err != nil { + log.Errorf("Inserting dns txt record for fqdn '" + fqdn + "' record '" + value + "': " + err.Error()) + return errors.New("Inserting dns txt record for fqdn '" + fqdn + "' record '" + value + "': " + err.Error()) + } else { + rows, err := response.RowsAffected() + if err != nil { + log.Errorf("Determining rows affected dns txt record for fqdn '" + fqdn + "' record '" + value + "': " + err.Error()) + return errors.New("Determining rows affected dns txt record for fqdn '" + fqdn + "' record '" + value + "': " + err.Error()) + } + if rows == 0 { + log.Errorf("Zero rows affected when inserting dns txt record for fqdn '" + fqdn + "' record '" + value + "': " + err.Error()) + return errors.New("Zero rows affected when inserting dns txt record for fqdn '" + fqdn + "' record '" + value + "': " + err.Error()) + } + } + + return nil +} + +func (d *DNSProviderTrafficRouter) CleanUp(domain, token, keyAuth string) error { + fqdn, value := dns01.GetRecord(domain, keyAuth) + tx, err := d.db.Begin() + + q := `DELETE FROM dnschallenges WHERE fqdn = $1 and record = $2` + response, err := tx.Exec(q, fqdn, value) + tx.Commit() + if err != nil { + log.Errorf("Deleting dns txt record for fqdn '" + fqdn + "' record '" + value + "': " + err.Error()) + return errors.New("Deleting dns txt record for fqdn '" + fqdn + "' record '" + value + "': " + err.Error()) + } else { + rows, err := response.RowsAffected() + if err != nil { + log.Errorf("Determining rows affected when deleting dns txt record for fqdn '" + fqdn + "' record '" + value + "': " + err.Error()) + return errors.New("Determining rows affected when deleting dns txt record for fqdn '" + fqdn + "' record '" + value + "': " + err.Error()) + } + if rows == 0 { + log.Errorf("Zero rows affected when deleting dns txt record for fqdn '" + fqdn + "' record '" + value + "': " + err.Error()) + return errors.New("Zero rows affected when deleting dns txt record for fqdn '" + fqdn + "' record '" + value + "': " + err.Error()) + } + } + + return nil +} + +func GenerateLetsEncryptCertificates(w http.ResponseWriter, r *http.Request) { + inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil) + if userErr != nil || sysErr != nil { + api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr) + return + } + defer inf.Close() + + ctx, _ := context.WithTimeout(r.Context(), LetsEncryptTimeout) + + req := tc.DeliveryServiceLetsEncryptSSLKeysReq{} + if err := api.Parse(r.Body, nil, &req); err != nil { + api.HandleErr(w, r, nil, http.StatusBadRequest, errors.New("parsing request: "+err.Error()), nil) + return + } + if *req.DeliveryService == "" { + req.DeliveryService = req.Key + } + + go GetLetsEncryptCertificates(inf.Config, req, ctx, inf.User) + + api.WriteRespAlert(w, r, tc.WarnLevel, "Beginning async call to Let's Encrypt for "+*req.DeliveryService+". This may take a few minutes.") Review comment: This seems more like an "info" level alert to me, tbh. Also, if you'd like, this is a good use case for [the `202 Accepted` HTTP status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/202). ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services