2022-06-08 14:05:52 +00:00
|
|
|
#!/usr/bin/env bats
|
|
|
|
# vim: ft=bats:list:ts=8:sts=4:sw=4:et:ai:si:
|
|
|
|
|
|
|
|
set -u
|
|
|
|
|
|
|
|
setup_file() {
|
2022-06-13 19:54:47 +00:00
|
|
|
load "../lib/setup_file.sh"
|
2022-06-08 14:05:52 +00:00
|
|
|
./instance-data load
|
2022-06-13 19:54:47 +00:00
|
|
|
|
2022-06-24 13:55:21 +00:00
|
|
|
CONFIG_DIR=$(dirname "${CONFIG_YAML}")
|
|
|
|
export CONFIG_DIR
|
|
|
|
|
|
|
|
tmpdir="${BATS_FILE_TMPDIR}"
|
2022-06-08 14:05:52 +00:00
|
|
|
export tmpdir
|
2022-06-13 19:54:47 +00:00
|
|
|
|
|
|
|
CFDIR="${BATS_TEST_DIRNAME}/testdata/cfssl"
|
|
|
|
export CFDIR
|
|
|
|
|
2022-06-08 14:05:52 +00:00
|
|
|
#gen the CA
|
2022-06-13 19:54:47 +00:00
|
|
|
cfssl gencert --initca "${CFDIR}/ca.json" 2>/dev/null | cfssljson --bare "${tmpdir}/ca"
|
2022-06-08 14:05:52 +00:00
|
|
|
#gen an intermediate
|
2022-06-13 19:54:47 +00:00
|
|
|
cfssl gencert --initca "${CFDIR}/intermediate.json" 2>/dev/null | cfssljson --bare "${tmpdir}/inter"
|
|
|
|
cfssl sign -ca "${tmpdir}/ca.pem" -ca-key "${tmpdir}/ca-key.pem" -config "${CFDIR}/profiles.json" -profile intermediate_ca "${tmpdir}/inter.csr" 2>/dev/null | cfssljson --bare "${tmpdir}/inter"
|
|
|
|
#gen server cert for crowdsec with the intermediate
|
|
|
|
cfssl gencert -ca "${tmpdir}/inter.pem" -ca-key "${tmpdir}/inter-key.pem" -config "${CFDIR}/profiles.json" -profile=server "${CFDIR}/server.json" 2>/dev/null | cfssljson --bare "${tmpdir}/server"
|
2022-06-08 14:05:52 +00:00
|
|
|
#gen client cert for the agent
|
2022-06-13 19:54:47 +00:00
|
|
|
cfssl gencert -ca "${tmpdir}/inter.pem" -ca-key "${tmpdir}/inter-key.pem" -config "${CFDIR}/profiles.json" -profile=client "${CFDIR}/agent.json" 2>/dev/null | cfssljson --bare "${tmpdir}/agent"
|
2022-06-08 14:05:52 +00:00
|
|
|
#gen client cert for the agent with an invalid OU
|
2022-06-13 19:54:47 +00:00
|
|
|
cfssl gencert -ca "${tmpdir}/inter.pem" -ca-key "${tmpdir}/inter-key.pem" -config "${CFDIR}/profiles.json" -profile=client "${CFDIR}/agent_invalid.json" 2>/dev/null | cfssljson --bare "${tmpdir}/agent_bad_ou"
|
2022-06-08 14:05:52 +00:00
|
|
|
#gen client cert for the agent directly signed by the CA, it should be refused by crowdsec as uses the intermediate
|
2022-06-13 19:54:47 +00:00
|
|
|
cfssl gencert -ca "${tmpdir}/ca.pem" -ca-key "${tmpdir}/ca-key.pem" -config "${CFDIR}/profiles.json" -profile=client "${CFDIR}/agent.json" 2>/dev/null | cfssljson --bare "${tmpdir}/agent_invalid"
|
2022-06-08 14:05:52 +00:00
|
|
|
|
2022-06-13 19:54:47 +00:00
|
|
|
cfssl gencert -ca "${tmpdir}/inter.pem" -ca-key "${tmpdir}/inter-key.pem" -config "${CFDIR}/profiles.json" -profile=client "${CFDIR}/agent.json" 2>/dev/null | cfssljson --bare "${tmpdir}/agent_revoked"
|
|
|
|
serial="$(openssl x509 -noout -serial -in "${tmpdir}/agent_revoked.pem" | cut -d '=' -f2)"
|
|
|
|
echo "ibase=16; ${serial}" | bc >"${tmpdir}/serials.txt"
|
|
|
|
cfssl gencrl "${tmpdir}/serials.txt" "${tmpdir}/ca.pem" "${tmpdir}/ca-key.pem" | base64 -d | openssl crl -inform DER -out "${tmpdir}/crl.pem"
|
2022-06-08 14:05:52 +00:00
|
|
|
|
2022-07-21 11:03:46 +00:00
|
|
|
cat "${tmpdir}/ca.pem" "${tmpdir}/inter.pem" > "${tmpdir}/bundle.pem"
|
|
|
|
|
2022-07-01 20:45:55 +00:00
|
|
|
config_set '
|
2022-06-08 14:05:52 +00:00
|
|
|
.api.server.tls.cert_file=strenv(tmpdir) + "/server.pem" |
|
|
|
|
.api.server.tls.key_file=strenv(tmpdir) + "/server-key.pem" |
|
|
|
|
.api.server.tls.ca_cert_path=strenv(tmpdir) + "/inter.pem" |
|
|
|
|
.api.server.tls.crl_path=strenv(tmpdir) + "/crl.pem" |
|
|
|
|
.api.server.tls.agents_allowed_ou=["agent-ou"]
|
2022-07-01 20:45:55 +00:00
|
|
|
'
|
2022-06-08 14:05:52 +00:00
|
|
|
|
2022-07-22 12:21:05 +00:00
|
|
|
# remove all machines
|
|
|
|
|
2023-06-01 07:42:27 +00:00
|
|
|
for machine in $(cscli machines list -o json | jq -r '.[].machineId'); do
|
2023-06-05 11:03:18 +00:00
|
|
|
cscli machines delete "${machine}" >/dev/null 2>&1
|
2022-07-22 12:21:05 +00:00
|
|
|
done
|
|
|
|
|
2022-06-24 13:55:21 +00:00
|
|
|
config_disable_agent
|
2022-06-08 14:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
teardown_file() {
|
|
|
|
load "../lib/teardown_file.sh"
|
|
|
|
}
|
|
|
|
|
|
|
|
setup() {
|
|
|
|
load "../lib/setup.sh"
|
2023-01-26 16:12:59 +00:00
|
|
|
config_set '
|
|
|
|
.api.server.tls.cert_file=strenv(tmpdir) + "/server.pem" |
|
|
|
|
.api.server.tls.key_file=strenv(tmpdir) + "/server-key.pem" |
|
|
|
|
.api.server.tls.ca_cert_path=strenv(tmpdir) + "/inter.pem" |
|
|
|
|
.api.server.tls.crl_path=strenv(tmpdir) + "/crl.pem" |
|
|
|
|
.api.server.tls.agents_allowed_ou=["agent-ou"]
|
|
|
|
'
|
2022-06-08 14:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
teardown() {
|
|
|
|
./instance-crowdsec stop
|
|
|
|
}
|
|
|
|
|
|
|
|
#----------
|
|
|
|
|
2023-01-26 16:12:59 +00:00
|
|
|
@test "missing key_file" {
|
|
|
|
config_set '.api.server.tls.key_file=""'
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
rune -0 wait-for \
|
|
|
|
--err "missing TLS key file" \
|
|
|
|
"${CROWDSEC}"
|
2023-01-26 16:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "missing cert_file" {
|
|
|
|
config_set '.api.server.tls.cert_file=""'
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
rune -0 wait-for \
|
|
|
|
--err "missing TLS cert file" \
|
|
|
|
"${CROWDSEC}"
|
2023-01-26 16:12:59 +00:00
|
|
|
}
|
|
|
|
|
2022-06-24 13:55:21 +00:00
|
|
|
@test "invalid OU for agent" {
|
2022-07-01 20:45:55 +00:00
|
|
|
config_set "${CONFIG_DIR}/local_api_credentials.yaml" '
|
2022-07-21 11:03:46 +00:00
|
|
|
.ca_cert_path=strenv(tmpdir) + "/bundle.pem" |
|
2022-06-08 14:05:52 +00:00
|
|
|
.key_path=strenv(tmpdir) + "/agent_bad_ou-key.pem" |
|
|
|
|
.cert_path=strenv(tmpdir) + "/agent_bad_ou.pem" |
|
|
|
|
.url="https://127.0.0.1:8080"
|
2022-07-01 20:45:55 +00:00
|
|
|
'
|
2022-06-08 14:05:52 +00:00
|
|
|
|
2022-07-01 20:45:55 +00:00
|
|
|
config_set "${CONFIG_DIR}/local_api_credentials.yaml" 'del(.login,.password)'
|
2022-06-08 14:05:52 +00:00
|
|
|
./instance-crowdsec start
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli machines list -o json
|
2022-06-08 14:05:52 +00:00
|
|
|
assert_output '[]'
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:55:21 +00:00
|
|
|
@test "we have exactly one machine registered with TLS" {
|
2022-07-01 20:45:55 +00:00
|
|
|
config_set "${CONFIG_DIR}/local_api_credentials.yaml" '
|
2022-07-21 11:03:46 +00:00
|
|
|
.ca_cert_path=strenv(tmpdir) + "/bundle.pem" |
|
2022-06-08 14:05:52 +00:00
|
|
|
.key_path=strenv(tmpdir) + "/agent-key.pem" |
|
|
|
|
.cert_path=strenv(tmpdir) + "/agent.pem" |
|
|
|
|
.url="https://127.0.0.1:8080"
|
2022-07-01 20:45:55 +00:00
|
|
|
'
|
2022-06-08 14:05:52 +00:00
|
|
|
|
2022-07-01 20:45:55 +00:00
|
|
|
config_set "${CONFIG_DIR}/local_api_credentials.yaml" 'del(.login,.password)'
|
2022-06-08 14:05:52 +00:00
|
|
|
./instance-crowdsec start
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli lapi status
|
|
|
|
rune -0 cscli machines list -o json
|
|
|
|
rune -0 jq -c '[. | length, .[0].machineId[0:32], .[0].isValidated, .[0].ipAddress, .[0].auth_type]' <(output)
|
2022-06-08 14:05:52 +00:00
|
|
|
|
|
|
|
assert_output '[1,"localhost@127.0.0.1",true,"127.0.0.1","tls"]'
|
2024-03-14 09:43:02 +00:00
|
|
|
rune -0 cscli machines delete localhost@127.0.0.1
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "a machine can still connect with a unix socket, no TLS" {
|
|
|
|
sock=$(config_get '.api.server.listen_socket')
|
|
|
|
export sock
|
|
|
|
|
|
|
|
# an agent is a machine too
|
|
|
|
config_disable_agent
|
|
|
|
./instance-crowdsec start
|
|
|
|
|
|
|
|
rune -0 cscli machines add with-socket --auto --force
|
|
|
|
rune -0 cscli lapi status
|
|
|
|
|
|
|
|
rune -0 cscli machines list -o json
|
|
|
|
rune -0 jq -c '[. | length, .[0].machineId[0:32], .[0].isValidated, .[0].ipAddress, .[0].auth_type]' <(output)
|
|
|
|
assert_output '[1,"with-socket",true,"127.0.0.1","password"]'
|
|
|
|
|
|
|
|
# TLS cannot be used with a unix socket
|
|
|
|
|
|
|
|
config_set "${CONFIG_DIR}/local_api_credentials.yaml" '
|
|
|
|
.ca_cert_path=strenv(tmpdir) + "/bundle.pem"
|
|
|
|
'
|
|
|
|
|
|
|
|
rune -1 cscli lapi status
|
|
|
|
assert_stderr --partial "loading api client: cannot use TLS with a unix socket"
|
|
|
|
|
|
|
|
config_set "${CONFIG_DIR}/local_api_credentials.yaml" '
|
|
|
|
del(.ca_cert_path) |
|
|
|
|
.key_path=strenv(tmpdir) + "/agent-key.pem"
|
|
|
|
'
|
|
|
|
|
|
|
|
rune -1 cscli lapi status
|
|
|
|
assert_stderr --partial "loading api client: cannot use TLS with a unix socket"
|
|
|
|
|
|
|
|
config_set "${CONFIG_DIR}/local_api_credentials.yaml" '
|
|
|
|
del(.key_path) |
|
|
|
|
.cert_path=strenv(tmpdir) + "/agent.pem"
|
|
|
|
'
|
|
|
|
|
|
|
|
rune -1 cscli lapi status
|
|
|
|
assert_stderr --partial "loading api client: cannot use TLS with a unix socket"
|
|
|
|
|
|
|
|
rune -0 cscli machines delete with-socket
|
2022-06-08 14:05:52 +00:00
|
|
|
}
|
|
|
|
|
2022-06-24 13:55:21 +00:00
|
|
|
@test "invalid cert for agent" {
|
2022-07-01 20:45:55 +00:00
|
|
|
config_set "${CONFIG_DIR}/local_api_credentials.yaml" '
|
2022-07-21 11:03:46 +00:00
|
|
|
.ca_cert_path=strenv(tmpdir) + "/bundle.pem" |
|
2022-06-08 14:05:52 +00:00
|
|
|
.key_path=strenv(tmpdir) + "/agent_invalid-key.pem" |
|
|
|
|
.cert_path=strenv(tmpdir) + "/agent_invalid.pem" |
|
|
|
|
.url="https://127.0.0.1:8080"
|
2022-07-01 20:45:55 +00:00
|
|
|
'
|
|
|
|
config_set "${CONFIG_DIR}/local_api_credentials.yaml" 'del(.login,.password)'
|
2022-06-08 14:05:52 +00:00
|
|
|
./instance-crowdsec start
|
2024-02-09 12:55:24 +00:00
|
|
|
rune -1 cscli lapi status
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli machines list -o json
|
2022-06-08 14:05:52 +00:00
|
|
|
assert_output '[]'
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:55:21 +00:00
|
|
|
@test "revoked cert for agent" {
|
2024-02-09 12:55:24 +00:00
|
|
|
truncate_log
|
2022-07-01 20:45:55 +00:00
|
|
|
config_set "${CONFIG_DIR}/local_api_credentials.yaml" '
|
2024-02-09 12:55:24 +00:00
|
|
|
.ca_cert_path=strenv(tmpdir) + "/bundle.pem" |
|
2022-06-08 14:05:52 +00:00
|
|
|
.key_path=strenv(tmpdir) + "/agent_revoked-key.pem" |
|
|
|
|
.cert_path=strenv(tmpdir) + "/agent_revoked.pem" |
|
|
|
|
.url="https://127.0.0.1:8080"
|
2022-07-01 20:45:55 +00:00
|
|
|
'
|
2022-06-08 14:05:52 +00:00
|
|
|
|
2022-07-01 20:45:55 +00:00
|
|
|
config_set "${CONFIG_DIR}/local_api_credentials.yaml" 'del(.login,.password)'
|
2022-06-08 14:05:52 +00:00
|
|
|
./instance-crowdsec start
|
2024-02-09 12:55:24 +00:00
|
|
|
rune -1 cscli lapi status
|
|
|
|
assert_log --partial "client certificate is revoked by CRL"
|
|
|
|
assert_log --partial "client certificate for CN=localhost OU=[agent-ou] is revoked"
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli machines list -o json
|
2022-06-08 14:05:52 +00:00
|
|
|
assert_output '[]'
|
2022-06-13 19:54:47 +00:00
|
|
|
}
|