|
| 1 | +// Copyright 2015 CoreOS, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package client |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "encoding/json" |
| 20 | + "net/http" |
| 21 | + "net/url" |
| 22 | + |
| 23 | + "golang.org/x/net/context" |
| 24 | +) |
| 25 | + |
| 26 | +type Role struct { |
| 27 | + Role string `json:"role"` |
| 28 | + Permissions Permissions `json:"permissions"` |
| 29 | + Grant *Permissions `json:"grant,omitempty"` |
| 30 | + Revoke *Permissions `json:"revoke,omitempty"` |
| 31 | +} |
| 32 | + |
| 33 | +type Permissions struct { |
| 34 | + KV rwPermission `json:"kv"` |
| 35 | +} |
| 36 | + |
| 37 | +type rwPermission struct { |
| 38 | + Read []string `json:"read"` |
| 39 | + Write []string `json:"write"` |
| 40 | +} |
| 41 | + |
| 42 | +type PermissionType int |
| 43 | + |
| 44 | +const ( |
| 45 | + ReadPermission PermissionType = iota |
| 46 | + WritePermission |
| 47 | + ReadWritePermission |
| 48 | +) |
| 49 | + |
| 50 | +// NewAuthRoleAPI constructs a new AuthRoleAPI that uses HTTP to |
| 51 | +// interact with etcd's role creation and modification features. |
| 52 | +func NewAuthRoleAPI(c Client) AuthRoleAPI { |
| 53 | + return &httpAuthRoleAPI{ |
| 54 | + client: c, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +type AuthRoleAPI interface { |
| 59 | + // Add a role. |
| 60 | + AddRole(ctx context.Context, role string) error |
| 61 | + |
| 62 | + // Remove a role. |
| 63 | + RemoveRole(ctx context.Context, role string) error |
| 64 | + |
| 65 | + // Get role details. |
| 66 | + GetRole(ctx context.Context, role string) (*Role, error) |
| 67 | + |
| 68 | + // Grant a role some permission prefixes for the KV store. |
| 69 | + GrantRoleKV(ctx context.Context, role string, prefixes []string, permType PermissionType) (*Role, error) |
| 70 | + |
| 71 | + // Revoke some some permission prefixes for a role on the KV store. |
| 72 | + RevokeRoleKV(ctx context.Context, role string, prefixes []string, permType PermissionType) (*Role, error) |
| 73 | + |
| 74 | + // List roles. |
| 75 | + ListRoles(ctx context.Context) ([]string, error) |
| 76 | +} |
| 77 | + |
| 78 | +type httpAuthRoleAPI struct { |
| 79 | + client httpClient |
| 80 | +} |
| 81 | + |
| 82 | +type authRoleAPIAction struct { |
| 83 | + verb string |
| 84 | + name string |
| 85 | + role *Role |
| 86 | +} |
| 87 | + |
| 88 | +type authRoleAPIList struct{} |
| 89 | + |
| 90 | +func (list *authRoleAPIList) HTTPRequest(ep url.URL) *http.Request { |
| 91 | + u := v2AuthURL(ep, "roles", "") |
| 92 | + req, _ := http.NewRequest("GET", u.String(), nil) |
| 93 | + req.Header.Set("Content-Type", "application/json") |
| 94 | + return req |
| 95 | +} |
| 96 | + |
| 97 | +func (l *authRoleAPIAction) HTTPRequest(ep url.URL) *http.Request { |
| 98 | + u := v2AuthURL(ep, "roles", l.name) |
| 99 | + if l.role == nil { |
| 100 | + req, _ := http.NewRequest(l.verb, u.String(), nil) |
| 101 | + return req |
| 102 | + } |
| 103 | + b, err := json.Marshal(l.role) |
| 104 | + if err != nil { |
| 105 | + panic(err) |
| 106 | + } |
| 107 | + body := bytes.NewReader(b) |
| 108 | + req, _ := http.NewRequest(l.verb, u.String(), body) |
| 109 | + req.Header.Set("Content-Type", "application/json") |
| 110 | + return req |
| 111 | +} |
| 112 | + |
| 113 | +func (r *httpAuthRoleAPI) ListRoles(ctx context.Context) ([]string, error) { |
| 114 | + resp, body, err := r.client.Do(ctx, &authRoleAPIList{}) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + if err := assertStatusCode(resp.StatusCode, http.StatusOK); err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + var userList struct { |
| 122 | + Roles []string `json:"roles"` |
| 123 | + } |
| 124 | + err = json.Unmarshal(body, &userList) |
| 125 | + if err != nil { |
| 126 | + return nil, err |
| 127 | + } |
| 128 | + return userList.Roles, nil |
| 129 | +} |
| 130 | + |
| 131 | +func (r *httpAuthRoleAPI) AddRole(ctx context.Context, rolename string) error { |
| 132 | + role := &Role{ |
| 133 | + Role: rolename, |
| 134 | + } |
| 135 | + return r.addRemoveRole(ctx, &authRoleAPIAction{ |
| 136 | + verb: "PUT", |
| 137 | + name: rolename, |
| 138 | + role: role, |
| 139 | + }) |
| 140 | +} |
| 141 | + |
| 142 | +func (r *httpAuthRoleAPI) RemoveRole(ctx context.Context, rolename string) error { |
| 143 | + return r.addRemoveRole(ctx, &authRoleAPIAction{ |
| 144 | + verb: "DELETE", |
| 145 | + name: rolename, |
| 146 | + }) |
| 147 | +} |
| 148 | + |
| 149 | +func (r *httpAuthRoleAPI) addRemoveRole(ctx context.Context, req *authRoleAPIAction) error { |
| 150 | + resp, body, err := r.client.Do(ctx, req) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + if err := assertStatusCode(resp.StatusCode, http.StatusOK, http.StatusCreated); err != nil { |
| 155 | + var sec authError |
| 156 | + err := json.Unmarshal(body, &sec) |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + return sec |
| 161 | + } |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +func (r *httpAuthRoleAPI) GetRole(ctx context.Context, rolename string) (*Role, error) { |
| 166 | + return r.modRole(ctx, &authRoleAPIAction{ |
| 167 | + verb: "GET", |
| 168 | + name: rolename, |
| 169 | + }) |
| 170 | +} |
| 171 | + |
| 172 | +func buildRWPermission(prefixes []string, permType PermissionType) rwPermission { |
| 173 | + var out rwPermission |
| 174 | + switch permType { |
| 175 | + case ReadPermission: |
| 176 | + out.Read = prefixes |
| 177 | + case WritePermission: |
| 178 | + out.Write = prefixes |
| 179 | + case ReadWritePermission: |
| 180 | + out.Read = prefixes |
| 181 | + out.Write = prefixes |
| 182 | + } |
| 183 | + return out |
| 184 | +} |
| 185 | + |
| 186 | +func (r *httpAuthRoleAPI) GrantRoleKV(ctx context.Context, rolename string, prefixes []string, permType PermissionType) (*Role, error) { |
| 187 | + rwp := buildRWPermission(prefixes, permType) |
| 188 | + role := &Role{ |
| 189 | + Role: rolename, |
| 190 | + Grant: &Permissions{ |
| 191 | + KV: rwp, |
| 192 | + }, |
| 193 | + } |
| 194 | + return r.modRole(ctx, &authRoleAPIAction{ |
| 195 | + verb: "PUT", |
| 196 | + name: rolename, |
| 197 | + role: role, |
| 198 | + }) |
| 199 | +} |
| 200 | + |
| 201 | +func (r *httpAuthRoleAPI) RevokeRoleKV(ctx context.Context, rolename string, prefixes []string, permType PermissionType) (*Role, error) { |
| 202 | + rwp := buildRWPermission(prefixes, permType) |
| 203 | + role := &Role{ |
| 204 | + Role: rolename, |
| 205 | + Revoke: &Permissions{ |
| 206 | + KV: rwp, |
| 207 | + }, |
| 208 | + } |
| 209 | + return r.modRole(ctx, &authRoleAPIAction{ |
| 210 | + verb: "PUT", |
| 211 | + name: rolename, |
| 212 | + role: role, |
| 213 | + }) |
| 214 | +} |
| 215 | + |
| 216 | +func (r *httpAuthRoleAPI) modRole(ctx context.Context, req *authRoleAPIAction) (*Role, error) { |
| 217 | + resp, body, err := r.client.Do(ctx, req) |
| 218 | + if err != nil { |
| 219 | + return nil, err |
| 220 | + } |
| 221 | + if err := assertStatusCode(resp.StatusCode, http.StatusOK); err != nil { |
| 222 | + var sec authError |
| 223 | + err := json.Unmarshal(body, &sec) |
| 224 | + if err != nil { |
| 225 | + return nil, err |
| 226 | + } |
| 227 | + return nil, sec |
| 228 | + } |
| 229 | + var role Role |
| 230 | + err = json.Unmarshal(body, &role) |
| 231 | + if err != nil { |
| 232 | + return nil, err |
| 233 | + } |
| 234 | + return &role, nil |
| 235 | +} |
0 commit comments