mirror of
https://github.com/pomerium/pomerium.git
synced 2025-08-03 00:40:25 +02:00
azure: support deriving credentials from client id, client secret and provider url (#1300)
This commit is contained in:
parent
882b6b54ee
commit
c4c8ef8e53
6 changed files with 104 additions and 13 deletions
|
@ -282,7 +282,39 @@ type ServiceAccount struct {
|
|||
}
|
||||
|
||||
// ParseServiceAccount parses the service account in the config options.
|
||||
func ParseServiceAccount(rawServiceAccount string) (*ServiceAccount, error) {
|
||||
func ParseServiceAccount(options directory.Options) (*ServiceAccount, error) {
|
||||
if options.ServiceAccount != "" {
|
||||
return parseServiceAccountFromString(options.ServiceAccount)
|
||||
}
|
||||
return parseServiceAccountFromOptions(options.ClientID, options.ClientSecret, options.ProviderURL)
|
||||
}
|
||||
|
||||
func parseServiceAccountFromOptions(clientID, clientSecret, providerURL string) (*ServiceAccount, error) {
|
||||
serviceAccount := ServiceAccount{
|
||||
ClientID: clientID,
|
||||
ClientSecret: clientSecret,
|
||||
}
|
||||
|
||||
var err error
|
||||
serviceAccount.DirectoryID, err = parseDirectoryIDFromURL(providerURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if serviceAccount.ClientID == "" {
|
||||
return nil, fmt.Errorf("client_id is required")
|
||||
}
|
||||
if serviceAccount.ClientSecret == "" {
|
||||
return nil, fmt.Errorf("client_secret is required")
|
||||
}
|
||||
if serviceAccount.DirectoryID == "" {
|
||||
return nil, fmt.Errorf("directory_id is required")
|
||||
}
|
||||
|
||||
return &serviceAccount, nil
|
||||
}
|
||||
|
||||
func parseServiceAccountFromString(rawServiceAccount string) (*ServiceAccount, error) {
|
||||
bs, err := base64.StdEncoding.DecodeString(rawServiceAccount)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -306,3 +338,17 @@ func ParseServiceAccount(rawServiceAccount string) (*ServiceAccount, error) {
|
|||
|
||||
return &serviceAccount, nil
|
||||
}
|
||||
|
||||
func parseDirectoryIDFromURL(providerURL string) (string, error) {
|
||||
u, err := url.Parse(providerURL)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
pathParts := strings.SplitN(u.Path, "/", 3)
|
||||
if len(pathParts) != 3 {
|
||||
return "", fmt.Errorf("no directory id found in path")
|
||||
}
|
||||
|
||||
return pathParts[1], nil
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package azure
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
@ -107,6 +108,43 @@ func Test(t *testing.T) {
|
|||
}, groups)
|
||||
}
|
||||
|
||||
func TestParseServiceAccount(t *testing.T) {
|
||||
t.Run("by options", func(t *testing.T) {
|
||||
serviceAccount, err := ParseServiceAccount(directory.Options{
|
||||
ProviderURL: "https://login.microsoftonline.com/0303f438-3c5c-4190-9854-08d3eb31bd9f/v2.0",
|
||||
ClientID: "CLIENT_ID",
|
||||
ClientSecret: "CLIENT_SECRET",
|
||||
})
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(t, &ServiceAccount{
|
||||
ClientID: "CLIENT_ID",
|
||||
ClientSecret: "CLIENT_SECRET",
|
||||
DirectoryID: "0303f438-3c5c-4190-9854-08d3eb31bd9f",
|
||||
}, serviceAccount)
|
||||
})
|
||||
t.Run("by service account", func(t *testing.T) {
|
||||
serviceAccount, err := ParseServiceAccount(directory.Options{
|
||||
ServiceAccount: base64.StdEncoding.EncodeToString([]byte(`{
|
||||
"client_id": "CLIENT_ID",
|
||||
"client_secret": "CLIENT_SECRET",
|
||||
"directory_id": "0303f438-3c5c-4190-9854-08d3eb31bd9f"
|
||||
}`)),
|
||||
})
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(t, &ServiceAccount{
|
||||
ClientID: "CLIENT_ID",
|
||||
ClientSecret: "CLIENT_SECRET",
|
||||
DirectoryID: "0303f438-3c5c-4190-9854-08d3eb31bd9f",
|
||||
}, serviceAccount)
|
||||
})
|
||||
}
|
||||
|
||||
func mustParseURL(rawurl string) *url.URL {
|
||||
u, err := url.Parse(rawurl)
|
||||
if err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue