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() % }
66influxdb_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() % }
2222get_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 % }
6565check_{{ 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 % }
8989set_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,46 @@ 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+ ' orgID' : orgID,
135+ ' type' : " buckets"
136+ }]
137+ }]
138+ } % }
139+
140+ check_auth_user_{{ name }}_to_{{ bucket }}:
141+ http.query:
142+ - name: ' {{ base_url }} /private/legacy/authorizations?token={{ token }} '
143+ - status: 200
144+ - method: GET
145+ - match: ' "{{ token }} "'
146+ - match_type: string
147+ - header_dict:
148+ Authorization: Token {{ influxdb[' user' ][' admin' ][' token' ] }}
149+
150+ auth_user_{{ name }}_to_{{ bucket }}:
151+ http.query:
152+ - name: ' {{ base_url }} /private/legacy/authorizations'
153+ - status: 201
154+ - method: POST
155+ - data: ' {{ auth_data | tojson }} '
156+ - header_dict:
157+ Authorization: Token {{ influxdb[' user' ][' admin' ][' token' ] }}
158+ - onfail:
159+ - http: check_auth_user_{{ name }}_to_{{ bucket }}
160+
123161{%- endfor % }
124- {% endif % }
162+ {%- endif % }
125163
126- {% endfor % }
127- {% endif % }
164+ {%- endfor % }
165+ {%- endif % }
0 commit comments