Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EVEREST-1799 Configure OIDC scope #1063

Merged
merged 13 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
347 changes: 175 additions & 172 deletions api/everest-server.gen.go

Large diffs are not rendered by default.

347 changes: 175 additions & 172 deletions client/everest-client.gen.go

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions commands/settings/oidc/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package oidc

import (
"os"
"strings"

"github.com/spf13/cobra"

"github.com/percona/everest/pkg/cli"
"github.com/percona/everest/pkg/common"
"github.com/percona/everest/pkg/logger"
"github.com/percona/everest/pkg/oidc"
"github.com/percona/everest/pkg/output"
Expand All @@ -33,7 +35,7 @@ var (
Args: cobra.NoArgs,
Long: "Configure OIDC settings",
Short: "Configure OIDC settings",
Example: `everestctl settings oidc configure --issuer-url https://example.com --client-id 123456`,
Example: `everestctl settings oidc configure --issuer-url https://example.com --client-id 123456 --scopes openid,profile,email,groups`,
PreRun: settingsOIDCConfigurePreRun,
Run: settingsOIDCConfigureRun,
}
Expand All @@ -42,8 +44,9 @@ var (

func init() {
// local command flags
settingsOIDCConfigureCmd.Flags().StringVar(&settingsOIDCConfigureCfg.IssuerURL, cli.FlagOIDCIssueURL, "", "OIDC issuer url")
settingsOIDCConfigureCmd.Flags().StringVar(&settingsOIDCConfigureCfg.ClientID, cli.FlagOIDCIssueClientID, "", "OIDC application client ID")
settingsOIDCConfigureCmd.Flags().StringVar(&settingsOIDCConfigureCfg.IssuerURL, cli.FlagOIDCIssuerURL, "", "OIDC issuer url")
settingsOIDCConfigureCmd.Flags().StringVar(&settingsOIDCConfigureCfg.ClientID, cli.FlagOIDCClientID, "", "OIDC application client ID")
settingsOIDCConfigureCmd.Flags().StringVar(&settingsOIDCConfigureCfg.Scopes, cli.FlagOIDCScopes, strings.Join(common.DefaultOIDCScopes, ","), "Comma-separated list of scopes")
}

func settingsOIDCConfigurePreRun(cmd *cobra.Command, _ []string) { //nolint:revive
Expand Down
6 changes: 6 additions & 0 deletions docs/spec/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2136,9 +2136,15 @@ components:
issuerURL:
type: string
description: OIDC provider url
scopes:
type: array
items:
type: string
description: OIDC scopes
required:
- clientId
- issuerURL
- scopes
DatabaseClusterList:
description: DatabaseClusterList is an object that contains the list of the existing database clusters.
properties:
Expand Down
1 change: 1 addition & 0 deletions internal/server/handlers/k8s/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (h *k8sHandler) GetSettings(ctx context.Context) (*api.Settings, error) {
OidcConfig: api.OIDCConfig{
ClientId: config.ClientID,
IssuerURL: config.IssuerURL,
Scopes: config.Scopes,
},
}, nil
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ const (

// settings flags

// FlagOIDCIssueURL is the name of the issuer-url flag.
FlagOIDCIssueURL = "issuer-url"
// FlagOIDCIssueClientID is the name of the client-id flag.
FlagOIDCIssueClientID = "client-id"
// FlagOIDCIssuerURL is the name of the issuer-url flag.
FlagOIDCIssuerURL = "issuer-url"
// FlagOIDCClientID is the name of the client-id flag.
FlagOIDCClientID = "client-id"
// FlagOIDCScopes is the name of the scope flag.
FlagOIDCScopes = "scopes"
// FlagRBACPolicyFile is the name of the policy-file flag.
FlagRBACPolicyFile = "policy-file"
)
15 changes: 12 additions & 3 deletions pkg/common/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import (
"gopkg.in/yaml.v3"
)

// DefaultOIDCScopes is the default scopes for OIDC.
var DefaultOIDCScopes = []string{"openid", "profile", "email"}

// EverestSettings represents the everest settings.
type EverestSettings struct {
OIDCConfigRaw string `mapstructure:"oidc.config"`
}

// OIDCConfig represents the OIDC provider configuration.
type OIDCConfig struct {
IssuerURL string `yaml:"issuerUrl"`
ClientID string `yaml:"clientId"`
IssuerURL string `yaml:"issuerUrl"`
ClientID string `yaml:"clientId"`
Scopes []string `yaml:"scopes"`
}

// Raw converts the OIDCConfig struct to a raw YAML string.
Expand All @@ -27,7 +31,12 @@ func (c *OIDCConfig) Raw() (string, error) {

// OIDCConfig returns the OIDCConfig struct from the raw string.
func (e *EverestSettings) OIDCConfig() (OIDCConfig, error) {
var oidc OIDCConfig
oidc := OIDCConfig{
// Starting from v1.5.0, users can configure the OIDC scopes. In order
// to keep backward compatibility, we set the default scopes if they're
// not set.
Scopes: DefaultOIDCScopes,
}
err := yaml.Unmarshal([]byte(e.OIDCConfigRaw), &oidc)
if err != nil {
return OIDCConfig{}, err
Expand Down
23 changes: 13 additions & 10 deletions pkg/common/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ func TestToMap(t *testing.T) {
{
name: "correct",
input: EverestSettings{
OIDCConfigRaw: "issuerUrl: url\nclientId: id\n",
OIDCConfigRaw: "issuerUrl: url\nclientId: id\nscopes:\n- openid\n - profile\n - email\n - groups\n",
},
expected: map[string]string{"oidc.config": "issuerUrl: url\nclientId: id\n"},
expected: map[string]string{"oidc.config": "issuerUrl: url\nclientId: id\nscopes:\n- openid\n - profile\n - email\n - groups\n"},
},
{
name: "empty oidc",
input: EverestSettings{
OIDCConfigRaw: "issuerUrl: \"\"\nclientId: \"\"\n",
OIDCConfigRaw: "issuerUrl: \"\"\nclientId: \"\"\nscopes: []\n",
},
expected: map[string]string{"oidc.config": "issuerUrl: \"\"\nclientId: \"\"\n"},
expected: map[string]string{"oidc.config": "issuerUrl: \"\"\nclientId: \"\"\nscopes: []\n"},
},
}

Expand All @@ -54,16 +54,16 @@ func TestFromMap(t *testing.T) {
{
name: "correct",
expected: EverestSettings{
OIDCConfigRaw: "issuerUrl: url\nclientId: id\n",
OIDCConfigRaw: "issuerUrl: url\nclientId: id\nscopes:\n- openid\n- profile\n- email\n- groups\n",
},
input: map[string]string{"oidc.config": "issuerUrl: url\nclientId: id\n"},
input: map[string]string{"oidc.config": "issuerUrl: url\nclientId: id\nscopes:\n- openid\n- profile\n- email\n- groups\n"},
},
{
name: "extra key",
expected: EverestSettings{
OIDCConfigRaw: "issuerUrl: url\nclientId: id\nextraKey: value\n",
OIDCConfigRaw: "issuerUrl: url\nclientId: id\nscopes:\n- openid\n- profile\n- email\n- groups\nextraKey: value\n",
},
input: map[string]string{"oidc.config": "issuerUrl: url\nclientId: id\nextraKey: value\n"},
input: map[string]string{"oidc.config": "issuerUrl: url\nclientId: id\nscopes:\n- openid\n- profile\n- email\n- groups\nextraKey: value\n"},
},
{
name: "missing key",
Expand Down Expand Up @@ -99,22 +99,25 @@ func TestOIDCConfig(t *testing.T) {
expected: OIDCConfig{
IssuerURL: "url",
ClientID: "id",
Scopes: []string{"openid", "profile", "email", "groups"},
},
rawConfig: "issuerUrl: url\nclientId: id\n",
rawConfig: "issuerUrl: url\nclientId: id\nscopes:\n- openid\n- profile\n- email\n- groups\n",
},
{
name: "extra key",
expected: OIDCConfig{
IssuerURL: "url",
ClientID: "id",
Scopes: []string{"openid", "profile", "email", "groups"},
},
rawConfig: "issuerUrl: url\nclientId: id\nextraKey: value\n",
rawConfig: "issuerUrl: url\nclientId: id\nscopes:\n- openid\n- profile\n- email\n- groups\nextraKey: value\n",
},
{
name: "missing key",
expected: OIDCConfig{
IssuerURL: "url",
ClientID: "",
Scopes: DefaultOIDCScopes,
},
rawConfig: "issuerUrl: url\n",
},
Expand Down
12 changes: 12 additions & 0 deletions pkg/oidc/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package oidc
import (
"context"
"errors"
"slices"
"strings"

"github.com/AlecAivazis/survey/v2"
"go.uber.org/zap"
Expand All @@ -45,6 +47,8 @@ type Config struct {
IssuerURL string
// ClientID ID of the client OIDC app.
ClientID string
// Scopes requested scopes.
Scopes string
}

// NewOIDC returns a new OIDC struct.
Expand All @@ -71,6 +75,7 @@ func NewOIDC(c Config, l *zap.SugaredLogger) (*OIDC, error) {
func (u *OIDC) Run(ctx context.Context) error {
issuerURL := u.config.IssuerURL
clientID := u.config.ClientID
scopes := strings.Split(u.config.Scopes, ",")

if issuerURL == "" {
if err := survey.AskOne(&survey.Input{
Expand All @@ -93,6 +98,12 @@ func (u *OIDC) Run(ctx context.Context) error {
return errors.New("clientID and/or issuerURL are not provided")
}

if !slices.ContainsFunc(scopes, func(s string) bool {
return s == "openid"
}) {
return errors.New("scopes must contain 'openid'")
}

// Check if we can connect to the provider.
_, err := getProviderConfig(ctx, issuerURL)
if err != nil {
Expand All @@ -106,6 +117,7 @@ func (u *OIDC) Run(ctx context.Context) error {
oidcCfg := common.OIDCConfig{
IssuerURL: issuerURL,
ClientID: clientID,
Scopes: scopes,
}

oidcRaw, err := oidcCfg.Raw()
Expand Down
4 changes: 2 additions & 2 deletions ui/apps/everest/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ const App = () => {
useEffect(() => {
const loadConfigs = async () => {
try {
const { oidcConfig = { clientId: '', issuerURL: '' } } =
const { oidcConfig = { clientId: '', issuerURL: '', scopes: [] } } =
await getEverestConfigs();
setConfigs({
oidc: {
authority: oidcConfig.issuerURL,
clientId: oidcConfig.clientId,
scope: oidcConfig.scopes.join(' '),
redirectUri: `${window.location.protocol}//${window.location.host}/`,
},
});
Expand Down Expand Up @@ -72,7 +73,6 @@ const App = () => {
oidcConfig={{
...configs?.oidc,
redirectUri: `${window.location.protocol}//${window.location.host}/login-callback`,
scope: 'openid profile email groups',
responseType: 'code',
autoSignIn: false,
automaticSilentRenew: false,
Expand Down
2 changes: 2 additions & 0 deletions ui/apps/everest/src/shared-types/configs.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ export type EverestConfigPayload = {
oidcConfig?: {
issuerURL: string;
clientId: string;
scopes: string[];
};
};

export type EverestConfig = {
oidc?: {
authority: string;
clientId: string;
scope: string;
redirectUri?: string;
};
};
Loading