Skip to content
Snippets Groups Projects
Unverified Commit 139c8727 authored by Massimiliano Adamo's avatar Massimiliano Adamo
Browse files

make code comply with SonarQube

parent 4949fa52
No related branches found
No related tags found
No related merge requests found
Pipeline #969 passed
...@@ -20,6 +20,9 @@ import ( ...@@ -20,6 +20,9 @@ import (
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
const errMsg string = "[ERR]"
const infoMsg string = "[INFO]"
var ( var (
appVersion string appVersion string
buildTime string buildTime string
...@@ -56,6 +59,7 @@ func appExit(status int) { ...@@ -56,6 +59,7 @@ func appExit(status int) {
} }
err := os.RemoveAll(certTmpDir) err := os.RemoveAll(certTmpDir)
if err != nil { if err != nil {
fmt.Printf("%v could not delete temporary directory: %v\n", errMsg, err)
} }
os.Exit(status) os.Exit(status)
} }
...@@ -69,7 +73,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca ...@@ -69,7 +73,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca
certPEM, err := ioutil.ReadFile(certificate) certPEM, err := ioutil.ReadFile(certificate)
if err != nil { if err != nil {
if fail == true { if fail == true {
fmt.Printf("[ERR] %v\n", err) fmt.Printf("%v %v\n", errMsg, err)
appExit(255) appExit(255)
} else { } else {
return false return false
...@@ -79,7 +83,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca ...@@ -79,7 +83,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca
certFullchainPEM, err := ioutil.ReadFile(fullchain) certFullchainPEM, err := ioutil.ReadFile(fullchain)
if err != nil { if err != nil {
if fail == true { if fail == true {
fmt.Printf("[ERR] %v\n", err) fmt.Printf("%v %v\n", errMsg, err)
appExit(255) appExit(255)
} else { } else {
return false return false
...@@ -89,7 +93,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca ...@@ -89,7 +93,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca
rootPEM, err := ioutil.ReadFile(ca) rootPEM, err := ioutil.ReadFile(ca)
if err != nil { if err != nil {
if fail == true { if fail == true {
fmt.Printf("[ERR] %v\n", err) fmt.Printf("%v %v\n", errMsg, err)
appExit(255) appExit(255)
} else { } else {
return false return false
...@@ -100,7 +104,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca ...@@ -100,7 +104,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca
ok := roots.AppendCertsFromPEM([]byte(rootPEM)) ok := roots.AppendCertsFromPEM([]byte(rootPEM))
if !ok { if !ok {
if fail == true { if fail == true {
fmt.Printf("[ERR] failed to parse root certificate\n") fmt.Printf("%v failed to parse root certificate\n", errMsg)
appExit(255) appExit(255)
} else { } else {
return false return false
...@@ -110,7 +114,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca ...@@ -110,7 +114,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca
block, _ := pem.Decode([]byte(certPEM)) block, _ := pem.Decode([]byte(certPEM))
if block == nil { if block == nil {
if fail == true { if fail == true {
fmt.Printf("[ERR] failed to parse certificate PEM\n") fmt.Printf("%v failed to parse certificate PEM\n", errMsg)
appExit(255) appExit(255)
} else { } else {
return false return false
...@@ -120,7 +124,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca ...@@ -120,7 +124,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca
cert, err := x509.ParseCertificate(block.Bytes) cert, err := x509.ParseCertificate(block.Bytes)
if err != nil { if err != nil {
if fail == true { if fail == true {
fmt.Printf("[ERR] failed to parse certificate %v\n", err) fmt.Printf("%v failed to parse certificate %v\n", errMsg, err)
appExit(255) appExit(255)
} else { } else {
return false return false
...@@ -130,7 +134,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca ...@@ -130,7 +134,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca
fullchainBlock, _ := pem.Decode([]byte(certFullchainPEM)) fullchainBlock, _ := pem.Decode([]byte(certFullchainPEM))
if fullchainBlock == nil { if fullchainBlock == nil {
if fail == true { if fail == true {
fmt.Printf("[ERR] failed to parse certificate PEM\n") fmt.Printf("%v failed to parse certificate PEM\n", errMsg)
appExit(255) appExit(255)
} else { } else {
return false return false
...@@ -139,7 +143,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca ...@@ -139,7 +143,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca
fullchainCert, fullchainErr := x509.ParseCertificate(fullchainBlock.Bytes) fullchainCert, fullchainErr := x509.ParseCertificate(fullchainBlock.Bytes)
if fullchainErr != nil { if fullchainErr != nil {
if fail == true { if fail == true {
fmt.Printf("[ERR] failed to parse certificate %v\n", fullchainErr) fmt.Printf("%v failed to parse certificate %v\n", errMsg, fullchainErr)
appExit(255) appExit(255)
} else { } else {
return false return false
...@@ -155,7 +159,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca ...@@ -155,7 +159,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca
if _, err := cert.Verify(opts); err != nil { if _, err := cert.Verify(opts); err != nil {
if fail == true { if fail == true {
fmt.Printf("[ERR] failed to parse certificate %v\n", err.Error()) fmt.Printf("%v failed to parse certificate %v\n", errMsg, err.Error())
appExit(255) appExit(255)
} else { } else {
return false return false
...@@ -163,7 +167,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca ...@@ -163,7 +167,7 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca
} }
if _, fullchainErr := fullchainCert.Verify(opts); fullchainErr != nil { if _, fullchainErr := fullchainCert.Verify(opts); fullchainErr != nil {
if fail == true { if fail == true {
fmt.Printf("[ERR] failed to parse certificate %v\n", err.Error()) fmt.Printf("%v failed to parse certificate %v\n", errMsg, err.Error())
} else { } else {
return false return false
} }
...@@ -175,13 +179,13 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca ...@@ -175,13 +179,13 @@ func checkCertificates(dnsname string, certificate string, fullchain string, ca
func checkPrivkey(privkey string, pubkey string, fail bool) bool { func checkPrivkey(privkey string, pubkey string, fail bool) bool {
_, errFileExist := os.Stat(privkey) _, errFileExist := os.Stat(privkey)
if os.IsNotExist(errFileExist) { if os.IsNotExist(errFileExist) {
fmt.Printf("[ERR] could not access the private key %v\n", privkey) fmt.Printf("%v could not access the private key %v\n", errMsg, privkey)
appExit(255) appExit(255)
} }
_, err := tls.LoadX509KeyPair(pubkey, privkey) _, err := tls.LoadX509KeyPair(pubkey, privkey)
if fail == true { if fail == true {
if err != nil { if err != nil {
fmt.Printf("[ERR] the private key %v does not match the the public certificate %v\n", privkey, pubkey) fmt.Printf("%v the private key %v does not match the the public certificate %v\n", errMsg, privkey, pubkey)
appExit(255) appExit(255)
} else { } else {
return false return false
...@@ -195,19 +199,19 @@ func GetRedisKey(redisurl string, redistoken string) string { ...@@ -195,19 +199,19 @@ func GetRedisKey(redisurl string, redistoken string) string {
client := &http.Client{} client := &http.Client{}
req, err := http.NewRequest("GET", redisurl, nil) req, err := http.NewRequest("GET", redisurl, nil)
if err != nil { if err != nil {
fmt.Printf("[ERR] Fail to read %v: %v\n", redisurl, err) fmt.Printf("%v Fail to read %v: %v\n", errMsg, redisurl, err)
appExit(255) appExit(255)
} }
req.SetBasicAuth("redis", redistoken) req.SetBasicAuth("redis", redistoken)
resp, err := client.Do(req) resp, err := client.Do(req)
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode < 200 || resp.StatusCode > 299 { if resp.StatusCode < 200 || resp.StatusCode > 299 {
fmt.Printf("[ERR] Fail to fetch %v\n", redisurl) fmt.Printf("%v Fail to fetch %v\n", errMsg, redisurl)
appExit(255) appExit(255)
} }
defer resp.Body.Close() defer resp.Body.Close()
if err != nil { if err != nil {
fmt.Printf("[ERR] Fail to read %v: %v\n", redisurl, err) fmt.Printf("%v Fail to read %v: %v\n", errMsg, redisurl, err)
appExit(255) appExit(255)
} }
return fmt.Sprintf(string(body)) return fmt.Sprintf(string(body))
...@@ -218,19 +222,19 @@ func GetVaultKey(vaulturl string, vaulttoken string) string { ...@@ -218,19 +222,19 @@ func GetVaultKey(vaulturl string, vaulttoken string) string {
vaultClient := &http.Client{} vaultClient := &http.Client{}
req, err := http.NewRequest("GET", vaulturl, nil) req, err := http.NewRequest("GET", vaulturl, nil)
if err != nil { if err != nil {
fmt.Printf("[ERR] Fail to read %v: %v\n", vaulturl, err) fmt.Printf("%v Fail to read %v: %v\n", errMsg, vaulturl, err)
appExit(255) appExit(255)
} }
req.Header.Add("X-vault-token", vaulttoken) req.Header.Add("X-vault-token", vaulttoken)
resp, err := vaultClient.Do(req) resp, err := vaultClient.Do(req)
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode < 200 || resp.StatusCode > 299 { if resp.StatusCode < 200 || resp.StatusCode > 299 {
fmt.Printf("[ERR] Fail to fetch %v\n", vaulturl) fmt.Printf("%v Fail to fetch %v\n", errMsg, vaulturl)
appExit(255) appExit(255)
} }
defer resp.Body.Close() defer resp.Body.Close()
if err != nil { if err != nil {
fmt.Printf("[ERR] Fail to read %v: %v\n", vaulturl, err) fmt.Printf("%v Fail to read %v: %v\n", errMsg, vaulturl, err)
appExit(255) appExit(255)
} }
return gjson.Get(string(body), "data.value").String() return gjson.Get(string(body), "data.value").String()
...@@ -245,7 +249,7 @@ func WriteToFile(content string, destination string, filemode os.FileMode) { ...@@ -245,7 +249,7 @@ func WriteToFile(content string, destination string, filemode os.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("[ERR] %v cannot be created\n", destination) fmt.Printf("%v %v cannot be created\n", errMsg, destination)
appExit(255) appExit(255)
} }
...@@ -261,17 +265,17 @@ func moveFile(source string, destination string, groupid int, filemode os.FileMo ...@@ -261,17 +265,17 @@ func moveFile(source string, destination string, groupid int, filemode os.FileMo
} }
err := os.Rename(source, destination) err := os.Rename(source, destination)
if err != nil { if err != nil {
fmt.Printf("[ERR] Fail to install %v: %v\n", destination, err) fmt.Printf("%v Fail to install %v: %v\n", errMsg, destination, err)
appExit(255) appExit(255)
} }
if runtime.GOOS != "windows" { if runtime.GOOS != "windows" {
err = os.Chown(destination, 0, groupid) err = os.Chown(destination, 0, groupid)
if err != nil { if err != nil {
fmt.Printf("[ERR] Changing file owner to %v", groupid) fmt.Printf("%v Changing file owner to %v", errMsg, groupid)
appExit(255) appExit(255)
} }
} }
fmt.Printf("[INFO] installed: %v\n", destination) fmt.Printf("%v installed: %v\n", infoMsg, destination)
} }
// ReadOSRelease from /etc/os-release // ReadOSRelease from /etc/os-release
...@@ -361,7 +365,7 @@ Options: ...@@ -361,7 +365,7 @@ Options:
tmpKeyDestination = "/tmp/acme-downloader/key/amce_key.pem" tmpKeyDestination = "/tmp/acme-downloader/key/amce_key.pem"
group, groupErr := user.LookupGroup(GroupName) group, groupErr := user.LookupGroup(GroupName)
if groupErr != nil { if groupErr != nil {
fmt.Printf("[ERR] Fail looking up %v user user info\n", GroupName) fmt.Printf("%v Fail looking up %v user user info\n", errMsg, GroupName)
appExit(255) appExit(255)
} }
GroupID, _ = strconv.Atoi(group.Gid) GroupID, _ = strconv.Atoi(group.Gid)
...@@ -376,7 +380,7 @@ Options: ...@@ -376,7 +380,7 @@ Options:
DayString := arguments["--days"].(string) DayString := arguments["--days"].(string)
Days, daysErr := strconv.Atoi(DayString) Days, daysErr := strconv.Atoi(DayString)
if daysErr != nil { if daysErr != nil {
fmt.Printf("[ERR] Days mut be an integer\n") fmt.Printf("%v Days mut be an integer\n", errMsg)
appExit(255) appExit(255)
} }
RedisBaseURL = "https://redis.geant.org/GET" RedisBaseURL = "https://redis.geant.org/GET"
...@@ -418,7 +422,7 @@ Options: ...@@ -418,7 +422,7 @@ Options:
existingCert := checkCertificates(CertName, certificateDestination, fullchainDestination, caDestination, keyDestination, Days, false) existingCert := checkCertificates(CertName, certificateDestination, fullchainDestination, caDestination, keyDestination, Days, false)
existingKey := checkPrivkey(keyDestination, certificateDestination, false) existingKey := checkPrivkey(keyDestination, certificateDestination, false)
if existingCert == true && existingKey == true { if existingCert == true && existingKey == true {
fmt.Printf("[INFO] the certificate is still valid\n") fmt.Printf("%v the certificate is still valid\n", infoMsg)
appExit(0) appExit(0)
} }
certificate := GetRedisKey(RedisCertURL, RedisToken) certificate := GetRedisKey(RedisCertURL, RedisToken)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment