docker深刻2-UI之portainer經過API來更新service的ACL
2018/11/5git
閱讀文檔github
resource_controls Manage access control on Docker resources POST /resource_controls Create a new resource control PUT /resource_controls/{id} Update a resource control DELETE /resource_controls/{id} Remove a resource control
本例在 mac 下操做,使用 httpie 來發送請求brew install httpie
docker
經過 jq 來格式化數據brew install jq
json
/tmp/httpie
portainer升級至1.19.2後,有比較特別的變化:api
1.19.2bash
Breaking changesapp
This version changes the default ownership for externally created resources from Public to Administrator restricted (#960, #2137). The migration process will automatically migrate any existing resource declared as Public to Administrators only.ide
儘管以前爲 service 設置過 ACL ,但在升級後發現仍是所有重置爲 Administrators 權限rest
下面是具體示範:code
http POST http://your-portainer-addr/api/auth Username="admin" Password="portainer" { "jwt": "xxJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTUzOTYxNzcwNX0.ifadEaqEo7LNWPuPBl8zQMZqeFvxfVPgAD6asNdMQYY" }
http GET http://your-portainer-addr/api/teams \ "Authorization: Bearer xxJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTUzOTYxNzcwNX0.ifadEaqEo7LNWPuPBl8zQMZqeFvxfVPgAD6asNdMQYY" [ { "Id": 1, "Name": "dev" }, { "Id": 2, "Name": "qa" }, { "Id": 3, "Name": "ops" } ]
mkdir /tmp/httpie && cd /tmp/httpie
http POST http://your-portainer-addr/api/resource_controls \ "Authorization: Bearer xxJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTUzOTYxNzcwNX0.ifadEaqEo7LNWPuPBl8zQMZqeFvxfVPgAD6asNdMQYY" \ @/tmp/httpie/1.json
http GET http://your-portainer-addr/api/endpoints/5/docker/services\?filters\='{"name":["dev-app1"]}' \ "Authorization: Bearer xxJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTUzOTYxNzcwNX0.ifadEaqEo7LNWPuPBl8zQMZqeFvxfVPgAD6asNdMQYY" |jq '.[] | {name: .Spec.Name, id: .ID, teams: .Portainer.ResourceControl.TeamAccesses[0].TeamId}'
http GET http://your-portainer-addr/api/endpoints/5/docker/services\?filters\='{"name":["dev-app1"]}' \ "Authorization: Bearer xxJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTUzOTYxNzcwNX0.ifadEaqEo7LNWPuPBl8zQMZqeFvxfVPgAD6asNdMQYY" |jq '.[].ID' > .id
s1='{"Type": "service", "Public": false, "ResourceID": "' s2='", "Users": [], "Teams": [2]}' for ID in `cat .id |sed 's/"//g'`;do echo $ID echo ${s1}${ID}${s2} >acl-create.json http POST http://your-portainer-addr/api/resource_controls \ "Authorization: Bearer xxJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTUzOTYxNzcwNX0.ifadEaqEo7LNWPuPBl8zQMZqeFvxfVPgAD6asNdMQYY" \ @/tmp/httpie/acl-create.json echo '---------' done
s3='{"Public": false, "Users":[], "Teams":[2]}' for ID in `cat .id |sed 's/"//g'`;do echo ${ID} echo ${s3} >acl-update.json echo '[+] Portainer.ResourceControl.ID:' portainer_svc_rc_id=`http GET "http://your-portainer-addr/api/endpoints/5/docker/services/${ID}" \ "Authorization: Bearer xxJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTUzOTYxNzcwNX0.ifadEaqEo7LNWPuPBl8zQMZqeFvxfVPgAD6asNdMQYY" |jq '.Portainer.ResourceControl.Id'` echo ${portainer_svc_rc_id} echo '[+] Update:' http PUT "http://your-portainer-addr/api/resource_controls/${portainer_svc_rc_id}" \ "Authorization: Bearer xxJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTUzOTYxNzcwNX0.ifadEaqEo7LNWPuPBl8zQMZqeFvxfVPgAD6asNdMQYY" \ @/tmp/httpie/acl-update.json echo '---------' done
一、Portainer-API-docs
https://app.swaggerhub.com/apis-docs/deviantony/Portainer/1.19.2#/
二、issuecomment
https://github.com/portainer/portainer/pull/2137#issuecomment-426421950
三、releases-tag-1.19.2
https://github.com/portainer/portainer/releases/tag/1.19.2