generated from pulumi/pulumi-provider-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor certificate generation in tests The process of generating certificates for testing has been refactored. The responsibility of creating the certificates has been moved from the lifecycle test to the provider suite setup. This change simplifies the lifecycle test and makes it easier to manage certificates across different tests. Additionally, a new method 'WithCerts' was introduced in TestHost interface replacing 'CreateCertBundle'. * The CAs are swapped but basically everything else is wired up * mTLS actually? * Rename CA in most of the spots where it matters * Fix comment
- Loading branch information
1 parent
1302582
commit 849c812
Showing
9 changed files
with
136 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,62 @@ | ||
package provider | ||
|
||
import ( | ||
"strings" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"errors" | ||
"fmt" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials" | ||
"google.golang.org/grpc/credentials/insecure" | ||
) | ||
|
||
type Config struct { | ||
Address string `pulumi:"address"` | ||
Port string `pulumi:"port,optional"` | ||
CaPem string `pulumi:"caPem,optional"` | ||
CertPem string `pulumi:"certPem,optional"` | ||
KeyPem string `pulumi:"keyPem,optional"` | ||
} | ||
|
||
func (c Config) NewGrpcClient() (*grpc.ClientConn, error) { | ||
parts := []string{} | ||
if c.Address != "" { | ||
parts = append(parts, c.Address) | ||
} | ||
target := c.Address | ||
if c.Port != "" { | ||
parts = append(parts, c.Port) | ||
target = target + ":" + c.Port | ||
} | ||
|
||
creds, err := c.TransportCredentials() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
target := strings.Join(parts, ":") | ||
return grpc.NewClient(target, | ||
grpc.WithTransportCredentials(insecure.NewCredentials()), | ||
grpc.WithTransportCredentials(creds), | ||
) | ||
} | ||
|
||
func (c Config) TransportCredentials() (credentials.TransportCredentials, error) { | ||
if c.CaPem == "" && c.CertPem == "" && c.KeyPem == "" { | ||
return insecure.NewCredentials(), nil | ||
} | ||
|
||
if c.CaPem != "" && c.CertPem != "" && c.KeyPem != "" { | ||
cert, err := tls.X509KeyPair([]byte(c.CertPem), []byte(c.KeyPem)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse X509 key pair: %w", err) | ||
} | ||
|
||
ca := x509.NewCertPool() | ||
if ok := ca.AppendCertsFromPEM([]byte(c.CaPem)); !ok { | ||
return nil, errors.New("failed to append ca cert") | ||
} | ||
|
||
return credentials.NewTLS(&tls.Config{ | ||
ServerName: "provisioner", | ||
Certificates: []tls.Certificate{cert}, | ||
RootCAs: ca, | ||
}), nil | ||
} | ||
|
||
return nil, errors.New("caPem, certPem, and keyPem must all be set together") | ||
} |
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,20 @@ | ||
package tests | ||
|
||
import ( | ||
"github.com/mdelapenya/tlscert" | ||
) | ||
|
||
type CertBundle struct { | ||
Ca *tlscert.Certificate | ||
Cert *tlscert.Certificate | ||
} | ||
|
||
func ServerCerts() (*CertBundle, error) { | ||
ca := tlscert.SelfSignedCA("test-ca") | ||
|
||
req := tlscert.NewRequest("test-cert") | ||
req.Parent = ca | ||
cert := tlscert.SelfSignedFromRequest(req) | ||
|
||
return &CertBundle{ca, 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
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