Skip to content

Commit 69d9ac6

Browse files
committed
Use Shared CA
Core now installs a shared CA to be be used by cluster microservices making key distribution easier. Use that rather than self-signing.
1 parent c56e55d commit 69d9ac6

File tree

5 files changed

+162
-15
lines changed

5 files changed

+162
-15
lines changed

charts/identity/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: A Helm chart for deploying Unikorn's IdP
44

55
type: application
66

7-
version: v0.2.6
8-
appVersion: v0.2.6
7+
version: v0.2.7
8+
appVersion: v0.2.7
99

1010
icon: https://raw.githubusercontent.com/unikorn-cloud/assets/main/images/logos/dark-on-light/icon.png

charts/identity/templates/identity/ingress.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ metadata:
99
cert-manager.io/cluster-issuer: {{ .Values.ingress.clusterIssuer | indent 2 }}
1010
{{- else if .Values.ingress.issuer }}
1111
cert-manager.io/issuer: {{ .Values.ingress.issuer | indent 2 }}
12-
{{- else }}
13-
cert-manager.io/issuer: "unikorn-identity-ingress"
1412
{{- end }}
1513
{{- if .Values.ingress.externalDns }}
1614
external-dns.alpha.kubernetes.io/hostname: {{ .Values.host }}

charts/identity/templates/identity/issuer-ingress.yaml

Lines changed: 0 additions & 10 deletions
This file was deleted.

charts/identity/values.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ ingress:
102102
issuer: ~
103103

104104
# clusterIssuer to use.
105-
clusterIssuer: ~
105+
# This defaults to the self-signed CA defined in Unikorn core.
106+
clusterIssuer: unikorn-issuer
106107

107108
# If true, will add the external DNS hostname annotation.
108109
externalDns: false

pkg/client/client.go

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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

Comments
 (0)