-
Notifications
You must be signed in to change notification settings - Fork 1.5k
CNTRLPLANE-2012: Add PKI config types, validation, and CR manifest generation #10593
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
Open
hasbro17
wants to merge
1
commit into
openshift:main
Choose a base branch
from
hasbro17:pki-1-types-and-manifest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,200
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,102 @@ | ||
| package manifests | ||
|
|
||
| import ( | ||
| "context" | ||
| "path" | ||
|
|
||
| "github.com/pkg/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "sigs.k8s.io/yaml" | ||
|
|
||
| configv1alpha1 "github.com/openshift/api/config/v1alpha1" | ||
| features "github.com/openshift/api/features" | ||
| "github.com/openshift/installer/pkg/asset" | ||
| "github.com/openshift/installer/pkg/asset/installconfig" | ||
| pkidefaults "github.com/openshift/installer/pkg/types/pki" | ||
| ) | ||
|
|
||
| var pkiCfgFilename = path.Join(manifestDir, "cluster-pki-02-config.yaml") | ||
|
|
||
| // PKIConfiguration generates the PKI custom resource manifest. | ||
| type PKIConfiguration struct { | ||
| FileList []*asset.File | ||
| } | ||
|
|
||
| var _ asset.WritableAsset = (*PKIConfiguration)(nil) | ||
|
|
||
| // Name returns a human friendly name for the asset. | ||
| func (*PKIConfiguration) Name() string { | ||
| return "PKI Config" | ||
| } | ||
|
|
||
| // Dependencies returns all of the dependencies directly needed to generate | ||
| // the asset. | ||
| func (*PKIConfiguration) Dependencies() []asset.Asset { | ||
| return []asset.Asset{ | ||
| &installconfig.InstallConfig{}, | ||
| } | ||
| } | ||
|
|
||
| // Generate generates the PKI custom resource manifest. | ||
| // The manifest is only generated when the ConfigurablePKI feature gate is enabled. | ||
| func (p *PKIConfiguration) Generate(_ context.Context, dependencies asset.Parents) error { | ||
| installConfig := &installconfig.InstallConfig{} | ||
| dependencies.Get(installConfig) | ||
|
|
||
| if !installConfig.Config.Enabled(features.FeatureGateConfigurablePKI) { | ||
| return nil | ||
| } | ||
|
|
||
| certMgmt := configv1alpha1.PKICertificateManagement{ | ||
| Mode: configv1alpha1.PKICertificateManagementModeDefault, | ||
| } | ||
|
|
||
| if installConfig.Config.PKI != nil { | ||
| profile := pkidefaults.DefaultPKIProfile() | ||
| profile.SignerCertificates = pkidefaults.CertificateConfigToUpstream(installConfig.Config.PKI.SignerCertificates) | ||
|
|
||
| certMgmt = configv1alpha1.PKICertificateManagement{ | ||
| Mode: configv1alpha1.PKICertificateManagementModeCustom, | ||
| Custom: configv1alpha1.CustomPKIPolicy{ | ||
| PKIProfile: profile, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| config := &configv1alpha1.PKI{ | ||
| TypeMeta: metav1.TypeMeta{ | ||
| APIVersion: configv1alpha1.SchemeGroupVersion.String(), | ||
| Kind: "PKI", | ||
| }, | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "cluster", | ||
| }, | ||
| Spec: configv1alpha1.PKISpec{ | ||
| CertificateManagement: certMgmt, | ||
| }, | ||
| } | ||
|
|
||
| configData, err := yaml.Marshal(config) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "failed to marshal PKI config") | ||
| } | ||
|
|
||
| p.FileList = []*asset.File{ | ||
| { | ||
| Filename: pkiCfgFilename, | ||
| Data: configData, | ||
| }, | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Files returns the files generated by the asset. | ||
| func (p *PKIConfiguration) Files() []*asset.File { | ||
| return p.FileList | ||
| } | ||
|
|
||
| // Load returns false since this asset is not written to disk by the installer. | ||
| func (p *PKIConfiguration) Load(f asset.FileFetcher) (bool, error) { | ||
| return false, nil | ||
| } |
This file contains hidden or 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 @@ | ||
| package manifests | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "sigs.k8s.io/yaml" | ||
|
|
||
| configv1 "github.com/openshift/api/config/v1" | ||
| configv1alpha1 "github.com/openshift/api/config/v1alpha1" | ||
| "github.com/openshift/installer/pkg/asset" | ||
| "github.com/openshift/installer/pkg/asset/installconfig" | ||
| "github.com/openshift/installer/pkg/types" | ||
| ) | ||
|
|
||
| func TestPKIConfigurationGenerate(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| installConfig *types.InstallConfig | ||
| expectEmpty bool | ||
| expectMode configv1alpha1.PKICertificateManagementMode | ||
| expectSignerAlgo configv1alpha1.KeyAlgorithm | ||
| expectSignerRSA int32 | ||
| expectSignerCurve configv1alpha1.ECDSACurve | ||
| expectDefaultsAlgo configv1alpha1.KeyAlgorithm | ||
| expectDefaultsRSA int32 | ||
| expectDefaultsCurve configv1alpha1.ECDSACurve | ||
| }{ | ||
| { | ||
| name: "feature gate disabled - no manifest generated", | ||
| installConfig: &types.InstallConfig{ | ||
| FeatureSet: configv1.Default, | ||
| }, | ||
| expectEmpty: true, | ||
| }, | ||
| { | ||
| name: "feature gate enabled, pki nil - mode Default", | ||
| installConfig: &types.InstallConfig{ | ||
| FeatureSet: configv1.TechPreviewNoUpgrade, | ||
| }, | ||
| expectEmpty: false, | ||
| expectMode: configv1alpha1.PKICertificateManagementModeDefault, | ||
| }, | ||
| { | ||
| name: "feature gate enabled, pki RSA-4096", | ||
| installConfig: &types.InstallConfig{ | ||
| FeatureSet: configv1.TechPreviewNoUpgrade, | ||
| PKI: &types.PKIConfig{ | ||
| SignerCertificates: types.CertificateConfig{ | ||
| Key: types.KeyConfig{ | ||
| Algorithm: types.KeyAlgorithmRSA, | ||
| RSA: types.RSAKeyConfig{KeySize: 4096}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expectEmpty: false, | ||
| expectMode: configv1alpha1.PKICertificateManagementModeCustom, | ||
| expectSignerAlgo: configv1alpha1.KeyAlgorithmRSA, | ||
| expectSignerRSA: 4096, | ||
| expectDefaultsAlgo: configv1alpha1.KeyAlgorithmRSA, | ||
| expectDefaultsRSA: 4096, | ||
| }, | ||
| { | ||
| name: "feature gate enabled, pki ECDSA P-384", | ||
| installConfig: &types.InstallConfig{ | ||
| FeatureSet: configv1.TechPreviewNoUpgrade, | ||
| PKI: &types.PKIConfig{ | ||
| SignerCertificates: types.CertificateConfig{ | ||
| Key: types.KeyConfig{ | ||
| Algorithm: types.KeyAlgorithmECDSA, | ||
| ECDSA: types.ECDSAKeyConfig{Curve: types.ECDSACurveP384}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expectEmpty: false, | ||
| expectMode: configv1alpha1.PKICertificateManagementModeCustom, | ||
| expectSignerAlgo: configv1alpha1.KeyAlgorithmECDSA, | ||
| expectSignerCurve: configv1alpha1.ECDSACurveP384, | ||
| expectDefaultsAlgo: configv1alpha1.KeyAlgorithmRSA, | ||
| expectDefaultsRSA: 4096, | ||
| }, | ||
| { | ||
| name: "feature gate enabled, pki RSA-2048 explicit", | ||
| installConfig: &types.InstallConfig{ | ||
| FeatureSet: configv1.TechPreviewNoUpgrade, | ||
| PKI: &types.PKIConfig{ | ||
| SignerCertificates: types.CertificateConfig{ | ||
| Key: types.KeyConfig{ | ||
| Algorithm: types.KeyAlgorithmRSA, | ||
| RSA: types.RSAKeyConfig{KeySize: 2048}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| expectEmpty: false, | ||
| expectMode: configv1alpha1.PKICertificateManagementModeCustom, | ||
| expectSignerAlgo: configv1alpha1.KeyAlgorithmRSA, | ||
| expectSignerRSA: 2048, | ||
| expectDefaultsAlgo: configv1alpha1.KeyAlgorithmRSA, | ||
| expectDefaultsRSA: 4096, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| parents := asset.Parents{} | ||
| parents.Add(installconfig.MakeAsset(tc.installConfig)) | ||
|
|
||
| pkiAsset := &PKIConfiguration{} | ||
| err := pkiAsset.Generate(context.Background(), parents) | ||
| if !assert.NoError(t, err) { | ||
| return | ||
| } | ||
|
|
||
| if tc.expectEmpty { | ||
| assert.Empty(t, pkiAsset.Files()) | ||
| return | ||
| } | ||
|
|
||
| if !assert.Len(t, pkiAsset.Files(), 1) { | ||
| return | ||
| } | ||
| assert.Equal(t, "manifests/cluster-pki-02-config.yaml", pkiAsset.Files()[0].Filename) | ||
|
|
||
| // Unmarshal and verify the CR structure | ||
| var pkiCR configv1alpha1.PKI | ||
| err = yaml.Unmarshal(pkiAsset.Files()[0].Data, &pkiCR) | ||
| if !assert.NoError(t, err) { | ||
| return | ||
| } | ||
|
|
||
| assert.Equal(t, "config.openshift.io/v1alpha1", pkiCR.APIVersion) | ||
| assert.Equal(t, "PKI", pkiCR.Kind) | ||
| assert.Equal(t, "cluster", pkiCR.Name) | ||
| assert.Equal(t, tc.expectMode, pkiCR.Spec.CertificateManagement.Mode) | ||
|
|
||
| if tc.expectMode != configv1alpha1.PKICertificateManagementModeCustom { | ||
| return | ||
| } | ||
|
|
||
| profile := pkiCR.Spec.CertificateManagement.Custom.PKIProfile | ||
|
|
||
| // Verify defaults | ||
| assert.Equal(t, tc.expectDefaultsAlgo, profile.Defaults.Key.Algorithm) | ||
| if tc.expectDefaultsAlgo == configv1alpha1.KeyAlgorithmECDSA { | ||
| assert.Equal(t, tc.expectDefaultsCurve, profile.Defaults.Key.ECDSA.Curve) | ||
| } else { | ||
| assert.Equal(t, tc.expectDefaultsRSA, profile.Defaults.Key.RSA.KeySize) | ||
| } | ||
|
|
||
| // Verify signerCertificates | ||
| assert.Equal(t, tc.expectSignerAlgo, profile.SignerCertificates.Key.Algorithm) | ||
| if tc.expectSignerAlgo == configv1alpha1.KeyAlgorithmRSA { | ||
| assert.Equal(t, tc.expectSignerRSA, profile.SignerCertificates.Key.RSA.KeySize) | ||
| } | ||
| if tc.expectSignerAlgo == configv1alpha1.KeyAlgorithmECDSA { | ||
| assert.Equal(t, tc.expectSignerCurve, profile.SignerCertificates.Key.ECDSA.Curve) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.