Signing Keys

As previously discussed, NKEYs are identities, and if someone gets a hold of an account or operator nkey they can do everything you can do as you.

NATS has a strategies to let you deal with scenarios where your private keys escape out in the wild.

The first and most important line of defense is Signing Keys. Signing Keys allow you have multiple NKEY identities of the same kind (Operator or Account) that have the same degree of trust as the standard Issuer nkey.

The concept behind the signing key is that you can issue a JWT for an operator or an account that lists multiple nkeys. Typically the issuer will match the Subject of the entity issuing the JWT. With SigningKeys, a JWT is considered valid if it is signed by the Subject of the Issuer or one of its signing keys. This enables guarding the private key of the Operator or Account more closely while allowing Accounts, Users or Activation Tokens be signed using alternate private keys.

If an issue should arise where somehow a signing key escapes into the wild, you would remove the compromised signing key from the entity, add a new one, and reissue the entity. When a JWT is validated, if the signing key is missing, the operation is rejected. You are also on the hook to re-issue all JWTs (accounts, users, activation tokens) that were signed with the compromised signing key.

This is effectively a large hammer. You can mitigate the process a bit by having a larger number of signing keys and then rotating the signing keys to get a distribution you can easily handle in case of a compromise. In a future release, we’ll have a revocation process were you can invalidate a single JWT by its unique JWT ID (JTI). For now a sledge hammer you have.

With greater security process, there’s greater complexity. With that said, nsc doesn’t track public or private signing keys. As these are only identities that when in use presume a manual use. That means that you the user will have to track and manage your private keys more closely.

Let’s get a feel for the workflow. We are going to:

  • Create an operator with a signing key
  • Create an account with a signing key
  • The account will be signed using the operator’s signing key
  • Create an user with the account’s signing key

All signing key operations revolve around the global nsc flag -K or --private-key. Whenever you want to modify an entity, you have to supply the parent key so that the JWT is signed. Normally this happens automatically but in the case of signing keys, you’ll have to supply the flag by hand.

Creating the operator:

  1. nsc add operator O2
  1. [ OK ] generated and stored operator key "OABX3STBZZRBHMWMIMVHNQVNUG2O3D54BMZXX5LMBYKSAPDSHIWPMMFY"
  2. [ OK ] added operator "O2"

To add a signing key we have to first generate one with nsc:

  1. nsc generate nkey --operator --store
  1. SOAEW6Z4HCCGSLZJYZQMGFQY2SY6ZKOPIAKUQ5VZY6CW23WWYRNHTQWVOA
  2. OAZBRNE7DQGDYT5CSAGWDMI5ENGKOEJ57BXVU6WUTHFEAO3CU5GLQYF5
  3. operator key stored ~/.nkeys/keys/O/AZ/OAZBRNE7DQGDYT5CSAGWDMI5ENGKOEJ57BXVU6WUTHFEAO3CU5GLQYF5.nk

On a production environment private keys should be saved to a file and always referenced from the secured file.

Now we are going to edit the operator by adding a signing key with the --sk flag providing the generated operator public key (the one starting with O):

  1. nsc edit operator --sk OAZBRNE7DQGDYT5CSAGWDMI5ENGKOEJ57BXVU6WUTHFEAO3CU5GLQYF5
  1. [ OK ] added signing key "OAZBRNE7DQGDYT5CSAGWDMI5ENGKOEJ57BXVU6WUTHFEAO3CU5GLQYF5"
  2. [ OK ] edited operator "O2"

Check our handy work:

  1. nsc describe operator
  1. ╭─────────────────────────────────────────────────────────────────────────╮
  2. Operator Details
  3. ├──────────────┬──────────────────────────────────────────────────────────┤
  4. Name O2
  5. Operator ID OABX3STBZZRBHMWMIMVHNQVNUG2O3D54BMZXX5LMBYKSAPDSHIWPMMFY
  6. Issuer ID OABX3STBZZRBHMWMIMVHNQVNUG2O3D54BMZXX5LMBYKSAPDSHIWPMMFY
  7. Issued 2019-12-05 14:36:16 UTC
  8. Expires
  9. ├──────────────┼──────────────────────────────────────────────────────────┤
  10. Signing Keys OAZBRNE7DQGDYT5CSAGWDMI5ENGKOEJ57BXVU6WUTHFEAO3CU5GLQYF5
  11. ╰──────────────┴──────────────────────────────────────────────────────────╯

Now let’s create an account called A and sign it with the generated operator private signing key. To sign it with the key specify the -K flag and the private key or a path to the private key:

  1. nsc add account A -K ~/.nkeys/keys/O/AZ/OAZBRNE7DQGDYT5CSAGWDMI5ENGKOEJ57BXVU6WUTHFEAO3CU5GLQYF5.nk
  1. [ OK ] generated and stored account key "ACDXQQ6KD5MVSFMK7GNF5ARK3OJC6PEICWCH5PQ7HO27VKGCXQHFE33B"
  2. [ OK ] added account "A"

Let’s generate an account signing key, again we use nk:

  1. nsc generate nkey --account --store
  1. SAAA4BVFTJMBOW3GAYB3STG3VWFSR4TP4QJKG2OCECGA26SKONPFGC4HHE
  2. ADUQTJD4TF4O6LTTHCKDKSHKGBN2NECCHHMWFREPKNO6MPA7ZETFEEF7
  3. account key stored ~/.nkeys/keys/A/DU/ADUQTJD4TF4O6LTTHCKDKSHKGBN2NECCHHMWFREPKNO6MPA7ZETFEEF7.nk

Let’s add the signing key to the account, and remember to sign the account with the operator signing key:

  1. nsc edit account --sk ADUQTJD4TF4O6LTTHCKDKSHKGBN2NECCHHMWFREPKNO6MPA7ZETFEEF7 -K ~/.nkeys/keys/O/AZ/OAZBRNE7DQGDYT5CSAGWDMI5ENGKOEJ57BXVU6WUTHFEAO3CU5GLQYF5.nk
  1. [ OK ] added signing key "ADUQTJD4TF4O6LTTHCKDKSHKGBN2NECCHHMWFREPKNO6MPA7ZETFEEF7"
  2. [ OK ] edited account "A"

Let’s take a look at the account

  1. nsc describe account
  1. ╭──────────────────────────────────────────────────────────────────────────────────────╮
  2. Account Details
  3. ├───────────────────────────┬──────────────────────────────────────────────────────────┤
  4. Name A
  5. Account ID ACDXQQ6KD5MVSFMK7GNF5ARK3OJC6PEICWCH5PQ7HO27VKGCXQHFE33B
  6. Issuer ID OAZBRNE7DQGDYT5CSAGWDMI5ENGKOEJ57BXVU6WUTHFEAO3CU5GLQYF5
  7. Issued 2019-12-05 14:48:22 UTC
  8. Expires
  9. ├───────────────────────────┼──────────────────────────────────────────────────────────┤
  10. Signing Keys ADUQTJD4TF4O6LTTHCKDKSHKGBN2NECCHHMWFREPKNO6MPA7ZETFEEF7
  11. ├───────────────────────────┼──────────────────────────────────────────────────────────┤
  12. Max Connections Unlimited
  13. Max Leaf Node Connections Unlimited
  14. Max Data Unlimited
  15. Max Exports Unlimited
  16. Max Imports Unlimited
  17. Max Msg Payload Unlimited
  18. Max Subscriptions Unlimited
  19. Exports Allows Wildcards True
  20. ├───────────────────────────┼──────────────────────────────────────────────────────────┤
  21. Imports None
  22. Exports None
  23. ╰───────────────────────────┴──────────────────────────────────────────────────────────╯

We can see that the signing key ADUQTJD4TF4O6LTTHCKDKSHKGBN2NECCHHMWFREPKNO6MPA7ZETFEEF7 was added to the account. Also the issuer is the operator signing key (specified by the -K).

Now let’s create a user and sign it with the account signing key starting with ADUQTJD4TF4O.

  1. nsc add user U -K ~/.nkeys/keys/A/DU/ADUQTJD4TF4O6LTTHCKDKSHKGBN2NECCHHMWFREPKNO6MPA7ZETFEEF7.nk
  1. [ OK ] generated and stored user key "UD47TOTKVDY4IQRGI6D7XMLZPHZVNV5FCD4CNQICLV3FXLQBY72A4UXL"
  2. [ OK ] generated user creds file "~/.nkeys/creds/O2/A/U.creds"
  3. [ OK ] added user "U" to account "A"

Check the account

  1. nsc describe user
  1. ╭─────────────────────────────────────────────────────────────────────────────────╮
  2. User
  3. ├──────────────────────┬──────────────────────────────────────────────────────────┤
  4. Name U
  5. User ID UD47TOTKVDY4IQRGI6D7XMLZPHZVNV5FCD4CNQICLV3FXLQBY72A4UXL
  6. Issuer ID ADUQTJD4TF4O6LTTHCKDKSHKGBN2NECCHHMWFREPKNO6MPA7ZETFEEF7
  7. Issuer Account ACDXQQ6KD5MVSFMK7GNF5ARK3OJC6PEICWCH5PQ7HO27VKGCXQHFE33B
  8. Issued 2019-12-05 14:50:07 UTC
  9. Expires
  10. ├──────────────────────┼──────────────────────────────────────────────────────────┤
  11. Response Permissions Not Set
  12. ├──────────────────────┼──────────────────────────────────────────────────────────┤
  13. Max Messages Unlimited
  14. Max Msg Payload Unlimited
  15. Network Src Any
  16. Time Any
  17. ╰──────────────────────┴──────────────────────────────────────────────────────────╯

As expected, the issuer is now the signing key we generated earlier. To map the user to the actual account, an Issuer Account field was added to the JWT that identifies the public key of account A.

Scoped Signing Keys

Scoped Signing Keys simplify user permission management. Previously if you wanted to limit the permissions of users, you had to specify permissions on a per-user basis. With scoped signing keys, you associate a signing key with a set of permissions. This configuration lives on the account JWT and is managed with the nsc edit signing-key command. You can add as many scoped signing keys as necessary.

To issue a user with a set of permissions, simply sign the user with the signing key having the permission set you want. The user configuration must not have any permissions assigned to it.

On connect, the nats-server will assign the permissions associated with that signing key to the user. If you update the permissions associated with a signing key, the server will immediately update permissions for users signed with that key.

  1. nsc add account A
  1. [ OK ] generated and stored account key "ADLGEVANYDKDQ6WYXPNBEGVUURXZY4LLLK5BJPOUDN6NGNXLNH4ATPWR"
  2. [ OK ] push jwt to account server:
  3. [ OK ] pushed account jwt to the account server
  4. > NGS created a new free billing account for your JWT, A [ADLGEVANYDKD].
  5. > Use the 'ngs' command to manage your billing plan.
  6. > If your account JWT is *not* in ~/.nsc, use the -d flag on ngs commands to locate it.
  7. [ OK ] pull jwt from account server
  8. [ OK ] added account "A"

Generate the signing key

  1. nsc edit account -n A --sk generate
  1. [ OK ] added signing key "AAZQXKDPOTGUCOCOGDW7HWWVR5WEGF3KYL7EKOEHW2XWRS2PT5AOTRH3"
  2. [ OK ] push jwt to account server
  3. [ OK ] pull jwt from account server
  4. [ OK ] account server modifications:
  5. > allow wildcard exports changed from true to false
  6. [ OK ] edited account "A"

Add a service to the account

  1. nsc edit signing-key --account A --role service --sk AAZQXKDPOTGUCOCOGDW7HWWVR5WEGF3KYL7EKOEHW2XWRS2PT5AOTRH3 --allow-sub "q.>" --deny-pub ">" --allow-pub-response
  1. [ OK ] set max responses to 1
  2. [ OK ] added deny pub ">"
  3. [ OK ] added sub "q.>"
  4. [ OK ] push jwt to account server
  5. [ OK ] pull jwt from account server
  6. [ OK ] edited signing key "AAZQXKDPOTGUCOCOGDW7HWWVR5WEGF3KYL7EKOEHW2XWRS2PT5AOTRH3"

Since the signing key has a unique role name within an account, it can be subsequently used for easier referencing.

  1. nsc add user U -K service
  1. [ OK ] generated and stored user key "UBFRJ6RNBYJWSVFBS7O4ZW5MM6J3EPE75II3ULPVUWOUH7K7A23D3RQE"
  2. [ OK ] generated user creds file `~/test/issue-2621/keys/creds/synadia/A/U.creds`
  3. [ OK ] added user "U" to account "A"

To see the permissions for the user enter nsc describe user - you will see in the report that the user is scoped, and has the permissions listed. You can inspect and modify the scopedpermissions with nsc edit signing keys - pushing updates to the account will reassign user permissions