Skip to content

Commit

Permalink
fix: deepcopy
Browse files Browse the repository at this point in the history
  • Loading branch information
gi8lino committed Aug 7, 2023
1 parent 34762e0 commit 6b6d51d
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

const (
version = "v0.0.7"
version = "v0.0.8"
)

var cfgFile string
Expand Down
8 changes: 3 additions & 5 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"certalert/internal/server"

"github.com/fsnotify/fsnotify"
"github.com/jinzhu/copier"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -56,17 +55,16 @@ the configuration if changes are detected.
log.Fatalf("Unable to parse config: %s", err)
}

copier.Copy(&config.AppCopy, &config.App) // perform a deep copy
// update the config copy with the new values
if err := config.RedactConfig(&config.App); err != nil {
config.AppCopy = config.App.DeepCopy()
if err := config.RedactConfig(&config.AppCopy); err != nil {
log.Fatalf("Unable to redact config: %s", err)
}

})
viper.WatchConfig()

copier.Copy(&config.AppCopy, &config.App) // perform a deep copy
// this is only necessary if starting the web server
config.AppCopy = config.App.DeepCopy()
if err := config.RedactConfig(&config.AppCopy); err != nil {
log.Fatalf("Unable to redact config: %s", err)
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.20
require (
github.com/fsnotify/fsnotify v1.6.0
github.com/gorilla/mux v1.8.0
github.com/jinzhu/copier v0.3.5
github.com/kataras/tablewriter v0.0.0-20180708051242-e063d29b7c23
github.com/pavlo-v-chernykh/keystore-go/v4 v4.4.1
github.com/prometheus/client_golang v1.16.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jinzhu/copier v0.3.5 h1:GlvfUwHk62RokgqVNvYsku0TATCF7bAHVwEXoBh3iJg=
github.com/jinzhu/copier v0.3.5/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kataras/tablewriter v0.0.0-20180708051242-e063d29b7c23 h1:M8exrBzuhWcU6aoHJlHWPe4qFjVKzkMGRal78f5jRRU=
Expand Down
3 changes: 1 addition & 2 deletions internal/config/redact.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ func redactVariable(s string) string {
return "<REDACTED>"
}

// RedactConfig redacts sensitive data from the config
// RedactConfig redacts sensitive data from a config
func RedactConfig(config *Config) error {

if utils.HasKey(config.Pushgateway, "Address") {
config.Pushgateway.Address = redactVariable(config.Pushgateway.Address)
}
Expand Down
44 changes: 44 additions & 0 deletions internal/config/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package config

import "certalert/internal/certificates"

// DeepCopy returns a deep copy of the config
func (c Config) DeepCopy() Config {
// Copying basic types (like string, int, bool)
// is straightforward since they don't contain internal references.
var newConfig Config
newConfig.Server = Server{
Hostname: c.Server.Hostname,
Port: c.Server.Port,
}
newConfig.Pushgateway = Pushgateway{
Address: c.Pushgateway.Address,
InsecureSkipVerify: c.Pushgateway.InsecureSkipVerify,
Job: c.Pushgateway.Job,
Auth: Auth{
Basic: Basic{
Username: c.Pushgateway.Auth.Basic.Username,
Password: c.Pushgateway.Auth.Basic.Password,
},
Bearer: Bearer{
Token: c.Pushgateway.Auth.Bearer.Token,
},
},
}

// For slices, you'll want to ensure you're creating a new slice
// and copying each element (especially if they are structs).
newCerts := make([]certificates.Certificate, len(c.Certs))
for i, cert := range c.Certs {
newCerts[i] = certificates.Certificate{
Name: cert.Name,
Enabled: cert.Enabled, // Assuming bool pointers are okay to copy directly
Path: cert.Path,
Password: cert.Password,
Type: cert.Type,
}
}
newConfig.Certs = newCerts

return newConfig
}
71 changes: 71 additions & 0 deletions internal/config/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package config

import (
"certalert/internal/certificates"
"testing"
)

func TestDeepCopy(t *testing.T) {
// Sample configuration for testing.
original := Config{
Server: Server{
Hostname: "localhost",
Port: 8080,
},
Pushgateway: Pushgateway{
Address: "http://pushgateway.address",
Job: "testJob",
Auth: Auth{
Basic: Basic{
Username: "user",
Password: "pass",
},
Bearer: Bearer{
Token: "token",
},
},
},
Certs: []certificates.Certificate{
{
Name: "cert1",
Path: "/path/to/cert1",
Password: "cert1password",
Type: "jks",
},
{
Name: "cert2",
Path: "/path/to/cert2",
Password: "cert2password",
Type: "pem",
},
},
}

copy := original.DeepCopy()

// Check that the copy is not the same as the original.
if &copy == &original {
t.Errorf("Copy is the same as the original")
}

// modify the copy and check that the original is not modified
copy.Server.Hostname = "modified"
if original.Server.Hostname == copy.Server.Hostname {
t.Errorf("Original is modified")
}
copy.Certs[0].Name = "modified"
if original.Certs[0].Name == copy.Certs[0].Name {
t.Errorf("Original is modified")
}

//modify the original and check that the copy is not modified
original.Server.Hostname = "original"
if original.Server.Hostname == copy.Server.Hostname {
t.Errorf("Copy is modified")
}
original.Certs[0].Name = "original"
if original.Certs[0].Name == copy.Certs[0].Name {
t.Errorf("Copy is modified")
}

}
5 changes: 4 additions & 1 deletion internal/handlers/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package handlers

import (
"certalert/internal/config"
"log"
"net/http"

log "github.com/sirupsen/logrus"
)

// ReloadHandler is a handler function that reloads the application configuration
func ReloadHandler(w http.ResponseWriter, r *http.Request) {
log.Debugf("Force reloading configuration")

if err := config.ParseConfig(&config.App); err != nil {
log.Fatalf("Unable to parse config: %s", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down

0 comments on commit 6b6d51d

Please sign in to comment.