|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package keystone |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "errors" |
| 22 | + "io/ioutil" |
| 23 | + |
| 24 | + "github.com/golang/glog" |
| 25 | + "github.com/gophercloud/gophercloud" |
| 26 | + |
| 27 | + "k8s.io/apiserver/pkg/authentication/user" |
| 28 | +) |
| 29 | + |
| 30 | +// KeystoneAuthenticator contacts openstack keystone to validate user's token passed in the request. |
| 31 | +// The keystone endpoint is passed during apiserver startup |
| 32 | +type KeystoneAuthenticator struct { |
| 33 | + authURL string |
| 34 | + client *gophercloud.ServiceClient |
| 35 | +} |
| 36 | + |
| 37 | +// AuthenticatePassword checks the token via Keystone call |
| 38 | +func (keystoneAuthenticator *KeystoneAuthenticator) AuthenticateToken(token string) (user.Info, bool, error) { |
| 39 | + |
| 40 | + // We can use the Keystone GET /v3/auth/tokens API to validate the token |
| 41 | + // and get information about the user as well |
| 42 | + // http://git.openstack.org/cgit/openstack/keystone/tree/api-ref/source/v3/authenticate-v3.inc#n437 |
| 43 | + // https://developer.openstack.org/api-ref/identity/v3/?expanded=validate-and-show-information-for-token-detail |
| 44 | + request_opts := gophercloud.RequestOpts{ |
| 45 | + MoreHeaders: map[string]string{ |
| 46 | + "X-Auth-Token": token, |
| 47 | + "X-Subject-Token": token, |
| 48 | + }, |
| 49 | + } |
| 50 | + url := keystoneAuthenticator.client.ServiceURL("auth", "tokens") |
| 51 | + response, err := keystoneAuthenticator.client.Request("GET", url, &request_opts) |
| 52 | + if err != nil { |
| 53 | + glog.V(4).Info("Failed: bad response from API call: %v", err) |
| 54 | + return nil, false, errors.New("Failed to authenticate") |
| 55 | + } |
| 56 | + |
| 57 | + defer response.Body.Close() |
| 58 | + bodyBytes, err := ioutil.ReadAll(response.Body) |
| 59 | + if err != nil { |
| 60 | + glog.V(4).Infof("Cannot get HTTP response body from keystone token validate: %v", err) |
| 61 | + return nil, false, errors.New("Failed to authenticate") |
| 62 | + } |
| 63 | + |
| 64 | + obj := struct { |
| 65 | + Token struct { |
| 66 | + User struct { |
| 67 | + Id string `json:"id"` |
| 68 | + Name string `json:"name"` |
| 69 | + } `json:"user"` |
| 70 | + Project struct { |
| 71 | + Id string `json:"id"` |
| 72 | + Name string `json:"name"` |
| 73 | + } `json:"project"` |
| 74 | + Roles []struct { |
| 75 | + Name string `json:"name"` |
| 76 | + } `json:"roles"` |
| 77 | + } `json:"token"` |
| 78 | + }{} |
| 79 | + |
| 80 | + err = json.Unmarshal(bodyBytes, &obj) |
| 81 | + if err != nil { |
| 82 | + glog.V(4).Infof("Cannot unmarshal response: %v", err) |
| 83 | + return nil, false, errors.New("Failed to authenticate") |
| 84 | + } |
| 85 | + |
| 86 | + var roles []string |
| 87 | + if obj.Token.Roles != nil && len(obj.Token.Roles) > 0 { |
| 88 | + roles = make([]string, len(obj.Token.Roles)) |
| 89 | + for i := 0; i < len(obj.Token.Roles); i++ { |
| 90 | + roles[i] = obj.Token.Roles[i].Name |
| 91 | + } |
| 92 | + } else { |
| 93 | + roles = make([]string, 0) |
| 94 | + } |
| 95 | + |
| 96 | + extra := map[string][]string{ |
| 97 | + "alpha.kubernetes.io/identity/roles": roles, |
| 98 | + "alpha.kubernetes.io/identity/project/id": []string{obj.Token.Project.Id}, |
| 99 | + "alpha.kubernetes.io/identity/project/name": []string{obj.Token.Project.Name}, |
| 100 | + } |
| 101 | + |
| 102 | + authenticated_user := &user.DefaultInfo{ |
| 103 | + Name: obj.Token.User.Name, |
| 104 | + UID: obj.Token.User.Id, |
| 105 | + Groups: []string{obj.Token.Project.Id}, |
| 106 | + Extra: extra, |
| 107 | + } |
| 108 | + |
| 109 | + return authenticated_user, true, nil |
| 110 | +} |
0 commit comments