Skip to content

Commit

Permalink
move generate cert to separate func without dependency on k8s or dapr…
Browse files Browse the repository at this point in the history
… config

Signed-off-by: Anton Troshin <[email protected]>
  • Loading branch information
antontroshin committed Feb 15, 2025
1 parent 696a80b commit 980a544
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 1 addition & 3 deletions cmd/generate_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ dapr mtls generate-certificate --valid-until <no of days>
dapr mtls generate-certificate --valid-until <no of days> --out ./certs
`,
Run: func(cmd *cobra.Command, args []string) {
rootCertBytes, issuerCertBytes, issuerKeyBytes, err := kubernetes.GenerateNewCertificates(
time.Hour*time.Duration(certificateValidUntil*24), //nolint:gosec
"")
rootCertBytes, issuerCertBytes, issuerKeyBytes, err := kubernetes.GenerateNewCertificatesStandalone(time.Hour * time.Duration(certificateValidUntil*24)) //nolint:gosec
if err != nil {
print.FailureStatusEvent(os.Stderr, fmt.Sprintf("error generating cert: %s", err))
os.Exit(1)
Expand Down
17 changes: 17 additions & 0 deletions pkg/kubernetes/renew_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,20 @@ func GenerateNewCertificates(validUntil time.Duration, privateKeyFile string) ([

return bundle.TrustAnchors, bundle.IssChainPEM, bundle.IssKeyPEM, nil
}

// GenerateNewCertificatesStandalone Generate certificates for local mtls generate-certificate command
// without dependency on kubernetes and dapr control plane configuration
func GenerateNewCertificatesStandalone(validUntil time.Duration) ([]byte, []byte, []byte, error) {
rootKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, nil, nil, err
}

var allowedClockSkew time.Duration
bundle, err := ca.GenerateBundle(rootKey, "cluster.local", allowedClockSkew, &validUntil)
if err != nil {
return nil, nil, nil, err
}

return bundle.TrustAnchors, bundle.IssChainPEM, bundle.IssKeyPEM, nil
}

0 comments on commit 980a544

Please sign in to comment.