-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
176 lines (152 loc) · 4.53 KB
/
Copy pathmain.go
File metadata and controls
176 lines (152 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"crypto/x509"
"errors"
"fmt"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/spf13/viper"
log "github.com/sirupsen/logrus"
)
var workpath = "/usr/local/bin/gorevoke"
var appVersion = "0.0.0"
var appBuild = "UNK"
var appBuildDate = "00000000-0000"
func init() {
log.SetFormatter(&log.TextFormatter{
QuoteEmptyFields: true,
FullTimestamp: true,
})
printver()
directory, err := filepath.Abs(filepath.Dir(os.Args[0])) //get the current working directory
if err != nil {
log.Fatal(err) //print the error if obtained
}
workpath = directory // set app working directory
// Set config file paths
viper.SetConfigName("gorevoke")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.gorevoke/")
viper.AddConfigPath("/etc/")
// Set defaults
viper.SetDefault("default.interval", 5)
viper.SetDefault("default.webserver", false)
viper.SetDefault("default.port", 4000)
viper.SetDefault("default.crldir", workpath + "/crl")
// Enable environment variable configuration
viper.AutomaticEnv()
viper.SetEnvPrefix("gorevoke")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
// Load config file
if configErr := viper.ReadInConfig(); configErr != nil {
if _, ok := configErr.(viper.ConfigFileNotFoundError); ok {
// Config file not found
log.Warn("no config file found, using defaults/environment")
} else {
// Config file was found but another error was produced
panic(fmt.Errorf("fatal error reading config file: %w", configErr))
}
}
// Initialize temp dir if not set
if !viper.IsSet("default.tmpdir") {
tmpdir, err := os.MkdirTemp("", "gorevoke-*")
if err != nil {
panic(fmt.Errorf("error creating temporary directory: %w", err))
}
viper.Set("default.tmpdir", tmpdir)
log.Info("Created temp directory for CRLs: ", tmpdir)
}
}
func main() {
// Catch SIGTERM in order to cleanup temp files
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
cleanup()
os.Exit(1)
}()
// Retrieve config values
crls := viper.GetStringMapString("crls")
refresh := viper.GetInt("default.interval")
webport := viper.GetString("default.port")
server := viper.GetBool("default.webserver")
tmpdir := viper.GetString("default.tmpdir")
crldir := viper.GetString("default.crldir")
if server {
go webserver(webport)
}
log.Info("CRLs in list: ", len(crls))
log.Info("Refresh interval: ", time.Duration(int(time.Second)*int(refresh)))
for {
for caId, caUrl := range crls {
var tmpfile string = tmpdir + "/" + caId + ".crl"
var httpfile string = crldir + "/" + caId + ".crl"
DownloadFile(tmpfile, caUrl) // Download CRL from remote
crlfile, err := os.ReadFile(tmpfile)
if err != nil {
log.Error("Problem opening downloaded file: ", err)
log.Info("Moving to next CRL entry.")
continue
} else {
crl, err := x509.ParseRevocationList(crlfile)
if err != nil {
log.Errorln("Skipping CRL: ", err)
continue
} else {
log.Infof("CRL %s is valid, issued by %s", caId, crl.Issuer.CommonName)
}
}
if _, err := os.Stat(httpfile); err == nil {
// file exists
log.Info("CRL already exists")
h1, err := getHash(tmpfile)
if err != nil {
log.Error("Error hashing: ", err)
}
h2, err2 := getHash(httpfile)
if err2 != nil {
log.Error("Error hashing: ", err2)
}
log.Debug(h1, h2, h1 == h2)
if h1 != h2 {
log.Info("File hashes do not match: ", h1, h2)
log.Info("Copying file to destination: ", httpfile)
copy(tmpfile, httpfile)
} else {
log.Info("No changes detected, proceeding.")
}
} else if errors.Is(err, os.ErrNotExist) {
// file does not exist
log.Info("CRL is new, copying to: ", httpfile)
bytes, err := copy(tmpfile, httpfile)
if err != nil {
log.Errorf("Error copying downloaded CRL to destination %s: %s", httpfile, err)
}
log.Debugf("Copied %d bytes from %s to %s", bytes, tmpfile, httpfile)
} else {
// catch anything else
return
}
}
time.Sleep(time.Duration(int(time.Second) * refresh)) // Defines time to sleep before repeating
}
}
func cleanup() {
tmpdir := viper.GetString("default.tmpdir")
log.Infof("Shutdown signal received, removing temp directory %s", tmpdir)
err := os.RemoveAll(tmpdir)
if err != nil {
log.Errorf("Could not remove %s: %s", tmpdir, err)
}
}
func printver() {
fmt.Printf("GoRevoke ver. %s\n", appVersion)
fmt.Printf("Build Type: %s\n", appBuild)
fmt.Printf("Build Date: %s\n", appBuildDate)
}