|
1 | 1 | package hashicorpvault
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "crypto/tls"
|
5 | 6 | "crypto/x509"
|
6 | 7 | "fmt"
|
@@ -607,7 +608,69 @@ func TestConfigureTLSRequireClientCertAndKey(t *testing.T) {
|
607 | 608 | spiretest.RequireGRPCStatus(t, err, codes.InvalidArgument, "both client cert and client key are required")
|
608 | 609 | }
|
609 | 610 |
|
610 |
| -// TODO: Test CreateKey |
| 611 | +func TestCreateKey(t *testing.T) { |
| 612 | + fakeVaultServer := newFakeVaultServer() |
| 613 | + fakeVaultServer.CertAuthResponseCode = 200 |
| 614 | + fakeVaultServer.CertAuthResponse = []byte(testCertAuthResponse) |
| 615 | + fakeVaultServer.CreateKeyResponseCode = 204 |
| 616 | + |
| 617 | + s, addr, err := fakeVaultServer.NewTLSServer() |
| 618 | + require.NoError(t, err) |
| 619 | + |
| 620 | + s.Start() |
| 621 | + defer s.Close() |
| 622 | + |
| 623 | + cp := &ClientParams{ |
| 624 | + VaultAddr: fmt.Sprintf("https://%v/", addr), |
| 625 | + CACertPath: testRootCert, |
| 626 | + ClientCertPath: testClientCert, |
| 627 | + ClientKeyPath: testClientKey, |
| 628 | + } |
| 629 | + |
| 630 | + cc, err := NewClientConfig(cp, hclog.Default()) |
| 631 | + require.NoError(t, err) |
| 632 | + |
| 633 | + renewCh := make(chan struct{}) |
| 634 | + client, err := cc.NewAuthenticatedClient(CERT, renewCh) |
| 635 | + require.NoError(t, err) |
| 636 | + |
| 637 | + err = client.CreateKey(context.Background(), "x509-CA-A", TransitKeyTypeRSA2048) |
| 638 | + require.NoError(t, err) |
| 639 | +} |
| 640 | + |
| 641 | +func TestCreateKeyErrorFromEndpoint(t *testing.T) { |
| 642 | + fakeVaultServer := newFakeVaultServer() |
| 643 | + fakeVaultServer.CertAuthResponseCode = 200 |
| 644 | + fakeVaultServer.CertAuthResponse = []byte(testCertAuthResponse) |
| 645 | + fakeVaultServer.CreateKeyResponseCode = 500 |
| 646 | + fakeVaultServer.CreateKeyResponse = []byte("test error") |
| 647 | + |
| 648 | + s, addr, err := fakeVaultServer.NewTLSServer() |
| 649 | + require.NoError(t, err) |
| 650 | + |
| 651 | + s.Start() |
| 652 | + defer s.Close() |
| 653 | + |
| 654 | + retry := 0 // Disable retry |
| 655 | + cp := &ClientParams{ |
| 656 | + MaxRetries: &retry, |
| 657 | + VaultAddr: fmt.Sprintf("https://%v/", addr), |
| 658 | + CACertPath: testRootCert, |
| 659 | + ClientCertPath: testClientCert, |
| 660 | + ClientKeyPath: testClientKey, |
| 661 | + } |
| 662 | + |
| 663 | + cc, err := NewClientConfig(cp, hclog.Default()) |
| 664 | + require.NoError(t, err) |
| 665 | + |
| 666 | + renewCh := make(chan struct{}) |
| 667 | + client, err := cc.NewAuthenticatedClient(CERT, renewCh) |
| 668 | + require.NoError(t, err) |
| 669 | + |
| 670 | + err = client.CreateKey(context.Background(), "x509-CA-A", TransitKeyTypeRSA2048) |
| 671 | + spiretest.RequireGRPCStatusHasPrefix(t, err, codes.Internal, "failed to create transit engine key: Error making API request.") |
| 672 | +} |
| 673 | + |
611 | 674 | // TODO: Test GetKey
|
612 | 675 | // TODO: Test SignData
|
613 | 676 |
|
|
0 commit comments