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

feat(kuma-cp) dataplane certificate rotation #722

Merged
merged 2 commits into from
May 12, 2020
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
173 changes: 138 additions & 35 deletions api/mesh/v1alpha1/mesh.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion api/mesh/v1alpha1/mesh.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package kuma.mesh.v1alpha1;

option go_package = "v1alpha1";

import "google/protobuf/duration.proto";
import "mesh/v1alpha1/metrics.proto";
import "google/protobuf/wrappers.proto";
import "google/protobuf/struct.proto";
Expand Down Expand Up @@ -52,8 +53,22 @@ message CertificateAuthorityBackend {
// builtin and provided)
string type = 2;

// DpCert defines settings for certificates generated for Dataplanes
message DpCert {
// Rotation defines rotation settings for Dataplane certificate
message Rotation {
// Time after which generated certificate for Dataplane will expire
google.protobuf.Duration expiration = 1;
}
// Rotation settings
Rotation rotation = 1;
}

// Dataplane certificate settings
DpCert dpCert = 3;

// Configuration of the backend
google.protobuf.Struct config = 3;
google.protobuf.Struct config = 4;
}

// Tracing defines tracing configuration of the mesh.
Expand Down
23 changes: 19 additions & 4 deletions pkg/core/ca/issuer/issuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/url"
"time"

"github.com/Kong/kuma/pkg/core"

"github.com/pkg/errors"
"github.com/spiffe/spire/pkg/common/x509util"

Expand All @@ -18,10 +20,19 @@ import (
const (
DefaultRsaBits = 2048
DefaultAllowedClockSkew = 10 * time.Second
DefaultWorkloadCertValidityPeriod = 90 * 24 * time.Hour
DefaultWorkloadCertValidityPeriod = 24 * time.Hour
)

func NewWorkloadCert(ca util_tls.KeyPair, mesh string, workload string) (*util_tls.KeyPair, error) {
type CertOptsFn = func(*x509.Certificate)

func WithExpirationTime(expiration time.Duration) CertOptsFn {
return func(certificate *x509.Certificate) {
now := core.Now()
certificate.NotAfter = now.Add(expiration)
}
}

func NewWorkloadCert(ca util_tls.KeyPair, mesh string, workload string, certOpts ...CertOptsFn) (*util_tls.KeyPair, error) {
caPrivateKey, caCert, err := loadKeyPair(ca)
if err != nil {
return nil, errors.Wrap(err, "failed to load CA key pair")
Expand All @@ -31,14 +42,14 @@ func NewWorkloadCert(ca util_tls.KeyPair, mesh string, workload string) (*util_t
if err != nil {
return nil, errors.Wrap(err, "failed to generate a private key")
}
workloadCert, err := newWorkloadCert(caPrivateKey, caCert, mesh, workload, workloadKey.Public())
workloadCert, err := newWorkloadCert(caPrivateKey, caCert, mesh, workload, workloadKey.Public(), certOpts...)
if err != nil {
return nil, errors.Wrap(err, "failed to generate X509 certificate")
}
return util_tls.ToKeyPair(workloadKey, workloadCert)
}

func newWorkloadCert(signer crypto.PrivateKey, parent *x509.Certificate, trustDomain string, workload string, publicKey crypto.PublicKey) ([]byte, error) {
func newWorkloadCert(signer crypto.PrivateKey, parent *x509.Certificate, trustDomain string, workload string, publicKey crypto.PublicKey, certOpts ...CertOptsFn) ([]byte, error) {
spiffeID := &url.URL{
Scheme: "spiffe",
Host: trustDomain,
Expand All @@ -59,6 +70,10 @@ func newWorkloadCert(signer crypto.PrivateKey, parent *x509.Certificate, trustDo
return nil, err
}

for _, opt := range certOpts {
opt(template)
}

return x509.CreateCertificate(rand.Reader, template, parent, publicKey, signer)
}

Expand Down
11 changes: 8 additions & 3 deletions pkg/plugins/ca/builtin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"fmt"

core_model "github.com/Kong/kuma/pkg/core/resources/model"

"github.com/golang/protobuf/ptypes/wrappers"
"github.com/pkg/errors"

Expand All @@ -14,8 +12,10 @@ import (
core_ca "github.com/Kong/kuma/pkg/core/ca"
ca_issuer "github.com/Kong/kuma/pkg/core/ca/issuer"
core_system "github.com/Kong/kuma/pkg/core/resources/apis/system"
core_model "github.com/Kong/kuma/pkg/core/resources/model"
core_store "github.com/Kong/kuma/pkg/core/resources/store"
secret_manager "github.com/Kong/kuma/pkg/core/secrets/manager"
util_proto "github.com/Kong/kuma/pkg/util/proto"
)

type builtinCaManager struct {
Expand Down Expand Up @@ -104,7 +104,12 @@ func (b *builtinCaManager) GenerateDataplaneCert(ctx context.Context, mesh strin
return core_ca.KeyPair{}, errors.Wrapf(err, "failed to load CA key pair for Mesh %q and backend %q", mesh, backend.Name)
}

keyPair, err := ca_issuer.NewWorkloadCert(ca, mesh, service)
var opts []ca_issuer.CertOptsFn
if backend.GetDpCert().GetRotation().GetExpiration() != nil {
duration := util_proto.ToDuration(*backend.GetDpCert().GetRotation().Expiration)
opts = append(opts, ca_issuer.WithExpirationTime(duration))
}
keyPair, err := ca_issuer.NewWorkloadCert(ca, mesh, service, opts...)
if err != nil {
return core_ca.KeyPair{}, errors.Wrapf(err, "failed to generate a Workload Identity cert for workload %q in Mesh %q using backend %q", service, mesh, backend)
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/plugins/ca/builtin/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import (
"context"
"crypto/x509"
"encoding/pem"
"time"

"github.com/golang/protobuf/ptypes/duration"

mesh_proto "github.com/Kong/kuma/api/mesh/v1alpha1"
"github.com/Kong/kuma/pkg/core"
core_ca "github.com/Kong/kuma/pkg/core/ca"
"github.com/Kong/kuma/pkg/core/resources/apis/system"
core_store "github.com/Kong/kuma/pkg/core/resources/store"
Expand All @@ -24,11 +28,20 @@ var _ = Describe("Builtin CA Manager", func() {
var secretManager secret_manager.SecretManager
var caManager core_ca.Manager

now := time.Now()

BeforeEach(func() {
core.Now = func() time.Time {
return now
}
secretManager = secret_manager.NewSecretManager(store.NewSecretStore(memory.NewStore()), cipher.None())
caManager = builtin.NewBuiltinCaManager(secretManager)
})

AfterEach(func() {
core.Now = time.Now
})

Context("Ensure", func() {
It("should create a CA", func() {
//given
Expand Down Expand Up @@ -106,6 +119,13 @@ var _ = Describe("Builtin CA Manager", func() {
backend := mesh_proto.CertificateAuthorityBackend{
Name: "builtin-1",
Type: "builtin",
DpCert: &mesh_proto.CertificateAuthorityBackend_DpCert{
Rotation: &mesh_proto.CertificateAuthorityBackend_DpCert_Rotation{
Expiration: &duration.Duration{
Seconds: 1,
},
},
},
}
err := caManager.Ensure(context.Background(), mesh, backend)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -124,6 +144,7 @@ var _ = Describe("Builtin CA Manager", func() {
Expect(err).ToNot(HaveOccurred())
Expect(cert.URIs).To(HaveLen(1))
Expect(cert.URIs[0].String()).To(Equal("spiffe://default/web"))
Expect(cert.NotAfter).To(Equal(now.UTC().Truncate(time.Second).Add(1 * time.Second))) // time in cert is in UTC and truncated to seconds
})

It("should throw an error on generate dataplane certs on non-existing CA", func() {
Expand Down
Loading