Skip to content

Commit 14f1bad

Browse files
committed
feat: create base tokens and group permissions
1 parent 39b5ea8 commit 14f1bad

File tree

1 file changed

+73
-40
lines changed

1 file changed

+73
-40
lines changed

influxdb/users.sls

Lines changed: 73 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ create_user_{{ name }}:
3838
- onfail:
3939
- http: get_user_{{ name }}
4040
41-
{%- set orgID = salt['cmd.shell']("curl -s -f -H'Authorization: Token " ~ influxdb['user']['admin']['token'] ~ "' '" ~ base_url ~ "/api/v2/orgs' | jq -r '.orgs[0].id'") %}
42-
{%- set id = salt['cmd.shell']("curl -s -f -H'Authorization: Token " ~ influxdb['user']['admin']['token'] ~ "' '" ~ base_url ~ "/api/v2/users?name=" ~ name ~ "' | jq -r '.users[0].id'") %}
41+
{%- set orgID = salt['cmd.shell']("curl -s -f -H'Authorization: Token " ~ influxdb['user']['admin']['token'] ~ "' '" ~ base_url ~ "/api/v2/orgs' | jq -r '.orgs[0].id'") %} # noqa: 204
42+
{%- set id = salt['cmd.shell']("curl -s -f -H'Authorization: Token " ~ influxdb['user']['admin']['token'] ~ "' '" ~ base_url ~ "/api/v2/users?name=" ~ name ~ "' | jq -r '.users[0].id'") %} # noqa: 204
4343
{%- if "admin" in config and config["admin"] == True %}
4444
check_{{ name }}_admin_in_org:
4545
http.query:
@@ -97,8 +97,9 @@ set_password_{{ name }}:
9797
{%- endif %}
9898
9999
{%- if "grants" in config %}
100+
{%- set permissions = [] %}
100101
{%- for bucket,access in config['grants'].items() %}
101-
{%- 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'") %}
102+
{%- 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'") %} # noqa: 204
102103
103104
check_grant_user_{{ name }}_to_{{ bucket }}:
104105
http.query:
@@ -121,61 +122,69 @@ grant_user_{{ name }}_to_{{ bucket }}:
121122
- onfail:
122123
- http: check_grant_user_{{ name }}_to_{{ bucket }}
123124
124-
{%- set token = '-'.join([name, access, bucket]) %}
125-
{%- set all_permissions = [{
126-
'action': 'read',
127-
'resource': {
128-
'id': bucketID,
129-
'orgID': orgID,
130-
'type': "buckets"
131-
}
132-
},{
133-
'action': 'write',
134-
'resource': {
135-
'id': bucketID,
136-
'orgID': orgID,
137-
'type': "buckets"
138-
}
139-
}] %}
140-
{%- set base_permissions = [{
141-
'action': access,
142-
'resource': {
143-
'id': bucketID,
144-
'orgID': orgID,
145-
'type': "buckets"
146-
}
147-
}] %}
148-
{%- set auth_data = {
149-
'token': token,
150-
'description': 'Grant ' ~ name ~ ' ' ~ access ~ ' access to bucket ' ~ bucket,
125+
{%- if access == 'all' %}
126+
{%- set _ = permissions.append({
127+
'action': 'read',
128+
'resource': {
129+
'id': bucketID,
130+
'orgID': orgID,
131+
'type': "buckets"
132+
}
133+
})
134+
%}
135+
{%- set _ = permissions.append({
136+
'action': 'write',
137+
'resource': {
138+
'id': bucketID,
139+
'orgID': orgID,
140+
'type': "buckets"
141+
}
142+
})
143+
%}
144+
{%- else %}
145+
{%- set _ = permissions.append({
146+
'action': access
147+
'resource': {
148+
'id': bucketID,
149+
'orgID': orgID,
150+
'type': "buckets"
151+
}
152+
})
153+
%}
154+
{%- endif %}
155+
{%- endfor %}
156+
157+
{%- set legacy_auth_data = {
158+
'token': name ~ '-legacy',
159+
'description': 'Grant ' ~ name ~ ' legacy access to buckets',
151160
'orgID': orgID,
152161
'userID': id,
153-
'permissions': all_permissions if access == 'all' else base_permissions
162+
'permissions': permissions
154163
} %}
155164
156-
check_auth_user_{{ name }}_to_{{ bucket }}:
165+
check_auth_user_{{ name }}_legacy:
157166
http.query:
158-
- name: '{{ base_url }}/private/legacy/authorizations?token={{ token }}'
167+
- name: '{{ base_url }}/private/legacy/authorizations?token={{ name }}-legacy'
159168
- status: 200
160169
- method: GET
161-
- match: '"{{ token }}"'
170+
- match: '"{{ legacy_auth_data.token }}"'
162171
- match_type: string
163172
- header_dict:
164173
Authorization: Token {{ influxdb['user']['admin']['token'] }}
165174
166-
auth_user_{{ name }}_to_{{ bucket }}:
175+
auth_user_{{ name }}_legacy:
167176
http.query:
168177
- name: '{{ base_url }}/private/legacy/authorizations'
169178
- status: 201
170179
- method: POST
171-
- data: '{{ auth_data | tojson }}'
180+
- data: '{{ legacy_auth_data | tojson }}'
172181
- header_dict:
173182
Authorization: Token {{ influxdb['user']['admin']['token'] }}
174183
- onfail:
175-
- http: check_auth_user_{{ name }}_to_{{ bucket }}
184+
- http: check_auth_user_{{ name }}_legacy
176185
177-
{%- set authID = salt['cmd.shell']("curl -s -f -H'Authorization: Token " ~ influxdb['user']['admin']['token'] ~ "' '" ~ base_url ~ "/private/legacy/authorizations?token=" ~ token ~ "' | jq -r '.authorizations[0].id'") %}
178-
password_auth_user_{{ name }}_to_{{ bucket }}:
186+
{%- set authID = salt['cmd.shell']("curl -s -f -H'Authorization: Token " ~ influxdb['user']['admin']['token'] ~ "' '" ~ base_url ~ "/private/legacy/authorizations?token=" ~ token ~ "' | jq -r '.authorizations[0].id'") %} # noqa: 204
187+
password_auth_user_{{ name }}_legacy:
179188
http.query:
180189
- name: '{{ base_url }}/private/legacy/authorizations/{{ authID }}/password'
181190
- status: 204
@@ -184,7 +193,31 @@ password_auth_user_{{ name }}_to_{{ bucket }}:
184193
- header_dict:
185194
Authorization: Token {{ influxdb['user']['admin']['token'] }}
186195
187-
{%- endfor %}
196+
{%- set auth_data = {
197+
'token': name,
198+
'description': 'Grant ' ~ name ~ ' access to buckets',
199+
'orgID': orgID,
200+
'userID': id,
201+
'permissions': permissions
202+
} %}
203+
check_auth_user_{{ name }}_v2:
204+
http.query:
205+
- name: '{{ base_url }}/api/v2/authorizations?user={{ name }}'
206+
- status: 200
207+
- method: GET
208+
- header_dict:
209+
Authorization: Token {{ influxdb['user']['admin']['token'] }}
210+
211+
auth_user_{{ name }}_v2:
212+
http.query:
213+
- name: '{{ base_url }}/api/v2/authorizations'
214+
- status: 201
215+
- method: POST
216+
- data: '{{ auth_data | tojson }}'
217+
- header_dict:
218+
Authorization: Token {{ influxdb['user']['admin']['token'] }}
219+
- onfail:
220+
- http: check_auth_user_{{ name }}_v2
188221
{%- endif %}
189222
190223
{%- endfor %}

0 commit comments

Comments
 (0)