Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Acme Downloader
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Geant DevOps
Acme Downloader
Commits
423a5845
Unverified
Commit
423a5845
authored
4 years ago
by
Max Adamo
Browse files
Options
Downloads
Patches
Plain Diff
fix exit status
parent
55387217
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.go
+50
-36
50 additions, 36 deletions
main.go
with
50 additions
and
36 deletions
main.go
+
50
−
36
View file @
423a5845
...
...
@@ -18,22 +18,40 @@ import (
)
var
(
appVersion
string
buildTime
string
CertBase
string
KeyBase
string
GroupName
string
RedisBaseURL
string
VaultBaseURL
string
certificateDestination
string
fullchainDestination
string
keyDestination
string
caDestination
string
Type
string
appVersion
string
buildTime
string
CertBase
string
KeyBase
string
GroupName
string
RedisBaseURL
string
VaultBaseURL
string
certificateDestination
string
fullchainDestination
string
keyDestination
string
caDestination
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
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
daysNumber
:=
time
.
Now
()
.
Local
()
.
Add
(
time
.
Second
*
time
.
Duration
(
Seconds
))
...
...
@@ -43,7 +61,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if
err
!=
nil
{
if
fail
==
true
{
fmt
.
Printf
(
"[ERROR] %v
\n
"
,
err
)
os
.
Exit
(
255
)
app
Exit
(
255
)
}
else
{
return
false
}
...
...
@@ -53,7 +71,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if
err
!=
nil
{
if
fail
==
true
{
fmt
.
Printf
(
"[ERROR] %v
\n
"
,
err
)
os
.
Exit
(
255
)
app
Exit
(
255
)
}
else
{
return
false
}
...
...
@@ -63,7 +81,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if
err
!=
nil
{
if
fail
==
true
{
fmt
.
Printf
(
"[ERROR] %v
\n
"
,
err
)
os
.
Exit
(
255
)
app
Exit
(
255
)
}
else
{
return
false
}
...
...
@@ -74,7 +92,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if
!
ok
{
if
fail
==
true
{
fmt
.
Printf
(
"[ERROR] failed to parse root certificate
\n
"
)
os
.
Exit
(
255
)
app
Exit
(
255
)
}
else
{
return
false
}
...
...
@@ -84,7 +102,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if
block
==
nil
{
if
fail
==
true
{
fmt
.
Printf
(
"[ERROR] failed to parse certificate PEM
\n
"
)
os
.
Exit
(
255
)
app
Exit
(
255
)
}
else
{
return
false
}
...
...
@@ -93,7 +111,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if
err
!=
nil
{
if
fail
==
true
{
fmt
.
Printf
(
"[ERROR] failed to parse certificate %v
\n
"
,
err
)
os
.
Exit
(
255
)
app
Exit
(
255
)
}
else
{
return
false
}
...
...
@@ -103,7 +121,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if
fullchainBlock
==
nil
{
if
fail
==
true
{
fmt
.
Printf
(
"[ERROR] failed to parse certificate PEM
\n
"
)
os
.
Exit
(
255
)
app
Exit
(
255
)
}
else
{
return
false
}
...
...
@@ -112,7 +130,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if
fullchainErr
!=
nil
{
if
fail
==
true
{
fmt
.
Printf
(
"[ERROR] failed to parse certificate %v
\n
"
,
fullchainErr
)
os
.
Exit
(
255
)
app
Exit
(
255
)
}
else
{
return
false
}
...
...
@@ -128,7 +146,7 @@ func checkCerificates(dnsname string, certificate string, fullchain string, ca s
if
_
,
err
:=
cert
.
Verify
(
opts
);
err
!=
nil
{
if
fail
==
true
{
fmt
.
Printf
(
"[ERROR] failed to parse certificate %v
\n
"
,
err
.
Error
())
os
.
Exit
(
255
)
app
Exit
(
255
)
}
else
{
return
false
}
...
...
@@ -154,7 +172,7 @@ func GetRedisKey(redisurl string, redistoken string) string {
defer
resp
.
Body
.
Close
()
if
err
!=
nil
{
fmt
.
Printf
(
"[ERROR] Fail to read %v: %v
\n
"
,
redisurl
,
err
)
os
.
Exit
(
255
)
app
Exit
(
255
)
}
return
fmt
.
Sprintf
(
string
(
body
))
}
...
...
@@ -169,7 +187,7 @@ func GetVaultKey(vaulturl string, vaulttoken string) string {
defer
resp
.
Body
.
Close
()
if
err
!=
nil
{
fmt
.
Printf
(
"[ERROR] Fail to read %v: %v
\n
"
,
vaulturl
,
err
)
os
.
Exit
(
255
)
app
Exit
(
255
)
}
return
gjson
.
Get
(
string
(
body
),
"data.value"
)
.
String
()
}
...
...
@@ -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
)
if
err
!=
nil
{
fmt
.
Printf
(
"[ERROR] %v cannot be created
\n
"
,
destination
)
os
.
Exit
(
255
)
app
Exit
(
255
)
}
fmt
.
Fprintf
(
file
,
"%v
\n
"
,
content
)
...
...
@@ -252,13 +270,12 @@ Options:
--ca-destination=CADESTINATION CA Destination [default: %v/COMODO_<type>.crt]
`
,
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
)
// Annoyingly docopt tries to use 'version' the way he wants and I am using build
if
arguments
[
"--build"
]
==
true
{
fmt
.
Printf
(
"acme-downloader version: %v, built on: %v
\n
"
,
appVersion
,
buildTime
)
os
.
Exit
(
0
)
app
Exit
(
0
)
}
VaultToken
:=
arguments
[
"--vault-token"
]
.
(
string
)
...
...
@@ -271,7 +288,7 @@ Options:
Days
,
daysErr
:=
strconv
.
Atoi
(
DayString
)
if
daysErr
!=
nil
{
fmt
.
Printf
(
"Days mut be an integer
\n
"
)
os
.
Exit
(
255
)
app
Exit
(
255
)
}
RedisBaseURL
=
"https://redis.geant.org/GET"
VaultBaseURL
=
"https://vault.geant.org/v1"
...
...
@@ -280,11 +297,6 @@ Options:
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
)
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
)
{
certificateDestination
=
fmt
.
Sprintf
(
"%v/%v.crt"
,
CertBase
,
CertName
)
}
else
{
...
...
@@ -310,7 +322,7 @@ Options:
existingCert
:=
checkCerificates
(
CertName
,
certificateDestination
,
fullchainDestination
,
caDestination
,
keyDestination
,
Days
,
false
)
if
existingCert
==
true
{
fmt
.
Printf
(
"the certificates are still valid
\n
"
)
os
.
Exit
(
0
)
app
Exit
(
0
)
}
certificate
:=
GetRedisKey
(
RedisCertURL
,
RedisToken
)
ca
:=
GetRedisKey
(
RedisCAURL
,
RedisToken
)
...
...
@@ -321,14 +333,13 @@ Options:
WriteToFile
(
fullChain
,
tmpFullchainDestination
,
GroupName
,
0644
,
0755
)
WriteToFile
(
ca
,
tmpCaDestination
,
GroupName
,
0644
,
0755
)
WriteToFile
(
privKey
,
tmpKeyDestination
,
GroupName
,
0640
,
0750
)
tempCertSlice
:=
[]
string
{
tmpCertificateDestination
,
tmpFullchainDestination
,
tmpCaDestination
,
tmpKeyDestination
}
newCert
:=
checkCerificates
(
CertName
,
tmpCertificateDestination
,
tmpFullchainDestination
,
tmpCaDestination
,
tmpKeyDestination
,
Days
,
false
)
if
newCert
==
false
{
fmt
.
Printf
(
"the certificates are malformed. Skippping installation
\n
"
)
for
_
,
element
:=
range
tempCertSlice
{
os
.
Remove
(
element
)
}
os
.
Exit
(
255
)
app
Exit
(
255
)
}
WriteToFile
(
certificate
,
certificateDestination
,
GroupName
,
0644
,
0755
)
...
...
@@ -341,4 +352,7 @@ Options:
fmt
.
Printf
(
"installed: %v
\n
"
,
fullchainDestination
)
fmt
.
Printf
(
"installed: %v
\n
"
,
keyDestination
)
// Exit 100, means application reload
appExit
(
1
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment