Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
12f8c979
Commit
12f8c979
authored
Sep 29, 2016
by
Evgeny L
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kubeadm: user-friendly certificates formatting
parent
448ceb38
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
12 deletions
+50
-12
pki.go
cmd/kubeadm/app/master/pki.go
+16
-7
csr.go
cmd/kubeadm/app/node/csr.go
+6
-5
cert.go
pkg/util/cert/cert.go
+28
-0
No files found.
cmd/kubeadm/app/master/pki.go
View file @
12f8c979
...
...
@@ -96,11 +96,7 @@ func newClientKeyAndCert(caCert *x509.Certificate, caKey *rsa.PrivateKey) (*rsa.
}
func
writeKeysAndCert
(
pkiPath
string
,
name
string
,
key
*
rsa
.
PrivateKey
,
cert
*
x509
.
Certificate
)
error
{
var
(
publicKeyPath
=
path
.
Join
(
pkiPath
,
fmt
.
Sprintf
(
"%s-pub.pem"
,
name
))
privateKeyPath
=
path
.
Join
(
pkiPath
,
fmt
.
Sprintf
(
"%s-key.pem"
,
name
))
certificatePath
=
path
.
Join
(
pkiPath
,
fmt
.
Sprintf
(
"%s.pem"
,
name
))
)
publicKeyPath
,
privateKeyPath
,
certificatePath
:=
pathsKeysCerts
(
pkiPath
,
name
)
if
key
!=
nil
{
if
err
:=
certutil
.
WriteKey
(
privateKeyPath
,
certutil
.
EncodePrivateKeyPEM
(
key
));
err
!=
nil
{
...
...
@@ -124,6 +120,12 @@ func writeKeysAndCert(pkiPath string, name string, key *rsa.PrivateKey, cert *x5
return
nil
}
func
pathsKeysCerts
(
pkiPath
,
name
string
)
(
string
,
string
,
string
)
{
return
path
.
Join
(
pkiPath
,
fmt
.
Sprintf
(
"%s-pub.pem"
,
name
)),
path
.
Join
(
pkiPath
,
fmt
.
Sprintf
(
"%s-key.pem"
,
name
)),
path
.
Join
(
pkiPath
,
fmt
.
Sprintf
(
"%s.pem"
,
name
))
}
func
newServiceAccountKey
()
(
*
rsa
.
PrivateKey
,
error
)
{
key
,
err
:=
certutil
.
NewPrivateKey
()
if
err
!=
nil
{
...
...
@@ -155,6 +157,9 @@ func CreatePKIAssets(s *kubeadmapi.KubeadmConfig) (*rsa.PrivateKey, *x509.Certif
if
err
:=
writeKeysAndCert
(
pkiPath
,
"ca"
,
caKey
,
caCert
);
err
!=
nil
{
return
nil
,
nil
,
fmt
.
Errorf
(
"<master/pki> failure while saving CA keys and certificate - %v"
,
err
)
}
fmt
.
Printf
(
"<master/pki> generated Certificate Authority key and certificate:
\n
%s
\n
"
,
certutil
.
FormatCert
(
caCert
))
pub
,
prv
,
cert
:=
pathsKeysCerts
(
pkiPath
,
"ca"
)
fmt
.
Printf
(
"Public: %s
\n
Private: %s
\n
Cert: %s
\n
"
,
pub
,
prv
,
cert
)
apiKey
,
apiCert
,
err
:=
newServerKeyAndCert
(
s
,
caCert
,
caKey
,
altNames
)
if
err
!=
nil
{
...
...
@@ -164,17 +169,21 @@ func CreatePKIAssets(s *kubeadmapi.KubeadmConfig) (*rsa.PrivateKey, *x509.Certif
if
err
:=
writeKeysAndCert
(
pkiPath
,
"apiserver"
,
apiKey
,
apiCert
);
err
!=
nil
{
return
nil
,
nil
,
fmt
.
Errorf
(
"<master/pki> failure while saving API server keys and certificate - %v"
,
err
)
}
fmt
.
Printf
(
"<master/pki> generated API Server key and certificate:
\n
%s
\n
"
,
certutil
.
FormatCert
(
apiCert
))
pub
,
prv
,
cert
=
pathsKeysCerts
(
pkiPath
,
"apiserver"
)
fmt
.
Printf
(
"Public: %s
\n
Private: %s
\n
Cert: %s
\n
"
,
pub
,
prv
,
cert
)
saKey
,
err
:=
newServiceAccountKey
()
if
err
!=
nil
{
return
nil
,
nil
,
fmt
.
Errorf
(
"<master/pki> failure while creating service account signing keys [%v]"
,
err
)
}
if
err
:=
writeKeysAndCert
(
pkiPath
,
"sa"
,
saKey
,
nil
);
err
!=
nil
{
return
nil
,
nil
,
fmt
.
Errorf
(
"<master/pki> failure while saving service account signing keys - %v"
,
err
)
}
fmt
.
Printf
(
"<master/pki> generated Service Account Signing keys:
\n
"
)
pub
,
prv
,
_
=
pathsKeysCerts
(
pkiPath
,
"sa"
)
fmt
.
Printf
(
"Public: %s
\n
Private: %s
\n
"
,
pub
,
prv
)
// TODO(phase1+) print a summary of SANs used and checksums (signatures) of each of the certificates
fmt
.
Printf
(
"<master/pki> created keys and certificates in %q
\n
"
,
pkiPath
)
return
caKey
,
caCert
,
nil
}
cmd/kubeadm/app/node/csr.go
View file @
12f8c979
...
...
@@ -74,15 +74,16 @@ func PerformTLSBootstrap(s *kubeadmapi.KubeadmConfig, apiEndpoint string, caCert
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"<node/csr> failed to generating private key [%v]"
,
err
)
}
cert
,
err
:=
csr
.
RequestNodeCertificate
(
csrClient
,
key
,
nodeName
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"<node/csr> failed to request signed certificate from the API server [%v]"
,
err
)
}
// TODO(phase1+) https://github.com/kubernetes/kubernetes/issues/33642
fmt
.
Println
(
"<node/csr> received signed certificate from the API server, generating kubelet configuration"
)
fmtCert
,
err
:=
certutil
.
FormatBytesCert
(
cert
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"<node/csr> failed to format certificate [%v]"
,
err
)
}
fmt
.
Printf
(
"<node/csr> received signed certificate from the API server:
\n
%s
\n
"
,
fmtCert
)
fmt
.
Println
(
"<node/csr> generating kubelet configuration"
)
finalConfig
:=
kubeadmutil
.
MakeClientConfigWithCerts
(
bareClientConfig
,
"kubernetes"
,
fmt
.
Sprintf
(
"kubelet-%s"
,
nodeName
),
key
,
cert
,
...
...
pkg/util/cert/cert.go
View file @
12f8c979
...
...
@@ -188,3 +188,31 @@ func GenerateSelfSignedCert(host, certPath, keyPath string, alternateIPs []net.I
return
nil
}
// FormatBytesCert receives byte array certificate and formats in human-readable format
func
FormatBytesCert
(
cert
[]
byte
)
(
string
,
error
)
{
block
,
_
:=
pem
.
Decode
(
cert
)
c
,
err
:=
x509
.
ParseCertificate
(
block
.
Bytes
)
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"failed to parse certificate [%v]"
,
err
)
}
return
FormatCert
(
c
),
nil
}
// FormatCert receives certificate and formats in human-readable format
func
FormatCert
(
c
*
x509
.
Certificate
)
string
{
var
ips
[]
string
for
_
,
ip
:=
range
c
.
IPAddresses
{
ips
=
append
(
ips
,
ip
.
String
())
}
altNames
:=
append
(
ips
,
c
.
DNSNames
...
)
res
:=
fmt
.
Sprintf
(
"Issuer: CN=%s | Subject: CN=%s | CA: %t
\n
"
,
c
.
Issuer
.
CommonName
,
c
.
Subject
.
CommonName
,
c
.
IsCA
,
)
res
+=
fmt
.
Sprintf
(
"Not before: %s Not After: %s"
,
c
.
NotBefore
,
c
.
NotAfter
)
if
len
(
altNames
)
>
0
{
res
+=
fmt
.
Sprintf
(
"
\n
Alternate Names: %v"
,
altNames
)
}
return
res
}
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