Created
June 30, 2015 12:47
-
-
Save kgutwin/f480195325e7d5eb2d7d to your computer and use it in GitHub Desktop.
cert-cli test scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# taken from https://docs.docker.com/articles/https/ | |
# you will have to enter a passphrase when prompted. | |
openssl genrsa -aes256 -out ca-key.pem 2048 | |
openssl req -subj "/CN=$HOSTNAME" -new -x509 -days 365 -key ca-key.pem \ | |
-sha256 -out ca.pem | |
openssl genrsa -out server-key.pem 2048 | |
openssl req -subj "/CN=$HOSTNAME" -new -key server-key.pem -out server.csr | |
echo subjectAltName = IP:127.0.0.1 > extfile.cnf | |
openssl x509 -req -days 365 -in server.csr -CA ca.pem -CAkey ca-key.pem \ | |
-CAcreateserial -out server-cert.pem -extfile extfile.cnf | |
openssl genrsa -out key.pem 2048 | |
openssl req -subj '/CN=client' -new -key key.pem -out client.csr | |
echo extendedKeyUsage = clientAuth > extfile.cnf | |
openssl x509 -req -days 365 -in client.csr -CA ca.pem -CAkey ca-key.pem \ | |
-CAcreateserial -out cert.pem -extfile extfile.cnf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
[ -f ca.pem ] || bash make-ca.sh | |
set -e | |
set -v | |
# Start vault server... | |
cat > config.json <<EOF | |
listener "tcp" { | |
address = "127.0.0.1:8300" | |
tls_cert_file = "server-cert.pem" | |
tls_key_file = "server-key.pem" | |
} | |
EOF | |
vault server -dev -config=config.json & | |
VAULT_PID=$! | |
function finally { | |
kill $VAULT_PID | |
} | |
trap finally EXIT | |
sleep 1 | |
# Insert a test secret and policy... | |
export VAULT_ADDR=http://127.0.0.1:8200 | |
vault write secret/hello value=world | |
vault read secret/hello | |
cat > policy.hcl <<EOF | |
path "sys" { | |
policy = "deny" | |
} | |
path "secret" { | |
policy = "write" | |
} | |
path "secret/foo" { | |
policy = "read" | |
} | |
EOF | |
vault policy-write cert-test policy.hcl | |
# Configure cert backend... | |
vault auth-enable cert | |
vault write auth/cert/certs/ca display_name=ca policies=cert-test certificate=@ca.pem lease=3600 | |
# Drop root privilege and test login... | |
rm ~/.vault-token | |
vault read secret/hello || echo "Should fail" | |
export VAULT_ADDR=https://127.0.0.1:8300 | |
export VAULT_CACERT=ca.pem | |
export VAULT_CLIENT_CERT=cert.pem | |
export VAULT_CLIENT_KEY=key.pem | |
vault status | |
vault auth -method=cert | |
cat ~/.vault-token && echo | |
vault read secret/hello | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You probably want to run this in a temporary directory, since it's going to drop a lot of files in the current directory. Also requires
vault
to be installed from a version equal to or ahead of https://github.com/kgutwin/vault/tree/6668a6d7ef65ab93f4f1ea9747ff1cb49ab180f5.