Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop gratuitous sync.Once in google CAs. #258

Merged
merged 1 commit into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/app/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ var serveCmd = &cobra.Command{
version := viper.GetString("gcp_private_ca_version")
switch version {
case "v1":
baseca, err = googlecav1.NewCertAuthorityService(viper.GetString("gcp_private_ca_parent"))
baseca, err = googlecav1.NewCertAuthorityService(cmd.Context(), viper.GetString("gcp_private_ca_parent"))
case "v1beta1":
baseca, err = googlecav1beta1.NewCertAuthorityService(viper.GetString("gcp_private_ca_parent"))
baseca, err = googlecav1beta1.NewCertAuthorityService(cmd.Context(), viper.GetString("gcp_private_ca_parent"))
default:
err = fmt.Errorf("invalid value for gcp_private_ca_version: %v", version)
}
Expand Down
29 changes: 6 additions & 23 deletions pkg/ca/googleca/v1/googleca.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"sync"

privateca "cloud.google.com/go/security/privateca/apiv1"
"github.com/sigstore/fulcio/pkg/ca"
Expand All @@ -33,36 +32,20 @@ import (
"google.golang.org/protobuf/types/known/durationpb"
)

var (
once sync.Once
c *privateca.CertificateAuthorityClient
cErr error
)

type CertAuthorityService struct {
parent string
client *privateca.CertificateAuthorityClient
}

func NewCertAuthorityService(parent string) (*CertAuthorityService, error) {
cas := &CertAuthorityService{
parent: parent,
}
var err error
cas.client, err = casClient()
func NewCertAuthorityService(ctx context.Context, parent string) (*CertAuthorityService, error) {
client, err := privateca.NewCertificateAuthorityClient(ctx)
if err != nil {
return nil, err
}
return cas, nil
}

func casClient() (*privateca.CertificateAuthorityClient, error) {
// Use a once block to avoid creating a new client every time.
once.Do(func() {
c, cErr = privateca.NewCertificateAuthorityClient(context.Background())
})

return c, cErr
return &CertAuthorityService{
parent: parent,
client: client,
}, nil
}

// getPubKeyFormat Returns the PublicKey KeyFormat required by gcp privateca.
Expand Down
29 changes: 6 additions & 23 deletions pkg/ca/googleca/v1beta1/googleca.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"sync"

privateca "cloud.google.com/go/security/privateca/apiv1beta1"
"github.com/sigstore/fulcio/pkg/ca"
Expand All @@ -33,36 +32,20 @@ import (
"google.golang.org/protobuf/types/known/durationpb"
)

var (
once sync.Once
c *privateca.CertificateAuthorityClient
cErr error
)

type CertAuthorityService struct {
parent string
client *privateca.CertificateAuthorityClient
}

func NewCertAuthorityService(parent string) (*CertAuthorityService, error) {
cas := &CertAuthorityService{
parent: parent,
}
var err error
cas.client, err = casClient()
func NewCertAuthorityService(ctx context.Context, parent string) (*CertAuthorityService, error) {
client, err := privateca.NewCertificateAuthorityClient(ctx)
if err != nil {
return nil, err
}
return cas, nil
}

func casClient() (*privateca.CertificateAuthorityClient, error) {
// Use a once block to avoid creating a new client every time.
once.Do(func() {
c, cErr = privateca.NewCertificateAuthorityClient(context.Background())
})

return c, cErr
return &CertAuthorityService{
parent: parent,
client: client,
}, nil
}

// getPubKeyType Returns the PublicKey type required by gcp privateca (to handle both PEM_RSA_KEY / PEM_EC_KEY)
Expand Down