Skip to content

Commit c1aec4b

Browse files
committed
feat: add v1 auths
1 parent 87f32f7 commit c1aec4b

File tree

1 file changed

+59
-19
lines changed

1 file changed

+59
-19
lines changed

influxdb/users.sls

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
{% from "influxdb/defaults.yaml.jinja2" import rawmap with context %}
1+
{%- from "influxdb/defaults.yaml.jinja2" import rawmap with context %}
22
{%- set influxdb = salt['grains.filter_by'](rawmap, grain='os_family', merge=salt['pillar.get']('influxdb')) %}
33
4-
{% if "user" in influxdb and "remote" not in influxdb %}
5-
{% for name,config in influxdb["user"].items() %}
4+
{%- if "user" in influxdb and "remote" not in influxdb %}
5+
{%- for name,config in influxdb["user"].items() %}
66
influxdb_user_{{ name }}:
77
influxdb_user.present:
88
- name: {{ name }}
99
- passwd: {{ config["password"] }}
10-
{% if "admin" in config %}
10+
{%- if "admin" in config %}
1111
- admin: {{ config["admin"] }}
12-
{% endif %}
13-
{% if "grants" in config %}
12+
{%- endif %}
13+
{%- if "grants" in config %}
1414
- grants: {{ config["grants"] }}
15-
{% endif %}
16-
{% endfor %}
17-
{% endif %}
15+
{%- endif %}
16+
{%- endfor %}
17+
{%- endif %}
1818
19-
{% if "user" in influxdb and "remote" in influxdb %}
19+
{%- if "user" in influxdb and "remote" in influxdb %}
2020
{%- set base_url = "https://" ~ influxdb['remote']['host'] ~ ":" ~ influxdb['remote']['port'] %}
21-
{% for name,config in influxdb["user"].items() %}
21+
{%- for name,config in influxdb["user"].items() %}
2222
get_user_{{ name }}:
2323
http.query:
2424
- name: '{{ base_url }}/api/v2/users/?name={{ name }}'
@@ -61,7 +61,7 @@ make_{{ name }}_admin_in_org:
6161
Authorization: Token {{ influxdb['user']['admin']['token'] }}
6262
- onfail:
6363
- http: check_{{ name }}_admin_in_org
64-
{% else %}
64+
{%- else %}
6565
check_{{ name }}_member_in_org:
6666
http.query:
6767
- name: '{{ base_url }}/api/v2/orgs/{{ orgID }}/members'
@@ -82,10 +82,10 @@ make_{{ name }}_member_in_org:
8282
Authorization: Token {{ influxdb['user']['admin']['token'] }}
8383
- onfail:
8484
- http: check_{{ name }}_member_in_org
85-
{% endif %}
85+
{%- endif %}
8686
8787
88-
{% if "password" in config %}
88+
{%- if "password" in config %}
8989
set_password_{{ name }}:
9090
http.query:
9191
- name: '{{ base_url }}/api/v2/users/{{ id }}/password'
@@ -94,9 +94,9 @@ set_password_{{ name }}:
9494
- data: '{"password": "{{ config["password"] }}"}'
9595
- header_dict:
9696
Authorization: Token {{ influxdb['user']['admin']['token'] }}
97-
{% endif %}
97+
{%- endif %}
9898
99-
{% if "grants" in config %}
99+
{%- if "grants" in config %}
100100
{%- for bucket,access in config['grants'].items() %}
101101
{%- set bucketID = salt['cmd.shell']("curl -s -f -H'Authorization: Token " ~ influxdb['user']['admin']['token'] ~ "' '" ~ base_url ~ "/api/v2/buckets?name=" ~ bucket ~ "' | jq -r '.buckets[0].id'") %}
102102
@@ -120,8 +120,48 @@ grant_user_{{ name }}_to_{{ bucket }}:
120120
Authorization: Token {{ influxdb['user']['admin']['token'] }}
121121
- onfail:
122122
- http: check_grant_user_{{ name }}_to_{{ bucket }}
123+
124+
{%- set token = '-'.join([name, access, bucket]) %}
125+
{%- set auth_data = {
126+
'token': token,
127+
'description': 'Grant ' ~ name ~ ' ' ~ access ~ ' access to bucket ' ~ bucket,
128+
'orgID': orgID,
129+
'userID': id,
130+
'permissions': [{
131+
'action': 'write' if access != 'read' else 'read',
132+
'resource': [{
133+
'id': bucketID,
134+
'name': bucket,
135+
'orgID': orgID,
136+
'type': "buckets"
137+
}]
138+
}]
139+
} %}
140+
141+
check_auth_user_{{ name }}_to_{{ bucket }}:
142+
http.query:
143+
- name: '{{ base_url }}/private/legacy/authorizations?token={{ token }}'
144+
- status: 200
145+
- method: GET
146+
- match: '"{{ token }}"'
147+
- match_type: string
148+
- header_dict:
149+
Authorization: Token {{ influxdb['user']['admin']['token'] }}
150+
151+
auth_user_{{ name }}_to_{{ bucket }}:
152+
http.query:
153+
- name: '{{ base_url }}/private/legacy/authorizations'
154+
- status: 201
155+
- method: POST
156+
- data: |
157+
'{{ auth_data | tojson }}'
158+
- header_dict:
159+
Authorization: Token {{ influxdb['user']['admin']['token'] }}
160+
- onfail:
161+
- http: check_auth_user_{{ name }}_to_{{ bucket }}
162+
123163
{%- endfor %}
124-
{% endif %}
164+
{%- endif %}
125165
126-
{% endfor %}
127-
{% endif %}
166+
{%- endfor %}
167+
{%- endif %}

0 commit comments

Comments
 (0)