Skip to content
Snippets Groups Projects
Unverified Commit 423a5845 authored by Max Adamo's avatar Max Adamo
Browse files

fix exit status

parent 55387217
No related branches found
No related tags found
No related merge requests found
...@@ -18,22 +18,40 @@ import ( ...@@ -18,22 +18,40 @@ import (
) )
var ( var (
appVersion string appVersion string
buildTime string buildTime string
CertBase string CertBase string
KeyBase string KeyBase string
GroupName string GroupName string
RedisBaseURL string RedisBaseURL string
VaultBaseURL string VaultBaseURL string
certificateDestination string certificateDestination string
fullchainDestination string fullchainDestination string
keyDestination string keyDestination string
caDestination string caDestination string
Type string Type string
tmpCertificateDestination = "/tmp/amce_cert.pem"
tmpFullchainDestination = "/tmp/amce_fullchain.pem"
tmpCaDestination = "/tmp/amce_ca.pem"
tmpKeyDestination = "/tmp/amce_key.pem"
tempCertSlice = []string{tmpCertificateDestination, tmpFullchainDestination, tmpCaDestination, tmpKeyDestination}
) )
// app exit
func appExit(status int) {
for _, element := range tempCertSlice {
err := os.Remove(element)
if err != nil {
}
}
os.Exit(status)
}
// check certificates // check certificates
func checkCerificates(dnsname string, certificate string, fullchain string, ca string, key string, days int, fail bool) bool { func checkCerificates(dnsname string, certificate string, fullchain string, ca string, key string, days int, fail bool) bool {
for _, element := range tempCertSlice {
fmt.Printf(element)
}
Seconds := days * 86400 Seconds := days * 86400
daysNumber := time.Now().Local().Add(time.Second * time.Duration(Seconds)) daysNumber := time.Now().Local().Add(time.Second * time.Duration(Seconds))
...@@ -43,7 +61,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s ...@@ -43,7 +61,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if err != nil { if err != nil {
if fail == true { if fail == true {
fmt.Printf("[ERROR] %v\n", err) fmt.Printf("[ERROR] %v\n", err)
os.Exit(255) appExit(255)
} else { } else {
return false return false
} }
...@@ -53,7 +71,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s ...@@ -53,7 +71,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if err != nil { if err != nil {
if fail == true { if fail == true {
fmt.Printf("[ERROR] %v\n", err) fmt.Printf("[ERROR] %v\n", err)
os.Exit(255) appExit(255)
} else { } else {
return false return false
} }
...@@ -63,7 +81,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s ...@@ -63,7 +81,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if err != nil { if err != nil {
if fail == true { if fail == true {
fmt.Printf("[ERROR] %v\n", err) fmt.Printf("[ERROR] %v\n", err)
os.Exit(255) appExit(255)
} else { } else {
return false return false
} }
...@@ -74,7 +92,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s ...@@ -74,7 +92,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if !ok { if !ok {
if fail == true { if fail == true {
fmt.Printf("[ERROR] failed to parse root certificate\n") fmt.Printf("[ERROR] failed to parse root certificate\n")
os.Exit(255) appExit(255)
} else { } else {
return false return false
} }
...@@ -84,7 +102,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s ...@@ -84,7 +102,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if block == nil { if block == nil {
if fail == true { if fail == true {
fmt.Printf("[ERROR] failed to parse certificate PEM\n") fmt.Printf("[ERROR] failed to parse certificate PEM\n")
os.Exit(255) appExit(255)
} else { } else {
return false return false
} }
...@@ -93,7 +111,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s ...@@ -93,7 +111,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if err != nil { if err != nil {
if fail == true { if fail == true {
fmt.Printf("[ERROR] failed to parse certificate %v\n", err) fmt.Printf("[ERROR] failed to parse certificate %v\n", err)
os.Exit(255) appExit(255)
} else { } else {
return false return false
} }
...@@ -103,7 +121,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s ...@@ -103,7 +121,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if fullchainBlock == nil { if fullchainBlock == nil {
if fail == true { if fail == true {
fmt.Printf("[ERROR] failed to parse certificate PEM\n") fmt.Printf("[ERROR] failed to parse certificate PEM\n")
os.Exit(255) appExit(255)
} else { } else {
return false return false
} }
...@@ -112,7 +130,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s ...@@ -112,7 +130,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if fullchainErr != nil { if fullchainErr != nil {
if fail == true { if fail == true {
fmt.Printf("[ERROR] failed to parse certificate %v\n", fullchainErr) fmt.Printf("[ERROR] failed to parse certificate %v\n", fullchainErr)
os.Exit(255) appExit(255)
} else { } else {
return false return false
} }
...@@ -128,7 +146,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s ...@@ -128,7 +146,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if _, err := cert.Verify(opts); err != nil { if _, err := cert.Verify(opts); err != nil {
if fail == true { if fail == true {
fmt.Printf("[ERROR] failed to parse certificate %v\n", err.Error()) fmt.Printf("[ERROR] failed to parse certificate %v\n", err.Error())
os.Exit(255) appExit(255)
} else { } else {
return false return false
} }
...@@ -154,7 +172,7 @@ func GetRedisKey(redisurl string, redistoken string) string { ...@@ -154,7 +172,7 @@ func GetRedisKey(redisurl string, redistoken string) string {
defer resp.Body.Close() defer resp.Body.Close()
if err != nil { if err != nil {
fmt.Printf("[ERROR] Fail to read %v: %v\n", redisurl, err) fmt.Printf("[ERROR] Fail to read %v: %v\n", redisurl, err)
os.Exit(255) appExit(255)
} }
return fmt.Sprintf(string(body)) return fmt.Sprintf(string(body))
} }
...@@ -169,7 +187,7 @@ func GetVaultKey(vaulturl string, vaulttoken string) string { ...@@ -169,7 +187,7 @@ func GetVaultKey(vaulturl string, vaulttoken string) string {
defer resp.Body.Close() defer resp.Body.Close()
if err != nil { if err != nil {
fmt.Printf("[ERROR] Fail to read %v: %v\n", vaulturl, err) fmt.Printf("[ERROR] Fail to read %v: %v\n", vaulturl, err)
os.Exit(255) appExit(255)
} }
return gjson.Get(string(body), "data.value").String() return gjson.Get(string(body), "data.value").String()
} }
...@@ -185,7 +203,7 @@ func WriteToFile(content string, destination string, groupname string, filemode ...@@ -185,7 +203,7 @@ func WriteToFile(content string, destination string, groupname string, filemode
file, err := os.OpenFile(destination, os.O_WRONLY|os.O_CREATE, filemode) file, err := os.OpenFile(destination, os.O_WRONLY|os.O_CREATE, filemode)
if err != nil { if err != nil {
fmt.Printf("[ERROR] %v cannot be created\n", destination) fmt.Printf("[ERROR] %v cannot be created\n", destination)
os.Exit(255) appExit(255)
} }
fmt.Fprintf(file, "%v\n", content) fmt.Fprintf(file, "%v\n", content)
...@@ -252,13 +270,12 @@ Options: ...@@ -252,13 +270,12 @@ Options:
--ca-destination=CADESTINATION CA Destination [default: %v/COMODO_<type>.crt] --ca-destination=CADESTINATION CA Destination [default: %v/COMODO_<type>.crt]
`, CertBase, CertBase, KeyBase, CertBase) `, CertBase, CertBase, KeyBase, CertBase)
// Annoyingly docopt tries to use 'version' the way he wants and I am using build
arguments, _ := docopt.Parse(usage, nil, true, appVersion, false) arguments, _ := docopt.Parse(usage, nil, true, appVersion, false)
// Annoyingly docopt tries to use 'version' the way he wants and I am using build
if arguments["--build"] == true { if arguments["--build"] == true {
fmt.Printf("acme-downloader version: %v, built on: %v\n", appVersion, buildTime) fmt.Printf("acme-downloader version: %v, built on: %v\n", appVersion, buildTime)
os.Exit(0) appExit(0)
} }
VaultToken := arguments["--vault-token"].(string) VaultToken := arguments["--vault-token"].(string)
...@@ -271,7 +288,7 @@ Options: ...@@ -271,7 +288,7 @@ Options:
Days, daysErr := strconv.Atoi(DayString) Days, daysErr := strconv.Atoi(DayString)
if daysErr != nil { if daysErr != nil {
fmt.Printf("Days mut be an integer\n") fmt.Printf("Days mut be an integer\n")
os.Exit(255) appExit(255)
} }
RedisBaseURL = "https://redis.geant.org/GET" RedisBaseURL = "https://redis.geant.org/GET"
VaultBaseURL = "https://vault.geant.org/v1" VaultBaseURL = "https://vault.geant.org/v1"
...@@ -280,11 +297,6 @@ Options: ...@@ -280,11 +297,6 @@ Options:
RedisCAURL := fmt.Sprintf("%v/%v:%v:redis_%v_chain_pem.txt", RedisBaseURL, TeamName, CertName, CertNameUndercored) RedisCAURL := fmt.Sprintf("%v/%v:%v:redis_%v_chain_pem.txt", RedisBaseURL, TeamName, CertName, CertNameUndercored)
RedisFullChainURL := fmt.Sprintf("%v/%v:%v:redis_%v_fullchain_pem.txt", RedisBaseURL, TeamName, CertName, CertNameUndercored) RedisFullChainURL := fmt.Sprintf("%v/%v:%v:redis_%v_fullchain_pem.txt", RedisBaseURL, TeamName, CertName, CertNameUndercored)
tmpCertificateDestination := "/tmp/amce_cert.pem"
tmpFullchainDestination := "/tmp/amce_fullchain.pem"
tmpCaDestination := "/tmp/amce_ca.pem"
tmpKeyDestination := "/tmp/amce_key.pem"
if arguments["--cert-destination"] == fmt.Sprintf("%v/<cert-name>.crt", CertBase) { if arguments["--cert-destination"] == fmt.Sprintf("%v/<cert-name>.crt", CertBase) {
certificateDestination = fmt.Sprintf("%v/%v.crt", CertBase, CertName) certificateDestination = fmt.Sprintf("%v/%v.crt", CertBase, CertName)
} else { } else {
...@@ -310,7 +322,7 @@ Options: ...@@ -310,7 +322,7 @@ Options:
existingCert := checkCerificates(CertName, certificateDestination, fullchainDestination, caDestination, keyDestination, Days, false) existingCert := checkCerificates(CertName, certificateDestination, fullchainDestination, caDestination, keyDestination, Days, false)
if existingCert == true { if existingCert == true {
fmt.Printf("the certificates are still valid\n") fmt.Printf("the certificates are still valid\n")
os.Exit(0) appExit(0)
} }
certificate := GetRedisKey(RedisCertURL, RedisToken) certificate := GetRedisKey(RedisCertURL, RedisToken)
ca := GetRedisKey(RedisCAURL, RedisToken) ca := GetRedisKey(RedisCAURL, RedisToken)
...@@ -321,14 +333,13 @@ Options: ...@@ -321,14 +333,13 @@ Options:
WriteToFile(fullChain, tmpFullchainDestination, GroupName, 0644, 0755) WriteToFile(fullChain, tmpFullchainDestination, GroupName, 0644, 0755)
WriteToFile(ca, tmpCaDestination, GroupName, 0644, 0755) WriteToFile(ca, tmpCaDestination, GroupName, 0644, 0755)
WriteToFile(privKey, tmpKeyDestination, GroupName, 0640, 0750) WriteToFile(privKey, tmpKeyDestination, GroupName, 0640, 0750)
tempCertSlice := []string{tmpCertificateDestination, tmpFullchainDestination, tmpCaDestination, tmpKeyDestination}
newCert := checkCerificates(CertName, tmpCertificateDestination, tmpFullchainDestination, tmpCaDestination, tmpKeyDestination, Days, false) newCert := checkCerificates(CertName, tmpCertificateDestination, tmpFullchainDestination, tmpCaDestination, tmpKeyDestination, Days, false)
if newCert == false { if newCert == false {
fmt.Printf("the certificates are malformed. Skippping installation\n") fmt.Printf("the certificates are malformed. Skippping installation\n")
for _, element := range tempCertSlice { for _, element := range tempCertSlice {
os.Remove(element) os.Remove(element)
} }
os.Exit(255) appExit(255)
} }
WriteToFile(certificate, certificateDestination, GroupName, 0644, 0755) WriteToFile(certificate, certificateDestination, GroupName, 0644, 0755)
...@@ -341,4 +352,7 @@ Options: ...@@ -341,4 +352,7 @@ Options:
fmt.Printf("installed: %v\n", fullchainDestination) fmt.Printf("installed: %v\n", fullchainDestination)
fmt.Printf("installed: %v\n", keyDestination) fmt.Printf("installed: %v\n", keyDestination)
// Exit 100, means application reload
appExit(1)
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment