Skip to content

Support reloading TSA worker keys via SIGHUP signal - #5652

Merged
muntac merged 6 commits into
concourse:masterfrom
cycloidio:gl_1105
Jul 22, 2020
Merged

muntac merged 6 commits into
concourse:masterfrom
cycloidio:gl_1105

Conversation

@gaelL

@gaelL gaelL commented May 25, 2020 •

Copy link
Copy Markdown
Contributor

Why is this PR needed?

This pull request fix the issue #1105 by adding TSA support for reloading authorized keys without restart based on signal SIGUSR1

As described in #1105 and #3551 it is really handy for HA purpose to be able to add worker keys without having to restart a Concourse web/TSA process.

It also rely on this pull request that have been merged concourse/flag#5

What is this PR trying to accomplish?

Provide a solution for user to dynamically add or remove worker keys without having to restart Concourse.

closes #1105 .

How does it accomplish that?

I choose to use signal SIGHUP that any user can send with the following command:

kill -s SIGHUP <PID of Concourse web>

The implementation is really simple and for a user point of view it's really easy to send this signal to concourse when you update any authorized keys file.
Signal usage have been inspired by Concourse worker process https://concourse-ci.org/concourse-worker.html#restarting-a-worker

Contributor Checklist

Let's discuss about those points if the technical solution is approved.

Reviewer Checklist

  • Code reviewed
  • Tests reviewed
  • Documentation reviewed
  • Release notes reviewed
  • PR acceptance performed
  • New config flags added? Ensure that they are added to the BOSH
    and Helm packaging; otherwise, ignored for the integration tests (for example, if they are Garden configs that are not displayed in the --help text).

@gaelL
gaelL requested a review from a team May 25, 2020 12:01
@gaelL

gaelL commented May 25, 2020

Copy link
Copy Markdown
Contributor Author

As discussed in #1105 @muntac feel free to have a look

Comment thread tsa/tsacmd/command.go Resolved
@gaelL
gaelL force-pushed the gl_1105 branch 2 times, most recently from 0d59a2e to ae3f297 Compare May 25, 2020 13:01
@xescugc

xescugc commented May 25, 2020

Copy link
Copy Markdown

IDK how the documentation part works, but I would suggest to comment it somewhere as if not it's a hidden feature hehe.

Could potentially be commented on the AuthorizedKeys flag description at least 😄

@gaelL gaelL changed the title tsa: handling of SIGUSR1 to reload workers keys tsa: handling of SIGHUP to reload workers keys May 26, 2020
Comment thread tsa/tsacmd/command.go Resolved
Comment thread go.mod Resolved
@muntac

muntac commented Jun 10, 2020

Copy link
Copy Markdown
Contributor

@gaelL I'm having trouble testing this feature out, and require some help. I'm bringing up this build via docker-compose. When I send kill -s SIGHUP to the worker process the process gets killed immediately.

Could you validate the feature on your end, and tell me the exact steps for doing so? Thanks.

@gaelL

gaelL commented Jun 11, 2020

Copy link
Copy Markdown
Contributor Author

@muntac this is how I test it:

This PR add a new goroutine on web/atc/tsa process. This goroutine will "run" each time it catch a kill -s SIGHUP.
Before we go forward what do we expect to see:

The goroutine code contain one logger line logger.Info("reloading-config"). So we expect to see reloading-config in one of the log of Concourse web container when we send the right kill signal.

Basic test with AuthorizedKeys

I simply run docker compose in one term. And in another term I run a kill signal on the concourse web process :

docker-compose up
docker exec -it concourse_web_1 bash -c 'kill -s SIGHUP $(pgrep concourse)'

In my docker compose term I have the following log displayed each time I send the kill signal:

web_1     | {"timestamp":"2020-06-11T09:15:44.719067381Z","level":"info","source":"tsa","message":"tsa.reloading-config","data":{}}

As noticed, this log line is the expected one.

Second test with authorizedKeys using team yaml file

For this it require a little patch to configure Concourse web to read the TEAM_AUTHORIZED_KEYS_FILE. But also a little change to generate the file for our test:

diff --git a/Dockerfile b/Dockerfile
index aad6300ba..8b5b8039c 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -30,6 +30,12 @@ RUN concourse generate-key -t ssh -b 1024 -f /concourse-keys/tsa_host_key
 RUN concourse generate-key -t ssh -b 1024 -f /concourse-keys/worker_key
 RUN cp /concourse-keys/worker_key.pub /concourse-keys/authorized_worker_keys

+RUN echo '- team: main\n\
+  ssh_keys:\n\
+    - '$(cat /concourse-keys/worker_key.pub)'\n\
+    - fake errorkey\n'\
+  > /concourse-keys/authorized_worker_keys.yml
+
 # 'web' keys
 ENV CONCOURSE_SESSION_SIGNING_KEY     /concourse-keys/session_signing_key
 ENV CONCOURSE_TSA_AUTHORIZED_KEYS     /concourse-keys/authorized_worker_keys
diff --git a/docker-compose.yml b/docker-compose.yml
index ad221f201..0d64c20a8 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -31,6 +31,7 @@ services:
       CONCOURSE_MAIN_TEAM_LOCAL_USER: test
       CONCOURSE_CLUSTER_NAME: dev
       CONCOURSE_ENABLE_ARCHIVE_PIPELINE: "true"
+      CONCOURSE_TSA_TEAM_AUTHORIZED_KEYS_FILE: /concourse-keys/authorized_worker_keys.yml

   worker:
     build: .

I add a new env var in docker compose to configure the usage of TEAM_AUTHORIZED_KEYS_FILE.
And I add a RUN in the Dockerfile to create a valid yaml file.
As example as content, I used the public key generated for the worker and a fake one to trigger also the error logger.

Same as above we expect the log line reloading-config, also logger.Info("load-team-authorized-keys-loaded", lager.Data{"team": t.Team, "key": k}) and the error one triggered by our fake key.

let's test it:

# Term 1 (adding --build to make sure we take into account our changes into Dockerfile)
docker-compose up --build

# Term 2
docker exec -it concourse_web_1 bash -c 'kill -s SIGHUP $(pgrep concourse)'

Output expected:

web_1     | {"timestamp":"2020-06-11T09:39:18.558549543Z","level":"info","source":"tsa","message":"tsa.reloading-config","data":{}}
web_1     | {"timestamp":"2020-06-11T09:39:18.558905580Z","level":"info","source":"tsa","message":"tsa.load-team-authorized-keys-loaded","data":{"key":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDMadAtprsAjBXQzwK8u5HCEHOtYYQmtHCNEL1SzBBlS9SUegfKAosS4secV7HdAU3jA7MG4rvj3AFRHQFMHzGfj/IvV3JcHCs8cwrw+8yLqP8L/if2Fi3PtLmTeUBacyYLH2H7zRzFhv0ywih7+UI9d6FZfVphWcepoV0OOqn6nQ==","team":"main"}}
web_1     | {"timestamp":"2020-06-11T09:39:18.558954648Z","level":"error","source":"tsa","message":"tsa.load-team-authorized-keys-parse","data":{"error":"Invalid format, ignoring (fake errorkey): ssh: no key found"}}

Let me know if you need more details or help to test it.

@muntac

muntac commented Jun 11, 2020

Copy link
Copy Markdown
Contributor

@gaelL Thanks a lot! I kept making the basic mistake of sending the signal to the worker (instead of the web where the TSA is) as I kept thinking "worker keys"! :-|

Anyway, it works for me now. I'm going to request some changes to the comments and then we should be good to go. Thanks for your patience.

@muntac muntac 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.

@gaelL requested some minor changes.

Comment thread tsa/tsacmd/command.go Resolved
Comment thread tsa/tsacmd/command.go Resolved
Comment thread tsa/tsacmd/command.go Resolved
Comment thread tsa/tsacmd/command.go Resolved
Comment thread tsa/tsacmd/command.go Resolved
@muntac

muntac commented Jun 11, 2020

Copy link
Copy Markdown
Contributor

Lastly, this would benefit from being mentioned in the docs as @xescugc suggested. It seems most appropriate as a subsection under the operating a web node section.

This would involve a PR to the docs repo and the change would be in here.

@gaelL Let me know if you are able to get around to that. Otherwise, I can try updating it myself.

@gaelL

gaelL commented Jun 12, 2020

Copy link
Copy Markdown
Contributor Author

thanks for this review. I will give a try with the doc

@xescugc

xescugc commented Jun 12, 2020

Copy link
Copy Markdown

On ce04b9f you renamed reloadWorkerKeys := make(chan os.Signal, 1) so all the references to c have to also be renamed.

When defer close(c) and <- c

@gaelL

gaelL commented Jun 12, 2020 •

Copy link
Copy Markdown
Contributor Author

Thanks, the code have been fixed and tested again. I'm waiting while I create and push in // the doc PR before doing a last rebase of this PR

@gaelL
gaelL force-pushed the gl_1105 branch 2 times, most recently from 5d2b2f7 to 656e9f0 Compare June 12, 2020 14:27
gaelL added 4 commits June 15, 2020 11:03
Fix concourse#1105 : TSA should support reloading authorized keys without restart

This commit give the ability for a user to send a signal to Concourse web process to reload
the keys in authorized keys files.

eg

```
kill -s SIGHUP <PID of Concourse web>
```

Signed-off-by: gaelL <[email protected]>
gaelL and others added 2 commits June 15, 2020 11:09
Co-authored-by: Muntasir Chowdhury <[email protected]>
Signed-off-by: gaelL <[email protected]>
@muntac

muntac commented Jun 15, 2020

Copy link
Copy Markdown
Contributor

Just realized we should write some sort of test for this feature. One option is to do it in the acceptance test suite, topgun.

In topgun/k8s we could bring up a Concourse deployment in a test, generate a new key, and then send the SIGHUP signal to the web process. Not exactly sure how to programatically test if the worker is using the new key 🤔

Still thinking through if this is the best place to test.

@jamieklassen

Copy link
Copy Markdown

I think you should actually be able to exercise this using similar setup/helpers to atc/integration. Maybe a tsa/integration suite would make sense. I haven't totally thought it through, but I know that ifrit.Runners have a signal channel, which seems like a reasonable interface to test against.

@gaelL

gaelL commented Jun 16, 2020

Copy link
Copy Markdown
Contributor Author

FYI I created the Doc pull request related to this one : concourse/docs#339

I will try to have a look on tests. It indeed does not look that easy to test. Any leads or help are welcome on this part :p

@muntac muntac 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.

@gaelL I thought about this a bit more.

An integration test would not be possible. We can get a TSA process going via Ifrit, and send a signal. But we won't be able to inspect the Authorized keys object within ifrit to see if it has changed.

The only way of validating it is running an acceptance test that basically tests out the scenario that originally called for this solution i.e. validate if we can bring up an additional worker and get it to talk to the TSA after updating and reloading the authorized keys. That would be a significant amount of work, especially relative to the size of the change.

Considering all that, the add-on nature of this feature, and the immediate benefits it would provide I'm okay with merging this in its current state. Thanks again for the contribution.

@muntac
muntac merged commit 937d479 into concourse:master Jul 22, 2020
@gaelL
gaelL deleted the gl_1105 branch July 24, 2020 09:57
@gaelL

gaelL commented Jul 24, 2020

Copy link
Copy Markdown
Contributor Author

@muntac Thanks for your help on this pull request 👍

@vito vito changed the title tsa: handling of SIGHUP to reload workers keys Support reloading TSA worker keys via SIGHUP signal Jul 29, 2020
@aoldershaw aoldershaw added the release/documented Documentation and release notes have been updated. label Aug 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement release/documented Documentation and release notes have been updated.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TSA should support reloading authorized keys without restart

6 participants