|
| 1 | +/* |
| 2 | +Copyright 2025 The Kube Bind 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 oidc |
| 18 | + |
| 19 | +import ( |
| 20 | + "crypto/tls" |
| 21 | + "crypto/x509" |
| 22 | + "fmt" |
| 23 | + "net" |
| 24 | + "os" |
| 25 | + "path" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/gorilla/mux" |
| 29 | + "github.com/xrstf/mockoidc" |
| 30 | +) |
| 31 | + |
| 32 | +type Server struct { |
| 33 | + server *mockoidc.MockOIDC |
| 34 | + tlsConfig *tls.Config |
| 35 | +} |
| 36 | + |
| 37 | +func New(caBundleFile string, listener net.Listener) (*Server, error) { |
| 38 | + // Add offline_access to supported scopes for refresh token support |
| 39 | + ensureOfflineAccessScope() |
| 40 | + var tlsConfig *tls.Config |
| 41 | + s := &Server{} |
| 42 | + if caBundleFile != "" { |
| 43 | + var err error |
| 44 | + tlsConfig, err = LoadTLSConfig(caBundleFile) |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("failed to load CA bundle file: %w", err) |
| 47 | + } |
| 48 | + s.tlsConfig = tlsConfig |
| 49 | + } |
| 50 | + |
| 51 | + server, err := mockoidc.NewServer(&mockoidc.ServerConfig{ |
| 52 | + TLSConfig: tlsConfig, |
| 53 | + Listener: listener, |
| 54 | + }) |
| 55 | + if err != nil { |
| 56 | + return nil, fmt.Errorf("failed to create mock OIDC server: %w", err) |
| 57 | + } |
| 58 | + s.server = server |
| 59 | + return s, nil |
| 60 | +} |
| 61 | + |
| 62 | +// Config gives the configuration for clients to connect to the embedded OIDC server. |
| 63 | +type Config struct { |
| 64 | + ClientID string |
| 65 | + ClientSecret string |
| 66 | + Issuer string |
| 67 | + |
| 68 | + AccessTTL time.Duration |
| 69 | + RefreshTTL time.Duration |
| 70 | + |
| 71 | + CodeChallengeMethodsSupported []string |
| 72 | + |
| 73 | + // CallbackURL is kube-bind specific and must match API server endpoints. |
| 74 | + CallbackURL string |
| 75 | +} |
| 76 | + |
| 77 | +var ErrServerNotRunning = fmt.Errorf("embedded OIDC server is not running") |
| 78 | + |
| 79 | +func (s *Server) TLSConfig() *tls.Config { |
| 80 | + return s.tlsConfig |
| 81 | +} |
| 82 | + |
| 83 | +func (s *Server) AddRoutes(mux *mux.Router) { |
| 84 | + s.server.AddRoutes(mux) |
| 85 | +} |
| 86 | + |
| 87 | +// URL returns the base URL of the embedded OIDC server. |
| 88 | +func (s *Server) Config() (*Config, error) { |
| 89 | + return &Config{ |
| 90 | + ClientID: s.server.Config().ClientID, |
| 91 | + ClientSecret: s.server.Config().ClientSecret, |
| 92 | + Issuer: s.server.Config().Issuer, |
| 93 | + |
| 94 | + AccessTTL: s.server.Config().AccessTTL, |
| 95 | + RefreshTTL: s.server.Config().RefreshTTL, |
| 96 | + |
| 97 | + CodeChallengeMethodsSupported: s.server.Config().CodeChallengeMethodsSupported, |
| 98 | + CallbackURL: path.Join(s.server.Addr(), "api/callback"), |
| 99 | + }, nil |
| 100 | +} |
| 101 | + |
| 102 | +func LoadTLSConfig(caFile string) (*tls.Config, error) { |
| 103 | + caCertPool := x509.NewCertPool() |
| 104 | + caCert, err := os.ReadFile(caFile) |
| 105 | + if err != nil { |
| 106 | + return nil, fmt.Errorf("failed to read CA file: %w", err) |
| 107 | + } |
| 108 | + if ok := caCertPool.AppendCertsFromPEM(caCert); !ok { |
| 109 | + return nil, fmt.Errorf("failed to append CA certs from PEM") |
| 110 | + } |
| 111 | + return &tls.Config{ |
| 112 | + RootCAs: caCertPool, |
| 113 | + }, nil |
| 114 | +} |
| 115 | + |
| 116 | +// ensureOfflineAccessScope adds "offline_access" to mockoidc's supported scopes if not already present |
| 117 | +func ensureOfflineAccessScope() { |
| 118 | + offlineAccess := "offline_access" |
| 119 | + |
| 120 | + // Check if offline_access is already in the supported scopes |
| 121 | + for _, scope := range mockoidc.ScopesSupported { |
| 122 | + if scope == offlineAccess { |
| 123 | + return // Already present |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // Add offline_access to the supported scopes |
| 128 | + mockoidc.ScopesSupported = append(mockoidc.ScopesSupported, offlineAccess) |
| 129 | +} |
0 commit comments