-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Fix Vault managed intermediate PKI bug #15525
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ```release-note:bug | ||
| ca: Fixed issue where using Vault as Connect CA with Vault-managed policies would error on start-up if the intermediate PKI mount existed but was empty | ||
| ``` |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -70,6 +70,8 @@ type VaultProvider struct { | |||
| clusterID string | ||||
| spiffeID *connect.SpiffeIDSigning | ||||
| logger hclog.Logger | ||||
|
|
||||
| isConsulMountedIntermediate bool | ||||
| } | ||||
|
|
||||
| func NewVaultProvider(logger hclog.Logger) *VaultProvider { | ||||
|
|
@@ -310,9 +312,10 @@ func (v *VaultProvider) GenerateRoot() (RootResult, error) { | |||
| }, | ||||
| }) | ||||
| if err != nil { | ||||
| return RootResult{}, err | ||||
| return RootResult{}, fmt.Errorf("failed to mount root CA backend: %w", err) | ||||
| } | ||||
|
|
||||
| // We want to initialize afterwards | ||||
| fallthrough | ||||
| case ErrBackendNotInitialized: | ||||
| uid, err := connect.CompactUID() | ||||
|
|
@@ -326,7 +329,7 @@ func (v *VaultProvider) GenerateRoot() (RootResult, error) { | |||
| "key_bits": v.config.PrivateKeyBits, | ||||
| }) | ||||
| if err != nil { | ||||
| return RootResult{}, err | ||||
| return RootResult{}, fmt.Errorf("failed to initialize root CA: %w", err) | ||||
| } | ||||
| var ok bool | ||||
| rootPEM, ok = resp.Data["certificate"].(string) | ||||
|
|
@@ -336,7 +339,7 @@ func (v *VaultProvider) GenerateRoot() (RootResult, error) { | |||
|
|
||||
| default: | ||||
| if err != nil { | ||||
| return RootResult{}, err | ||||
| return RootResult{}, fmt.Errorf("unexpected error while setting root PKI backend: %w", err) | ||||
| } | ||||
| } | ||||
|
|
||||
|
|
@@ -381,19 +384,47 @@ func (v *VaultProvider) setupIntermediatePKIPath() error { | |||
| Config: mountConfig, | ||||
| }) | ||||
| if err != nil { | ||||
| return err | ||||
| return fmt.Errorf("failed to mount intermediate PKI backend: %w", err) | ||||
| } | ||||
| // Required to determine if we should tune the mount | ||||
| // if the VaultProvider is ever reconfigured. | ||||
| v.isConsulMountedIntermediate = true | ||||
|
|
||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On an earlier meeting did we decide that this needed a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When we talked I thought I should be initializing the mount from within
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if we do end up mounting the intermediate PKI, there's no other action to be done (unlike I thought about making the code paths for root and intermediate more parallel to each other but deferred it to future Chris |
||||
| } else if err == ErrBackendNotInitialized { | ||||
| // If this is the first time calling setupIntermediatePKIPath, the backend | ||||
| // will not have been initialized. Since the mount is ready we can suppress | ||||
| // this error. | ||||
|
Comment on lines
+399
to
+401
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||||
| } else { | ||||
| return err | ||||
| return fmt.Errorf("unexpected error while fetching intermediate CA: %w", err) | ||||
| } | ||||
| } else { | ||||
| err := v.tuneMountNamespaced(v.config.IntermediatePKINamespace, v.config.IntermediatePKIPath, &mountConfig) | ||||
| if err != nil { | ||||
| v.logger.Warn("Could not update intermediate PKI mount settings", "path", v.config.IntermediatePKIPath, "error", err) | ||||
| // If Consul was responsible for mounting the intermediate PKI path | ||||
| // we should update the mount with any new config. | ||||
| if v.isConsulMountedIntermediate { | ||||
| // This codepath requires the Vault policy: | ||||
| // | ||||
| // path "/sys/mounts/<intermediate_pki_path>/tune" { | ||||
| // capabilities = [ "update" ] | ||||
| // } | ||||
| // | ||||
| err := v.tuneMountNamespaced(v.config.IntermediatePKINamespace, v.config.IntermediatePKIPath, &mountConfig) | ||||
| if err != nil { | ||||
| v.logger.Warn("Intermediate PKI path was mounted by Consul but could not be tuned", | ||||
| "namespace", v.config.IntermediatePKINamespace, | ||||
| "path", v.config.IntermediatePKIPath, | ||||
| "error", err, | ||||
| ) | ||||
| } | ||||
|
|
||||
| } else { | ||||
| v.logger.Info("Found existing Intermediate PKI path mount", | ||||
| "namespace", v.config.IntermediatePKINamespace, | ||||
| "path", v.config.IntermediatePKIPath, | ||||
| ) | ||||
| } | ||||
|
||||
| if lessThanHalfTimePassed(c.timeNow(), intermediateCert.NotBefore, intermediateCert.NotAfter) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updating comment to match code, since it no longer checks for existence
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: this would only get set to true the FIRST time a consul instance ran through this code. After the mount existed it wouldn't set this if it matters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I intended it to be a "best guess" field that only sets if the
VaultProviderEVER creates the intermediate mount. If the mount ever existed then we'll never attempt to tune the mount but output a log to help operators diagnose it if they're ever confused by the behavior.