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



##########
File path: traffic_ops/app/db/reencrypt/reencrypt.go
##########
@@ -0,0 +1,406 @@
+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 (
+       
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/trafficvault/backends/postgres"
+
+       "github.com/jmoiron/sqlx"
+       _ "github.com/lib/pq"
+
+       "crypto/aes"
+       "encoding/base64"
+       "encoding/json"
+       "errors"
+       "flag"
+       "fmt"
+       "io/ioutil"
+       "os"
+       "path/filepath"
+       "time"
+)
+
+const PROPERTIES_FILE = "./reencrypt.conf"
+
+func main() {
+       previousKeyLocation := flag.String("previousKey", "", "The file path 
for the previous base64 encoded AES key.")
+       newKeyLocation := flag.String("newKey", "", "The file path for the new 
base64 encoded AES key.")
+       flag.Parse()
+
+       if previousKeyLocation == nil || *previousKeyLocation == "" {
+               fmt.Println("previousKey flag is required.")
+               os.Exit(0)
+       }
+       if newKeyLocation == nil || *newKeyLocation == "" {
+               fmt.Println("newKey flag is required.")
+               os.Exit(0)
+       }
+
+       newKey, err := readKey(*newKeyLocation)
+       if err != nil {
+               fmt.Println("reading newKey: ", err.Error())
+               os.Exit(0)
+       }
+
+       previousKey, err := readKey(*previousKeyLocation)
+       if err != nil {
+               fmt.Println("reading previousKey: ", err.Error())
+               os.Exit(0)
+       }
+
+       dbConfBytes, err := ioutil.ReadFile(PROPERTIES_FILE)
+       if err != nil {

Review comment:
       I think we should consider making this location configurable via a 
command line argument

##########
File path: traffic_ops/app/db/reencrypt/reencrypt.go
##########
@@ -0,0 +1,406 @@
+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 (
+       
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/trafficvault/backends/postgres"
+
+       "github.com/jmoiron/sqlx"
+       _ "github.com/lib/pq"
+
+       "crypto/aes"
+       "encoding/base64"
+       "encoding/json"
+       "errors"
+       "flag"
+       "fmt"
+       "io/ioutil"
+       "os"
+       "path/filepath"
+       "time"
+)
+
+const PROPERTIES_FILE = "./reencrypt.conf"
+
+func main() {
+       previousKeyLocation := flag.String("previousKey", "", "The file path 
for the previous base64 encoded AES key.")
+       newKeyLocation := flag.String("newKey", "", "The file path for the new 
base64 encoded AES key.")
+       flag.Parse()
+
+       if previousKeyLocation == nil || *previousKeyLocation == "" {
+               fmt.Println("previousKey flag is required.")
+               os.Exit(0)
+       }
+       if newKeyLocation == nil || *newKeyLocation == "" {
+               fmt.Println("newKey flag is required.")
+               os.Exit(0)
+       }
+
+       newKey, err := readKey(*newKeyLocation)
+       if err != nil {
+               fmt.Println("reading newKey: ", err.Error())
+               os.Exit(0)
+       }
+
+       previousKey, err := readKey(*previousKeyLocation)
+       if err != nil {
+               fmt.Println("reading previousKey: ", err.Error())
+               os.Exit(0)
+       }
+
+       dbConfBytes, err := ioutil.ReadFile(PROPERTIES_FILE)
+       if err != nil {
+               fmt.Println("reading db conf '", PROPERTIES_FILE, "': ", 
err.Error())
+               os.Exit(0)
+       }
+
+       pgCfg := Config{}
+       err = json.Unmarshal(dbConfBytes, &pgCfg)
+       if err != nil {
+               fmt.Println("unmarshalling '", PROPERTIES_FILE, "': ", 
err.Error())
+               os.Exit(0)
+       }
+
+       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 {
+               fmt.Println("opening database: " + err.Error())
+               os.Exit(0)
+       }
+
+       if err = reEncryptSslKeys(db, previousKey, newKey); err != nil {
+               fmt.Println("re-encrypting SSL Keys: ", err.Error())
+               os.Exit(0)
+       }
+       if err = reEncryptUrlSigKeys(db, previousKey, newKey); err != nil {
+               fmt.Println("re-encrypting URL Sig Keys: ", err.Error())
+               os.Exit(0)
+       }
+       if err = reEncryptUriSigningKeys(db, previousKey, newKey); err != nil {
+               fmt.Println("re-encrypting URI Signing Keys: ", err.Error())
+               os.Exit(0)
+       }
+       if err = reEncryptDNSSECKeys(db, previousKey, newKey); err != nil {
+               fmt.Println("re-encrypting DNSSEC Keys: ", err.Error())
+               os.Exit(0)
+       }
+
+       if err = updateKeyFile(previousKey, *previousKeyLocation, newKey); err 
!= nil {
+               fmt.Println("updating the key file: ", err.Error())
+               os.Exit(0)
+       }
+
+       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{}, errors.New("AES key cannot be decoded from 
base64")

Review comment:
       This should return the error as well, I think, to help diagnose the 
problem




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