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



##########
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 {
+               die("updating the key file: " + err.Error())
+       }
+
+       fmt.Println("Successfully re-encrypted keys for SSL Keys, URL Sig Keys, 
URI Signing Keys, and DNSSEC Keys.")
+}
+
+type Config struct {
+       DBName   string `json:"dbname"`
+       Hostname string `json:"hostname"`
+       User     string `json:"user"`
+       Password string `json:"password"`
+       Port     int    `json:"port"`
+       SSL      bool   `json:"ssl"`
+}
+
+func readKey(keyLocation string) ([]byte, error) {
+       var keyBase64 string
+       keyBase64Bytes, err := ioutil.ReadFile(keyLocation)
+       if err != nil {
+               return []byte{}, errors.New("reading file '" + keyLocation + 
"':" + err.Error())
+       }
+       keyBase64 = string(keyBase64Bytes)
+
+       key, err := base64.StdEncoding.DecodeString(keyBase64)
+       if err != nil {
+               return []byte{}, fmt.Errorf("AES key cannot be decoded from 
base64: %w", err)
+       }
+
+       // verify the key works
+       _, err = aes.NewCipher(key)
+       if err != nil {
+               return []byte{}, err
+       }
+
+       return key, nil
+}
+
+func reEncryptSslKeys(tx *sql.Tx, previousKey []byte, newKey []byte) error {
+       rows, err := tx.Query("SELECT id, data FROM sslkey")
+       if err != nil {
+               return fmt.Errorf("querying: %w", err)
+       }
+       defer rows.Close()
+
+       sslKeyMap := map[int][]byte{}
+
+       for rows.Next() {
+               id := 0
+               var encryptedSslKeys []byte
+               if err = rows.Scan(&id, &encryptedSslKeys); err != nil {
+                       return fmt.Errorf("getting SSL Keys: %w", err)
+               }
+               jsonKeys, err := util.AESDecrypt(encryptedSslKeys, previousKey)

Review comment:
       So as I was using this tool, I had a thought: what if we accidentally 
mixed up the keys -- e.g. passed the new key as `previous-key` and the old key 
as `new-key`? I think this might happily decrypt the data using the wrong key, 
which would result in garbage data, then re-encrypt the garbage data with the 
new key. I think we could reverse ourselves out of that situation if it 
happened, but we should probably include sanity-checks here between decrypting 
and re-encrypting. For example, making sure the decrypted data is actually 
JSON. Maybe we should try unmarshalling it into its struct and checking that 
the resulting struct isn't all empty?

##########
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:
       So after trying this out, I think this behavior might be a little 
surprising to the user. If there was only one copy of the original key in 
existence (which would be crazy I know), running this tool would erase it, 
making it impossible to roll back. Should we remove this step? It's easy enough 
to manually `mv` the files after.




-- 
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