|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one or more contributor |
| 2 | +# license agreements; and to You under the Apache License, Version 2.0. |
| 3 | +--- |
| 4 | +# Grant the specified users permissions to the specified database. |
| 5 | +# dbName - name of the database |
| 6 | +# admins - all users with admin access |
| 7 | +# readers - all users that have read access on the database |
| 8 | +# writers - all users that have write access on the database |
| 9 | + |
| 10 | +# If a component uses admin credentials, the admin user will not be added to the list (as it already has all access rights). |
| 11 | +- set_fact: |
| 12 | + readerList: "{{ readers | default([]) | difference([db.credentials.admin.user]) }}" |
| 13 | + writerList: "{{ writers | default([]) | difference([db.credentials.admin.user]) }}" |
| 14 | + adminList: "{{ admins | default([]) | difference([db.credentials.admin.user]) }}" |
| 15 | + |
| 16 | +# http://docs.couchdb.org/en/2.0.0/api/database/security.html |
| 17 | +- name: grant permissions for CouchDB |
| 18 | + uri: |
| 19 | + url: "{{ db.protocol }}://{{ db.host }}:{{ db.port }}/{{ dbName }}/_security" |
| 20 | + method: PUT |
| 21 | + status_code: 200 |
| 22 | + body_format: json |
| 23 | + body: | |
| 24 | + { |
| 25 | + "admins": { |
| 26 | + "names": [ {{ adminList | join('", "') }} ], |
| 27 | + "roles": [] |
| 28 | + }, |
| 29 | + "members": { |
| 30 | + "names": [ "{{ readerList | union(writerList) | join('", "') }}" ], |
| 31 | + "roles": [] |
| 32 | + } |
| 33 | + } |
| 34 | + user: "{{ db.credentials.admin.user }}" |
| 35 | + password: "{{ db.credentials.admin.pass }}" |
| 36 | + force_basic_auth: yes |
| 37 | + when: db.provider == 'CouchDB' |
| 38 | + |
| 39 | +# https://console.bluemix.net/docs/services/Cloudant/api/authorization.html#authorization |
| 40 | +- name: grant permissions for Cloudant |
| 41 | + uri: |
| 42 | + url: "{{ db.protocol }}://{{ db.host }}:{{ db.port }}/{{ dbName }}/_security" |
| 43 | + method: PUT |
| 44 | + status_code: 200 |
| 45 | + body_format: json |
| 46 | + body: | |
| 47 | + { |
| 48 | + "cloudant": { |
| 49 | + {% for item in readerList | union(writerList) | union(adminList) %}"{{ item }}": [ {% if item in readerList %}"_reader"{% if item in writerList %}, "_writer"{% if item in adminList %}, "_admin"{% endif %}{% endif %}{% endif %} ], {% endfor %} |
| 50 | + } |
| 51 | + } |
| 52 | + user: "{{ db.credentials.admin.user }}" |
| 53 | + password: "{{ db.credentials.admin.pass }}" |
| 54 | + force_basic_auth: yes |
| 55 | + when: db.provider == 'Cloudant' |
0 commit comments