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
f883fd2c
Unverified
Commit
f883fd2c
authored
Oct 08, 2018
by
k8s-ci-robot
Committed by
GitHub
Oct 08, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #69536 from awly/robust-cert-loading
Allow inverted key/cert order in combined PEM file
parents
d4c48718
4b6a6a1c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
27 additions
and
129 deletions
+27
-129
BUILD
staging/src/k8s.io/client-go/util/certificate/BUILD
+0
-1
certificate_store.go
...rc/k8s.io/client-go/util/certificate/certificate_store.go
+3
-22
certificate_store_test.go
...s.io/client-go/util/certificate/certificate_store_test.go
+24
-106
No files found.
staging/src/k8s.io/client-go/util/certificate/BUILD
View file @
f883fd2c
...
@@ -24,7 +24,6 @@ go_test(
...
@@ -24,7 +24,6 @@ go_test(
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/watch:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/watch:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/typed/certificates/v1beta1:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/typed/certificates/v1beta1:go_default_library",
"//staging/src/k8s.io/client-go/util/cert:go_default_library",
],
],
)
)
...
...
staging/src/k8s.io/client-go/util/certificate/certificate_store.go
View file @
f883fd2c
...
@@ -21,7 +21,6 @@ import (
...
@@ -21,7 +21,6 @@ import (
"crypto/x509"
"crypto/x509"
"encoding/pem"
"encoding/pem"
"fmt"
"fmt"
"io/ioutil"
"os"
"os"
"path/filepath"
"path/filepath"
"strings"
"strings"
...
@@ -171,11 +170,9 @@ func (s *fileStore) Current() (*tls.Certificate, error) {
...
@@ -171,11 +170,9 @@ func (s *fileStore) Current() (*tls.Certificate, error) {
}
}
func
loadFile
(
pairFile
string
)
(
*
tls
.
Certificate
,
error
)
{
func
loadFile
(
pairFile
string
)
(
*
tls
.
Certificate
,
error
)
{
certBlock
,
keyBlock
,
err
:=
loadCertKeyBlocks
(
pairFile
)
// LoadX509KeyPair knows how to parse combined cert and private key from
if
err
!=
nil
{
// the same file.
return
nil
,
err
cert
,
err
:=
tls
.
LoadX509KeyPair
(
pairFile
,
pairFile
)
}
cert
,
err
:=
tls
.
X509KeyPair
(
pem
.
EncodeToMemory
(
certBlock
),
pem
.
EncodeToMemory
(
keyBlock
))
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"could not convert data from %q into cert/key pair: %v"
,
pairFile
,
err
)
return
nil
,
fmt
.
Errorf
(
"could not convert data from %q into cert/key pair: %v"
,
pairFile
,
err
)
}
}
...
@@ -187,22 +184,6 @@ func loadFile(pairFile string) (*tls.Certificate, error) {
...
@@ -187,22 +184,6 @@ func loadFile(pairFile string) (*tls.Certificate, error) {
return
&
cert
,
nil
return
&
cert
,
nil
}
}
func
loadCertKeyBlocks
(
pairFile
string
)
(
cert
*
pem
.
Block
,
key
*
pem
.
Block
,
err
error
)
{
data
,
err
:=
ioutil
.
ReadFile
(
pairFile
)
if
err
!=
nil
{
return
nil
,
nil
,
fmt
.
Errorf
(
"could not load cert/key pair from %q: %v"
,
pairFile
,
err
)
}
certBlock
,
rest
:=
pem
.
Decode
(
data
)
if
certBlock
==
nil
{
return
nil
,
nil
,
fmt
.
Errorf
(
"could not decode the first block from %q from expected PEM format"
,
pairFile
)
}
keyBlock
,
_
:=
pem
.
Decode
(
rest
)
if
keyBlock
==
nil
{
return
nil
,
nil
,
fmt
.
Errorf
(
"could not decode the second block from %q from expected PEM format"
,
pairFile
)
}
return
certBlock
,
keyBlock
,
nil
}
func
(
s
*
fileStore
)
Update
(
certData
,
keyData
[]
byte
)
(
*
tls
.
Certificate
,
error
)
{
func
(
s
*
fileStore
)
Update
(
certData
,
keyData
[]
byte
)
(
*
tls
.
Certificate
,
error
)
{
ts
:=
time
.
Now
()
.
Format
(
"2006-01-02-15-04-05"
)
ts
:=
time
.
Now
()
.
Format
(
"2006-01-02-15-04-05"
)
pemFilename
:=
s
.
filename
(
ts
)
pemFilename
:=
s
.
filename
(
ts
)
...
...
staging/src/k8s.io/client-go/util/certificate/certificate_store_test.go
View file @
f883fd2c
...
@@ -17,12 +17,11 @@ limitations under the License.
...
@@ -17,12 +17,11 @@ limitations under the License.
package
certificate
package
certificate
import
(
import
(
"bytes"
"io/ioutil"
"io/ioutil"
"os"
"os"
"path/filepath"
"path/filepath"
"testing"
"testing"
"k8s.io/client-go/util/cert"
)
)
func
TestUpdateSymlinkExistingFileError
(
t
*
testing
.
T
)
{
func
TestUpdateSymlinkExistingFileError
(
t
*
testing
.
T
)
{
...
@@ -178,96 +177,6 @@ func TestUpdateSymlinkReplaceExistingSymlink(t *testing.T) {
...
@@ -178,96 +177,6 @@ func TestUpdateSymlinkReplaceExistingSymlink(t *testing.T) {
}
}
}
}
func
TestLoadCertKeyBlocksNoFile
(
t
*
testing
.
T
)
{
dir
,
err
:=
ioutil
.
TempDir
(
""
,
"k8s-test-load-cert-key-blocks"
)
if
err
!=
nil
{
t
.
Fatalf
(
"Unable to create the test directory %q: %v"
,
dir
,
err
)
}
defer
func
()
{
if
err
:=
os
.
RemoveAll
(
dir
);
err
!=
nil
{
t
.
Errorf
(
"Unable to clean up test directory %q: %v"
,
dir
,
err
)
}
}()
pairFile
:=
filepath
.
Join
(
dir
,
"kubelet-pair.pem"
)
if
_
,
_
,
err
:=
loadCertKeyBlocks
(
pairFile
);
err
==
nil
{
t
.
Errorf
(
"Got no error, but expected %q not found."
,
pairFile
)
}
}
func
TestLoadCertKeyBlocksEmptyFile
(
t
*
testing
.
T
)
{
dir
,
err
:=
ioutil
.
TempDir
(
""
,
"k8s-test-load-cert-key-blocks"
)
if
err
!=
nil
{
t
.
Fatalf
(
"Unable to create the test directory %q: %v"
,
dir
,
err
)
}
defer
func
()
{
if
err
:=
os
.
RemoveAll
(
dir
);
err
!=
nil
{
t
.
Errorf
(
"Unable to clean up test directory %q: %v"
,
dir
,
err
)
}
}()
pairFile
:=
filepath
.
Join
(
dir
,
"kubelet-pair.pem"
)
if
err
:=
ioutil
.
WriteFile
(
pairFile
,
nil
,
0600
);
err
!=
nil
{
t
.
Fatalf
(
"Unable to create the file %q: %v"
,
pairFile
,
err
)
}
if
_
,
_
,
err
:=
loadCertKeyBlocks
(
pairFile
);
err
==
nil
{
t
.
Errorf
(
"Got no error, but expected %q not found."
,
pairFile
)
}
}
func
TestLoadCertKeyBlocksPartialFile
(
t
*
testing
.
T
)
{
dir
,
err
:=
ioutil
.
TempDir
(
""
,
"k8s-test-load-cert-key-blocks"
)
if
err
!=
nil
{
t
.
Fatalf
(
"Unable to create the test directory %q: %v"
,
dir
,
err
)
}
defer
func
()
{
if
err
:=
os
.
RemoveAll
(
dir
);
err
!=
nil
{
t
.
Errorf
(
"Unable to clean up test directory %q: %v"
,
dir
,
err
)
}
}()
pairFile
:=
filepath
.
Join
(
dir
,
"kubelet-pair.pem"
)
if
err
:=
ioutil
.
WriteFile
(
pairFile
,
storeCertData
.
certificatePEM
,
0600
);
err
!=
nil
{
t
.
Fatalf
(
"Unable to create the file %q: %v"
,
pairFile
,
err
)
}
if
_
,
_
,
err
:=
loadCertKeyBlocks
(
pairFile
);
err
==
nil
{
t
.
Errorf
(
"Got no error, but expected %q invalid."
,
pairFile
)
}
}
func
TestLoadCertKeyBlocks
(
t
*
testing
.
T
)
{
dir
,
err
:=
ioutil
.
TempDir
(
""
,
"k8s-test-load-cert-key-blocks"
)
if
err
!=
nil
{
t
.
Fatalf
(
"Unable to create the test directory %q: %v"
,
dir
,
err
)
}
defer
func
()
{
if
err
:=
os
.
RemoveAll
(
dir
);
err
!=
nil
{
t
.
Errorf
(
"Unable to clean up test directory %q: %v"
,
dir
,
err
)
}
}()
pairFile
:=
filepath
.
Join
(
dir
,
"kubelet-pair.pem"
)
data
:=
append
(
storeCertData
.
certificatePEM
,
[]
byte
(
"
\n
"
)
...
)
data
=
append
(
data
,
storeCertData
.
keyPEM
...
)
if
err
:=
ioutil
.
WriteFile
(
pairFile
,
data
,
0600
);
err
!=
nil
{
t
.
Fatalf
(
"Unable to create the file %q: %v"
,
pairFile
,
err
)
}
certBlock
,
keyBlock
,
err
:=
loadCertKeyBlocks
(
pairFile
)
if
err
!=
nil
{
t
.
Errorf
(
"Got %v, but expected no error."
,
pairFile
)
}
if
certBlock
.
Type
!=
cert
.
CertificateBlockType
{
t
.
Errorf
(
"Got %q loaded from the pair file, expected a %q."
,
certBlock
.
Type
,
cert
.
CertificateBlockType
)
}
if
keyBlock
.
Type
!=
cert
.
RSAPrivateKeyBlockType
{
t
.
Errorf
(
"Got %q loaded from the pair file, expected a %q."
,
keyBlock
.
Type
,
cert
.
RSAPrivateKeyBlockType
)
}
}
func
TestLoadFile
(
t
*
testing
.
T
)
{
func
TestLoadFile
(
t
*
testing
.
T
)
{
dir
,
err
:=
ioutil
.
TempDir
(
""
,
"k8s-test-load-cert-key-blocks"
)
dir
,
err
:=
ioutil
.
TempDir
(
""
,
"k8s-test-load-cert-key-blocks"
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -280,21 +189,30 @@ func TestLoadFile(t *testing.T) {
...
@@ -280,21 +189,30 @@ func TestLoadFile(t *testing.T) {
}()
}()
pairFile
:=
filepath
.
Join
(
dir
,
"kubelet-pair.pem"
)
pairFile
:=
filepath
.
Join
(
dir
,
"kubelet-pair.pem"
)
data
:=
append
(
storeCertData
.
certificatePEM
,
[]
byte
(
"
\n
"
)
...
)
data
=
append
(
data
,
storeCertData
.
keyPEM
...
)
if
err
:=
ioutil
.
WriteFile
(
pairFile
,
data
,
0600
);
err
!=
nil
{
t
.
Fatalf
(
"Unable to create the file %q: %v"
,
pairFile
,
err
)
}
cert
,
err
:=
loadFile
(
pairFile
)
tests
:=
[]
struct
{
if
err
!=
nil
{
desc
string
t
.
Fatalf
(
"Could not load certificate from disk: %v"
,
err
)
data
[]
byte
}
}{
if
cert
==
nil
{
{
desc
:
"cert and key"
,
data
:
bytes
.
Join
([][]
byte
{
storeCertData
.
certificatePEM
,
storeCertData
.
keyPEM
},
[]
byte
(
"
\n
"
))},
t
.
Fatalf
(
"There was no error, but no certificate data was returned."
)
{
desc
:
"key and cert"
,
data
:
bytes
.
Join
([][]
byte
{
storeCertData
.
keyPEM
,
storeCertData
.
certificatePEM
},
[]
byte
(
"
\n
"
))},
}
}
if
cert
.
Leaf
==
nil
{
for
_
,
tt
:=
range
tests
{
t
.
Fatalf
(
"Got an empty leaf, expected private data."
)
t
.
Run
(
tt
.
desc
,
func
(
t
*
testing
.
T
)
{
if
err
:=
ioutil
.
WriteFile
(
pairFile
,
tt
.
data
,
0600
);
err
!=
nil
{
t
.
Fatalf
(
"Unable to create the file %q: %v"
,
pairFile
,
err
)
}
cert
,
err
:=
loadFile
(
pairFile
)
if
err
!=
nil
{
t
.
Fatalf
(
"Could not load certificate from disk: %v"
,
err
)
}
if
cert
==
nil
{
t
.
Fatalf
(
"There was no error, but no certificate data was returned."
)
}
if
cert
.
Leaf
==
nil
{
t
.
Fatalf
(
"Got an empty leaf, expected private data."
)
}
})
}
}
}
}
...
...
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