Skip to content

Commit 5cc913a

Browse files
committed
feat(cadvisor): add support for basic auth
Signed-off-by: Paul des Garets <pdesgarets@users.noreply.github.com>
1 parent c294350 commit 5cc913a

File tree

8 files changed

+65
-3
lines changed

8 files changed

+65
-3
lines changed

roles/cadvisor/defaults/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ cadvisor_whitelisted_container_labels: []
1414
cadvisor_store_container_labels: true
1515
cadvisor_docker_only: false
1616

17+
cadvisor_basic_auth_users: {}
18+
1719
cadvisor_system_group: "root"
1820
cadvisor_system_user: "{{ cadvisor_system_group }}"
1921

2022
# Local path to stash the archive and its extraction
2123
cadvisor_local_cache_path: "/tmp/cadvisor-{{ ansible_facts['system'] | lower }}-{{ _cadvisor_go_ansible_arch }}/{{ cadvisor_version }}"
2224

2325
cadvisor_binary_install_dir: "/usr/local/bin"
26+
cadvisor_config_dir: "/etc/cadvisor"

roles/cadvisor/meta/argument_specs.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ argument_specs:
7373
description: "do not report raw cgroup metrics, except the root cgroup"
7474
type: "bool"
7575
default: false
76+
cadvisor_basic_auth_users:
77+
description:
78+
- "Dictionary of username / password for HTTP basic authentication"
79+
- >-
80+
Warning : The endpoints `/api/*` and `/metrics` are exposed without authentication. See https://github.com/google/cadvisor/blob/master/docs/web.md#web-ui-authentication and https://github.com/google/cadvisor/issues/3401 for more details.
81+
type: "dict"
82+
default: {}
7683
cadvisor_system_group:
7784
description:
7885
- "I(Advanced)"
@@ -91,3 +98,6 @@ argument_specs:
9198
cadvisor_local_cache_path:
9299
description: 'Local path to stash the archive and its extraction'
93100
default: "/tmp/cadvisor-{{ ansible_facts['system'] | lower }}-{{ _cadvisor_go_ansible_arch }}/{{ cadvisor_version }}"
101+
cadvisor_config_dir:
102+
description: "Directory for cAdvisor configuration files"
103+
default: "/etc/cadvisor"

roles/cadvisor/molecule/alternative/molecule.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ provisioner:
1616
whitelisted_container_labels: [ "com.docker.compose.image" ]
1717
env_metadata_whitelist: [ "PATH" ]
1818
store_container_labels: false
19+
cadvisor_basic_auth_users:
20+
foo: bar

roles/cadvisor/molecule/alternative/tests/test_alternative.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,13 @@ def test_protecthome_property(host):
3131
])
3232
def test_socket(host, sockets):
3333
assert host.socket(sockets).is_listening
34+
35+
36+
def test_forbidden_access(host):
37+
output = host.check_output('curl -s -o /dev/null -w "%{http_code}" -L http://127.0.0.1:8000/')
38+
assert '401' in output
39+
40+
41+
def test_granted_access(host):
42+
output = host.check_output('curl -s -o /dev/null -w "%{http_code}" -L http://127.0.0.1:8000/ -u foo:bar')
43+
assert '200' in output

roles/cadvisor/tasks/configure.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
3+
- name: Configure
4+
ansible.builtin.include_role:
5+
name: prometheus.prometheus._common
6+
tasks_from: configure.yml
7+
vars:
8+
_common_config_dir: "{{ cadvisor_config_dir if (cadvisor_basic_auth_users | length > 0) else None }}"
9+
_common_system_group: "{{ cadvisor_system_group }}"
10+
_common_system_user: "{{ cadvisor_system_user }}"
11+
tags:
12+
- cadvisor
13+
- configure
14+
- cadvisor_configure
15+
16+
- name: Generate htpasswd file
17+
ansible.builtin.template:
18+
src: "htpasswd.j2"
19+
dest: "{{ cadvisor_config_dir }}/htpasswd"
20+
owner: "{{ cadvisor_system_user }}"
21+
group: "{{ cadvisor_system_group }}"
22+
mode: 0640
23+
become: true
24+
when: cadvisor_basic_auth_users | length > 0
25+
notify:
26+
- Restart cadvisor
27+
tags:
28+
- cadvisor
29+
- configure
30+
- cadvisor_configure

roles/cadvisor/tasks/main.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434
- cadvisor_configure
3535

3636
- name: Configure
37-
ansible.builtin.include_role:
38-
name: prometheus.prometheus._common
39-
tasks_from: configure.yml
37+
ansible.builtin.include_tasks:
38+
file: configure.yml
4039
tags:
4140
- cadvisor_configure
4241

roles/cadvisor/templates/cadvisor.service.j2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ ExecStart={{ cadvisor_binary_install_dir }}/cadvisor \
2323
{% endif -%}
2424
{% if cadvisor_env_metadata_whitelist | length > 0 %}
2525
'--env_metadata_whitelist={{ cadvisor_env_metadata_whitelist | join(',') }}' \
26+
{% endif %}
27+
{% if cadvisor_basic_auth_users | length > 0 %}
28+
'--http_auth_file={{ cadvisor_config_dir }}/htpasswd' \
29+
'--http_auth_realm=cadvisor' \
2630
{% endif %}
2731
'--store_container_labels={{ cadvisor_store_container_labels | lower }}' \
2832
'--listen_ip={{ cadvisor_listen_ip }}' \
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{{ ansible_managed | comment }}
2+
{% for user, password in cadvisor_basic_auth_users.items() %}
3+
{{ user }}:{{ password | string | password_hash('bcrypt', ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' | shuffle(seed=inventory_hostname) | join)[:22], rounds=9) }}
4+
{% endfor %}

0 commit comments

Comments
 (0)