rawlinp commented on a change in pull request #5913:
URL: https://github.com/apache/trafficcontrol/pull/5913#discussion_r651309838



##########
File path: traffic_ops/app/db/reencrypt/reencrypt.go
##########
@@ -0,0 +1,370 @@
+/*
+
+Name
+       reencrypt
+
+Synopsis
+       reencrypt [--previous-key value] [--new-key value] [--cfg value]
+
+Description
+  The reencrypt app is used to re-encrypt all data in the Postgres Traffic 
Vault
+  using a new base64-encoded AES key.
+
+Options
+       --previous-key
+        The file path for the previous base64-encoded AES key.
+
+       --new-key
+        The file path for the new base64-encoded AES key.
+
+       --cfg
+        The path for the configuration file. Default is `./reencrypt.conf`.
+
+*/
+
+package main
+
+/*
+ * 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 (
+       "crypto/aes"
+       "database/sql"
+       "encoding/base64"
+       "encoding/json"
+       "errors"
+       "flag"
+       "fmt"
+       "io/ioutil"
+       "os"
+       "path/filepath"
+       "time"
+
+       "github.com/apache/trafficcontrol/lib/go-util"
+
+       "github.com/jmoiron/sqlx"
+       _ "github.com/lib/pq"
+)
+
+const PROPERTIES_FILE = "./reencrypt.conf"
+
+func main() {
+       previousKeyLocation := flag.String("previous-key", 
"/opt/traffic_ops/app/conf/aes.key", "(Optional) The file path for the previous 
base64 encoded AES key. Default is /opt/traffic_ops/app/conf/aes.key.")
+       newKeyLocation := flag.String("new-key", 
"/opt/traffic_ops/app/conf/new.key", "(Optional) The file path for the new 
base64 encoded AES key. Default is /opt/traffic_ops/app/conf/new.key.")
+       cfg := flag.String("cfg", PROPERTIES_FILE, "(Optional) The path for the 
configuration file. Default is "+PROPERTIES_FILE+".")
+       help := flag.Bool("help", false, "(Optional) Print usage information 
and exit.")
+       flag.Parse()
+
+       if *help {
+               flag.Usage()
+               os.Exit(0)
+       }
+
+       newKey, err := readKey(*newKeyLocation)
+       if err != nil {
+               die("reading new-key: " + err.Error())
+       }
+
+       previousKey, err := readKey(*previousKeyLocation)
+       if err != nil {
+               die("reading previous-key: " + err.Error())
+       }
+
+       dbConfBytes, err := ioutil.ReadFile(*cfg)
+       if err != nil {
+               die("reading db conf '" + *cfg + "': " + err.Error())
+       }
+
+       pgCfg := Config{}
+       err = json.Unmarshal(dbConfBytes, &pgCfg)
+       if err != nil {
+               die("unmarshalling '" + *cfg + "': " + err.Error())
+       }
+
+       sslStr := "require"
+       if !pgCfg.SSL {
+               sslStr = "disable"
+       }
+       db, err := sqlx.Open("postgres", 
fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s&fallback_application_name=trafficvault",
 pgCfg.User, pgCfg.Password, pgCfg.Hostname, pgCfg.Port, pgCfg.DBName, sslStr))
+       if err != nil {
+               die("opening database: " + err.Error())
+       }
+
+       tx, err := db.Begin()
+       if err != nil {
+               die(fmt.Sprintf("transaction begin failed %v %v ", err, tx))
+       }
+       defer tx.Commit()
+
+       if err = reEncryptSslKeys(tx, previousKey, newKey); err != nil {
+               tx.Rollback()
+               die("re-encrypting SSL Keys: " + err.Error())
+       }
+       if err = reEncryptUrlSigKeys(tx, previousKey, newKey); err != nil {
+               tx.Rollback()
+               die("re-encrypting URL Sig Keys: " + err.Error())
+       }
+       if err = reEncryptUriSigningKeys(tx, previousKey, newKey); err != nil {
+               tx.Rollback()
+               die("re-encrypting URI Signing Keys: " + err.Error())
+       }
+       if err = reEncryptDNSSECKeys(tx, previousKey, newKey); err != nil {
+               tx.Rollback()
+               die("re-encrypting DNSSEC Keys: " + err.Error())
+       }
+
+       if err = updateKeyFile(previousKey, *previousKeyLocation, newKey); err 
!= nil {

Review comment:
       Oh, I remember reading that in the code on my first review but forgot 
about that. I think doing an `mv` after is ok, then we don't also have to 
remove old backup key files later. If we were to automate this, we'd probably 
wrap it in a script that generates a new key and handles key backup and other 
stuff. Then re-encryption would basically be a single button.




-- 
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:
[email protected]


Reply via email to