forked from d2iq-archive/mesos-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup: introduce DoerFunc, DoerFactory, ConfigMap, and an auth plug…
…in registry
- Loading branch information
James DeFelice
committed
Jul 12, 2016
1 parent
af8d21d
commit 7a3406d
Showing
10 changed files
with
208 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/mesosphere/mesos-dns/httpcli/basic" | ||
"github.com/mesosphere/mesos-dns/httpcli/iam" | ||
) | ||
|
||
// initAuth registers HTTP client factories for supported authentication types | ||
func initAuth() { | ||
basic.Register() | ||
iam.Register() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package basic | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/mesosphere/mesos-dns/httpcli" | ||
) | ||
|
||
// Register registers a DoerFactory for HTTP Basic authentication | ||
func Register() { | ||
httpcli.Register(httpcli.AuthBasic, httpcli.DoerFactory(func(cm httpcli.ConfigMap, c *http.Client) httpcli.Doer { | ||
obj := cm.FindOrPanic(httpcli.AuthBasic) | ||
config, ok := obj.(Credentials) | ||
if !ok { | ||
panic(fmt.Sprintf("expected Credentials instead of %#+v", obj)) | ||
} | ||
return Doer(c, config) | ||
})) | ||
} | ||
|
||
// Doer wraps an HTTP transactor given basic credentials | ||
func Doer(client httpcli.Doer, credentials Credentials) httpcli.Doer { | ||
return httpcli.DoerFunc(func(req *http.Request) (*http.Response, error) { | ||
req.SetBasicAuth(credentials.Principal, credentials.Secret) | ||
return client.Do(req) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,61 @@ | ||
package httpcli | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"sync" | ||
) | ||
|
||
// AuthMechanism enumerates the supported authentication strategies | ||
type AuthMechanism string | ||
|
||
// ErrDuplicateAuthRegistration signifies a configuration error in which the same | ||
// AuthMechanism is being registered multiple times. | ||
var ErrDuplicateAuthRegistration = errors.New("duplicate auth mechanism registration") | ||
|
||
// AuthNone, et al. represent the complete set of supported authentication mechanisms | ||
const ( | ||
// AuthNone specifies no authentication mechanism | ||
AuthNone AuthMechanism = "none" | ||
AuthNone AuthMechanism = "" // AuthNone specifies no authentication mechanism | ||
AuthBasic = "basic" // AuthBasic specifies to use HTTP Basic | ||
AuthIAM = "iam" // AuthIAM specifies to use IAM / JDK authentication | ||
) | ||
|
||
// AuthBasic specifies to use HTTP Basic | ||
AuthBasic AuthMechanism = "basic" | ||
var registry = struct { | ||
sync.Mutex | ||
factories map[AuthMechanism]DoerFactory | ||
}{ | ||
factories: map[AuthMechanism]DoerFactory{ | ||
AuthNone: DoerFactory(func(_ ConfigMap, client *http.Client) Doer { return client }), | ||
}, | ||
} | ||
|
||
// AuthIAM specifies to use IAM / JDK authentication | ||
AuthIAM AuthMechanism = "iam" | ||
) | ||
// ConfigMap maps authentication configuration types to values | ||
type ConfigMap map[AuthMechanism]interface{} | ||
|
||
// FindOrPanic returns the mapped configuration for the given auth mechanism or else panics | ||
func (cm ConfigMap) FindOrPanic(am AuthMechanism) interface{} { | ||
obj, ok := cm[am] | ||
if !ok { | ||
panic(fmt.Sprintf("missing configuration for auth mechanism %q", am)) | ||
} | ||
return obj | ||
} | ||
|
||
// Register associates an AuthMechanism with a DoerFactory | ||
func Register(am AuthMechanism, df DoerFactory) { | ||
registry.Lock() | ||
defer registry.Unlock() | ||
|
||
if _, ok := registry.factories[am]; ok { | ||
panic(ErrDuplicateAuthRegistration) | ||
} | ||
registry.factories[am] = df | ||
} | ||
|
||
func factoryFor(am AuthMechanism) (df DoerFactory, ok bool) { | ||
registry.Lock() | ||
defer registry.Unlock() | ||
df, ok = registry.factories[am] | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package iam | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/dgrijalva/jwt-go" | ||
"github.com/mesosphere/mesos-dns/errorutil" | ||
"github.com/mesosphere/mesos-dns/httpcli" | ||
) | ||
|
||
// Register registers a DoerFactory for IAM (JWT-based) authentication | ||
func Register() { | ||
httpcli.Register(httpcli.AuthIAM, httpcli.DoerFactory(func(cm httpcli.ConfigMap, c *http.Client) httpcli.Doer { | ||
obj := cm.FindOrPanic(httpcli.AuthIAM) | ||
config, ok := obj.(Config) | ||
if !ok { | ||
panic(fmt.Sprintf("expected Config instead of %#+v", obj)) | ||
} | ||
return Doer(c, config) | ||
})) | ||
} | ||
|
||
// Doer wraps an HTTP transactor given an IAM configuration | ||
func Doer(client *http.Client, config Config) httpcli.Doer { | ||
return httpcli.DoerFunc(func(req *http.Request) (*http.Response, error) { | ||
// TODO if we still have a valid token, try using it first | ||
token := jwt.New(jwt.SigningMethodRS256) | ||
token.Claims["uid"] = config.ID | ||
token.Claims["exp"] = time.Now().Add(time.Hour).Unix() | ||
// SignedString will treat secret as PEM-encoded key | ||
tokenStr, err := token.SignedString([]byte(config.Secret)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
authReq := struct { | ||
UID string `json:"uid"` | ||
Token string `json:"token,omitempty"` | ||
}{ | ||
UID: config.ID, | ||
Token: tokenStr, | ||
} | ||
|
||
b, err := json.Marshal(authReq) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
authBody := bytes.NewBuffer(b) | ||
resp, err := client.Post(config.LoginEndpoint, "application/json", authBody) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer errorutil.Ignore(resp.Body.Close) | ||
if resp.StatusCode != 200 { | ||
return nil, httpcli.ErrAuthFailed | ||
} | ||
|
||
var authResp struct { | ||
Token string `json:"token"` | ||
} | ||
err = json.NewDecoder(resp.Body).Decode(&authResp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if req.Header == nil { | ||
req.Header = make(http.Header) | ||
} | ||
req.Header.Set("Authorization", "token="+authResp.Token) | ||
|
||
return client.Do(req) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters