Skip to content

Add replace-certificate command - #1355

Merged
LukeButters merged 12 commits into
mainfrom
luke/replace-certificate
Nov 20, 2026
Merged

LukeButters merged 12 commits into
mainfrom
luke/replace-certificate

Conversation

@LukeButters

@LukeButters LukeButters commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

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.

Tentacle replace-certificate --instance <instance> --server https://my-octopus --apiKey API-XXXX

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:

Tentacle replace-certificate --instance <instance> --server https://my-octopus --apiKey API-XXXX --apply-current-cert-to-tentacles-with-thumbprint <old thumbprint printed by the first run>

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

  • Run end to end only with a single listening deployment target in one space. Not run end to end with polling Tentacles, workers, multiple spaces, websockets or proxies, or against a released older Tentacle; unit tests cover a polling worker, multiple spaces and re-running with --apply-current-cert-to-tentacles-with-thumbprint.
  • Other values encrypted by Clean up the Linux config encryption key change #1352 (e.g. proxy passwords) stay unreadable after a downgrade until set again.
  • Assumes an Octopus Server with spaces.

@LukeButters
LukeButters requested a review from a team as a code owner November 18, 2026 03:14
var store = new XmlFileKeyValueStore(fileSystem, configFile);

var selector = Substitute.For<IApplicationInstanceSelector>();
selector.Current.Returns(_ => new ApplicationInstanceConfiguration(null, configFile, store, store));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 sburmanoctopus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things here:

  • Saving before Modify means a failed API call leaves Tentacle and Octopus out of sync. Generate in memory via ICertificateGenerator, update Octopus, then save.
  • GenerateNewCertificate swallows InvalidOperationException and only caches in memory, so Octopus could end up trusting a cert that's lost on restart. Worth failing loudly here.

@LukeButters LukeButters Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not too concerned about this not being atomic.

Comment thread source/Octopus.Tentacle/Program.cs Outdated
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");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a docs update (or follow-up issue) for this command?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}");

@sburmanoctopus sburmanoctopus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@LukeButters
LukeButters merged commit 2817773 into main Nov 20, 2026
47 checks passed
@LukeButters
LukeButters deleted the luke/replace-certificate branch November 20, 2026 04:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants