-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added cert meta data to payload (#926)
* added cert meta data to config payload if specified in the config
- Loading branch information
1 parent
b35fdab
commit f010cf3
Showing
6 changed files
with
389 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) F5, Inc. | ||
// | ||
// This source code is licensed under the Apache License, Version 2.0 license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
package cert | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func LoadCertificates(certPath, keyPath string) (*tls.Certificate, *x509.CertPool, error) { | ||
cert, err := tls.LoadX509KeyPair(certPath, keyPath) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
cert.Leaf, err = x509.ParseCertificate(cert.Certificate[0]) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
pool := x509.NewCertPool() | ||
pool.AddCert(cert.Leaf) | ||
|
||
return &cert, pool, nil | ||
} | ||
|
||
func LoadCertificate(certPath string) (*x509.Certificate, error) { | ||
fileContents, err := os.ReadFile(certPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
certPEMBlock, _ := pem.Decode(fileContents) | ||
if certPEMBlock == nil { | ||
return nil, fmt.Errorf("could not decode: cert was not PEM format") | ||
} | ||
|
||
cert, err := x509.ParseCertificate(certPEMBlock.Bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return cert, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright (c) F5, Inc. | ||
// | ||
// This source code is licensed under the Apache License, Version 2.0 license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
package cert | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nginx/agent/v3/test/helpers" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
keyFileName = "key.pem" | ||
certFileName = "cert.pem" | ||
caFileName = "ca.pem" | ||
nonPemCertFileName = "cert.nonpem" | ||
certificateType = "CERTIFICATE" | ||
privateKeyType = "RSA PRIVATE KEY" | ||
) | ||
|
||
func TestLoadCertificates(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
|
||
key, cert := helpers.GenerateSelfSignedCert(t) | ||
|
||
keyContents := helpers.Cert{Name: keyFileName, Type: privateKeyType, Contents: key} | ||
certContents := helpers.Cert{Name: certFileName, Type: certificateType, Contents: cert} | ||
|
||
keyFile := helpers.WriteCertFiles(t, tmpDir, keyContents) | ||
certFile := helpers.WriteCertFiles(t, tmpDir, certContents) | ||
|
||
testCases := []struct { | ||
testName string | ||
certFile string | ||
keyFile string | ||
isError bool | ||
}{ | ||
{ | ||
testName: "valid files", | ||
certFile: certFile, | ||
keyFile: keyFile, | ||
isError: false, | ||
}, | ||
{ | ||
testName: "invalid cert file", | ||
certFile: "/invalid/cert.pem", | ||
keyFile: keyFile, | ||
isError: true, | ||
}, | ||
{ | ||
testName: "invalid key file", | ||
certFile: certFile, | ||
keyFile: "/invalid/key.pem", | ||
isError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.testName, func(t *testing.T) { | ||
certificate, pool, loadErr := LoadCertificates(tc.certFile, tc.keyFile) | ||
if tc.isError { | ||
assert.Nil(t, certificate) | ||
assert.Nil(t, pool) | ||
require.Error(t, loadErr) | ||
} else { | ||
assert.Equal(t, cert, certificate.Certificate[0]) | ||
assert.NotNil(t, pool) | ||
require.NoError(t, loadErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestLoadCertificate(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
|
||
_, cert := helpers.GenerateSelfSignedCert(t) | ||
|
||
certContents := helpers.Cert{Name: certFileName, Type: certificateType, Contents: cert} | ||
certNonPemContents := helpers.Cert{Name: nonPemCertFileName, Type: "", Contents: cert} | ||
|
||
certFile := helpers.WriteCertFiles(t, tmpDir, certContents) | ||
nonPEMFile := helpers.WriteCertFiles(t, tmpDir, certNonPemContents) | ||
require.NotEmpty(t, nonPEMFile) | ||
|
||
helpers.CreateFileWithErrorCheck(t, tmpDir, nonPemCertFileName) | ||
|
||
testCases := []struct { | ||
testName string | ||
certFile string | ||
isError bool | ||
}{ | ||
{ | ||
testName: "valid cert file", | ||
certFile: certFile, | ||
isError: false, | ||
}, | ||
{ | ||
testName: "invalid cert file", | ||
certFile: "/invalid/cert.pem", | ||
isError: true, | ||
}, | ||
{ | ||
testName: "non-PEM cert file", | ||
certFile: "", | ||
isError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.testName, func(t *testing.T) { | ||
certificate, loadErr := LoadCertificate(tc.certFile) | ||
if tc.isError { | ||
assert.Nil(t, certificate) | ||
require.Error(t, loadErr) | ||
} else { | ||
assert.Equal(t, cert, certificate.Raw) | ||
require.NoError(t, loadErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.