From 2bfa2101409d7840c8ffb6b4caf15dbbdaa42cb5 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Fri, 19 Aug 2022 16:40:52 +0200 Subject: [PATCH] Add oidc_idp role - Deploy cacert-idp from oidc_idp subdirectory - configure systemd and start cacert-idp service --- deployment/roles/oidc_idp/README.md | 25 +--- deployment/roles/oidc_idp/defaults/main.yml | 4 +- deployment/roles/oidc_idp/handlers/main.yml | 7 +- deployment/roles/oidc_idp/meta/main.yml | 55 ++------- deployment/roles/oidc_idp/tasks/main.yml | 111 +++++++++++++++++- .../oidc_idp/templates/cacert-idp.service.j2 | 14 +++ .../oidc_idp/templates/idp_config.toml.j2 | 2 + 7 files changed, 148 insertions(+), 70 deletions(-) create mode 100644 deployment/roles/oidc_idp/templates/cacert-idp.service.j2 create mode 100644 deployment/roles/oidc_idp/templates/idp_config.toml.j2 diff --git a/deployment/roles/oidc_idp/README.md b/deployment/roles/oidc_idp/README.md index 225dd44..976afdb 100644 --- a/deployment/roles/oidc_idp/README.md +++ b/deployment/roles/oidc_idp/README.md @@ -1,38 +1,19 @@ Role Name ========= -A brief description of the role goes here. - -Requirements ------------- - -Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. +Setup the CAcert OpenID Connect identity provider application. Role Variables -------------- A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. -Dependencies ------------- - -A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. - -Example Playbook ----------------- - -Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: - - - hosts: servers - roles: - - { role: username.rolename, x: 42 } - License ------- -BSD +GPL-2.0-or-later Author Information ------------------ -An optional section for the role authors to include contact information, or a website (HTML is not allowed). +Jan Dittberner diff --git a/deployment/roles/oidc_idp/defaults/main.yml b/deployment/roles/oidc_idp/defaults/main.yml index 3f81055..58205fb 100644 --- a/deployment/roles/oidc_idp/defaults/main.yml +++ b/deployment/roles/oidc_idp/defaults/main.yml @@ -1,2 +1,4 @@ --- -# defaults file for roles/oidc_idp +cacert_os_user: cacert +cacert_os_group: cacert +cacert_home: /srv/cacert diff --git a/deployment/roles/oidc_idp/handlers/main.yml b/deployment/roles/oidc_idp/handlers/main.yml index abef7bb..0901f0e 100644 --- a/deployment/roles/oidc_idp/handlers/main.yml +++ b/deployment/roles/oidc_idp/handlers/main.yml @@ -1,2 +1,7 @@ --- -# handlers file for roles/oidc_idp +- name: idp_systemd_reload + ansible.builtin.systemd: + state: started + name: cacert-idp + daemon_reload: true + enabled: true diff --git a/deployment/roles/oidc_idp/meta/main.yml b/deployment/roles/oidc_idp/meta/main.yml index c572acc..1f76312 100644 --- a/deployment/roles/oidc_idp/meta/main.yml +++ b/deployment/roles/oidc_idp/meta/main.yml @@ -1,52 +1,17 @@ +--- galaxy_info: - author: your name - description: your role description - company: your company (optional) + author: Jan Dittberner + description: CAcert OpenID Connect identity provider application setup + company: CAcert - # If the issue tracker for your role is not on github, uncomment the - # next line and provide a value - # issue_tracker_url: http://example.com/issue/tracker - - # Choose a valid license ID from https://spdx.org - some suggested licenses: - # - BSD-3-Clause (default) - # - MIT - # - GPL-2.0-or-later - # - GPL-3.0-only - # - Apache-2.0 - # - CC-BY-4.0 - license: license (GPL-2.0-or-later, MIT, etc) + license: GPL-2.0-or-later min_ansible_version: 2.1 - # If this a Container Enabled role, provide the minimum Ansible Container version. - # min_ansible_container_version: - - # - # Provide a list of supported platforms, and for each platform a list of versions. - # If you don't wish to enumerate all versions for a particular platform, use 'all'. - # To view available platforms and versions (or releases), visit: - # https://galaxy.ansible.com/api/v1/platforms/ - # - # platforms: - # - name: Fedora - # versions: - # - all - # - 25 - # - name: SomePlatform - # versions: - # - all - # - 1.0 - # - 7 - # - 99.99 + platforms: + - name: Debian + versions: + - bullseye + - bookworm galaxy_tags: [] - # List tags for your role here, one per line. A tag is a keyword that describes - # and categorizes the role. Users find roles by searching for tags. Be sure to - # remove the '[]' above, if you add tags to this list. - # - # NOTE: A tag is limited to a single word comprised of alphanumeric characters. - # Maximum 20 tags per role. - -dependencies: [] - # List your role dependencies here, one per line. Be sure to remove the '[]' above, - # if you add dependencies to this list. diff --git a/deployment/roles/oidc_idp/tasks/main.yml b/deployment/roles/oidc_idp/tasks/main.yml index 3ea98dc..6f022ba 100644 --- a/deployment/roles/oidc_idp/tasks/main.yml +++ b/deployment/roles/oidc_idp/tasks/main.yml @@ -1,2 +1,111 @@ --- -# tasks file for roles/oidc_idp +- name: Create CAcert group + ansible.builtin.group: + name: "{{ cacert_os_group }}" + state: present + system: true + +- name: Create CAcert user + ansible.builtin.user: + name: "{{ cacert_os_user }}" + group: "{{ cacert_os_group }}" + home: "{{ cacert_home }}" + state: present + system: true + +- name: Create CAcert directories + ansible.builtin.file: + path: "{{ cacert_home }}/{{ item.path }}" + owner: "{{ cacert_os_user }}" + group: "{{ cacert_os_group }}" + mode: "{{ item.mode }}" + state: directory + loop: + - { path: etc, mode: '0750' } + - { path: bin, mode: '0750' } + - { path: download, mode: '0750' } + +- name: Copy IDP binary + ansible.builtin.copy: + src: ../oidc_idp/cacert-idp + dest: "{{ cacert_home }}/bin/cacert-idp" + owner: root + group: "{{ cacert_os_group }}" + mode: "0750" + +- name: Check whether certificate exists + ansible.builtin.stat: + path: "{{ idp_tls.cert }}" + register: idp_cert_st + +- name: Create IDP key and certificate with mkcert + block: + + - name: Create temporary directory for IDP key and certificate + ansible.builtin.tempfile: + prefix: "idp-cert." + state: directory + register: idp_cert_temp_dir + + - name: Create IDP key and certificate + ansible.builtin.command: + cmd: "/home/{{ ansible_user | default(ansible_env.USER) }}/.local/bin/mkcert -cert-file {{ idp_cert_temp_dir.path }}/idp.pem -key-file {{ idp_cert_temp_dir.path }}/idp.key.pem {{ oidc_urls.idp.host }}" + + - name: Move IDP certificate and key to target + ansible.builtin.copy: + src: "{{ idp_cert_temp_dir.path }}/{{ item.src }}" + dest: "{{ item.dest }}" + owner: root + group: "{{ cacert_os_group }}" + mode: "{{ item.mode }}" + remote_src: true + loop: + - {src: idp.pem, dest: "{{ idp_tls.cert }}", mode: '0644'} + - {src: idp.key.pem, dest: "{{ idp_tls.key }}", mode: '0640'} + become: true + + - name: Remove temporary directory + ansible.builtin.file: + path: "{{ idp_cert_temp_dir.path }}" + state: absent + + when: use_mkcert and not idp_cert_st.stat.exists + become: false + +- name: Copy IDP key and certificate from inventory + block: + + - name: Copy IDP certificate + ansible.builtin.copy: + dest: "{{ idp_tls.cert }}" + owner: root + group: "{{ cacert_os_group }}" + mode: '0644' + content: "{{ idp_tls.certdata }}" + + - name: Copy IDP key + ansible.builtin.copy: + dest: "{{ idp_tls.key }}" + owner: root + group: "{{ cacert_os_group }}" + mode: '0640' + content: "{{ idp_tls.keydata }}" + + when: not use_mkcert +- name: Create IDP configuration + ansible.builtin.template: + src: idp_config.toml.j2 + dest: "{{ cacert_home }}/etc/cacert-idp.toml" + owner: root + group: "{{ cacert_os_group }}" + mode: '0640' + notify: idp_systemd_reload + +- name: Create IDP systemd unit file + ansible.builtin.template: + src: cacert-idp.service.j2 + dest: /etc/systemd/system/cacert-idp.service + owner: root + group: root + mode: "0640" + notify: idp_systemd_reload diff --git a/deployment/roles/oidc_idp/templates/cacert-idp.service.j2 b/deployment/roles/oidc_idp/templates/cacert-idp.service.j2 new file mode 100644 index 0000000..e41f828 --- /dev/null +++ b/deployment/roles/oidc_idp/templates/cacert-idp.service.j2 @@ -0,0 +1,14 @@ +[Unit] +Description=CAcert OpenID Connect identity provider +After=network.target +Documentation=https://code.cacert.org/cacert/oidc-idp + +[Service] +ExecStart={{ cacert_home }}/bin/cacert-idp --conf "{{ cacert_home }}/etc/cacert-idp.toml" +WorkingDirectory={{ cacert_home }} +User={{ cacert_os_user }} +Group={{ cacert_os_group }} + +[Install] +WantedBy=multi-user.target + diff --git a/deployment/roles/oidc_idp/templates/idp_config.toml.j2 b/deployment/roles/oidc_idp/templates/idp_config.toml.j2 new file mode 100644 index 0000000..ddec0f4 --- /dev/null +++ b/deployment/roles/oidc_idp/templates/idp_config.toml.j2 @@ -0,0 +1,2 @@ +[security] +csrf.key = "{{ idp_csrf_key | default(lookup('community.general.random_string', length=32, base64=true)) }}"