-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use dynamic NodeAddresses/HostnameStatus in Kubernetes certs
This is a PR on a path towards removing `ApplyDynamicConfig`. This fixes Kubernetes API server certificate generation to use dynamic data to generate cert with proper SANs for IPs of the node. As part of that refactored a bit apid certificate generation (without any changes). Added two unit-tests for apid and Kubernetes certificate generation. Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
- Loading branch information
Showing
12 changed files
with
675 additions
and
82 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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package secrets | ||
|
||
import "net" | ||
|
||
// AltNames defines certificate alternative names. | ||
type AltNames struct { | ||
IPs []net.IP | ||
DNSNames []string | ||
} | ||
|
||
// Append list of SANs splitting into IPs/DNS names. | ||
func (altNames *AltNames) Append(sans ...string) { | ||
for _, san := range sans { | ||
if ip := net.ParseIP(san); ip != nil { | ||
altNames.AppendIPs(ip) | ||
} else { | ||
altNames.AppendDNSNames(san) | ||
} | ||
} | ||
} | ||
|
||
// AppendIPs skipping duplicates. | ||
func (altNames *AltNames) AppendIPs(ips ...net.IP) { | ||
for _, ip := range ips { | ||
found := false | ||
|
||
for _, addr := range altNames.IPs { | ||
if addr.Equal(ip) { | ||
found = true | ||
|
||
break | ||
} | ||
} | ||
|
||
if !found { | ||
altNames.IPs = append(altNames.IPs, ip) | ||
} | ||
} | ||
} | ||
|
||
// AppendDNSNames skipping duplicates. | ||
func (altNames *AltNames) AppendDNSNames(dnsNames ...string) { | ||
for _, dnsName := range dnsNames { | ||
found := false | ||
|
||
for _, name := range altNames.DNSNames { | ||
if name == dnsName { | ||
found = true | ||
|
||
break | ||
} | ||
} | ||
|
||
if !found { | ||
altNames.DNSNames = append(altNames.DNSNames, dnsName) | ||
} | ||
} | ||
} |
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
164 changes: 164 additions & 0 deletions
164
internal/app/machined/pkg/controllers/secrets/api_test.go
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,164 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
//nolint:dupl | ||
package secrets_test | ||
|
||
import ( | ||
"context" | ||
stdlibx509 "crypto/x509" | ||
"log" | ||
"net" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/cosi-project/runtime/pkg/controller/runtime" | ||
"github.com/cosi-project/runtime/pkg/resource" | ||
"github.com/cosi-project/runtime/pkg/state" | ||
"github.com/cosi-project/runtime/pkg/state/impl/inmem" | ||
"github.com/cosi-project/runtime/pkg/state/impl/namespaced" | ||
"github.com/stretchr/testify/suite" | ||
"github.com/talos-systems/crypto/x509" | ||
"github.com/talos-systems/go-retry/retry" | ||
"inet.af/netaddr" | ||
|
||
secretsctrl "github.com/talos-systems/talos/internal/app/machined/pkg/controllers/secrets" | ||
"github.com/talos-systems/talos/pkg/logging" | ||
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine" | ||
"github.com/talos-systems/talos/pkg/machinery/role" | ||
"github.com/talos-systems/talos/pkg/resources/config" | ||
"github.com/talos-systems/talos/pkg/resources/k8s" | ||
"github.com/talos-systems/talos/pkg/resources/network" | ||
"github.com/talos-systems/talos/pkg/resources/secrets" | ||
) | ||
|
||
type APISuite struct { | ||
suite.Suite | ||
|
||
state state.State | ||
|
||
runtime *runtime.Runtime | ||
wg sync.WaitGroup | ||
|
||
ctx context.Context | ||
ctxCancel context.CancelFunc | ||
} | ||
|
||
func (suite *APISuite) SetupTest() { | ||
suite.ctx, suite.ctxCancel = context.WithTimeout(context.Background(), 3*time.Minute) | ||
|
||
suite.state = state.WrapCore(namespaced.NewState(inmem.Build)) | ||
|
||
var err error | ||
|
||
suite.runtime, err = runtime.NewRuntime(suite.state, logging.Wrap(log.Writer())) | ||
suite.Require().NoError(err) | ||
|
||
suite.Require().NoError(suite.runtime.RegisterController(&secretsctrl.APIController{})) | ||
|
||
suite.startRuntime() | ||
} | ||
|
||
func (suite *APISuite) startRuntime() { | ||
suite.wg.Add(1) | ||
|
||
go func() { | ||
defer suite.wg.Done() | ||
|
||
suite.Assert().NoError(suite.runtime.Run(suite.ctx)) | ||
}() | ||
} | ||
|
||
func (suite *APISuite) TestReconcileControlPlane() { | ||
rootSecrets := secrets.NewRoot(secrets.RootOSID) | ||
|
||
talosCA, err := x509.NewSelfSignedCertificateAuthority( | ||
x509.Organization("talos"), | ||
) | ||
suite.Require().NoError(err) | ||
|
||
rootSecrets.OSSpec().CA = &x509.PEMEncodedCertificateAndKey{ | ||
Crt: talosCA.CrtPEM, | ||
Key: talosCA.KeyPEM, | ||
} | ||
rootSecrets.OSSpec().CertSANDNSNames = []string{"example.com"} | ||
rootSecrets.OSSpec().CertSANIPs = []netaddr.IP{netaddr.MustParseIP("10.4.3.2"), netaddr.MustParseIP("10.2.1.3")} | ||
rootSecrets.OSSpec().Token = "something" | ||
suite.Require().NoError(suite.state.Create(suite.ctx, rootSecrets)) | ||
|
||
machineType := config.NewMachineType() | ||
machineType.SetMachineType(machine.TypeControlPlane) | ||
suite.Require().NoError(suite.state.Create(suite.ctx, machineType)) | ||
|
||
networkStatus := network.NewStatus(network.NamespaceName, network.StatusID) | ||
networkStatus.TypedSpec().AddressReady = true | ||
networkStatus.TypedSpec().HostnameReady = true | ||
suite.Require().NoError(suite.state.Create(suite.ctx, networkStatus)) | ||
|
||
hostnameStatus := network.NewHostnameStatus(network.NamespaceName, network.HostnameID) | ||
hostnameStatus.TypedSpec().Hostname = "foo" | ||
hostnameStatus.TypedSpec().Domainname = "example.com" | ||
suite.Require().NoError(suite.state.Create(suite.ctx, hostnameStatus)) | ||
|
||
nodeAddresses := network.NewNodeAddress(network.NamespaceName, network.FilteredNodeAddressID(network.NodeAddressAccumulativeID, k8s.NodeAddressFilterNoK8s)) | ||
nodeAddresses.TypedSpec().Addresses = []netaddr.IP{netaddr.MustParseIP("10.2.1.3"), netaddr.MustParseIP("172.16.0.1")} | ||
suite.Require().NoError(suite.state.Create(suite.ctx, nodeAddresses)) | ||
|
||
suite.Assert().NoError(retry.Constant(10*time.Second, retry.WithUnits(100*time.Millisecond)).Retry( | ||
func() error { | ||
certs, err := suite.state.Get(suite.ctx, resource.NewMetadata(secrets.NamespaceName, secrets.APIType, secrets.APIID, resource.VersionUndefined)) | ||
if err != nil { | ||
if state.IsNotFoundError(err) { | ||
return retry.ExpectedError(err) | ||
} | ||
|
||
return err | ||
} | ||
|
||
apiCerts := certs.(*secrets.API).TypedSpec() | ||
|
||
suite.Assert().Equal(talosCA.CrtPEM, apiCerts.CA.Crt) | ||
suite.Assert().Nil(apiCerts.CA.Key) | ||
|
||
serverCert, err := apiCerts.Server.GetCert() | ||
suite.Require().NoError(err) | ||
|
||
suite.Assert().Equal([]string{"example.com", "foo", "foo.example.com"}, serverCert.DNSNames) | ||
suite.Assert().Equal([]net.IP{net.ParseIP("10.4.3.2").To4(), net.ParseIP("10.2.1.3").To4(), net.ParseIP("172.16.0.1").To4()}, serverCert.IPAddresses) | ||
|
||
suite.Assert().Equal("foo.example.com", serverCert.Subject.CommonName) | ||
suite.Assert().Empty(serverCert.Subject.Organization) | ||
|
||
suite.Assert().Equal(stdlibx509.KeyUsageDigitalSignature|stdlibx509.KeyUsageKeyEncipherment, serverCert.KeyUsage) | ||
suite.Assert().Equal([]stdlibx509.ExtKeyUsage{stdlibx509.ExtKeyUsageServerAuth}, serverCert.ExtKeyUsage) | ||
|
||
clientCert, err := apiCerts.Client.GetCert() | ||
suite.Require().NoError(err) | ||
|
||
suite.Assert().Empty(clientCert.DNSNames) | ||
suite.Assert().Empty(clientCert.IPAddresses) | ||
|
||
suite.Assert().Equal("foo.example.com", clientCert.Subject.CommonName) | ||
suite.Assert().Equal([]string{string(role.Impersonator)}, clientCert.Subject.Organization) | ||
|
||
suite.Assert().Equal(stdlibx509.KeyUsageDigitalSignature|stdlibx509.KeyUsageKeyEncipherment, clientCert.KeyUsage) | ||
suite.Assert().Equal([]stdlibx509.ExtKeyUsage{stdlibx509.ExtKeyUsageClientAuth}, clientCert.ExtKeyUsage) | ||
|
||
return nil | ||
}, | ||
)) | ||
} | ||
|
||
func (suite *APISuite) TearDownTest() { | ||
suite.T().Log("tear down") | ||
|
||
suite.ctxCancel() | ||
|
||
suite.wg.Wait() | ||
} | ||
|
||
func TestAPISuite(t *testing.T) { | ||
suite.Run(t, new(APISuite)) | ||
} |
Oops, something went wrong.