|
| 1 | +/* |
| 2 | +Copyright 2024 the Unikorn 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 client |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "crypto/tls" |
| 22 | + "crypto/x509" |
| 23 | + "errors" |
| 24 | + "fmt" |
| 25 | + "net/http" |
| 26 | + |
| 27 | + "github.com/spf13/pflag" |
| 28 | + "go.opentelemetry.io/otel" |
| 29 | + "go.opentelemetry.io/otel/propagation" |
| 30 | + |
| 31 | + "github.com/unikorn-cloud/core/pkg/authorization/accesstoken" |
| 32 | + "github.com/unikorn-cloud/identity/pkg/openapi" |
| 33 | + |
| 34 | + corev1 "k8s.io/api/core/v1" |
| 35 | + |
| 36 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 37 | +) |
| 38 | + |
| 39 | +var ( |
| 40 | + // ErrFormatError is returned when a secret doesn't meet the specification. |
| 41 | + ErrFormatError = errors.New("secret incorrectly formatted") |
| 42 | +) |
| 43 | + |
| 44 | +// Client wraps up the raw OpenAPI client with things to make it useable e.g. |
| 45 | +// authorization and TLS. |
| 46 | +type Client struct { |
| 47 | + // client is a Kubenetes client. |
| 48 | + client client.Client |
| 49 | + // namespace is the namespace the client is running in. |
| 50 | + namespace string |
| 51 | + // host is the identity host name. |
| 52 | + host string |
| 53 | + // caSecretNamespace tells us where to source the CA secret. |
| 54 | + caSecretNamespace string |
| 55 | + // caSecretName is the root CA secret of the identity endpoint. |
| 56 | + caSecretName string |
| 57 | +} |
| 58 | + |
| 59 | +// New creates a new client. |
| 60 | +func New(client client.Client, namespace string) *Client { |
| 61 | + return &Client{ |
| 62 | + client: client, |
| 63 | + namespace: namespace, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// AddFlags adds the options to the CLI flags. |
| 68 | +func (c *Client) AddFlags(f *pflag.FlagSet) { |
| 69 | + f.StringVar(&c.host, "identity-host", "", "Identity endpoint URL.") |
| 70 | + f.StringVar(&c.caSecretNamespace, "identity-ca-secret-namespace", "", "Identity endpoint CA certificate secret namespace.") |
| 71 | + f.StringVar(&c.caSecretName, "identity-ca-secret-name", "", "Identity endpoint CA certificate secret.") |
| 72 | +} |
| 73 | + |
| 74 | +// tlsClientConfig abstracts away private TLS CAs or self signed certificates. |
| 75 | +func (c *Client) tlsClientConfig(ctx context.Context) (*tls.Config, error) { |
| 76 | + if c.caSecretName == "" { |
| 77 | + //nolint:nilnil |
| 78 | + return nil, nil |
| 79 | + } |
| 80 | + |
| 81 | + namespace := c.namespace |
| 82 | + |
| 83 | + if c.caSecretNamespace != "" { |
| 84 | + namespace = c.caSecretNamespace |
| 85 | + } |
| 86 | + |
| 87 | + secret := &corev1.Secret{} |
| 88 | + |
| 89 | + if err := c.client.Get(ctx, client.ObjectKey{Namespace: namespace, Name: c.caSecretName}, secret); err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + |
| 93 | + if secret.Type != corev1.SecretTypeTLS { |
| 94 | + return nil, fmt.Errorf("%w: issuer CA not of type kubernetes.io/tls", ErrFormatError) |
| 95 | + } |
| 96 | + |
| 97 | + cert, ok := secret.Data[corev1.TLSCertKey] |
| 98 | + if !ok { |
| 99 | + return nil, fmt.Errorf("%w: issuer CA missing tls.crt", ErrFormatError) |
| 100 | + } |
| 101 | + |
| 102 | + certPool := x509.NewCertPool() |
| 103 | + |
| 104 | + if ok := certPool.AppendCertsFromPEM(cert); !ok { |
| 105 | + return nil, fmt.Errorf("%w: failed to load identity CA certificate", ErrFormatError) |
| 106 | + } |
| 107 | + |
| 108 | + config := &tls.Config{ |
| 109 | + RootCAs: certPool, |
| 110 | + MinVersion: tls.VersionTLS13, |
| 111 | + } |
| 112 | + |
| 113 | + return config, nil |
| 114 | +} |
| 115 | + |
| 116 | +// httpClient returns a new http client that will transparently do oauth2 header |
| 117 | +// injection and refresh token updates. |
| 118 | +func (c *Client) httpClient(ctx context.Context) (*http.Client, error) { |
| 119 | + // Handle non-system CA certificates for the OIDC discovery protocol |
| 120 | + // and oauth2 token refresh. This will return nil if none is specified |
| 121 | + // and default to the system roots. |
| 122 | + tlsClientConfig, err := c.tlsClientConfig(ctx) |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + |
| 127 | + client := &http.Client{ |
| 128 | + Transport: &http.Transport{ |
| 129 | + TLSClientConfig: tlsClientConfig, |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + return client, nil |
| 134 | +} |
| 135 | + |
| 136 | +// accessTokenInjector implements OAuth2 bearer token authorization. |
| 137 | +func accessTokenInjector(ctx context.Context, req *http.Request) error { |
| 138 | + req.Header.Set("Authorization", "bearer "+accesstoken.FromContext(ctx)) |
| 139 | + |
| 140 | + otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header)) |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +// Client returns a new OpenAPI client that can be used to access the API. |
| 146 | +func (c *Client) Client(ctx context.Context) (*openapi.ClientWithResponses, error) { |
| 147 | + httpClient, err := c.httpClient(ctx) |
| 148 | + if err != nil { |
| 149 | + return nil, err |
| 150 | + } |
| 151 | + |
| 152 | + client, err := openapi.NewClientWithResponses(c.host, openapi.WithHTTPClient(httpClient), openapi.WithRequestEditorFn(accessTokenInjector)) |
| 153 | + if err != nil { |
| 154 | + return nil, err |
| 155 | + } |
| 156 | + |
| 157 | + return client, nil |
| 158 | +} |
0 commit comments