Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
galene
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
galene
Commits
b3727824
Commit
b3727824
authored
Feb 26, 2021
by
Juliusz Chroboczek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Protect against simultaneous generation of certificates.
parent
c19b356e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
73 additions
and
31 deletions
+73
-31
webserver/certificate.go
webserver/certificate.go
+73
-31
No files found.
webserver/certificate.go
View file @
b3727824
...
...
@@ -10,6 +10,7 @@ import (
"math/big"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
)
...
...
@@ -20,9 +21,14 @@ type certInfo struct {
certTime
time
.
Time
}
// certMu protects writing to certificate
var
certMu
sync
.
Mutex
// certificate holds our current certificate, of type certInfo
var
certificate
atomic
.
Value
func
generateCertificate
(
dataDir
string
)
(
tls
.
Certificate
,
error
)
{
// generateCertificate generates a self-signed certficate
func
generateCertificate
()
(
tls
.
Certificate
,
error
)
{
priv
,
err
:=
rsa
.
GenerateKey
(
rand
.
Reader
,
2048
)
if
err
!=
nil
{
return
tls
.
Certificate
{},
err
...
...
@@ -53,7 +59,7 @@ func generateCertificate(dataDir string) (tls.Certificate, error) {
},
nil
}
func
file
Time
(
filename
string
)
time
.
Time
{
func
mod
Time
(
filename
string
)
time
.
Time
{
fi
,
err
:=
os
.
Stat
(
filename
)
if
err
!=
nil
{
if
!
os
.
IsNotExist
(
err
)
{
...
...
@@ -64,40 +70,76 @@ func fileTime(filename string) time.Time {
return
fi
.
ModTime
()
}
func
getCertificate
(
dataDir
string
)
(
*
tls
.
Certificate
,
error
)
{
// loadCertificate returns the current certificate if it is still valid.
func
loadCertificate
(
certFile
string
,
certTime
time
.
Time
,
keyFile
string
,
keyTime
time
.
Time
)
*
certInfo
{
info
,
ok
:=
certificate
.
Load
()
.
(
*
certInfo
)
if
!
ok
{
return
nil
}
if
!
info
.
certTime
.
Equal
(
certTime
)
||
!
info
.
keyTime
.
Equal
(
keyTime
)
{
return
nil
}
return
info
}
// storeCertificate returns the current certificate if it is still valid,
// and either reads or generates a new one otherwise.
func
storeCertificate
(
certFile
string
,
certTime
time
.
Time
,
keyFile
string
,
keyTime
time
.
Time
)
(
info
*
certInfo
,
err
error
)
{
certMu
.
Lock
()
defer
certMu
.
Unlock
()
// the certificate may have been updated since we checked
info
=
loadCertificate
(
certFile
,
certTime
,
keyFile
,
keyTime
)
if
info
!=
nil
{
return
}
var
cert
tls
.
Certificate
nocert
:=
certTime
.
Equal
(
time
.
Time
{})
nokey
:=
keyTime
.
Equal
(
time
.
Time
{})
if
nocert
!=
nokey
{
err
=
errors
.
New
(
"only one of cert.pem and key.pem exists"
)
return
}
else
if
nokey
{
log
.
Printf
(
"Generating self-signed certificate"
)
cert
,
err
=
generateCertificate
()
if
err
!=
nil
{
return
}
}
else
{
cert
,
err
=
tls
.
LoadX509KeyPair
(
certFile
,
keyFile
)
if
err
!=
nil
{
return
}
}
info
=
&
certInfo
{
certificate
:
&
cert
,
certTime
:
certTime
,
keyTime
:
keyTime
,
}
certificate
.
Store
(
info
)
return
}
func
getCertificate
(
dataDir
string
)
(
*
tls
.
Certificate
,
error
)
{
certFile
:=
filepath
.
Join
(
dataDir
,
"cert.pem"
)
keyFile
:=
filepath
.
Join
(
dataDir
,
"key.pem"
)
certTime
:=
fileTime
(
certFile
)
keyTime
:=
fileTime
(
keyFile
)
if
!
ok
||
!
info
.
certTime
.
Equal
(
certTime
)
||
!
info
.
keyTime
.
Equal
(
keyTime
)
{
var
cert
tls
.
Certificate
nocert
:=
certTime
.
Equal
(
time
.
Time
{})
nokey
:=
keyTime
.
Equal
(
time
.
Time
{})
if
nocert
!=
nokey
{
return
nil
,
errors
.
New
(
"only one of cert.pem and key.pem exists"
)
}
else
if
nokey
{
log
.
Printf
(
"Generating self-signed certificate"
)
var
err
error
cert
,
err
=
generateCertificate
(
dataDir
)
if
err
!=
nil
{
return
nil
,
err
}
}
else
{
var
err
error
cert
,
err
=
tls
.
LoadX509KeyPair
(
certFile
,
keyFile
)
if
err
!=
nil
{
return
nil
,
err
}
}
info
=
&
certInfo
{
certificate
:
&
cert
,
certTime
:
certTime
,
keyTime
:
keyTime
,
certTime
:=
modTime
(
certFile
)
keyTime
:=
modTime
(
keyFile
)
info
:=
loadCertificate
(
certFile
,
certTime
,
keyFile
,
keyTime
)
if
info
==
nil
{
var
err
error
info
,
err
=
storeCertificate
(
certFile
,
certTime
,
keyFile
,
keyTime
,
)
if
info
==
nil
||
err
!=
nil
{
return
nil
,
err
}
certificate
.
Store
(
info
)
}
return
info
.
certificate
,
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment