mattjackson220 commented on a change in pull request #5913: URL: https://github.com/apache/trafficcontrol/pull/5913#discussion_r651299117
########## 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: probably worth putting those checks in anyway though. its too important of data to risk -- 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]
