You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2021/06/14 07:14:18 UTC

[GitHub] [trafficcontrol] rawlinp commented on a change in pull request #5913: Re-encrypt Tool for TV Postgres

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



##########
File path: lib/go-util/encrypt.go
##########
@@ -0,0 +1,75 @@
+package util
+
+/*
+ * 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"
+	"crypto/cipher"
+	"crypto/rand"
+	"io"
+)
+
+// AESEncrypt encrypts a byte array using an AES key.
+// This returns an encrypted byte array and an error if the encryption was not successful.
+func AESEncrypt(bytesToEncrypt []byte, aesKey []byte) (string, error) {

Review comment:
       I noticed this function signature is slightly different than the same thing Steve is doing in [his PR](https://github.com/apache/trafficcontrol/pull/5924/files?file-filters%5B%5D=.go&file-filters%5B%5D=.md&file-filters%5B%5D=.rst&file-filters%5B%5D=dotfile#diff-8ad36dc80419697c937fba5d09e68f4cdeadcf84c685f92a3254615cd1141fe8R32). I think returning a string vs []byte may be the only difference. Should we just make this return []byte to match?

##########
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(1)
+	}
+
+	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 {

Review comment:
       Should this config be documented? It seems like it matches TO's `database.conf`, so we could just copy/paste?




-- 
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:
users@infra.apache.org