Skip to content

Commit c4c9565

Browse files
committed
Add option to configure an external OAuth server
Signed-off-by: Simo Sorce <[email protected]>
1 parent 0706074 commit c4c9565

File tree

7 files changed

+128
-10
lines changed

7 files changed

+128
-10
lines changed

pkg/cmd/server/apis/config/types.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,10 @@ type MasterConfig struct {
399399
EtcdConfig *EtcdConfig
400400
// OAuthConfig, if present start the /oauth endpoint in this process
401401
OAuthConfig *OAuthConfig
402+
403+
// ExternalOAuthConfig, if present configures an External OAuth Server
404+
ExternalOAuthConfig *ExternalOAuthConfig
405+
402406
// DNSConfig, if present start the DNS server in this process
403407
DNSConfig *DNSConfig
404408

@@ -889,6 +893,19 @@ type OAuthTemplates struct {
889893
Error string
890894
}
891895

896+
type ExternalOAuthConfig struct {
897+
// MetadataFile is a path to a file containing the discovery endpoint for OAuth 2.0 Authorization Server Metadata
898+
// for an External OAuth server.
899+
// See IETF Draft: // https://tools.ietf.org/html/draft-ietf-oauth-discovery-04#section-2
900+
MetadataFile string
901+
902+
// MasterPublicURL is used for building valid client redirect URLs for internal and external access
903+
MasterPublicURL string
904+
905+
// AssetPublicURL is used for building valid client redirect URLs for external access
906+
AssetPublicURL string
907+
}
908+
892909
type ServiceAccountConfig struct {
893910
// ManagedNames is a list of service account names that will be auto-created in every namespace.
894911
// If no names are specified, the ServiceAccountsController will not be started.

pkg/cmd/server/apis/config/v1/types.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ type MasterConfig struct {
247247
EtcdConfig *EtcdConfig `json:"etcdConfig"`
248248
// OAuthConfig, if present start the /oauth endpoint in this process
249249
OAuthConfig *OAuthConfig `json:"oauthConfig"`
250+
251+
// ExternalOAuthConfig, if present configures an External OAuth Server
252+
ExternalOAuthConfig *ExternalOAuthConfig `json:"externalOAuthConfig"`
253+
250254
// DNSConfig, if present start the DNS server in this process
251255
DNSConfig *DNSConfig `json:"dnsConfig"`
252256

@@ -773,6 +777,20 @@ type OAuthTemplates struct {
773777
Error string `json:"error"`
774778
}
775779

780+
// ExternalOAuthConfig allos to configure external OAuth server discovery
781+
type ExternalOAuthConfig struct {
782+
// MetadataFile is a path to a file containing the discovery endpoint for OAuth 2.0 Authorization Server Metadata
783+
// for an External OAuth server.
784+
// See IETF Draft: // https://tools.ietf.org/html/draft-ietf-oauth-discovery-04#section-2
785+
MetadataFile string `json:"metadataFile"`
786+
787+
// MasterPublicURL is used for building valid client redirect URLs for internal and external access
788+
MasterPublicURL string `json:"masterPublicURL"`
789+
790+
// AssetPublicURL is used for building valid client redirect URLs for external access
791+
AssetPublicURL string `json:"assetPublicURL"`
792+
}
793+
776794
// ServiceAccountConfig holds the necessary configuration options for a service account
777795
type ServiceAccountConfig struct {
778796
// ManagedNames is a list of service account names that will be auto-created in every namespace.

pkg/cmd/server/apis/config/v1/zz_generated.deepcopy.go

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cmd/server/apis/config/zz_generated.deepcopy.go

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cmd/server/origin/master.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,15 @@ func (c *MasterConfig) newOpenshiftNonAPIConfig(kubeAPIServerConfig apiserver.Co
8484
SharedInformerFactory: c.ClientGoKubeInformers,
8585
},
8686
ExtraConfig: NonAPIExtraConfig{
87-
EnableOAuth: c.Options.OAuthConfig != nil,
87+
EnableOAuth: c.Options.OAuthConfig != nil || c.Options.ExternalOAuthConfig != nil,
8888
},
8989
}
9090
if c.Options.OAuthConfig != nil {
9191
ret.ExtraConfig.MasterPublicURL = c.Options.OAuthConfig.MasterPublicURL
9292
}
93+
if c.Options.ExternalOAuthConfig != nil {
94+
ret.ExtraConfig.OAuthMetadataFile = c.Options.ExternalOAuthConfig.MetadataFile
95+
}
9396

9497
return ret
9598
}

pkg/cmd/server/origin/nonapiserver.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313
)
1414

1515
type NonAPIExtraConfig struct {
16-
MasterPublicURL string
17-
EnableOAuth bool
16+
MasterPublicURL string
17+
EnableOAuth bool
18+
OAuthMetadataFile string
1819
}
1920

2021
type OpenshiftNonAPIConfig struct {
@@ -60,7 +61,7 @@ func (c completedOpenshiftNonAPIConfig) New(delegationTarget genericapiserver.De
6061
// TODO move this up to the spot where we wire the oauth endpoint
6162
// Set up OAuth metadata only if we are configured to use OAuth
6263
if c.ExtraConfig.EnableOAuth {
63-
initOAuthAuthorizationServerMetadataRoute(s.GenericAPIServer.Handler.NonGoRestfulMux, oauthMetadataEndpoint, c.ExtraConfig.MasterPublicURL)
64+
initOAuthAuthorizationServerMetadataRoute(s.GenericAPIServer.Handler.NonGoRestfulMux, c.ExtraConfig)
6465
}
6566

6667
return s, nil
@@ -76,15 +77,26 @@ const (
7677
// initOAuthAuthorizationServerMetadataRoute initializes an HTTP endpoint for OAuth 2.0 Authorization Server Metadata discovery
7778
// https://tools.ietf.org/id/draft-ietf-oauth-discovery-04.html#rfc.section.2
7879
// masterPublicURL should be internally and externally routable to allow all users to discover this information
79-
func initOAuthAuthorizationServerMetadataRoute(mux *genericmux.PathRecorderMux, path, masterPublicURL string) {
80+
func initOAuthAuthorizationServerMetadataRoute(mux *genericmux.PathRecorderMux, ExtraConfig *NonAPIExtraConfig) {
8081
// Build OAuth metadata once
81-
metadata, err := json.MarshalIndent(oauthutil.GetOauthMetadata(masterPublicURL), "", " ")
82-
if err != nil {
83-
glog.Errorf("Unable to initialize OAuth authorization server metadata route: %v", err)
84-
return
82+
var metadata []byte
83+
var err error
84+
85+
if len(ExtraConfig.OAuthMetadataFile) > 0 {
86+
metadata, err = oauthutil.LoadOAuthMetadataFile(ExtraConfig.OAuthMetadataFile)
87+
if err != nil {
88+
glog.Error(err)
89+
return
90+
}
91+
} else {
92+
metadata, err = json.MarshalIndent(oauthutil.GetOauthMetadata(ExtraConfig.MasterPublicURL), "", " ")
93+
if err != nil {
94+
glog.Errorf("Unable to initialize OAuth authorization server metadata route: %v", err)
95+
return
96+
}
8597
}
8698

87-
mux.UnlistedHandleFunc(path, func(w http.ResponseWriter, req *http.Request) {
99+
mux.UnlistedHandleFunc(oauthMetadataEndpoint, func(w http.ResponseWriter, req *http.Request) {
88100
w.Header().Set("Content-Type", "application/json")
89101
w.WriteHeader(http.StatusOK)
90102
w.Write(metadata)

pkg/oauth/util/discovery.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package util
22

33
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
48
"github.com/RangelReale/osin"
59
"github.com/openshift/origin/pkg/authorization/authorizer/scope"
610
"github.com/openshift/origin/pkg/oauth/apis/oauth/validation"
@@ -51,3 +55,17 @@ func GetOauthMetadata(masterPublicURL string) OauthAuthorizationServerMetadata {
5155
CodeChallengeMethodsSupported: validation.CodeChallengeMethodsSupported,
5256
}
5357
}
58+
59+
func LoadOAuthMetadataFile(metadataFile string) ([]byte, error) {
60+
data, err := ioutil.ReadFile(metadataFile)
61+
if err != nil {
62+
return nil, fmt.Errorf("Unable to read External OAuth Metadata file: %v", err)
63+
}
64+
65+
oauthMetadata := OauthAuthorizationServerMetadata{}
66+
if err := json.Unmarshal(data, &oauthMetadata); err != nil {
67+
return nil, fmt.Errorf("Unable to decode External OAuth Metadata file: %v", err)
68+
}
69+
70+
return data, nil
71+
}

0 commit comments

Comments
 (0)