Skip to content

Commit

Permalink
Tweak Client (#76)
Browse files Browse the repository at this point in the history
This works out better with how the servers are initialised for now.
  • Loading branch information
spjmurray authored Jun 13, 2024
1 parent 929c330 commit 1276e4f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
4 changes: 2 additions & 2 deletions charts/identity/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: A Helm chart for deploying Unikorn's IdP

type: application

version: v0.2.7
appVersion: v0.2.7
version: v0.2.8
appVersion: v0.2.8

icon: https://raw.githubusercontent.com/unikorn-cloud/assets/main/images/logos/dark-on-light/icon.png
44 changes: 25 additions & 19 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,52 +41,58 @@ var (
ErrFormatError = errors.New("secret incorrectly formatted")
)

type Options struct {
// host is the identity host name.
host string
// caSecretNamespace tells us where to source the CA secret.
caSecretNamespace string
// caSecretName is the root CA secret of the identity endpoint.
caSecretName string
}

// AddFlags adds the options to the CLI flags.
func (o *Options) AddFlags(f *pflag.FlagSet) {
f.StringVar(&o.host, "identity-host", "", "Identity endpoint URL.")
f.StringVar(&o.caSecretNamespace, "identity-ca-secret-namespace", "", "Identity endpoint CA certificate secret namespace.")
f.StringVar(&o.caSecretName, "identity-ca-secret-name", "", "Identity endpoint CA certificate secret.")
}

// Client wraps up the raw OpenAPI client with things to make it useable e.g.
// authorization and TLS.
type Client struct {
// client is a Kubenetes client.
client client.Client
// namespace is the namespace the client is running in.
namespace string
// host is the identity host name.
host string
// caSecretNamespace tells us where to source the CA secret.
caSecretNamespace string
// caSecretName is the root CA secret of the identity endpoint.
caSecretName string
// options allows setting of option from the CLI
options *Options
}

// New creates a new client.
func New(client client.Client, namespace string) *Client {
func New(client client.Client, namespace string, options *Options) *Client {
return &Client{
client: client,
namespace: namespace,
options: options,
}
}

// AddFlags adds the options to the CLI flags.
func (c *Client) AddFlags(f *pflag.FlagSet) {
f.StringVar(&c.host, "identity-host", "", "Identity endpoint URL.")
f.StringVar(&c.caSecretNamespace, "identity-ca-secret-namespace", "", "Identity endpoint CA certificate secret namespace.")
f.StringVar(&c.caSecretName, "identity-ca-secret-name", "", "Identity endpoint CA certificate secret.")
}

// tlsClientConfig abstracts away private TLS CAs or self signed certificates.
func (c *Client) tlsClientConfig(ctx context.Context) (*tls.Config, error) {
if c.caSecretName == "" {
if c.options.caSecretName == "" {
//nolint:nilnil
return nil, nil
}

namespace := c.namespace

if c.caSecretNamespace != "" {
namespace = c.caSecretNamespace
if c.options.caSecretNamespace != "" {
namespace = c.options.caSecretNamespace
}

secret := &corev1.Secret{}

if err := c.client.Get(ctx, client.ObjectKey{Namespace: namespace, Name: c.caSecretName}, secret); err != nil {
if err := c.client.Get(ctx, client.ObjectKey{Namespace: namespace, Name: c.options.caSecretName}, secret); err != nil {
return nil, err
}

Expand Down Expand Up @@ -149,7 +155,7 @@ func (c *Client) Client(ctx context.Context) (*openapi.ClientWithResponses, erro
return nil, err
}

client, err := openapi.NewClientWithResponses(c.host, openapi.WithHTTPClient(httpClient), openapi.WithRequestEditorFn(accessTokenInjector))
client, err := openapi.NewClientWithResponses(c.options.host, openapi.WithHTTPClient(httpClient), openapi.WithRequestEditorFn(accessTokenInjector))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 1276e4f

Please sign in to comment.