From 336e7852e3415b989ebcf0623b114dac43e27959 Mon Sep 17 00:00:00 2001 From: lhahn Date: Sun, 20 Aug 2023 10:30:34 +0200 Subject: [PATCH] Git initial commit --- LICENSE | 9 +++ README.md | 3 + defaults/main.yml | 23 ++++++ handlers/main.yml | 1 + meta/main.yml | 16 ++++ tasks/main.yml | 107 +++++++++++++++++++++++++++ templates/usr/share/easy-rsa/vars.j2 | 18 +++++ vars/main.yml | 7 ++ 8 files changed, 184 insertions(+) create mode 100755 LICENSE create mode 100755 README.md create mode 100755 defaults/main.yml create mode 100755 handlers/main.yml create mode 100755 meta/main.yml create mode 100755 tasks/main.yml create mode 100755 templates/usr/share/easy-rsa/vars.j2 create mode 100755 vars/main.yml diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..2071b23 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100755 index 0000000..21189ba --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# cloud-easyrsa + +Ansible role to provide easyrsa for VPN CA setup. Can be used standalone aswell. \ No newline at end of file diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100755 index 0000000..3dfe409 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,23 @@ +--- +cloud_apps: /opt +cloud_storage: /opt/storage +cloud_stage: prod +cloud_update: false + +easy_rsa_ca: "{{ ansible_hostname }}" +easy_rsa_servers: + - name: some_server + state: present +easy_rsa_clients: + - name: username + state: present + - name: username.app + state: absent +easy_rsa_vars_conf: + country: "COUNTRY" + province: "PROVINCE" + city: "CITY" + company: "YOUR COMPANY" + mail: "admin@your-company.com" + + diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100755 index 0000000..ed97d53 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1 @@ +--- diff --git a/meta/main.yml b/meta/main.yml new file mode 100755 index 0000000..d6e3044 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,16 @@ +--- +galaxy_info: + role_name: easy-rsa + namespace: hahn-cloud + author: Lars Hahn + company: OpenDevChain + license: MIT + description: Role to setup an easy-rsa for PKI, CA and certificates. + min_ansible_version: 2.7 + platforms: + - name: Debian + versions: + - 10 + galaxy_tags: + - easy-rsa +dependencies: [] \ No newline at end of file diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100755 index 0000000..cbd92f2 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,107 @@ +--- +- name: install openvpn, easy-rsa and recommendations + apt: + update_cache: yes + state: "{% if cloud_update | bool %}latest{% else %}present{% endif %}" + install_recommends: yes + pkg: + - "easy-rsa" + +- name: Setup easy-rsa variables + template: + src: "templates{{ easy_rsa_home }}/vars.j2" + dest: "{{ easy_rsa_home }}/vars" + mode: 0644 + owner: root + group: root + +- name: initialise PKI + shell: + creates: "{{ easy_rsa_home }}/pki" + chdir: "{{ easy_rsa_home }}" + cmd: ./easyrsa init-pki + +- name: setup certificate authority + shell: + creates: "{{ easy_rsa_home }}/pki/ca.crt" + chdir: "{{ easy_rsa_home }}" + cmd: "echo '{{ easy_rsa_ca }}' | ./easyrsa build-ca nopass" + +- name: setup diffie-hellman key-pair for key exchange + shell: + creates: "{{ easy_rsa_home }}/pki/dh.pem" + chdir: "{{ easy_rsa_home }}" + cmd: ./easyrsa gen-dh + +- name: setup server certificates + shell: + cmd: | + echo '{{ srvr }}' | ./easyrsa gen-req {{ srvr }} nopass + echo 'yes' | ./easyrsa sign-req server {{ srvr }} + chdir: "{{ easy_rsa_home }}" + creates: "{{ easy_rsa_home }}/pki/issued/{{ srvr }}.crt" + loop: "{{ easy_rsa_servers_active }}" + loop_control: + loop_var: srvr + label: "{{ srvr }}" + +- name: setup client certificates + shell: + cmd: | + echo '{{ client }}' | ./easyrsa gen-req {{ client }} nopass + echo 'yes' | ./easyrsa sign-req client {{ client }} + chdir: "{{ easy_rsa_home }}" + creates: "{{ easy_rsa_home }}/pki/issued/{{ client }}.crt" + loop: "{{ easy_rsa_clients_active }}" + loop_control: + loop_var: client + label: "{{ client }}" + +- name: verify certificate integrety + command: "openssl verify -CAfile {{ easy_rsa_home }}/pki/ca.crt {{ easy_rsa_home }}/pki/issued/{{ cert }}.crt" + register: easy_rsa_cert_check + changed_when: ((easy_rsa_cert_check.stdout.split(' ') | length) > 1) and (easy_rsa_cert_check.stdout.split(' ')[1] != "OK") + loop: "{{ easy_rsa_entities_active }}" + loop_control: + loop_var: cert + label: "{{ cert }}" + +- name: find abstent easy-rsa certifcates + find: + paths: "{{ easy_rsa_home }}/pki/issued/" + pattern: "{{ cert }}.crt" + loop: "{{ easy_rsa_entities_passive }}" + loop_control: + loop_var: cert + label: "{{ cert }}" + register: easy_rsa_absent_certs + +- name: remove absent easy-rsa clients certs + file: + state: absent + path: "{{ client }}" + loop: "{{ easy_rsa_absent_certs.results | json_query('[*].files[*].path') | flatten }}" + loop_control: + loop_var: client + label: "{{ client | basename }}" + when: easy_rsa_absent_certs.results | length > 0 + +- name: find abstent easy-rsa keys + find: + paths: "{{ easy_rsa_home }}/pki/private/" + pattern: "{{ cert }}.key" + loop: "{{ easy_rsa_entities_passive }}" + loop_control: + loop_var: cert + label: "{{ cert }}" + register: easy_rsa_absent_keys + +- name: remove absent easy-rsa clients keys + file: + state: absent + path: "{{ client }}" + loop: "{{ easy_rsa_absent_keys.results | json_query('[*].files[*].path') | flatten }}" + loop_control: + loop_var: client + label: "{{ client | basename }}" + when: easy_rsa_absent_keys.results | length > 0 \ No newline at end of file diff --git a/templates/usr/share/easy-rsa/vars.j2 b/templates/usr/share/easy-rsa/vars.j2 new file mode 100755 index 0000000..80d3954 --- /dev/null +++ b/templates/usr/share/easy-rsa/vars.j2 @@ -0,0 +1,18 @@ +set_var EASYRSA "$PWD" +set_var EASYRSA_PKI "$EASYRSA/pki" +set_var EASYRSA_DN "cn_only" +set_var EASYRSA_REQ_COUNTRY "{{ easy_rsa_vars_conf.country }}" +set_var EASYRSA_REQ_PROVINCE "{{ easy_rsa_vars_conf.province }}" +set_var EASYRSA_REQ_CITY "{{ easy_rsa_vars_conf.city }}" +set_var EASYRSA_REQ_ORG "{{ easy_rsa_vars_conf.company }} CERTIFICATE AUTHORITY" +set_var EASYRSA_REQ_EMAIL "{{ easy_rsa_vars_conf.mail }}" +set_var EASYRSA_REQ_OU "{{ easy_rsa_vars_conf.company }} EASY CA" +set_var EASYRSA_KEY_SIZE 2048 +set_var EASYRSA_ALGO rsa +set_var EASYRSA_CA_EXPIRE 7500 +set_var EASYRSA_CERT_EXPIRE 365 +set_var EASYRSA_NS_SUPPORT "no" +set_var EASYRSA_NS_COMMENT "{{ easy_rsa_vars_conf.company }} CERTIFICATE AUTHORITY" +set_var EASYRSA_EXT_DIR "$EASYRSA/x509-types" +set_var EASYRSA_SSL_CONF "$EASYRSA/openssl-easyrsa.cnf" +set_var EASYRSA_DIGEST "sha256" \ No newline at end of file diff --git a/vars/main.yml b/vars/main.yml new file mode 100755 index 0000000..8fd7599 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,7 @@ +--- +easy_rsa_home: "/usr/share/easy-rsa" +easy_rsa_servers_active: "{{ easy_rsa_servers | json_query('[?state==`present`].name') }}" +easy_rsa_clients_active: "{{ easy_rsa_clients | json_query('[?state==`present`].name') }}" + +easy_rsa_entities_active: "{{ (easy_rsa_servers + easy_rsa_clients) | json_query('[?state==`present`].name') }}" +easy_rsa_entities_passive: "{{ (easy_rsa_servers + easy_rsa_clients) | json_query('[?state!=`present`].name') }}"