2016-07-26 01:30:28 +05:30
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-10-14 05:20:20 +05:30
|
|
|
"net/http"
|
2016-10-28 04:25:23 +05:30
|
|
|
"strings"
|
2016-08-02 11:23:12 +05:30
|
|
|
"time"
|
2016-07-26 01:30:28 +05:30
|
|
|
|
2016-10-14 05:20:20 +05:30
|
|
|
"golang.org/x/net/context"
|
2016-07-26 01:30:28 +05:30
|
|
|
|
2016-11-23 05:05:46 +05:30
|
|
|
"github.com/Sirupsen/logrus"
|
2016-08-11 11:01:42 +05:30
|
|
|
"github.com/coreos/dex/storage"
|
|
|
|
"github.com/coreos/dex/storage/kubernetes/k8sapi"
|
2016-07-26 01:30:28 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
kindAuthCode = "AuthCode"
|
|
|
|
kindAuthRequest = "AuthRequest"
|
|
|
|
kindClient = "OAuth2Client"
|
|
|
|
kindRefreshToken = "RefreshToken"
|
|
|
|
kindKeys = "SigningKey"
|
2016-10-06 04:34:48 +05:30
|
|
|
kindPassword = "Password"
|
2016-07-26 01:30:28 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
resourceAuthCode = "authcodes"
|
|
|
|
resourceAuthRequest = "authrequests"
|
|
|
|
resourceClient = "oauth2clients"
|
|
|
|
resourceRefreshToken = "refreshtokens"
|
|
|
|
resourceKeys = "signingkeies" // Kubernetes attempts to pluralize.
|
2016-10-06 04:34:48 +05:30
|
|
|
resourcePassword = "passwords"
|
2016-07-26 01:30:28 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Config values for the Kubernetes storage type.
|
|
|
|
type Config struct {
|
2016-11-04 03:02:23 +05:30
|
|
|
InCluster bool `json:"inCluster"`
|
|
|
|
KubeConfigFile string `json:"kubeConfigFile"`
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Open returns a storage using Kubernetes third party resource.
|
2016-11-23 05:05:46 +05:30
|
|
|
func (c *Config) Open(logger logrus.FieldLogger) (storage.Storage, error) {
|
|
|
|
cli, err := c.open(logger)
|
2016-08-02 11:23:12 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cli, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// open returns a client with no garbage collection.
|
2016-11-23 05:05:46 +05:30
|
|
|
func (c *Config) open(logger logrus.FieldLogger) (*client, error) {
|
2016-10-23 20:28:28 +05:30
|
|
|
if c.InCluster && (c.KubeConfigFile != "") {
|
|
|
|
return nil, errors.New("cannot specify both 'inCluster' and 'kubeConfigFile'")
|
|
|
|
}
|
|
|
|
if !c.InCluster && (c.KubeConfigFile == "") {
|
|
|
|
return nil, errors.New("must specify either 'inCluster' or 'kubeConfigFile'")
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
cluster k8sapi.Cluster
|
|
|
|
user k8sapi.AuthInfo
|
|
|
|
namespace string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if c.InCluster {
|
|
|
|
cluster, user, namespace, err = inClusterConfig()
|
|
|
|
} else {
|
2016-10-23 20:28:28 +05:30
|
|
|
cluster, user, namespace, err = loadKubeConfig(c.KubeConfigFile)
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-11-23 05:05:46 +05:30
|
|
|
cli, err := newClient(cluster, user, namespace, logger)
|
2016-10-14 05:20:20 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("create client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
2016-10-27 01:38:03 +05:30
|
|
|
// Try to synchronously create the third party resources once. This doesn't mean
|
|
|
|
// they'll immediately be available, but ensures that the client will actually try
|
|
|
|
// once.
|
|
|
|
if err := cli.createThirdPartyResources(); err != nil {
|
2016-12-13 04:24:01 +05:30
|
|
|
logger.Errorf("failed creating third party resources: %v", err)
|
2016-10-27 01:38:03 +05:30
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
if err := cli.createThirdPartyResources(); err != nil {
|
2016-12-13 04:24:01 +05:30
|
|
|
logger.Errorf("failed creating third party resources: %v", err)
|
2016-10-27 01:38:03 +05:30
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-time.After(30 * time.Second):
|
|
|
|
}
|
2016-10-14 05:20:20 +05:30
|
|
|
}
|
2016-10-27 01:38:03 +05:30
|
|
|
}()
|
|
|
|
}
|
2016-10-14 05:20:20 +05:30
|
|
|
|
|
|
|
// If the client is closed, stop trying to create third party resources.
|
|
|
|
cli.cancel = cancel
|
|
|
|
return cli, nil
|
|
|
|
}
|
|
|
|
|
2016-10-27 01:38:03 +05:30
|
|
|
// createThirdPartyResources attempts to create the third party resources dex
|
|
|
|
// requires or identifies that they're already enabled.
|
|
|
|
//
|
|
|
|
// Creating a third party resource does not mean that they'll be immediately available.
|
|
|
|
//
|
|
|
|
// TODO(ericchiang): Provide an option to wait for the third party resources
|
|
|
|
// to actually be available.
|
2016-10-14 05:20:20 +05:30
|
|
|
func (cli *client) createThirdPartyResources() error {
|
|
|
|
for _, r := range thirdPartyResources {
|
|
|
|
err := cli.postResource("extensions/v1beta1", "", "thirdpartyresources", r)
|
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(httpError); ok {
|
|
|
|
if e.StatusCode() == http.StatusConflict {
|
2016-12-13 04:24:01 +05:30
|
|
|
cli.logger.Errorf("third party resource already created %q", r.ObjectMeta.Name)
|
2016-10-14 05:20:20 +05:30
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2016-12-13 04:24:01 +05:30
|
|
|
cli.logger.Errorf("create third party resource %q", r.ObjectMeta.Name)
|
2016-10-14 05:20:20 +05:30
|
|
|
}
|
|
|
|
return nil
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *client) Close() error {
|
2016-10-14 05:20:20 +05:30
|
|
|
if cli.cancel != nil {
|
|
|
|
cli.cancel()
|
|
|
|
}
|
2016-07-26 01:30:28 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *client) CreateAuthRequest(a storage.AuthRequest) error {
|
|
|
|
return cli.post(resourceAuthRequest, cli.fromStorageAuthRequest(a))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *client) CreateClient(c storage.Client) error {
|
|
|
|
return cli.post(resourceClient, cli.fromStorageClient(c))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *client) CreateAuthCode(c storage.AuthCode) error {
|
|
|
|
return cli.post(resourceAuthCode, cli.fromStorageAuthCode(c))
|
|
|
|
}
|
|
|
|
|
2016-10-06 04:34:48 +05:30
|
|
|
func (cli *client) CreatePassword(p storage.Password) error {
|
|
|
|
return cli.post(resourcePassword, cli.fromStoragePassword(p))
|
|
|
|
}
|
|
|
|
|
2016-08-03 10:27:36 +05:30
|
|
|
func (cli *client) CreateRefresh(r storage.RefreshToken) error {
|
2016-12-23 05:26:09 +05:30
|
|
|
return cli.post(resourceRefreshToken, cli.fromStorageRefreshToken(r))
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *client) GetAuthRequest(id string) (storage.AuthRequest, error) {
|
|
|
|
var req AuthRequest
|
|
|
|
if err := cli.get(resourceAuthRequest, id, &req); err != nil {
|
|
|
|
return storage.AuthRequest{}, err
|
|
|
|
}
|
|
|
|
return toStorageAuthRequest(req), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *client) GetAuthCode(id string) (storage.AuthCode, error) {
|
|
|
|
var code AuthCode
|
|
|
|
if err := cli.get(resourceAuthCode, id, &code); err != nil {
|
|
|
|
return storage.AuthCode{}, err
|
|
|
|
}
|
|
|
|
return toStorageAuthCode(code), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *client) GetClient(id string) (storage.Client, error) {
|
2016-10-28 04:25:23 +05:30
|
|
|
c, err := cli.getClient(id)
|
|
|
|
if err != nil {
|
2016-07-26 01:30:28 +05:30
|
|
|
return storage.Client{}, err
|
|
|
|
}
|
|
|
|
return toStorageClient(c), nil
|
|
|
|
}
|
|
|
|
|
2016-10-28 04:25:23 +05:30
|
|
|
func (cli *client) getClient(id string) (Client, error) {
|
|
|
|
var c Client
|
|
|
|
name := cli.idToName(id)
|
|
|
|
if err := cli.get(resourceClient, name, &c); err != nil {
|
|
|
|
return Client{}, err
|
|
|
|
}
|
|
|
|
if c.ID != id {
|
|
|
|
return Client{}, fmt.Errorf("get client: ID %q mapped to client with ID %q", id, c.ID)
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2016-10-06 04:34:48 +05:30
|
|
|
func (cli *client) GetPassword(email string) (storage.Password, error) {
|
2016-10-28 04:25:23 +05:30
|
|
|
p, err := cli.getPassword(email)
|
|
|
|
if err != nil {
|
2016-10-06 04:34:48 +05:30
|
|
|
return storage.Password{}, err
|
|
|
|
}
|
|
|
|
return toStoragePassword(p), nil
|
|
|
|
}
|
|
|
|
|
2016-10-28 04:25:23 +05:30
|
|
|
func (cli *client) getPassword(email string) (Password, error) {
|
|
|
|
// TODO(ericchiang): Figure out whose job it is to lowercase emails.
|
|
|
|
email = strings.ToLower(email)
|
|
|
|
var p Password
|
|
|
|
name := cli.idToName(email)
|
|
|
|
if err := cli.get(resourcePassword, name, &p); err != nil {
|
|
|
|
return Password{}, err
|
|
|
|
}
|
|
|
|
if email != p.Email {
|
|
|
|
return Password{}, fmt.Errorf("get email: email %q mapped to password with email %q", email, p.Email)
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2016-07-26 01:30:28 +05:30
|
|
|
func (cli *client) GetKeys() (storage.Keys, error) {
|
|
|
|
var keys Keys
|
|
|
|
if err := cli.get(resourceKeys, keysName, &keys); err != nil {
|
|
|
|
return storage.Keys{}, err
|
|
|
|
}
|
|
|
|
return toStorageKeys(keys), nil
|
|
|
|
}
|
|
|
|
|
2016-08-03 10:27:36 +05:30
|
|
|
func (cli *client) GetRefresh(id string) (storage.RefreshToken, error) {
|
2016-12-23 05:26:09 +05:30
|
|
|
r, err := cli.getRefreshToken(id)
|
|
|
|
if err != nil {
|
2016-08-03 10:27:36 +05:30
|
|
|
return storage.RefreshToken{}, err
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
2016-12-23 05:26:09 +05:30
|
|
|
return toStorageRefreshToken(r), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *client) getRefreshToken(id string) (r RefreshToken, err error) {
|
|
|
|
err = cli.get(resourceRefreshToken, id, &r)
|
|
|
|
return
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *client) ListClients() ([]storage.Client, error) {
|
|
|
|
return nil, errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2016-08-03 10:27:36 +05:30
|
|
|
func (cli *client) ListRefreshTokens() ([]storage.RefreshToken, error) {
|
2016-07-26 01:30:28 +05:30
|
|
|
return nil, errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2016-11-17 04:27:27 +05:30
|
|
|
func (cli *client) ListPasswords() (passwords []storage.Password, err error) {
|
|
|
|
var passwordList PasswordList
|
|
|
|
if err = cli.list(resourcePassword, &passwordList); err != nil {
|
|
|
|
return passwords, fmt.Errorf("failed to list passwords: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, password := range passwordList.Passwords {
|
|
|
|
p := storage.Password{
|
|
|
|
Email: password.Email,
|
|
|
|
Hash: password.Hash,
|
|
|
|
Username: password.Username,
|
|
|
|
UserID: password.UserID,
|
|
|
|
}
|
|
|
|
passwords = append(passwords, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-26 01:30:28 +05:30
|
|
|
func (cli *client) DeleteAuthRequest(id string) error {
|
|
|
|
return cli.delete(resourceAuthRequest, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *client) DeleteAuthCode(code string) error {
|
|
|
|
return cli.delete(resourceAuthCode, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *client) DeleteClient(id string) error {
|
2016-10-28 04:25:23 +05:30
|
|
|
// Check for hash collition.
|
|
|
|
c, err := cli.getClient(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return cli.delete(resourceClient, c.ObjectMeta.Name)
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *client) DeleteRefresh(id string) error {
|
|
|
|
return cli.delete(resourceRefreshToken, id)
|
|
|
|
}
|
|
|
|
|
2016-10-06 04:34:48 +05:30
|
|
|
func (cli *client) DeletePassword(email string) error {
|
2016-10-28 04:25:23 +05:30
|
|
|
// Check for hash collition.
|
|
|
|
p, err := cli.getPassword(email)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return cli.delete(resourcePassword, p.ObjectMeta.Name)
|
2016-10-06 04:34:48 +05:30
|
|
|
}
|
|
|
|
|
2016-12-23 05:26:09 +05:30
|
|
|
func (cli *client) UpdateRefreshToken(id string, updater func(old storage.RefreshToken) (storage.RefreshToken, error)) error {
|
|
|
|
r, err := cli.getRefreshToken(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
updated, err := updater(toStorageRefreshToken(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
updated.ID = id
|
|
|
|
|
|
|
|
newToken := cli.fromStorageRefreshToken(updated)
|
|
|
|
newToken.ObjectMeta = r.ObjectMeta
|
|
|
|
return cli.put(resourceRefreshToken, r.ObjectMeta.Name, newToken)
|
|
|
|
}
|
|
|
|
|
2016-07-26 01:30:28 +05:30
|
|
|
func (cli *client) UpdateClient(id string, updater func(old storage.Client) (storage.Client, error)) error {
|
2016-10-28 04:25:23 +05:30
|
|
|
c, err := cli.getClient(id)
|
|
|
|
if err != nil {
|
2016-07-26 01:30:28 +05:30
|
|
|
return err
|
|
|
|
}
|
2016-10-28 04:25:23 +05:30
|
|
|
|
2016-07-26 01:30:28 +05:30
|
|
|
updated, err := updater(toStorageClient(c))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-28 04:25:23 +05:30
|
|
|
updated.ID = c.ID
|
2016-07-26 01:30:28 +05:30
|
|
|
|
|
|
|
newClient := cli.fromStorageClient(updated)
|
|
|
|
newClient.ObjectMeta = c.ObjectMeta
|
2016-10-28 04:25:23 +05:30
|
|
|
return cli.put(resourceClient, c.ObjectMeta.Name, newClient)
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
2016-10-06 04:34:48 +05:30
|
|
|
func (cli *client) UpdatePassword(email string, updater func(old storage.Password) (storage.Password, error)) error {
|
2016-10-28 04:25:23 +05:30
|
|
|
p, err := cli.getPassword(email)
|
|
|
|
if err != nil {
|
2016-10-06 04:34:48 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
updated, err := updater(toStoragePassword(p))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-28 04:25:23 +05:30
|
|
|
updated.Email = p.Email
|
2016-10-06 04:34:48 +05:30
|
|
|
|
|
|
|
newPassword := cli.fromStoragePassword(updated)
|
|
|
|
newPassword.ObjectMeta = p.ObjectMeta
|
2016-10-28 04:25:23 +05:30
|
|
|
return cli.put(resourcePassword, p.ObjectMeta.Name, newPassword)
|
2016-10-06 04:34:48 +05:30
|
|
|
}
|
|
|
|
|
2016-07-26 01:30:28 +05:30
|
|
|
func (cli *client) UpdateKeys(updater func(old storage.Keys) (storage.Keys, error)) error {
|
|
|
|
firstUpdate := false
|
|
|
|
var keys Keys
|
|
|
|
if err := cli.get(resourceKeys, keysName, &keys); err != nil {
|
|
|
|
if err != storage.ErrNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
firstUpdate = true
|
|
|
|
}
|
|
|
|
var oldKeys storage.Keys
|
|
|
|
if !firstUpdate {
|
|
|
|
oldKeys = toStorageKeys(keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
updated, err := updater(oldKeys)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newKeys := cli.fromStorageKeys(updated)
|
|
|
|
if firstUpdate {
|
|
|
|
return cli.post(resourceKeys, newKeys)
|
|
|
|
}
|
|
|
|
newKeys.ObjectMeta = keys.ObjectMeta
|
|
|
|
return cli.put(resourceKeys, keysName, newKeys)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *client) UpdateAuthRequest(id string, updater func(a storage.AuthRequest) (storage.AuthRequest, error)) error {
|
|
|
|
var req AuthRequest
|
|
|
|
err := cli.get(resourceAuthRequest, id, &req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
updated, err := updater(toStorageAuthRequest(req))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newReq := cli.fromStorageAuthRequest(updated)
|
|
|
|
newReq.ObjectMeta = req.ObjectMeta
|
|
|
|
return cli.put(resourceAuthRequest, id, newReq)
|
|
|
|
}
|
2016-10-13 07:18:23 +05:30
|
|
|
|
|
|
|
func (cli *client) GarbageCollect(now time.Time) (result storage.GCResult, err error) {
|
|
|
|
var authRequests AuthRequestList
|
|
|
|
if err := cli.list(resourceAuthRequest, &authRequests); err != nil {
|
|
|
|
return result, fmt.Errorf("failed to list auth requests: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var delErr error
|
|
|
|
for _, authRequest := range authRequests.AuthRequests {
|
|
|
|
if now.After(authRequest.Expiry) {
|
|
|
|
if err := cli.delete(resourceAuthRequest, authRequest.ObjectMeta.Name); err != nil {
|
2016-12-13 04:24:01 +05:30
|
|
|
cli.logger.Errorf("failed to delete auth request: %v", err)
|
2016-10-13 07:18:23 +05:30
|
|
|
delErr = fmt.Errorf("failed to delete auth request: %v", err)
|
|
|
|
}
|
|
|
|
result.AuthRequests++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if delErr != nil {
|
|
|
|
return result, delErr
|
|
|
|
}
|
|
|
|
|
|
|
|
var authCodes AuthCodeList
|
|
|
|
if err := cli.list(resourceAuthCode, &authCodes); err != nil {
|
|
|
|
return result, fmt.Errorf("failed to list auth codes: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, authCode := range authCodes.AuthCodes {
|
|
|
|
if now.After(authCode.Expiry) {
|
|
|
|
if err := cli.delete(resourceAuthCode, authCode.ObjectMeta.Name); err != nil {
|
2016-12-13 04:24:01 +05:30
|
|
|
cli.logger.Errorf("failed to delete auth code %v", err)
|
2016-10-13 07:18:23 +05:30
|
|
|
delErr = fmt.Errorf("failed to delete auth code: %v", err)
|
|
|
|
}
|
|
|
|
result.AuthCodes++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, delErr
|
|
|
|
}
|