Repository navigation
Add replace-certificate command - #1355
Conversation
…stead of in setup
| var store = new XmlFileKeyValueStore(fileSystem, configFile); | ||
|
|
||
| var selector = Substitute.For<IApplicationInstanceSelector>(); | ||
| selector.Current.Returns(_ => new ApplicationInstanceConfiguration(null, configFile, store, store)); |
There was a problem hiding this comment.
Overtime we might improve this. I think tentacle is a PITA since it uses /etc/octopus which can only be changed by setting env vars but we don't want to set env vars in a test
sburmanoctopus
left a comment
There was a problem hiding this comment.
This is looking good.
Although, I think we should address the fact that we can break multiple registrations before we merge this.
| if (machine.Endpoint is not (ListeningTentacleEndpointResource or PollingTentacleEndpointResource)) | ||
| throw new ControlledFailureException($"'{machine.Name}' uses a {machine.Endpoint.CommunicationStyle} endpoint. Only listening and polling Tentacles can have their certificate replaced."); | ||
|
|
||
| var certificate = configuration.Value.GenerateNewCertificate(); |
There was a problem hiding this comment.
Two things here:
- Saving before
Modifymeans a failed API call leaves Tentacle and Octopus out of sync. Generate in memory viaICertificateGenerator, update Octopus, then save. GenerateNewCertificateswallowsInvalidOperationExceptionand only caches in memory, so Octopus could end up trusting a cert that's lost on restart. Worth failing loudly here.
There was a problem hiding this comment.
Done in e5edf09, slightly differently. The new certificate is generated in memory and saved with ImportCertificate, which throws if it can't be saved (no in-memory fallback). It's still saved before Octopus is updated, so Octopus never trusts a certificate we've lost. The command prints the old thumbprint, and if an update fails part way through, re-running with --apply-current-cert-to-tentacles-with-thumbprint <old> points the remaining registrations at the saved certificate without generating another one.
There was a problem hiding this comment.
I am not too concerned about this not being atomic.
| builder.RegisterCommand<ProxyConfigurationCommand>("proxy", "Configure the HTTP proxy used by Octopus"); | ||
| builder.RegisterCommand<PollingProxyConfigurationCommand>("polling-proxy", "Configure the HTTP proxy used by polling Tentacles to reach the Octopus Server"); | ||
| builder.RegisterCommand<ServerCommsCommand>("server-comms", "Configure how the Tentacle communicates with an Octopus Server"); | ||
| builder.RegisterCommand<ReplaceCertificateCommand>("replace-certificate", "Creates and installs a new certificate for this Tentacle and updates its existing deployment target or worker in Octopus to trust it"); |
There was a problem hiding this comment.
Is there a docs update (or follow-up issue) for this command?
There was a problem hiding this comment.
There aren't user docs for commands in this repo. I'll follow up on the docs site separately.
…t in all spaces Finds targets and workers by the plaintext thumbprint instead of --name/--id, uses the polling proxy like deregister-from, and saves the new certificate before updating Octopus. --old-thumbprint finishes a run that failed part way through.
…nd refuse to run without a current certificate
| this.log = log; | ||
|
|
||
| api = AddOptionSet(new ApiEndpointOptions(Options)); | ||
| Options.Add("apply-current-cert-to-tentacles-with-thumbprint=", "Does not create a new certificate. Instead, updates every deployment target and worker in Octopus that has this thumbprint to trust this Tentacle's current certificate.", s => applyCurrentCertToTentaclesWithThumbprint = s); |
There was a problem hiding this comment.
if the command fails, the user will have to re-run with this.
we also log:
log.Info($"Replaced certificate {oldThumbprint} with {certificate.Thumbprint}. If this does not complete, re-run this command with --apply-current-cert-to-tentacles-with-thumbprint {oldThumbprint}");
Fixes: LEV-1171
Updating the certificate on Tentacle is currently a PITA, it requires deleting the instance and re-creating it correctly with al rows and environments.
With an upcoming PR the keys will be encrypted in some other location (making it more secure but increasing the chance someone might just loose access to their cert).
This PR adds a simpler
replace-certificate, which does what it says on the tin.The command will even work if the existing cert can't be decrypted, e.g., you moved your tentacle.config between machines but didn't bring the machine-id file with you.
How it works.
It looks up every deployment target and worker, in every space the API key can access, whose thumbprint matches the old one. The old thumbprint is stored unencrypted in the Tentacle config. It then generates a new certificate and updates only the endpoint thumbprint on each of them. Environments, roles, tenants and policy are left alone. Previously you would have had to re-set the roles/envs using
register-with --force. The trusted servers in the Tentacle config are not encrypted, so they are kept as they are.The new certificate is saved before Octopus is updated. The command then prints
If this does not complete, re-run this command with --apply-current-cert-to-tentacles-with-thumbprint <old thumbprint>.--apply-current-cert-to-tentacles-with-thumbprint <thumbprint>does not create a new certificate. It updates every target and worker that still has that thumbprint to trust the Tentacle's current certificate. It is only for finishing a run that did not complete:API calls go through the configured polling proxy, the same as
deregister-from.Tried this out locally.
Since this is something that exists to help out in the case one was to lose their key to decrypt their cert, we tested that case. Specifically we ran a tentacle that had the new key changes (where the key is under /var/) then we ran an old tentacle pretending that we needed to downgrade the tentacle. This command was able to run, update the certs and let tentacle serve requests for octopus again.
Not verified
--apply-current-cert-to-tentacles-with-thumbprint.