-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Po-Yao Chen
committed
Nov 7, 2024
1 parent
e75673b
commit 5af04b2
Showing
8 changed files
with
256 additions
and
27 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package utils | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMemCertReloader_Reload(t *testing.T) { | ||
t.Parallel() | ||
type expect struct { | ||
cert *tls.Certificate | ||
wantErr assert.ErrorAssertionFunc | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
setup func(t *testing.T, certPath, keyPath string) (*MemCertReloader, *expect) | ||
certPath string | ||
keyPath string | ||
wantCert *tls.Certificate | ||
wantErr assert.ErrorAssertionFunc | ||
}{ | ||
{ | ||
name: "happy path", | ||
certPath: "testdata/client.crt", | ||
keyPath: "testdata/client.key", | ||
setup: func(t *testing.T, certPath, keyPath string) (*MemCertReloader, *expect) { | ||
certPEM, err := os.ReadFile(certPath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
keyPEM, err := os.ReadFile(keyPath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
reloader, err := NewCertReloader( | ||
CertReloadConfig{ | ||
CertKeyGetter: func() ([]byte, []byte, error) { | ||
return certPEM, keyPEM, nil | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
wantCrt, err := tls.X509KeyPair(certPEM, keyPEM) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
want := &expect{ | ||
cert: &wantCrt, | ||
wantErr: assert.NoError, | ||
} | ||
return reloader, want | ||
}, | ||
}, | ||
{ | ||
name: "getter error", | ||
certPath: "testdata/invalid.crt", | ||
keyPath: "testdata/invalid.key", | ||
setup: func(t *testing.T, certPath, keyPath string) (*MemCertReloader, *expect) { | ||
reloader := &MemCertReloader{ | ||
getter: func() ([]byte, []byte, error) { | ||
return nil, nil, fmt.Errorf("get error") | ||
}, | ||
} | ||
want := &expect{ | ||
wantErr: assert.Error, | ||
} | ||
return reloader, want | ||
}, | ||
}, | ||
{ | ||
name: "unchanged cert", | ||
certPath: "testdata/client.crt", | ||
keyPath: "testdata/client.key", | ||
setup: func(t *testing.T, certPath, keyPath string) (*MemCertReloader, *expect) { | ||
certPEM, err := os.ReadFile(certPath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
keyPEM, err := os.ReadFile(keyPath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
reloader, err := NewCertReloader( | ||
CertReloadConfig{ | ||
CertKeyGetter: func() ([]byte, []byte, error) { | ||
return certPEM, keyPEM, nil | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
wantCert, err := tls.X509KeyPair(certPEM, keyPEM) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
reloader.cert = &wantCert | ||
want := &expect{ | ||
cert: &wantCert, | ||
wantErr: assert.NoError, | ||
} | ||
return reloader, want | ||
}, | ||
}, | ||
{ | ||
name: "invalid key pair", | ||
certPath: "testdata/ca.crt", | ||
keyPath: "testdata/client.key", | ||
setup: func(t *testing.T, certPath, keyPath string) (*MemCertReloader, *expect) { | ||
certPEM, err := os.ReadFile(certPath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
keyPEM, err := os.ReadFile(keyPath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
reloader := &MemCertReloader{ | ||
getter: func() ([]byte, []byte, error) { | ||
return certPEM, keyPEM, nil | ||
}, | ||
} | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
want := &expect{ | ||
wantErr: assert.Error, | ||
} | ||
return reloader, want | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
reloader, want := tt.setup(t, tt.certPath, tt.keyPath) | ||
gotErr := reloader.Reload() | ||
if !want.wantErr(t, gotErr, "unexpected error") { | ||
return | ||
} | ||
assert.Equal(t, reloader.cert, want.cert, "unexpected result") | ||
}) | ||
} | ||
} |
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,11 @@ | ||
// Copyright 2022 Yahoo Inc. | ||
// Licensed under the terms of the Apache License 2.0. Please see LICENSE file in project root for terms. | ||
|
||
package utils | ||
|
||
//go:generate certstrap init --passphrase "" --common-name "ca" --years 80 | ||
//go:generate certstrap request-cert --passphrase "" --common-name client | ||
//go:generate certstrap sign client --passphrase "" --CA ca --years 80 | ||
//go:generate mkdir -p ./testdata | ||
//go:generate mv -f ./out/ca.crt ./out/client.crt ./out/client.key ./testdata | ||
//go:generate rm -rf ./out |
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,28 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIE3DCCAsSgAwIBAgIBATANBgkqhkiG9w0BAQsFADANMQswCQYDVQQDEwJjYTAg | ||
Fw0yNDExMDcxNzQ4NTFaG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||
IcMTNXYwZiL2dF1uqkMxPVo0w+NRyQtVwdYDdqRCqHKY7TFz7wJPjDrYetm5OwfQ | ||
JritO2h5mqxr+ubg2mWECxhHT9D4N6w0dhUCJX/zbH7QF8mvuEarlQB3ct+Ew868 | ||
DoWbvWD3pDRqD8Fjt1CraXm1FWhR84uPLga8XpOQ+NvJWnQFXWLFaqAtBs3a8eaj | ||
nmptRl0Iue/esTSQBRpeqzu73dzCtkeFrR2Nst1Ycpmbl7cFat1m8tvBwAM7Pbku | ||
0Bom1qT6daOZrvbIDXKAlaBAseT6o892PswWUjRSC7ZqhrUHMQTq1oJs4lxbtQTQ | ||
pnOuVwQLWOv+vlaoCufnysP65zxHAvzMt25L7/yyTmp4f+eixn7YReQg+4px9DeE | ||
2loWjq4YTbEcXCzgJ8HR4uhppHKZXJhB/vx7Qg386zgtRtpa/QeGAtfeUFyCHlmu | ||
P35g9wKaonBtN9DQyFN9sBJ9ugLGZ/YeXwCkPzT3OoylNRM+rr2h1E80fiF76jOz | ||
Gx5Uv7/b5ie5/917MPaJfdk99AZ/VOb5m4HBNqB4O8CmgTdbYJjDJAuK8YBz5rxO | ||
-----END CERTIFICATE----- |
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,24 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIEGzCCAgOgAwIBAgIQK2zwL4S3fDViQnDt0QMyqzANBgkqhkiG9w0BAQsFADAN | ||
MQswCQYDVQQDEwJjYTAgFw0yNDExMDcxNzQ4NTFaGA8yMTA0MTEwNzE3NTg1MVow | ||
ETEPMA0GA1UEAxMGY2xpZW50MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC | ||
AQEAxKRuswos/E15VoODmFJnDKkmgwB4pfdOb1GBi8r/+stRWldkX72DkOYQVJ/T | ||
qshJocMelEItDI/HwP3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do3x8L1oBXsUvPIAQZfV7tgIKk | ||
xl89dyPLYCjAQc9bnAp+YiqS2CRTQDMVmPe+8SpRFLgGjc7YTwTKMdm+lZWj169/ | ||
56KGiU0neQozv92gKPygazzqZ/W/BBTXxsRtSwOHzdQ0SpONeyIdNKqyh+xuc6M= | ||
-----END CERTIFICATE----- |
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,27 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIIEpAIBAAKCAQEAxKRuswos/E15VoODmFJnDKkmgwB4pfdOb1GBi8r/+stRWldk | ||
X72DkOYQVJ/TqshJocMelEItDI/HwP3kQ3tHTjVklp8ekrqxkFxh3lpxAKTMKzwb | ||
PFsLpdBm+jB5E5Rz7DA9d9mWVwxzrwOmRM9FWwiRM8NqjELSvigyf+Q3ZuZsgFIx | ||
HpO5vQ9h8wHrJ4Xx4MJAAltNe8wD5GGFoZ3S+gJaEOqilPl5RXFu6jUbj6tmFTEK | ||
owCoWMeBi9ZQEHavkTbOEdSqVSe4mkvM3Hznn//wgnx1Wxd8BYEpeAYYOAqTg5A0 | ||
hdNOrv67smGcsJpb6TNNEqzcJpvdHwA38FyUlQIDAQABAoIBAHEdcXxWNyhvXIQS | ||
pOlDRXn392pWJBC8YQcR6KZEgNmAiWyhZuDYAE2iufQj+Dt+eTSvK/D23DkkoDB5 | ||
HAxhZtQrTJCEOa8H68pyCe5BMk2/fp7ENZqTePDKH+J9bbiApepQmZtOs/eg9w7O | ||
158vZ+ME25neKHVEnzu5ncsJuYDbu5EFYViL9cftOgKR7tuiDkOx1s6lwvPygKHK | ||
yyv7n7CicnsbSBHyOluqWuCn+jZ7xBDahsPAdI/vxcNuAN/qOQx8taUg+XPQjnP/ | ||
0nM6nYOVx/EPA1HBJzsnXrAkoVeblI18lTF5GXb0QK6dtEJI0xhj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