-
Notifications
You must be signed in to change notification settings - Fork 97
Gemalto KeySecure
Note: This guide refers to the Gemalto KeySecure support added by #63. This feature has not been released, yet
This guide shows how to setup a KES server that uses a Gemalto KeySecure instance as a persistent and secure key store:
╔══════════════════════════════════════════════╗
┌────────────┐ ║ ┌────────────┐ ┌─────────────┐ ║
│ KES Client ├───────────╫──┤ KES Server ├─────────────┤ KeySecure │ ║
└────────────┘ ║ └────────────┘ └─────────────┘ ║
╚══════════════════════════════════════════════╝
This guide assumes that you have a running KeySecure instance. It has been tested with KeySecure k170v
version 1.9.1
.
To connect to your KeySecure instance via the ksctl
CLI you need a config.ymal
file similar to:
KSCTL_URL: <your-keysecure-endpoint>
KSCTL_USERNAME: <your-user/admin-name>
KSCTL_PASSWORD: <your-user/admin-password>
KSCTL_VERBOSITY: false
KSCTL_RESP: json
KSCTL_NOSSLVERIFY: true
KSCTL_TIMEOUT: 30
Please make sure to use correct values for
KSCTL_URL
,KSCTL_USERNAME
andKSCTL_PASSWORD
If your KeySecure instance has been configured with a TLS certificate trusted by your machine then you can also setKSCTL_NOSSLVERIFY: false
.
-
First, we create a new group for KES:
ksctl groups create --name KES-Service
-
Next, we create a new user that becomes part of the group:
ksctl users create --name <username> --pword '<password>'
Note that this will print a JSON object containing a
user_id
which will be needed later on. If you already have an existing user that you want to assign to theKES-Service
group you can skip this step and proceed with 3. -
Then, we assign the user to the previously created
KES-Service
group:ksctl groups adduser --name KES-Service --userid "<user-ID>"
The user ID will be printed when creating the user or can be obtained via the
ksctl users list
command. A user-ID is similar to:local|8791ce13-2766-4948-a828-71bac67131c9
. -
Now, there is one user who is part of the
KES-Service
group. Next, we have create a policy and attach it to theKES-Service
group. The followingkes-policy.json
policy grants members of theKES-Service
group create, read and delete permissions:{ "allow": true, "name": "kes-policy", "actions":[ "CreateKey", "ExportKey", "ReadKey", "DeleteKey" ], "resources": [ "kylo:kylo:vault:secrets:*" ] }
This policy allows KES to create, fetch and delete master keys. If you want to prevent KES from e.g. deleting master keys omit the DeleteKey action. Similarly, you can restrict the master keys that can be accessed by KES via the
resources
definition.The following command creates the policy:
ksctl policy create --jsonfile kes-policy.json
-
Once the policy has been created, we have to attach it to the
KES-Service
group such that it becomes active. Therefore, we need the followingkes-attachment.json
policy attachment specification:{ "cust": { "groups": ["KES-Service"] } }
The following command attaches our
kes-policy
to theKES-Service
group:ksctl polattach create -p kes-policy -g kes-attachment.json
-
Finally, we can create a refresh token that will be used by the KES server to obtain short-lived authentication tokens. The following command returns a new refresh token:
ksctl tokens create --user <username> --password '<password>' --issue-rt | jq -r .refresh_token
Here, we have to use a user that is a member of the
KES-Service
group.This command will output a refresh token - similar to:
CEvk5cdHLG7si05LReIeDbXE3PKD082YdUFAnxX75md3jzV0BnyHyAmPPJiA0
First, we need to generate a TLS private key and certificate for our KES server. A KES server can only be run with TLS - since secure-by-default. Here we use self-signed certificates for simplicity. For a production setup we highly recommend to use a certificate signed by CA (e.g. your internal CA or a public CA like Let's Encrypt)
-
First, create the TLS private key:
openssl ecparam -genkey -name prime256v1 | openssl ec -out server.key
-
Then, create the corresponding TLS X.509 certificate:
openssl req -new -x509 -days 30 -key server.key -out server.cert \ -subj "/C=/ST=/L=/O=/CN=localhost" -addext "subjectAltName = IP:127.0.0.1"
You can ignore output messages like:
req: No value provided for Subject Attribute C, skipped
. OpenSSL just tells you that you haven't specified a country, state, a.s.o for the certificate subject. Since we generate a self-signed certificate we don't have to worry about this. -
Then, create private key and certificate for your application:
kes tool identity new --key=app.key --cert=app.cert app
You can compute the
app
identity via:kes tool identity of app.cert
-
Now we have defined all entities in our demo setup. Let's wire everything together by creating the config file
server-config.yml
:address: 0.0.0.0:7373 root: disabled # We disable the root identity since we don't need it in this guide tls: key: server.key cert: server.cert policy: my-app: paths: - /v1/key/create/my-app* - /v1/key/generate/my-app* - /v1/key/decrypt/my-app* identities: - ${APP_IDENTITY} keys: gemalto: keysecure: endpoint: "" # The REST API endpoint of your KeySecure instance - e.g. https://127.0.0.1 credentials: token: "" # Your refresh token domain: "" # Your domain. If empty, defaults to root domain. retry: 15s tls: ca: "" # Optionally, specify the certificate of the CA that issued the KeySecure TLS certificate.
Please use your refresh token.
-
Finally we can start a KES server in a new window/tab:
export APP_IDENTITY=$(kes tool identity of app.cert) kes server --config=server-config.yml --auth=off
--auth=off
is required since our root.cert and app.cert certificates are self-signed.
If starting the server fails with an error message similar to:x509: certificate is not valid for any names, but wanted to match <your-endpoint>
then your KeySecure instance serves a TLS certificate with neither a common name (subject) nor a subject alternative name (SAN). Such a certificate is invalid. Please update the TLS certificate of your KeySecure instance. You can analyze a certificate with:openssl x509 -text -noout <certificate>
-
In the previous window/tab we now can connect to the server by:
export KES_CLIENT_CERT=app.cert export KES_CLIENT_KEY=app.key kes key create app-key -k
-k
is required because we use self-signed certificates -
Finally, we can derive and decrypt data keys from the previously created
app-key
:kes key derive app-key -k { plaintext : ... ciphertext: ... }
kes key decrypt app-key -k <base64-ciphertext>