From 26289afd095298fd9239e253654edff9d175bba5 Mon Sep 17 00:00:00 2001 From: lhahn Date: Sun, 20 Aug 2023 11:14:06 +0200 Subject: [PATCH] Git initial commit --- LICENSE | 9 +++ README.md | 3 + defaults/main.yml | 50 ++++++++++++++ handlers/main.yml | 5 ++ meta/main.yml | 16 +++++ tasks/main.yml | 67 +++++++++++++++++++ templates/etc/systemd/system/vault.service.j2 | 36 ++++++++++ templates/opt/vault/vault.conf.hcl.j2 | 36 ++++++++++ vars/main.yml | 8 +++ 9 files changed, 230 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/etc/systemd/system/vault.service.j2 create mode 100755 templates/opt/vault/vault.conf.hcl.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..08caaf2 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# cloud-vault + +Ansible role in order to setup Hashicorp Vault. \ No newline at end of file diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100755 index 0000000..e904740 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,50 @@ +--- +cloud_apps: /opt +cloud_storage: /opt/storage +cloud_stage: prod +cloud_update: false + +vault_source: https://releases.hashicorp.com/vault +vault_version: 1.9.3 +vault_arch: linux_amd64 + +vault_pid: "{{ cloud_apps }}/vault/vault.pid" + +vault_ui: "true" +vault_no_mlock: "false" + +vault_port: 8200 +vault_listener: 127.0.0.1 +vault_api_addr: "http://{{ vault_listener }}:{{ vault_port }}" +vault_cluster_addr: "" +vault_cluster_name: "vault" + + +vault_storage: + storage: "storage" + type: "file" + conf: | + path = "{{ vault_home }}/data" + +vault_listener_tcp: + address: "{{ vault_listener }}:{{ vault_port }}" + tls_disable: "true" + proxy_protocol_behavior: "{{ vault_proxy_behaviour }}" + + +vault_config_options: + max_lease_ttl: "10h" + default_lease_ttl: "10h" + +vault_config_blocks: + - conf: telemetry + type: + options: | + prometheus_retention_time = "30s" + disable_hostname = true + + +# required only, when vault_proxy_behaviour is e.g. set to allow_authorized +# see https://www.vaultproject.io/docs/configuration/listener/tcp#proxy_protocol_authorized_addrs +vault_proxy_authorised_addr: [] +vault_proxy_behaviour: use_always \ No newline at end of file diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100755 index 0000000..9b97a9b --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,5 @@ +--- +- name: restart vault + systemd: + name: vault + state: restarted diff --git a/meta/main.yml b/meta/main.yml new file mode 100755 index 0000000..5d15a14 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,16 @@ +--- +galaxy_info: + role_name: vault + namespace: hahn-cloud + author: Lars Hahn + company: OpenDevChain + license: MIT + description: Role to setup vault from HashiCorp + min_ansible_version: 2.7 + platforms: + - name: Debian + versions: + - 11 + galaxy_tags: + - vault +dependencies: [] diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100755 index 0000000..f7d7dfc --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,67 @@ +--- +- name: setup vault group + group: + name: "{{ vault_usr }}" + state: "present" + +- name: setup vault user + user: + name: "{{ vault_usr }}" + group: "{{ vault_grp }}" + groups: + - "{{ vault_grp }}" + comment: Virtual Vault User + shell: /sbin/nologin + state: present + +- name: setup vault directories + file: + state: directory + path: "{{ item }}" + owner: "{{ vault_usr }}" + group: "{{ vault_grp }}" + mode: 0750 + loop: + - "{{ vault_inst }}" + - "{{ vault_home }}" + - "{{ vault_log }}" + +- name: setup vault installation link + file: + state: link + src: "{{ vault_inst }}" + dest: "{{ vault_link }}" + owner: "{{ vault_usr }}" + group: "{{ vault_grp }}" + mode: 0750 + +- name: download vault and unarchive + unarchive: + src: "{{ vault_source }}/{{ vault_version }}/vault_{{ vault_version }}_{{ vault_arch }}.zip" + dest: "{{ vault_link }}" + remote_src: yes + owner: "{{ vault_usr }}" + group: "{{ vault_grp }}" + mode: "o-rwx" + creates: "{{ vault_link }}/vault" + +- name: setup vault config + template: + src: opt/vault/vault.conf.hcl.j2 + dest: "{{ vault_home }}/vault.conf.hcl" + owner: "{{ vault_usr }}" + group: "{{ vault_grp }}" + notify: restart vault + +- name: setup vault systemd unit + template: + src: etc/systemd/system/vault.service.j2 + dest: /etc/systemd/system/vault.service + notify: restart vault + +- name: enable vault systemd unit + systemd: + name: vault + enabled: yes + daemon_reload: yes + state: started diff --git a/templates/etc/systemd/system/vault.service.j2 b/templates/etc/systemd/system/vault.service.j2 new file mode 100755 index 0000000..113d9be --- /dev/null +++ b/templates/etc/systemd/system/vault.service.j2 @@ -0,0 +1,36 @@ +[Unit] +Description=Vault agent +Requires=network-online.target +After=network-online.target + +[Service] +User={{ vault_usr }} +Group={{ vault_grp }} +ProtectSystem=full +ProtectHome=read-only +PrivateTmp=yes +PrivateDevices=yes +SecureBits=keep-caps +AmbientCapabilities=CAP_IPC_LOCK +Capabilities=CAP_IPC_LOCK+ep +CapabilityBoundingSet=CAP_SYSLOG CAP_IPC_LOCK +NoNewPrivileges=yes +ExecStart={{ vault_link }}/vault server -config {{ vault_home }}/vault.conf.hcl +ExecReload=/bin/kill --signal HUP $MAINPID +ExecStop={{ vault_link }}/vault operator step-down +KillMode=process +KillSignal=SIGINT +LimitMEMLOCK=infinity +LimitNOFILE=65536 +PIDFile={{ vault_pid }} +Restart=on-failure +RestartSec=5 +StartLimitInterval=20 +StartLimitBurst=5 +TimeoutStartSec=30 +StandardOutput=append:{{ vault_log }}/vault.log +StandardError=append:{{ vault_log }}/vault.err + + +[Install] +WantedBy=multi-user.target diff --git a/templates/opt/vault/vault.conf.hcl.j2 b/templates/opt/vault/vault.conf.hcl.j2 new file mode 100755 index 0000000..1b5dc9b --- /dev/null +++ b/templates/opt/vault/vault.conf.hcl.j2 @@ -0,0 +1,36 @@ +disable_mlock = {{ vault_no_mlock }} +ui = {{ vault_ui }} + + +api_addr = "{{ vault_api_addr }}" +{% if vault_cluster_addr != '' %} +cluster_addr = "{{ vault_cluster_addr }}" +cluster_name = "{{ vault_cluster_name }}" +disable_clustering = false +{% else %} +disable_clustering = true +{% endif %} + +pid_file = "{{ vault_pid }}" + +{{vault_storage.storage }} "{{ vault_storage.type }}" { + {{ vault_storage.conf }} +} + +listener "tcp" { +{% for key, value in vault_listener_tcp.items() %} + {{ key }} = "{{ value }}" +{% endfor %} +} + +{% for key, value in vault_config_options.items() %} +{{ key }} = "{{ value }}" +{% endfor %} + +{% for block in vault_config_blocks %} +{{ block.conf }}{% if block.type is defined and block.type is not none and block.type != '' %} "{{ block.type }}"{% endif %} { +{% filter indent(width=2) %} + {{ block.options }} +{% endfilter %} +} +{% endfor %} diff --git a/vars/main.yml b/vars/main.yml new file mode 100755 index 0000000..98c55e5 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,8 @@ +--- +vault_usr: vault +vault_grp: "{{ vault_usr }}" + +vault_link: "{{ cloud_apps }}/vault/inst" +vault_inst: "{{ cloud_apps }}/vault/vault_{{ vault_version }}" +vault_home: "{{ cloud_apps }}/vault/home" +vault_log: "{{ cloud_apps }}/vault/log"